From 8b37f1cbc30cb22d49e07e811bc57189bfdef166 Mon Sep 17 00:00:00 2001 From: Stephanie Roy Date: Mon, 20 Mar 2023 11:51:12 -0400 Subject: [PATCH 1/2] Create multiple Redux actions for the table data --- webview/src/experiments/components/App.tsx | 67 ++++++++++- .../components/table/tableDataSlice.ts | 106 +++++++++++++----- 2 files changed, 142 insertions(+), 31 deletions(-) diff --git a/webview/src/experiments/components/App.tsx b/webview/src/experiments/components/App.tsx index 3d0543b83e..bfdc822894 100644 --- a/webview/src/experiments/components/App.tsx +++ b/webview/src/experiments/components/App.tsx @@ -6,7 +6,22 @@ import { } from 'dvc/src/webview/contract' import { TableData } from 'dvc/src/experiments/webview/contract' import Experiments from './Experiments' -import { update } from './table/tableDataSlice' +import { + update, + updateChanges, + updateColumnOrder, + updateColumns, + updateColumnWidths, + updateFilteredCounts, + updateFilters, + updateHasCheckpoints, + updateHasColumns, + updateHasConfig, + updateHasRunningExperiment, + updateHasValidDvcYaml, + updateRows, + updateSorts +} from './table/tableDataSlice' import { useVsCodeMessaging } from '../../shared/hooks/useVsCodeMessaging' export const App: React.FC> = () => { @@ -14,9 +29,57 @@ export const App: React.FC> = () => { useVsCodeMessaging( useCallback( + // eslint-disable-next-line sonarjs/cognitive-complexity ({ data }: { data: MessageToWebview }) => { if (data.type === MessageToWebviewType.SET_DATA) { - dispatch(update(data.data)) + dispatch(update(!!data.data)) + for (const key of Object.keys(data.data)) { + switch (key) { + case 'changes': + dispatch(updateChanges(data.data.changes)) + continue + case 'columnOrder': + dispatch(updateColumnOrder(data.data.columnOrder)) + continue + case 'columns': + dispatch(updateColumns(data.data.columns)) + continue + case 'columnsWidths': + dispatch(updateColumnWidths(data.data.columnWidths)) + continue + case 'filteredCounts': + dispatch(updateFilteredCounts(data.data.filteredCounts)) + continue + case 'filters': + dispatch(updateFilters(data.data.filters)) + continue + case 'hasCheckpoints': + dispatch(updateHasCheckpoints(data.data.hasCheckpoints)) + continue + case 'hasColumns': + dispatch(updateHasColumns(data.data.hasColumns)) + continue + case 'hasConfig': + dispatch(updateHasConfig(data.data.hasConfig)) + continue + case 'hasRunningExperiment': + dispatch( + updateHasRunningExperiment(data.data.hasRunningExperiment) + ) + continue + case 'hasValidDvcYaml': + dispatch(updateHasValidDvcYaml(data.data.hasValidDvcYaml)) + continue + case 'rows': + dispatch(updateRows(data.data.rows)) + continue + case 'sorts': + updateSorts(data.data.sorts) + continue + default: + continue + } + } } }, [dispatch] diff --git a/webview/src/experiments/components/table/tableDataSlice.ts b/webview/src/experiments/components/table/tableDataSlice.ts index caa4b50df4..7ab584a952 100644 --- a/webview/src/experiments/components/table/tableDataSlice.ts +++ b/webview/src/experiments/components/table/tableDataSlice.ts @@ -33,39 +33,87 @@ export const tableDataSlice = createSlice({ initialState: tableDataInitialState, name: 'tableData', reducers: { - update: (state, action: PayloadAction) => { - if (action.payload) { - return { - ...state, - ...action.payload, - columnWidths: keepReferenceIfEqual( - state.columnWidths, - action.payload.columnWidths - ) as Record, - columns: keepEqualOldReferencesInArray( - state.columns, - action.payload.columns - ) as Column[], - filteredCounts: keepReferenceIfEqual( - state.filteredCounts, - action.payload.filteredCounts - ) as FilteredCounts, - hasData: true, - rows: keepEqualOldReferencesInArray( - state.rows, - action.payload.rows - ) as Row[], - sorts: keepEqualOldReferencesInArray( - state.sorts, - action.payload.sorts - ) as SortDefinition[] - } + update: (state, action: PayloadAction) => { + state.hasData = action.payload + if (!action.payload) { + return tableDataInitialState } - return tableDataInitialState + }, + updateChanges: (state, action: PayloadAction) => { + state.changes = action.payload + }, + updateColumnOrder: (state, action: PayloadAction) => { + state.columnOrder = action.payload + }, + updateColumnWidths: ( + state, + action: PayloadAction> + ) => { + state.columnWidths = keepReferenceIfEqual( + state.columnWidths, + action.payload + ) as Record + }, + updateColumns: (state, action: PayloadAction) => { + state.columns = keepEqualOldReferencesInArray( + state.columns, + action.payload + ) as Column[] + }, + updateFilteredCounts: (state, action: PayloadAction) => { + state.filteredCounts = keepReferenceIfEqual( + state.filteredCounts, + action.payload + ) as FilteredCounts + }, + updateFilters: (state, action: PayloadAction) => { + state.filters = action.payload + }, + updateHasCheckpoints: (state, action: PayloadAction) => { + state.hasCheckpoints = action.payload + }, + updateHasColumns: (state, action: PayloadAction) => { + state.hasColumns = action.payload + }, + updateHasConfig: (state, action: PayloadAction) => { + state.hasConfig = action.payload + }, + updateHasRunningExperiment: (state, action: PayloadAction) => { + state.hasRunningExperiment = action.payload + }, + updateHasValidDvcYaml: (state, action: PayloadAction) => { + state.hasValidDvcYaml = action.payload + }, + updateRows: (state, action: PayloadAction) => { + state.rows = keepEqualOldReferencesInArray( + state.rows, + action.payload + ) as Row[] + }, + updateSorts: (state, action: PayloadAction) => { + state.sorts = keepEqualOldReferencesInArray( + state.sorts, + action.payload + ) as SortDefinition[] } } }) -export const { update } = tableDataSlice.actions +export const { + update, + updateChanges, + updateColumnOrder, + updateColumnWidths, + updateColumns, + updateFilteredCounts, + updateFilters, + updateHasCheckpoints, + updateHasColumns, + updateHasConfig, + updateHasRunningExperiment, + updateHasValidDvcYaml, + updateRows, + updateSorts +} = tableDataSlice.actions export default tableDataSlice.reducer From a44a5c6ecd2f28af3e63f3bed7d6937f626b59cb Mon Sep 17 00:00:00 2001 From: Stephanie Roy Date: Mon, 20 Mar 2023 13:21:54 -0400 Subject: [PATCH 2/2] Add missing dispatch --- webview/src/experiments/components/App.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webview/src/experiments/components/App.tsx b/webview/src/experiments/components/App.tsx index bfdc822894..2c6cd53a5f 100644 --- a/webview/src/experiments/components/App.tsx +++ b/webview/src/experiments/components/App.tsx @@ -74,7 +74,7 @@ export const App: React.FC> = () => { dispatch(updateRows(data.data.rows)) continue case 'sorts': - updateSorts(data.data.sorts) + dispatch(updateSorts(data.data.sorts)) continue default: continue