From b8ed534c90a2a92300e995839136c153a2cb5060 Mon Sep 17 00:00:00 2001 From: sunilyadav840 Date: Thu, 29 Apr 2021 17:51:05 +0530 Subject: [PATCH] Move Explorer._refreshNotebooksEnabledStateForAccount into React --- .../SettingsComponent.test.tsx.snap | 16 +++--- src/Explorer/Explorer.tsx | 55 ++++--------------- .../__snapshots__/SettingsPane.test.tsx.snap | 8 +-- .../StringInputPane.test.tsx.snap | 8 +-- .../UploadItemsPane.test.tsx.snap | 4 +- ...eteDatabaseConfirmationPanel.test.tsx.snap | 4 +- src/Main.tsx | 38 +++++++++++++ 7 files changed, 69 insertions(+), 64 deletions(-) diff --git a/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap b/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap index 30cd9914c..9241597cb 100644 --- a/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap +++ b/src/Explorer/Controls/Settings/__snapshots__/SettingsComponent.test.tsx.snap @@ -223,7 +223,6 @@ exports[`SettingsComponent renders 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -464,7 +463,7 @@ exports[`SettingsComponent renders 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -481,6 +480,7 @@ exports[`SettingsComponent renders 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], @@ -738,7 +738,6 @@ exports[`SettingsComponent renders 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -979,7 +978,7 @@ exports[`SettingsComponent renders 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -996,6 +995,7 @@ exports[`SettingsComponent renders 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], @@ -1266,7 +1266,6 @@ exports[`SettingsComponent renders 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -1507,7 +1506,7 @@ exports[`SettingsComponent renders 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -1524,6 +1523,7 @@ exports[`SettingsComponent renders 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], @@ -1781,7 +1781,6 @@ exports[`SettingsComponent renders 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -2022,7 +2021,7 @@ exports[`SettingsComponent renders 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -2039,6 +2038,7 @@ exports[`SettingsComponent renders 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], diff --git a/src/Explorer/Explorer.tsx b/src/Explorer/Explorer.tsx index 3a090fd7e..0ca61fcce 100644 --- a/src/Explorer/Explorer.tsx +++ b/src/Explorer/Explorer.tsx @@ -94,6 +94,8 @@ export interface ExplorerParams { closeDialog: () => void; openDialog: (props: DialogProps) => void; tabsManager: TabsManager; + refreshSparkEnabledStateForAccount: () => void; + isSparkEnabledForAccount: boolean; } export default class Explorer { @@ -186,7 +188,6 @@ export default class Explorer { public notebookWorkspaceManager: NotebookWorkspaceManager; public sparkClusterConnectionInfo: ko.Observable; public isSparkEnabled: ko.Observable; - public isSparkEnabledForAccount: ko.Observable; public arcadiaToken: ko.Observable; public arcadiaWorkspaces: ko.ObservableArray; public hasStorageAnalyticsAfecFeature: ko.Observable; @@ -205,6 +206,10 @@ export default class Explorer { content: string; }; + // Refresh spark + public refreshSparkEnabledStateForAccount: () => void; + public isSparkEnabledForAccount: boolean; + // React adapters private commandBarComponentAdapter: CommandBarComponentAdapter; @@ -218,6 +223,8 @@ export default class Explorer { this.closeSidePanel = params?.closeSidePanel; this.closeDialog = params?.closeDialog; this.openDialog = params?.openDialog; + this.refreshSparkEnabledStateForAccount = params?.refreshSparkEnabledStateForAccount; + this.isSparkEnabledForAccount = params?.isSparkEnabledForAccount; const startKey: number = TelemetryProcessor.traceStart(Action.InitializeDataExplorer, { dataExplorerArea: Constants.Areas.ResourceTree, @@ -244,8 +251,7 @@ export default class Explorer { }); this.isNotebooksEnabledForAccount = ko.observable(false); this.isNotebooksEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons()); - this.isSparkEnabledForAccount = ko.observable(false); - this.isSparkEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons()); + // this.isSparkEnabledForAccount.subscribe((isEnabledForAccount: boolean) => this.refreshCommandBarButtons()); this.hasStorageAnalyticsAfecFeature = ko.observable(false); this.hasStorageAnalyticsAfecFeature.subscribe((enabled: boolean) => this.refreshCommandBarButtons()); this.isSynapseLinkUpdating = ko.observable(false); @@ -261,7 +267,7 @@ export default class Explorer { this._isAfecFeatureRegistered(Constants.AfecFeatures.StorageAnalytics).then((isRegistered) => this.hasStorageAnalyticsAfecFeature(isRegistered) ); - Promise.all([this._refreshNotebooksEnabledStateForAccount(), this._refreshSparkEnabledStateForAccount()]).then( + Promise.all([this._refreshNotebooksEnabledStateForAccount(), this.refreshSparkEnabledStateForAccount()]).then( async () => { this.isNotebookEnabled( userContext.authType !== AuthType.ResourceToken && @@ -284,7 +290,7 @@ export default class Explorer { this.isSparkEnabled( (this.isNotebookEnabled() && - this.isSparkEnabledForAccount() && + this.isSparkEnabledForAccount && this.arcadiaWorkspaces() && this.arcadiaWorkspaces().length > 0) || userContext.features.enableSpark @@ -372,16 +378,6 @@ export default class Explorer { direction: SplitterDirection.Vertical, }); this.defaultExperience = ko.observable(); - // this.databaseAccount.subscribe((databaseAccount) => { - // const defaultExperience: string = DefaultExperienceUtility.getDefaultExperienceFromDatabaseAccount( - // databaseAccount - // ); - // this.defaultExperience(defaultExperience); - // // TODO. Remove this entirely - // updateUserContext({ - // apiType: DefaultExperienceUtility.mapDefaultExperienceStringToEnum(defaultExperience), - // }); - // }); this.isFixedCollectionWithSharedThroughputSupported = ko.computed(() => { if (userContext.features.enableFixedCollectionWithSharedThroughput) { @@ -1659,35 +1655,6 @@ export default class Explorer { } } - public _refreshSparkEnabledStateForAccount = async (): Promise => { - const subscriptionId = userContext.subscriptionId; - const armEndpoint = configContext.ARM_ENDPOINT; - const authType = userContext.authType; - if (!subscriptionId || !armEndpoint || authType === AuthType.EncryptedToken) { - // explorer is not aware of the database account yet - this.isSparkEnabledForAccount(false); - return; - } - - const featureUri = `subscriptions/${subscriptionId}/providers/Microsoft.Features/providers/Microsoft.DocumentDb/features/${Constants.AfecFeatures.Spark}`; - const resourceProviderClient = new ResourceProviderClientFactory().getOrCreate(featureUri); - try { - const sparkNotebooksFeature: DataModels.AfecFeature = await resourceProviderClient.getAsync( - featureUri, - Constants.ArmApiVersions.armFeatures - ); - const isEnabled = - (sparkNotebooksFeature && - sparkNotebooksFeature.properties && - sparkNotebooksFeature.properties.state === "Registered") || - false; - this.isSparkEnabledForAccount(isEnabled); - } catch (error) { - Logger.logError(getErrorMessage(error), "Explorer/isSparkEnabledForAccount"); - this.isSparkEnabledForAccount(false); - } - }; - public _isAfecFeatureRegistered = async (featureName: string): Promise => { const subscriptionId = userContext.subscriptionId; const armEndpoint = configContext.ARM_ENDPOINT; diff --git a/src/Explorer/Panes/SettingsPane/__snapshots__/SettingsPane.test.tsx.snap b/src/Explorer/Panes/SettingsPane/__snapshots__/SettingsPane.test.tsx.snap index 69f4ef1b7..6eaff37d0 100644 --- a/src/Explorer/Panes/SettingsPane/__snapshots__/SettingsPane.test.tsx.snap +++ b/src/Explorer/Panes/SettingsPane/__snapshots__/SettingsPane.test.tsx.snap @@ -199,7 +199,6 @@ exports[`Settings Pane should render Default properly 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -440,7 +439,7 @@ exports[`Settings Pane should render Default properly 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -457,6 +456,7 @@ exports[`Settings Pane should render Default properly 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], @@ -837,7 +837,6 @@ exports[`Settings Pane should render Gremlin properly 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -1078,7 +1077,7 @@ exports[`Settings Pane should render Gremlin properly 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -1095,6 +1094,7 @@ exports[`Settings Pane should render Gremlin properly 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], diff --git a/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap b/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap index fbdc9fc80..af1de6b16 100644 --- a/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap +++ b/src/Explorer/Panes/StringInputPane/__snapshots__/StringInputPane.test.tsx.snap @@ -202,7 +202,6 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -443,7 +442,7 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -460,6 +459,7 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], @@ -716,7 +716,6 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -957,7 +956,7 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -974,6 +973,7 @@ exports[`StringInput Pane should render Create new directory properly 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], diff --git a/src/Explorer/Panes/UploadItemsPane/__snapshots__/UploadItemsPane.test.tsx.snap b/src/Explorer/Panes/UploadItemsPane/__snapshots__/UploadItemsPane.test.tsx.snap index fc59040bf..3c56e946d 100644 --- a/src/Explorer/Panes/UploadItemsPane/__snapshots__/UploadItemsPane.test.tsx.snap +++ b/src/Explorer/Panes/UploadItemsPane/__snapshots__/UploadItemsPane.test.tsx.snap @@ -199,7 +199,6 @@ exports[`Upload Items Pane should render Default properly 1`] = ` "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -440,7 +439,7 @@ exports[`Upload Items Pane should render Default properly 1`] = ` "isSchemaEnabled": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -457,6 +456,7 @@ exports[`Upload Items Pane should render Default properly 1`] = ` }, "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], diff --git a/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap b/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap index 88da6e77a..2d2847898 100644 --- a/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap +++ b/src/Explorer/Panes/__snapshots__/DeleteDatabaseConfirmationPanel.test.tsx.snap @@ -200,7 +200,6 @@ exports[`Delete Database Confirmation Pane submit() Should call delete database "visible": [Function], }, ], - "_refreshSparkEnabledStateForAccount": [Function], "_resetNotebookWorkspace": [Function], "addCollectionPane": AddCollectionPane { "_isSynapseLinkEnabled": [Function], @@ -444,7 +443,7 @@ exports[`Delete Database Confirmation Pane submit() Should call delete database "isSelectedDatabaseShared": [Function], "isServerlessEnabled": [Function], "isSparkEnabled": [Function], - "isSparkEnabledForAccount": [Function], + "isSparkEnabledForAccount": undefined, "isSynapseLinkUpdating": [Function], "isTabsContentExpanded": [Function], "memoryUsageInfo": [Function], @@ -462,6 +461,7 @@ exports[`Delete Database Confirmation Pane submit() Should call delete database "refreshAllDatabases": [Function], "refreshDatabaseAccount": [Function], "refreshNotebookList": [Function], + "refreshSparkEnabledStateForAccount": undefined, "refreshTreeTitle": [Function], "resourceTokenCollection": [Function], "resourceTokenCollectionId": [Function], diff --git a/src/Main.tsx b/src/Main.tsx index 8970ffbc8..d222e0dd2 100644 --- a/src/Main.tsx +++ b/src/Main.tsx @@ -28,6 +28,11 @@ import "../less/TableStyles/fulldatatables.less"; import "../less/TableStyles/queryBuilder.less"; import "../less/tree.less"; import { AuthType } from "./AuthType"; +import { AfecFeatures, ArmApiVersions } from "./Common/Constants"; +import { getErrorMessage } from "./Common/ErrorHandlingUtils"; +import { logError } from "./Common/Logger"; +import { configContext } from "./ConfigContext"; +import { AfecFeature } from "./Contracts/DataModels"; import "./Explorer/Controls/Accordion/AccordionComponent.less"; import "./Explorer/Controls/CollapsiblePanel/CollapsiblePanelComponent.less"; import { Dialog, DialogProps } from "./Explorer/Controls/Dialog"; @@ -55,6 +60,7 @@ import { useSidePanel } from "./hooks/useSidePanel"; import { useTabs } from "./hooks/useTabs"; import { KOCommentEnd, KOCommentIfStart } from "./koComment"; import "./Libs/jquery"; +import { ResourceProviderClientFactory } from "./ResourceProvider/ResourceProviderClientFactory"; import "./Shared/appInsights"; import { userContext } from "./UserContext"; @@ -68,6 +74,36 @@ const App: React.FunctionComponent = () => { const [dialogProps, setDialogProps] = useState(); const [showDialog, setShowDialog] = useState(false); + const [isSparkEnabledForAccount, setIsSparkEnabledForAccount] = useState(false); + + const refreshSparkEnabledStateForAccount = async (): Promise => { + const subscriptionId = userContext.subscriptionId; + const armEndpoint = configContext.ARM_ENDPOINT; + const authType = userContext.authType; + if (!subscriptionId || !armEndpoint || authType === AuthType.EncryptedToken) { + // explorer is not aware of the database account yet + setIsSparkEnabledForAccount(false); + return; + } + + const featureUri = `subscriptions/${subscriptionId}/providers/Microsoft.Features/providers/Microsoft.DocumentDb/features/${AfecFeatures.Spark}`; + const resourceProviderClient = new ResourceProviderClientFactory().getOrCreate(featureUri); + try { + const sparkNotebooksFeature: AfecFeature = await resourceProviderClient.getAsync( + featureUri, + ArmApiVersions.armFeatures + ); + const isEnabled = + (sparkNotebooksFeature && + sparkNotebooksFeature.properties && + sparkNotebooksFeature.properties.state === "Registered") || + false; + setIsSparkEnabledForAccount(isEnabled); + } catch (error) { + logError(getErrorMessage(error), "Explorer/isSparkEnabledForAccount"); + setIsSparkEnabledForAccount(false); + } + }; const openDialog = (props: DialogProps) => { setDialogProps(props); @@ -89,6 +125,8 @@ const App: React.FunctionComponent = () => { openDialog, closeDialog, tabsManager, + refreshSparkEnabledStateForAccount, + isSparkEnabledForAccount, }; const config = useConfig();