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

Commit 0322851

Browse files
committed
fix of options dont been updated on memory (was ok on disk)
1 parent b368eed commit 0322851

File tree

6 files changed

+50
-57
lines changed

6 files changed

+50
-57
lines changed

src/cdm/ComponentsModel.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export type RowSelectOption = {
88
label: string,
99
}
1010

11+
export type ColumnOption = {
12+
value: string;
13+
label: string;
14+
color: string;
15+
}
16+
1117
export type SelectValue = {
1218
label: string;
1319
value: string;

src/cdm/TableStateInterface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SortingState } from "@tanstack/react-table";
22
import { ConfigColumn, RowDataType, TableColumn } from "cdm/FolderModel";
33
import { FilterSettings, GlobalSettings, LocalSettings } from "cdm/SettingsModel";
4+
import { ColumnOption } from "cdm/ComponentsModel";
45
import { DatabaseView } from "DatabaseView";
56
import { Literal } from "obsidian-dataview";
67
import { StoreApi, UseBoundStore } from "zustand";
@@ -80,6 +81,7 @@ export interface ColumnsState {
8081
getAllColumns: () => TableColumn[];
8182
getValueOfAllColumnsAsociatedWith: <K extends keyof TableColumn>(key: K) => TableColumn[K][];
8283
getVisibilityRecord: () => Record<string, boolean>;
84+
getColumnOptions: (id: string) => ColumnOption[];
8385
}
8486
}
8587
export interface ColumnSortingState {

src/components/cellTypes/SelectCell.tsx

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const SelectCell = (popperProps: CellComponentProps) => {
2424
const selectRow = tableState.data((state) => state.rows[row.index]);
2525
const columnsInfo = tableState.columns((state) => state.info);
2626
const configInfo = tableState.configState((state) => state.info);
27-
const selectCell = tableState.data(
27+
const cellValue = tableState.data(
2828
(state) =>
2929
ParseService.parseRowToCell(
3030
state.rows[row.index],
@@ -37,44 +37,28 @@ const SelectCell = (popperProps: CellComponentProps) => {
3737
const [showSelect, setShowSelect] = useState(false);
3838

3939
const columnActions = tableState.columns((state) => state.actions);
40-
40+
const columnOptions = columnsInfo.getColumnOptions(column.id);
4141
function getColor() {
42-
const match = tableColumn.options.find(
43-
(option: { label: string }) => option.label === selectCell
42+
const match = columnOptions.find(
43+
(option: { label: string }) => option.label === cellValue
4444
);
4545
if (match) {
46-
return match.backgroundColor;
46+
return match.color;
4747
} else {
4848
// In case of new select, generate random color
4949
const color = randomColor();
50-
columnActions.addOptionToColumn(tableColumn, selectCell, color);
50+
columnActions.addOptionToColumn(tableColumn, cellValue, color);
5151
return color;
5252
}
5353
}
5454

5555
const defaultValue = useMemo(
5656
() => ({
57-
label: selectCell?.toString(),
58-
value: selectCell?.toString(),
59-
color: selectCell ? getColor() : grey(200),
57+
label: cellValue?.toString(),
58+
value: cellValue?.toString(),
59+
color: cellValue ? getColor() : grey(200),
6060
}),
61-
[selectCell]
62-
);
63-
64-
const multiOptions = useMemo(
65-
() =>
66-
tableColumn.options
67-
.filter(
68-
(option: RowSelectOption) =>
69-
option && option.label !== undefined && option.label !== null
70-
)
71-
.sort((a, b) => a.label.localeCompare(b.label))
72-
.map((option: RowSelectOption) => ({
73-
value: option.label,
74-
label: option.label,
75-
color: option.backgroundColor,
76-
})),
77-
[selectRow]
61+
[cellValue]
7862
);
7963

8064
const handleOnChange = async (
@@ -89,27 +73,23 @@ const SelectCell = (popperProps: CellComponentProps) => {
8973
);
9074

9175
// Update on disk & memory
92-
dataActions.updateCell(
76+
await dataActions.updateCell(
9377
row.index,
9478
tableColumn,
9579
newCell,
9680
columnsInfo.getAllColumns(),
9781
configInfo.getLocalSettings(),
9882
true
9983
);
100-
// Add new option to column options
10184

102-
if (
103-
selectValue &&
104-
!tableColumn.options.find((option) => option.label === selectValue)
105-
) {
85+
// Add new option to column options
86+
if (actionMeta.action === "create-option") {
10687
await columnActions.addOptionToColumn(
10788
tableColumn,
10889
selectValue,
10990
randomColor()
11091
);
11192
}
112-
setShowSelect(false);
11393
};
11494

11595
function SelectComponent() {
@@ -127,8 +107,8 @@ const SelectCell = (popperProps: CellComponentProps) => {
127107
IndicatorSeparator: () => null,
128108
}}
129109
styles={CustomTagsStyles}
130-
options={multiOptions}
131-
onBlur={() => setShowSelect(false)}
110+
options={columnsInfo.getColumnOptions(column.id)}
111+
onMenuClose={() => setShowSelect(false)}
132112
onChange={handleOnChange}
133113
isMulti={false}
134114
menuPortalTarget={activeDocument.body}
@@ -160,9 +140,9 @@ const SelectCell = (popperProps: CellComponentProps) => {
160140
onClick={() => setShowSelect(true)}
161141
style={{ width: column.getSize() }}
162142
>
163-
{selectCell && (
164-
<Relationship value={selectCell} backgroundColor={getColor()} />
165-
)}
143+
{cellValue ? (
144+
<Relationship value={cellValue} backgroundColor={getColor()} />
145+
) : null}
166146
</div>
167147
)}
168148
</>

src/components/cellTypes/TagsCell.tsx

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ const TagsCell = (tagsProps: CellComponentProps) => {
3838
// Tags reference state
3939
const [showSelectTags, setShowSelectTags] = useState(false);
4040

41+
const columnOptions = columnsInfo.getColumnOptions(column.id);
4142
function getColor(tag: string) {
42-
const match = tableColumn.options.find(
43+
const match = columnOptions.find(
4344
(option: { label: string }) => option.label === tag
4445
);
4546
if (match) {
46-
return match.backgroundColor;
47+
return match.color;
4748
} else {
4849
// In case of new tag, generate random color
4950
const color = randomColor();
@@ -62,22 +63,6 @@ const TagsCell = (tagsProps: CellComponentProps) => {
6263
}));
6364
}, [tagsCell]);
6465

65-
const multiOptions = useMemo(
66-
() =>
67-
tableColumn.options
68-
.filter(
69-
(option: RowSelectOption) =>
70-
option && option.label !== undefined && option.label !== null
71-
)
72-
.sort((a, b) => a.label.localeCompare(b.label))
73-
.map((option: RowSelectOption) => ({
74-
value: option.label,
75-
label: option.label,
76-
color: option.backgroundColor,
77-
})),
78-
[tagsCell]
79-
);
80-
8166
const handleOnChange = async (
8267
newValue: OnChangeValue<SelectValue, true>,
8368
actionMeta: ActionMeta<RowSelectOption>
@@ -125,7 +110,7 @@ const TagsCell = (tagsProps: CellComponentProps) => {
125110
openMenuOnFocus
126111
menuPosition="fixed"
127112
styles={CustomTagsStyles}
128-
options={multiOptions}
113+
options={columnOptions}
129114
onBlur={() => setShowSelectTags(false)}
130115
onChange={handleOnChange}
131116
menuPortalTarget={activeDocument.body}

src/stateManagement/columns/handlers/InfoColumnFunctions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ColumnOption, RowSelectOption } from "cdm/ComponentsModel";
12
import { TableColumn } from "cdm/FolderModel";
23
import { ColumnsState, TableActionResponse } from "cdm/TableStateInterface";
34
import { AbstractTableAction } from "stateManagement/AbstractTableAction";
@@ -21,6 +22,24 @@ export default class InfoColumnFunctions extends AbstractTableAction<ColumnsStat
2122
return get().columns;
2223
}
2324

25+
implementation.info.getColumnOptions = (id: string): ColumnOption[] => {
26+
const opColumn = get().columns.find((c) => c.id === id);
27+
if (!opColumn) {
28+
return [];
29+
}
30+
return opColumn.options
31+
.filter(
32+
(option: RowSelectOption) =>
33+
option && option.label !== undefined && option.label !== null
34+
)
35+
.sort((a, b) => a.label.localeCompare(b.label))
36+
.map((option: RowSelectOption) => ({
37+
value: option.label,
38+
label: option.label,
39+
color: option.backgroundColor,
40+
}))
41+
};
42+
2443
response.implementation = implementation;
2544
return this.goNext(response);
2645
}

src/stateManagement/useColumnsStore.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function emptyColumnsState(): Omit<ColumnsState, "columns" | "shadowColumns"> {
4040
getValueOfAllColumnsAsociatedWith: null,
4141
getVisibilityRecord: null,
4242
getAllColumns: null,
43+
getColumnOptions: null,
4344
},
4445
};
4546
}

0 commit comments

Comments
 (0)