From 4fda518a3f935c89499f464c8ab89eb0582b1d01 Mon Sep 17 00:00:00 2001 From: Laurent Nguyen Date: Mon, 22 Jul 2024 17:16:05 +0200 Subject: [PATCH] Replace read/save methods with more generic ones --- .../DocumentsTabV2/DocumentsTabStateUtil.ts | 89 ++++++------------- .../Tabs/DocumentsTabV2/DocumentsTabV2.tsx | 8 +- .../DocumentsTableComponent.tsx | 28 ++++-- 3 files changed, 51 insertions(+), 74 deletions(-) diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabStateUtil.ts b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabStateUtil.ts index 611116bee..15ed1c593 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabStateUtil.ts +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabStateUtil.ts @@ -1,8 +1,8 @@ // Definitions of State data -import { TableColumnSizingOptions } from "@fluentui/react-components"; import { loadState, saveState, saveStateDebounced } from "Shared/AppStatePersistenceUtility"; import { userContext } from "UserContext"; +import * as ViewModels from "../../../Contracts/ViewModels"; // Component states export interface DocumentsTabStateData { @@ -24,84 +24,45 @@ export const saveDocumentsTabState = (state: DocumentsTabStateData): void => { saveStateDebounced({ componentName: ComponentName }, state); }; -type ColumnSizesMap = { [columnId: string]: WidthDefinition }; -type WidthDefinition = { idealWidth?: number; minWidth?: number }; +export type ColumnSizesMap = { [columnId: string]: WidthDefinition }; +export type WidthDefinition = { idealWidth?: number; minWidth?: number }; -const defaultSize: WidthDefinition = { - idealWidth: 200, - minWidth: 50, -}; - -const ColumnSizesSubComponentName = "ColumnSizes"; -export const readColumnSizes = ( - databaseName: string, - containerName: string, - columnIds: string[], -): TableColumnSizingOptions => { +export const readSubComponentState = ( + subComponentName: "ColumnSizes" | "FilterHistory", + collection: ViewModels.CollectionBase, + defaultValue: T, +): T => { const globalAccountName = userContext.databaseAccount?.name; // TODO what if databaseAccount doesn't exist? const state = loadState({ - globalAccountName, - databaseName, - containerName, componentName: ComponentName, - subComponentName: ColumnSizesSubComponentName, - }) as ColumnSizesMap; + subComponentName, + globalAccountName, + databaseName: collection.databaseId, + containerName: collection.id(), + }) as T; - const columnSizesPx: ColumnSizesMap = {}; - columnIds.forEach((columnId) => { - columnSizesPx[columnId] = (state && state[columnId]) || defaultSize; - }); - - return columnSizesPx; + return state || defaultValue; }; -export const saveColumnSizes = (databaseName: string, containerName: string, columnSizesMap: ColumnSizesMap): void => { +export const saveSubComponentState = ( + subComponentName: "ColumnSizes" | "FilterHistory", + collection: ViewModels.CollectionBase, + state: T, + debounce?: boolean, +): void => { const globalAccountName = userContext.databaseAccount?.name; // TODO what if databaseAccount doesn't exist? - saveStateDebounced( + (debounce ? saveStateDebounced : saveState)( { componentName: ComponentName, - subComponentName: ColumnSizesSubComponentName, + subComponentName, globalAccountName, - databaseName, - containerName, + databaseName: collection.databaseId, + containerName: collection.id(), }, - columnSizesMap, - ); -}; - -const filterHistorySubComponentName = "FilterHistory"; -export type FilterHistory = string[]; -export const readFilterHistory = (databaseName: string, containerName: string): FilterHistory => { - const globalAccountName = userContext.databaseAccount?.name; - // TODO what if databaseAccount doesn't exist? - - const state = loadState({ - globalAccountName, - databaseName, - containerName, - componentName: ComponentName, - subComponentName: filterHistorySubComponentName, - }) as FilterHistory; - - return state || []; -}; - -export const saveFilterHistory = (databaseName: string, containerName: string, filterHistory: FilterHistory): void => { - const globalAccountName = userContext.databaseAccount?.name; - // TODO what if databaseAccount doesn't exist? - - saveState( - { - componentName: ComponentName, - subComponentName: filterHistorySubComponentName, - globalAccountName, - databaseName, - containerName, - }, - filterHistory, + state, ); }; diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx index 74bd95a6f..0a5e2433d 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx @@ -24,9 +24,9 @@ import { querySampleDocuments, readSampleDocument } from "Explorer/QueryCopilot/ import { DocumentsTabStateData, readDocumentsTabState, - readFilterHistory, + readSubComponentState, saveDocumentsTabState, - saveFilterHistory, + saveSubComponentState, } from "Explorer/Tabs/DocumentsTabV2/DocumentsTabStateUtil"; import { getPlatformTheme } from "Explorer/Theme/ThemeUtil"; import { useSelectedNode } from "Explorer/useSelectedNode"; @@ -521,7 +521,7 @@ export const DocumentsTabComponent: React.FunctionComponent(() => - readFilterHistory(_collection.databaseId, _collection.id()), + readSubComponentState("FilterHistory", _collection, []), ); const setKeyboardActions = useKeyboardActionGroup(KeyboardActionGroup.ACTIVE_TAB); @@ -1690,7 +1690,7 @@ export const DocumentsTabComponent: React.FunctionComponent = ({ items, onSelectedRowsChange, @@ -70,9 +80,15 @@ export const DocumentsTableComponent: React.FC = isSelectionDisabled, collection, }: IDocumentsTableComponentProps) => { - const [columnSizingOptions, setColumnSizingOptions] = React.useState(() => - readColumnSizes(collection.databaseId, collection.id(), ["id"].concat(columnHeaders.partitionKeyHeaders)), - ); + const [columnSizingOptions, setColumnSizingOptions] = React.useState(() => { + const columnIds = ["id"].concat(columnHeaders.partitionKeyHeaders); + const columnSizesMap: ColumnSizesMap = readSubComponentState("ColumnSizes", collection, {}); + const columnSizesPx: ColumnSizesMap = {}; + columnIds.forEach((columnId) => { + columnSizesPx[columnId] = (columnSizesMap && columnSizesMap[columnId]) || defaultSize; + }); + return columnSizesPx; + }); const onColumnResize = React.useCallback((_, { columnId, width }) => { setColumnSizingOptions((state) => { @@ -84,7 +100,7 @@ export const DocumentsTableComponent: React.FC = }, }; - saveColumnSizes(collection.databaseId, collection.id(), newSizingOptions); + saveSubComponentState("ColumnSizes", collection, newSizingOptions, true); return newSizingOptions; });