Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 98ff640

Browse files
committed
migrating all to useSelector hook of zustand. Demo
1 parent 1d2ae24 commit 98ff640

File tree

8 files changed

+51
-36
lines changed

8 files changed

+51
-36
lines changed

src/cdm/TableStateInterface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export interface ConfigState {
2727
alterFilters: (filters: Partial<FilterSettings>) => void;
2828
alterConfig: (config: Partial<LocalSettings>) => void;
2929
}
30+
info: {
31+
getLocalSettings: () => LocalSettings;
32+
}
3033
}
3134

3235
export interface DataState {
@@ -57,6 +60,7 @@ export interface ColumnsState {
5760
alterIsHidden: (column: TableColumn, isHidden: boolean) => void;
5861
}
5962
info: {
63+
getAllColumns: () => TableColumn[];
6064
getValueOfAllColumnsAsociatedWith: <K extends keyof TableColumn>(key: K) => TableColumn[K][];
6165
getVisibilityRecord: () => Record<string, boolean>;
6266
}

src/components/cellTypes/CheckboxCell.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ function CheckboxCell(props: CellComponentProps) {
88
const { row, column, table } = defaultCell;
99
const tableColumn = column.columnDef as TableColumn;
1010

11-
const [rows, dataActions] = table.options.meta.tableState.data((state) => [
12-
state.rows,
13-
state.actions,
14-
]);
15-
const columns = table.options.meta.tableState.columns(
16-
(state) => state.columns
11+
const dataActions = table.options.meta.tableState.data(
12+
(state) => state.actions
1713
);
18-
const ddbbConfig = table.options.meta.tableState.configState(
19-
(state) => state.ddbbConfig
14+
15+
const checkboxCell = table.options.meta.tableState.data(
16+
(state) => state.rows[row.index]
17+
);
18+
19+
const columnsInfo = table.options.meta.tableState.columns(
20+
(state) => state.info
2021
);
22+
23+
const configInfo = table.options.meta.tableState.configState(
24+
(state) => state.info
25+
);
26+
2127
/** state of cell value */
2228
const [checked, setChecked] = useState(
23-
Boolean(rows[row.index][tableColumn.key])
29+
Boolean(checkboxCell[tableColumn.key])
2430
);
2531
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
2632
const newValue = event.target.checked ? 1 : 0;
@@ -29,8 +35,8 @@ function CheckboxCell(props: CellComponentProps) {
2935
row.index,
3036
column.columnDef as TableColumn,
3137
newValue,
32-
columns,
33-
ddbbConfig
38+
columnsInfo.getAllColumns(),
39+
configInfo.getLocalSettings()
3440
);
3541
setChecked(event.target.checked);
3642
};

src/components/cellTypes/EditorCell.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const EditorCell = (props: EditorCellComponentProps) => {
1111
/** Ref to cell container */
1212
const editableMdRef = useRef<HTMLInputElement>();
1313
/** Columns information */
14-
const columns = table.options.meta.tableState.columns(
15-
(state) => state.columns
14+
const columnsInfo = table.options.meta.tableState.columns(
15+
(state) => state.info
1616
);
1717
const dataActions = table.options.meta.tableState.data(
1818
(state) => state.actions
1919
);
20-
const ddbbConfig = table.options.meta.tableState.configState(
21-
(state) => state.ddbbConfig
20+
const configInfo = table.options.meta.tableState.configState(
21+
(state) => state.info
2222
);
2323

2424
const [editorValue, setEditorValue] = useState(cellValue);
@@ -48,8 +48,8 @@ const EditorCell = (props: EditorCellComponentProps) => {
4848
row.index,
4949
column.columnDef as TableColumn,
5050
changedValue.trim(),
51-
columns,
52-
ddbbConfig
51+
columnsInfo.getAllColumns(),
52+
configInfo.getLocalSettings()
5353
);
5454
};
5555

src/components/cellTypes/NumberCell.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const NumberCell = (props: CellComponentProps) => {
77
const { cell, row, column, table } = defaultCell;
88

99
/** Columns information */
10-
const columns = table.options.meta.tableState.columns(
11-
(state) => state.columns
10+
const columnsInfo = table.options.meta.tableState.columns(
11+
(state) => state.info
1212
);
1313
const dataActions = table.options.meta.tableState.data(
1414
(state) => state.actions
1515
);
16-
const ddbbConfig = table.options.meta.tableState.configState(
17-
(state) => state.ddbbConfig
16+
const configInfo = table.options.meta.tableState.configState(
17+
(state) => state.info
1818
);
1919

2020
const [cellValue, setCellValue] = useState(cell.getValue());
@@ -49,8 +49,8 @@ const NumberCell = (props: CellComponentProps) => {
4949
row.index,
5050
column.columnDef as TableColumn,
5151
changedValue,
52-
columns,
53-
ddbbConfig
52+
columnsInfo.getAllColumns(),
53+
configInfo.getLocalSettings()
5454
);
5555
}
5656

src/components/portals/CalendarPortal.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ import { TableColumn } from "cdm/FolderModel";
99
const CalendarPortal = (calendarProps: CellComponentProps) => {
1010
const { defaultCell } = calendarProps;
1111
const { row, column, table } = defaultCell;
12+
const { tableState } = table.options.meta;
1213
const tableColumn = column.columnDef as TableColumn;
13-
const [rows, dataActions] = table.options.meta.tableState.data((state) => [
14-
state.rows,
15-
state.actions,
16-
]);
17-
const columns = table.options.meta.tableState.columns(
18-
(state) => state.columns
19-
);
20-
const ddbbConfig = table.options.meta.tableState.configState(
21-
(state) => state.ddbbConfig
22-
);
14+
const dataActions = tableState.data((state) => state.actions);
15+
16+
const calendarPortalRow = tableState.data((state) => state.rows[row.index]);
17+
18+
const columns = tableState.columns((state) => state.columns);
19+
const ddbbConfig = tableState.configState((state) => state.ddbbConfig);
2320

2421
/** state of cell value */
2522
const [showDatePicker, setShowDatePicker] = useState(false);
2623
const [calendarValue, setCalendarValue] = useState(
27-
rows[row.index][tableColumn.key]
24+
calendarPortalRow[tableColumn.key]
2825
);
2926

3027
function handleSpanOnClick(event: any) {

src/stateManagement/columns/handlers/InfoColumnFunctions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default class InfoColumnFunctions extends AbstractTableAction<ColumnsStat
1717
return visibilityRecord;
1818
};
1919

20+
implementation.info.getAllColumns = () => {
21+
return get().columns;
22+
}
23+
2024
response.implementation = implementation;
2125
return this.goNext(response);
2226
}

src/stateManagement/useColumnsStore.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function mockColumnsState(): ColumnsState {
4040
info: {
4141
getValueOfAllColumnsAsociatedWith: null,
4242
getVisibilityRecord: null,
43+
getAllColumns: null,
4344
},
4445
};
4546
}

src/stateManagement/useConfigStore.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const useConfigStore = (view: DatabaseView) => {
77
const { global_settings } = view.plugin.settings;
88
const { config, filters } = view.diskConfig.yaml;
99
return create<ConfigState>()(
10-
(set) => ({
10+
(set, get) => ({
1111
ddbbConfig: config,
1212
filters: filters,
1313
global: global_settings,
@@ -22,7 +22,10 @@ const useConfigStore = (view: DatabaseView) => {
2222
view.diskConfig.updateConfig(alteredConfig);
2323
return ({ ddbbConfig: { ...state.ddbbConfig, ...alteredConfig } });
2424
}),
25-
}
25+
},
26+
info: {
27+
getLocalSettings: () => get().ddbbConfig,
28+
},
2629
})
2730
);
2831
}

0 commit comments

Comments
 (0)