mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2024-11-30 17:37:01 +00:00
migrate refreshNoteBookEnabledStateForAccount to react
This commit is contained in:
parent
9878bf0d5e
commit
a10b61b8af
@ -30,7 +30,7 @@ import { Action, ActionModifiers } from "../Shared/Telemetry/TelemetryConstants"
|
|||||||
import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor";
|
import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor";
|
||||||
import { ArcadiaResourceManager } from "../SparkClusterManager/ArcadiaResourceManager";
|
import { ArcadiaResourceManager } from "../SparkClusterManager/ArcadiaResourceManager";
|
||||||
import { userContext } from "../UserContext";
|
import { userContext } from "../UserContext";
|
||||||
import { decryptJWTToken, getAuthorizationHeader } from "../Utils/AuthorizationUtils";
|
import { decryptJWTToken } from "../Utils/AuthorizationUtils";
|
||||||
import { stringToBlob } from "../Utils/BlobUtils";
|
import { stringToBlob } from "../Utils/BlobUtils";
|
||||||
import { fromContentUri, toRawContentUri } from "../Utils/GitHubUtils";
|
import { fromContentUri, toRawContentUri } from "../Utils/GitHubUtils";
|
||||||
import * as NotificationConsoleUtils from "../Utils/NotificationConsoleUtils";
|
import * as NotificationConsoleUtils from "../Utils/NotificationConsoleUtils";
|
||||||
@ -94,6 +94,8 @@ export interface ExplorerParams {
|
|||||||
closeDialog: () => void;
|
closeDialog: () => void;
|
||||||
openDialog: (props: DialogProps) => void;
|
openDialog: (props: DialogProps) => void;
|
||||||
tabsManager: TabsManager;
|
tabsManager: TabsManager;
|
||||||
|
refreshNotebooksEnabledStateForAccount: () => void;
|
||||||
|
isNotebooksEnabledForAccount: ko.Observable<boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Explorer {
|
export default class Explorer {
|
||||||
@ -195,6 +197,7 @@ export default class Explorer {
|
|||||||
public notebookManager?: NotebookManager;
|
public notebookManager?: NotebookManager;
|
||||||
public openDialog: ExplorerParams["openDialog"];
|
public openDialog: ExplorerParams["openDialog"];
|
||||||
public closeDialog: ExplorerParams["closeDialog"];
|
public closeDialog: ExplorerParams["closeDialog"];
|
||||||
|
public refreshNotebooksEnabledStateForAccount: ExplorerParams["refreshNotebooksEnabledStateForAccount"];
|
||||||
|
|
||||||
private _panes: ContextualPaneBase[] = [];
|
private _panes: ContextualPaneBase[] = [];
|
||||||
private _isInitializingNotebooks: boolean;
|
private _isInitializingNotebooks: boolean;
|
||||||
@ -218,6 +221,8 @@ export default class Explorer {
|
|||||||
this.closeSidePanel = params?.closeSidePanel;
|
this.closeSidePanel = params?.closeSidePanel;
|
||||||
this.closeDialog = params?.closeDialog;
|
this.closeDialog = params?.closeDialog;
|
||||||
this.openDialog = params?.openDialog;
|
this.openDialog = params?.openDialog;
|
||||||
|
this.refreshNotebooksEnabledStateForAccount = params?.refreshNotebooksEnabledStateForAccount;
|
||||||
|
this.isNotebooksEnabledForAccount = params?.isNotebooksEnabledForAccount;
|
||||||
|
|
||||||
const startKey: number = TelemetryProcessor.traceStart(Action.InitializeDataExplorer, {
|
const startKey: number = TelemetryProcessor.traceStart(Action.InitializeDataExplorer, {
|
||||||
dataExplorerArea: Constants.Areas.ResourceTree,
|
dataExplorerArea: Constants.Areas.ResourceTree,
|
||||||
@ -242,7 +247,6 @@ export default class Explorer {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.isNotebooksEnabledForAccount = ko.observable(false);
|
|
||||||
this.isNotebooksEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons());
|
this.isNotebooksEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons());
|
||||||
this.isSparkEnabledForAccount = ko.observable(false);
|
this.isSparkEnabledForAccount = ko.observable(false);
|
||||||
this.isSparkEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons());
|
this.isSparkEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons());
|
||||||
@ -261,7 +265,7 @@ export default class Explorer {
|
|||||||
this._isAfecFeatureRegistered(Constants.AfecFeatures.StorageAnalytics).then((isRegistered) =>
|
this._isAfecFeatureRegistered(Constants.AfecFeatures.StorageAnalytics).then((isRegistered) =>
|
||||||
this.hasStorageAnalyticsAfecFeature(isRegistered)
|
this.hasStorageAnalyticsAfecFeature(isRegistered)
|
||||||
);
|
);
|
||||||
Promise.all([this._refreshNotebooksEnabledStateForAccount(), this._refreshSparkEnabledStateForAccount()]).then(
|
Promise.all([this.refreshNotebooksEnabledStateForAccount(), this._refreshSparkEnabledStateForAccount()]).then(
|
||||||
async () => {
|
async () => {
|
||||||
this.isNotebookEnabled(
|
this.isNotebookEnabled(
|
||||||
userContext.authType !== AuthType.ResourceToken &&
|
userContext.authType !== AuthType.ResourceToken &&
|
||||||
@ -1612,53 +1616,6 @@ export default class Explorer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _refreshNotebooksEnabledStateForAccount(): Promise<void> {
|
|
||||||
const authType = userContext.authType;
|
|
||||||
if (
|
|
||||||
authType === AuthType.EncryptedToken ||
|
|
||||||
authType === AuthType.ResourceToken ||
|
|
||||||
authType === AuthType.MasterKey
|
|
||||||
) {
|
|
||||||
this.isNotebooksEnabledForAccount(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const databaseAccount = this.databaseAccount();
|
|
||||||
const databaseAccountLocation = databaseAccount && databaseAccount.location.toLowerCase();
|
|
||||||
const disallowedLocationsUri = `${configContext.BACKEND_ENDPOINT}/api/disallowedLocations`;
|
|
||||||
const authorizationHeader = getAuthorizationHeader();
|
|
||||||
try {
|
|
||||||
const response = await fetch(disallowedLocationsUri, {
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({
|
|
||||||
resourceTypes: [Constants.ArmResourceTypes.notebookWorkspaces],
|
|
||||||
}),
|
|
||||||
headers: {
|
|
||||||
[authorizationHeader.header]: authorizationHeader.token,
|
|
||||||
[Constants.HttpHeaders.contentType]: "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error("Failed to fetch disallowed locations");
|
|
||||||
}
|
|
||||||
|
|
||||||
const disallowedLocations: string[] = await response.json();
|
|
||||||
if (!disallowedLocations) {
|
|
||||||
Logger.logInfo("No disallowed locations found", "Explorer/isNotebooksEnabledForAccount");
|
|
||||||
this.isNotebooksEnabledForAccount(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const isAccountInAllowedLocation = !disallowedLocations.some(
|
|
||||||
(disallowedLocation) => disallowedLocation === databaseAccountLocation
|
|
||||||
);
|
|
||||||
this.isNotebooksEnabledForAccount(isAccountInAllowedLocation);
|
|
||||||
} catch (error) {
|
|
||||||
Logger.logError(getErrorMessage(error), "Explorer/isNotebooksEnabledForAccount");
|
|
||||||
this.isNotebooksEnabledForAccount(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public _refreshSparkEnabledStateForAccount = async (): Promise<void> => {
|
public _refreshSparkEnabledStateForAccount = async (): Promise<void> => {
|
||||||
const subscriptionId = userContext.subscriptionId;
|
const subscriptionId = userContext.subscriptionId;
|
||||||
const armEndpoint = configContext.ARM_ENDPOINT;
|
const armEndpoint = configContext.ARM_ENDPOINT;
|
||||||
|
57
src/Main.tsx
57
src/Main.tsx
@ -28,6 +28,10 @@ import "../less/TableStyles/fulldatatables.less";
|
|||||||
import "../less/TableStyles/queryBuilder.less";
|
import "../less/TableStyles/queryBuilder.less";
|
||||||
import "../less/tree.less";
|
import "../less/tree.less";
|
||||||
import { AuthType } from "./AuthType";
|
import { AuthType } from "./AuthType";
|
||||||
|
import { ArmResourceTypes, HttpHeaders } from "./Common/Constants";
|
||||||
|
import { getErrorMessage } from "./Common/ErrorHandlingUtils";
|
||||||
|
import { logError, logInfo } from "./Common/Logger";
|
||||||
|
import { configContext } from "./ConfigContext";
|
||||||
import "./Explorer/Controls/Accordion/AccordionComponent.less";
|
import "./Explorer/Controls/Accordion/AccordionComponent.less";
|
||||||
import "./Explorer/Controls/CollapsiblePanel/CollapsiblePanelComponent.less";
|
import "./Explorer/Controls/CollapsiblePanel/CollapsiblePanelComponent.less";
|
||||||
import { Dialog, DialogProps } from "./Explorer/Controls/Dialog";
|
import { Dialog, DialogProps } from "./Explorer/Controls/Dialog";
|
||||||
@ -57,12 +61,14 @@ import { KOCommentEnd, KOCommentIfStart } from "./koComment";
|
|||||||
import "./Libs/jquery";
|
import "./Libs/jquery";
|
||||||
import "./Shared/appInsights";
|
import "./Shared/appInsights";
|
||||||
import { userContext } from "./UserContext";
|
import { userContext } from "./UserContext";
|
||||||
|
import { getAuthorizationHeader } from "./Utils/AuthorizationUtils";
|
||||||
|
|
||||||
initializeIcons();
|
initializeIcons();
|
||||||
|
|
||||||
const App: React.FunctionComponent = () => {
|
const App: React.FunctionComponent = () => {
|
||||||
const [isNotificationConsoleExpanded, setIsNotificationConsoleExpanded] = useState(false);
|
const [isNotificationConsoleExpanded, setIsNotificationConsoleExpanded] = useState(false);
|
||||||
const [notificationConsoleData, setNotificationConsoleData] = useState(undefined);
|
const [notificationConsoleData, setNotificationConsoleData] = useState(undefined);
|
||||||
|
const [isNotebooksEnabledForAccount, setIsNotebooksEnabledForAccount] = useState(false);
|
||||||
//TODO: Refactor so we don't need to pass the id to remove a console data
|
//TODO: Refactor so we don't need to pass the id to remove a console data
|
||||||
const [inProgressConsoleDataIdToBeDeleted, setInProgressConsoleDataIdToBeDeleted] = useState("");
|
const [inProgressConsoleDataIdToBeDeleted, setInProgressConsoleDataIdToBeDeleted] = useState("");
|
||||||
|
|
||||||
@ -80,6 +86,55 @@ const App: React.FunctionComponent = () => {
|
|||||||
const { isPanelOpen, panelContent, headerText, openSidePanel, closeSidePanel } = useSidePanel();
|
const { isPanelOpen, panelContent, headerText, openSidePanel, closeSidePanel } = useSidePanel();
|
||||||
const { tabs, activeTab, tabsManager } = useTabs();
|
const { tabs, activeTab, tabsManager } = useTabs();
|
||||||
|
|
||||||
|
const refreshNotebooksEnabledStateForAccount = async (): Promise<void> => {
|
||||||
|
const authType = userContext.authType;
|
||||||
|
if (
|
||||||
|
authType === AuthType.EncryptedToken ||
|
||||||
|
authType === AuthType.ResourceToken ||
|
||||||
|
authType === AuthType.MasterKey
|
||||||
|
) {
|
||||||
|
setIsNotebooksEnabledForAccount(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const databaseAccount = userContext.databaseAccount;
|
||||||
|
const databaseAccountLocation = databaseAccount && databaseAccount.location.toLowerCase();
|
||||||
|
const disallowedLocationsUri = `${configContext.BACKEND_ENDPOINT}/api/disallowedLocations`;
|
||||||
|
const authorizationHeader = getAuthorizationHeader();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(disallowedLocationsUri, {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({
|
||||||
|
resourceTypes: [ArmResourceTypes.notebookWorkspaces],
|
||||||
|
}),
|
||||||
|
headers: {
|
||||||
|
[authorizationHeader.header]: authorizationHeader.token,
|
||||||
|
[HttpHeaders.contentType]: "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to fetch disallowed locations");
|
||||||
|
}
|
||||||
|
|
||||||
|
const disallowedLocations: string[] = await response.json();
|
||||||
|
if (!disallowedLocations) {
|
||||||
|
logInfo("No disallowed locations found", "Explorer/isNotebooksEnabledForAccount");
|
||||||
|
setIsNotebooksEnabledForAccount(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const isAccountInAllowedLocation = !disallowedLocations.some(
|
||||||
|
(disallowedLocation) => disallowedLocation === databaseAccountLocation
|
||||||
|
);
|
||||||
|
|
||||||
|
setIsNotebooksEnabledForAccount(isAccountInAllowedLocation);
|
||||||
|
} catch (error) {
|
||||||
|
logError(getErrorMessage(error), "Explorer/isNotebooksEnabledForAccount");
|
||||||
|
setIsNotebooksEnabledForAccount(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const explorerParams: ExplorerParams = {
|
const explorerParams: ExplorerParams = {
|
||||||
setIsNotificationConsoleExpanded,
|
setIsNotificationConsoleExpanded,
|
||||||
setNotificationConsoleData,
|
setNotificationConsoleData,
|
||||||
@ -89,6 +144,8 @@ const App: React.FunctionComponent = () => {
|
|||||||
openDialog,
|
openDialog,
|
||||||
closeDialog,
|
closeDialog,
|
||||||
tabsManager,
|
tabsManager,
|
||||||
|
refreshNotebooksEnabledStateForAccount,
|
||||||
|
isNotebooksEnabledForAccount,
|
||||||
};
|
};
|
||||||
|
|
||||||
const config = useConfig();
|
const config = useConfig();
|
||||||
|
Loading…
Reference in New Issue
Block a user