mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2024-12-03 19:07:13 +00:00
3aa4bbe266
* Health check retry addition * format issue * Address comments * Test Check * Added await * code cleanup
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
import { ConnectionStatusType } from "Common/Constants";
|
|
import { useNotebook } from "Explorer/Notebook/useNotebook";
|
|
import create, { UseStore } from "zustand";
|
|
import * as ViewModels from "../Contracts/ViewModels";
|
|
import { useTabs } from "../hooks/useTabs";
|
|
|
|
export interface SelectedNodeState {
|
|
selectedNode: ViewModels.TreeNode;
|
|
setSelectedNode: (node: ViewModels.TreeNode) => void;
|
|
isDatabaseNodeOrNoneSelected: () => boolean;
|
|
findSelectedCollection: () => ViewModels.Collection;
|
|
isDataNodeSelected: (
|
|
databaseId: string,
|
|
collectionId?: string,
|
|
subnodeKinds?: ViewModels.CollectionTabKind[]
|
|
) => boolean;
|
|
isConnectedToContainer: () => boolean;
|
|
}
|
|
|
|
export const useSelectedNode: UseStore<SelectedNodeState> = create((set, get) => ({
|
|
selectedNode: undefined,
|
|
setSelectedNode: (node: ViewModels.TreeNode) => set({ selectedNode: node }),
|
|
isDatabaseNodeOrNoneSelected: (): boolean => {
|
|
const selectedNode = get().selectedNode;
|
|
return !selectedNode || selectedNode.nodeKind === "Database";
|
|
},
|
|
findSelectedCollection: (): ViewModels.Collection => {
|
|
const selectedNode = get().selectedNode;
|
|
return (selectedNode.nodeKind === "Collection" ? selectedNode : selectedNode.collection) as ViewModels.Collection;
|
|
},
|
|
isDataNodeSelected: (
|
|
databaseId: string,
|
|
collectionId?: string,
|
|
subnodeKinds?: ViewModels.CollectionTabKind[]
|
|
): boolean => {
|
|
const selectedNode = get().selectedNode;
|
|
if (!selectedNode) {
|
|
return false;
|
|
}
|
|
|
|
const isNodeSelected = collectionId
|
|
? (selectedNode as ViewModels.Collection).databaseId === databaseId && selectedNode.id() === collectionId
|
|
: selectedNode.id() === databaseId;
|
|
|
|
if (!isNodeSelected) {
|
|
return false;
|
|
}
|
|
|
|
if (subnodeKinds === undefined || !Array.isArray(subnodeKinds)) {
|
|
return true;
|
|
}
|
|
|
|
const activeTab = useTabs.getState().activeTab;
|
|
const selectedSubnodeKind = collectionId
|
|
? (selectedNode as ViewModels.Collection).selectedSubnodeKind()
|
|
: (selectedNode as ViewModels.Database).selectedSubnodeKind();
|
|
|
|
return (
|
|
activeTab &&
|
|
subnodeKinds.includes(activeTab.tabKind) &&
|
|
selectedSubnodeKind !== undefined &&
|
|
subnodeKinds.includes(selectedSubnodeKind)
|
|
);
|
|
},
|
|
isConnectedToContainer: (): boolean => {
|
|
return useNotebook.getState().connectionInfo?.status === ConnectionStatusType.Connected;
|
|
},
|
|
}));
|