import { IconButton, ITextFieldStyles, Pivot, PivotItem, PrimaryButton, Stack, Text, TextField } from "@fluentui/react"; import { handleError } from "Common/ErrorHandlingUtils"; import { sendMessage } from "Common/MessageHandler"; import { MessageTypes } from "Contracts/ExplorerContracts"; import React, { useEffect, useState } from "react"; import { userContext } from "UserContext"; import { listKeys, listReadOnlyKeys } from "Utils/arm/generatedClients/cosmos/databaseAccounts"; import { DatabaseAccountListKeysResult, DatabaseAccountListReadOnlyKeysResult, } from "Utils/arm/generatedClients/cosmos/types"; export const ConnectTab: React.FC = (): JSX.Element => { const [primaryMasterKey, setPrimaryMasterKey] = useState(""); const [secondaryMasterKey, setSecondaryMasterKey] = useState(""); const [primaryReadonlyMasterKey, setPrimaryReadonlyMasterKey] = useState(""); const [secondaryReadonlyMasterKey, setSecondaryReadonlyMasterKey] = useState(""); const uri: string = userContext.databaseAccount.properties?.documentEndpoint; const primaryConnectionStr = `AccountEndpoint=${uri};AccountKey=${primaryMasterKey};`; const secondaryConnectionStr = `AccountEndpoint=${uri};AccountKey=${secondaryMasterKey};`; const primaryReadonlyConnectionStr = `AccountEndpoint=${uri};AccountKey=${primaryReadonlyMasterKey};`; const secondaryReadonlyConnectionStr = `AccountEndpoint=${uri};AccountKey=${secondaryReadonlyMasterKey};`; const maskedValue: string = "*********************************************************************************************************************************"; const [showPrimaryMasterKey, setShowPrimaryMasterKey] = useState(false); const [showSecondaryMasterKey, setShowSecondaryMasterKey] = useState(false); const [showPrimaryReadonlyMasterKey, setShowPrimaryReadonlyMasterKey] = useState(false); const [showSecondaryReadonlyMasterKey, setShowSecondaryReadonlyMasterKey] = useState(false); const [showPrimaryConnectionStr, setShowPrimaryConnectionStr] = useState(false); const [showSecondaryConnectionStr, setShowSecondaryConnectionStr] = useState(false); const [showPrimaryReadonlyConnectionStr, setShowPrimaryReadonlyConnectionStr] = useState(false); const [showSecondaryReadonlyConnectionStr, setShowSecondaryReadonlyConnectionStr] = useState(false); useEffect(() => { fetchKeys(); }, []); const fetchKeys = async (): Promise => { try { if (userContext.hasWriteAccess) { const listKeysResult: DatabaseAccountListKeysResult = await listKeys( userContext.subscriptionId, userContext.resourceGroup, userContext.databaseAccount.name, ); setPrimaryMasterKey(listKeysResult.primaryMasterKey); setSecondaryMasterKey(listKeysResult.secondaryMasterKey); setPrimaryReadonlyMasterKey(listKeysResult.primaryReadonlyMasterKey); setSecondaryReadonlyMasterKey(listKeysResult.secondaryReadonlyMasterKey); } else { const listReadonlyKeysResult: DatabaseAccountListReadOnlyKeysResult = await listReadOnlyKeys( userContext.subscriptionId, userContext.resourceGroup, userContext.databaseAccount.name, ); setPrimaryReadonlyMasterKey(listReadonlyKeysResult.primaryReadonlyMasterKey); setSecondaryReadonlyMasterKey(listReadonlyKeysResult.secondaryReadonlyMasterKey); } } catch (error) { handleError(error, "listKeys", "listKeys request has failed: "); throw error; } }; const onCopyBtnClicked = (selector: string): void => { const textfield: HTMLInputElement = document.querySelector(selector); textfield.select(); document.execCommand("copy"); }; const textfieldStyles: Partial = { root: { width: "100%" }, field: { backgroundColor: "rgb(230, 230, 230)" }, fieldGroup: { borderColor: "rgb(138, 136, 134)" }, suffix: { backgroundColor: "rgb(230, 230, 230)", margin: 0, padding: 0, }, }; const renderCopyButton = (selector: string) => ( onCopyBtnClicked(selector)} styles={{ root: { height: "100%", backgroundColor: "rgb(230, 230, 230)", border: "none", }, rootHovered: { backgroundColor: "rgb(220, 220, 220)", }, rootPressed: { backgroundColor: "rgb(210, 210, 210)", }, }} /> ); return (
renderCopyButton("#uriTextfield")} />
{userContext.hasWriteAccess && ( renderCopyButton("#primaryKeyTextfield"), })} /> setShowPrimaryMasterKey(!showPrimaryMasterKey)} /> renderCopyButton("#secondaryKeyTextfield"), })} /> setShowSecondaryMasterKey(!showSecondaryMasterKey)} /> renderCopyButton("#primaryConStrTextfield"), })} /> setShowPrimaryConnectionStr(!showPrimaryConnectionStr)} /> renderCopyButton("#secondaryConStrTextfield"), })} /> setShowSecondaryConnectionStr(!showSecondaryConnectionStr)} /> )} renderCopyButton("#primaryReadonlyKeyTextfield"), })} /> setShowPrimaryReadonlyMasterKey(!showPrimaryReadonlyMasterKey)} /> renderCopyButton("#secondaryReadonlyKeyTextfield"), })} /> setShowSecondaryReadonlyMasterKey(!showSecondaryReadonlyMasterKey)} /> renderCopyButton("#primaryReadonlyConStrTextfield"), })} /> setShowPrimaryReadonlyConnectionStr(!showPrimaryReadonlyConnectionStr)} /> renderCopyButton("#secondaryReadonlyConStrTextfield"), })} /> setShowSecondaryReadonlyConnectionStr(!showSecondaryReadonlyConnectionStr)} /> Download sample app Don’t have an app ready? No worries, download one of our sample app with a platform of your choice. Connection string is already included in the app. sendMessage({ type: MessageTypes.OpenQuickstartBlade, }) } text="Download sample app" />
); };