From 47201603c3428c7479d44490deb9d3404053951d Mon Sep 17 00:00:00 2001 From: "Craig Boger (from Dev Box)" Date: Wed, 31 Jan 2024 17:13:05 -0800 Subject: [PATCH] Added rough button to command bar to allow switching of client endpoint. --- src/Common/CosmosClient.ts | 1 + .../CommandBarComponentButtonFactory.tsx | 105 +++++++++++++++++- src/hooks/useKnockoutExplorer.ts | 14 +-- 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/src/Common/CosmosClient.ts b/src/Common/CosmosClient.ts index ed6e9ab33..90052726b 100644 --- a/src/Common/CosmosClient.ts +++ b/src/Common/CosmosClient.ts @@ -135,6 +135,7 @@ enum SDKSupportedCapabilities { } // Need to put in some kind of function here to recreate the CosmosClient with a new endpoint. +// changeClientEndpoint....... let _client: Cosmos.CosmosClient; diff --git a/src/Explorer/Menus/CommandBar/CommandBarComponentButtonFactory.tsx b/src/Explorer/Menus/CommandBar/CommandBarComponentButtonFactory.tsx index b145c1a7a..1e36067d6 100644 --- a/src/Explorer/Menus/CommandBar/CommandBarComponentButtonFactory.tsx +++ b/src/Explorer/Menus/CommandBar/CommandBarComponentButtonFactory.tsx @@ -23,7 +23,7 @@ import * as Constants from "../../../Common/Constants"; import { Platform, configContext } from "../../../ConfigContext"; import * as ViewModels from "../../../Contracts/ViewModels"; import { JunoClient } from "../../../Juno/JunoClient"; -import { userContext } from "../../../UserContext"; +import { updateUserContext, userContext } from "../../../UserContext"; import { getCollectionName, getDatabaseName } from "../../../Utils/APITypeUtils"; import { isRunningOnNationalCloud } from "../../../Utils/CloudUtils"; import { useSidePanel } from "../../../hooks/useSidePanel"; @@ -169,6 +169,12 @@ export function createStaticCommandBarButtons( } // Attempting to add region selection button here. + addDivider(); + const [selectedRegion, setSelectedRegion] = React.useState( + userContext.databaseAccount.properties.locations[0].locationName, + ); + const newReadRegionSelectionBtn = createReadRegionSelectionGroup(container, selectedRegion, setSelectedRegion); + buttons.push(newReadRegionSelectionBtn); return buttons; } @@ -603,6 +609,103 @@ function createManageGitHubAccountButton(container: Explorer): CommandButtonComp }; } +// function createReadRegionSelectionGroup(container: Explorer): CommandButtonComponentProps { +// const label = "Select a Read Region"; +// return { +// iconAlt: label, +// commandButtonLabel: label, +// hasPopup: false, +// disabled: false, +// ariaLabel: label, +// onCommandClick: async () => { +// console.log( +// `CURRENT DOCUMENT ENDPOINT: ${JSON.stringify(userContext.databaseAccount.properties.documentEndpoint)}`, +// ); +// userContext.databaseAccount.properties.readLocations.forEach((readLocation) => { +// console.log(`CURRENT READ ENDPOINT(S): ${JSON.stringify(readLocation)}`); +// }); +// const updatedDatabaseAccount = { +// ...userContext.databaseAccount, +// properties: { +// ...userContext.databaseAccount.properties, +// documentEndpoint: "https://test-craig-nosql-periodic-eastus.documents.azure.com:443/", +// }, +// }; +// updateUserContext({ +// databaseAccount: updatedDatabaseAccount, +// }); +// }, +// }; +// } + +function createReadRegionSelectionGroup( + container: Explorer, + selectedRegion: string, + setSelectedRegion: (region: string) => void, +): CommandButtonComponentProps { + const children = userContext.databaseAccount.properties.readLocations.map((readLocation) => ({ + commandButtonLabel: readLocation.locationName, + onCommandClick: async () => { + const updatedDatabaseAccount = { + ...userContext.databaseAccount, + properties: { + ...userContext.databaseAccount.properties, + documentEndpoint: readLocation.documentEndpoint, + }, + }; + updateUserContext({ + databaseAccount: updatedDatabaseAccount, + }); + setSelectedRegion(readLocation.locationName); + }, + hasPopup: false, + ariaLabel: `Select ${readLocation.locationName}`, + })); + const label = selectedRegion || "Select a Read Region"; + return { + iconAlt: label, + commandButtonLabel: label, + onCommandClick: () => {}, + hasPopup: true, + ariaLabel: label, + isDropdown: true, + children, + dropdownWidth: 100, + }; +} + +// function createAccountRegionSelectionButton(container: Explorer): JSX.Element { +// const [selectedEndpoint, setSelectedEndpoint] = useState(userContext.databaseAccount.properties.documentEndpoint); + +// const handleChange = (event: React.ChangeEvent<{ value: unknown }>) => { +// const endpoint = event.target.value as string; +// setSelectedEndpoint(endpoint); + +// const updatedDatabaseAccount = { +// ...userContext.databaseAccount, +// properties: { +// ...userContext.databaseAccount.properties, +// documentEndpoint: endpoint, +// }, +// }; +// updateUserContext({ +// databaseAccount: updatedDatabaseAccount, +// }); +// }; + +// return ( +// +// ); +// } + function createStaticCommandBarButtonsForResourceToken( container: Explorer, selectedNodeState: SelectedNodeState, diff --git a/src/hooks/useKnockoutExplorer.ts b/src/hooks/useKnockoutExplorer.ts index ac2a4d301..b04b16c72 100644 --- a/src/hooks/useKnockoutExplorer.ts +++ b/src/hooks/useKnockoutExplorer.ts @@ -280,25 +280,13 @@ async function configureHostedWithAAD(config: AAD): Promise { throw new Error(`List keys failed: ${e.message}`); } } - // Updating database account endpoint for testing purposes. - const updatedDatabaseAccount = { - ...config.databaseAccount, - properties: { - ...config.databaseAccount.properties, - documentEndpoint: "https://test-craig-nosql-periodic.documents.azure.com:443/", - }, - }; updateUserContext({ subscriptionId, resourceGroup, aadToken, - databaseAccount: updatedDatabaseAccount, + databaseAccount: config.databaseAccount, masterKey: keys.primaryMasterKey, }); - console.log(`INITIALIZED ENDPOINT: ${JSON.stringify(userContext.databaseAccount.properties.documentEndpoint)}`); - userContext.databaseAccount.properties.readLocations.forEach((readLocation) => { - console.log(`REGIONAL READ ENDPOINT(S): ${JSON.stringify(readLocation)}`); - }); const explorer = new Explorer(); return explorer; }