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

Commit edbfd12

Browse files
committed
migration set label
1 parent 1d972e4 commit edbfd12

File tree

6 files changed

+117
-107
lines changed

6 files changed

+117
-107
lines changed

src/cdm/TableStateInterface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface DataState {
1818
addRow: (filename: string, columns: TableColumn[], ddbbConfig: LocalSettings) => void;
1919
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, isMovingFile?: boolean) => void;
2020
parseDataOfColumn: (column: TableColumn, input: string, ddbbConfig: LocalSettings) => void;
21+
updateDataAfterLabelChange: (column: TableColumn, label: string, columns: TableColumn[], ddbbConfig: LocalSettings) => void;
2122
removeRow: (row: RowDataType) => void;
2223
removeDataOfColumn: (column: TableColumn) => void;
2324
}
@@ -31,6 +32,7 @@ export interface ColumnsState {
3132
alterSorting: (column: TableColumn) => void;
3233
addOptionToColumn: (column: TableColumn, option: string, backgroundColor: string) => void;
3334
alterColumnType: (column: TableColumn, input: string, parsedRows?: RowDataType[]) => void;
35+
alterColumnLabel: (column: TableColumn, label: string) => void;
3436
}
3537
export interface ColumnSortingState {
3638
state: SortingState;

src/components/HeaderMenu.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActionTypes, InputType, StyleVariables } from "helpers/Constants";
1+
import { InputType, StyleVariables } from "helpers/Constants";
22
import { dbTrim, c, getLabelHeader } from "helpers/StylesHelper";
33
import AdjustmentsIcon from "components/img/AdjustmentsIcon";
44
import React, { useEffect, useState } from "react";
@@ -12,9 +12,14 @@ import { TableColumn } from "cdm/FolderModel";
1212

1313
const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
1414
const { table, column } = headerMenuProps.headerProps;
15-
const dispatch = table.options.meta.dispatch;
16-
const columns = table.options.meta.tableState.columns(
17-
(state) => state.columns
15+
const [columns, alterColumnLabel] = table.options.meta.tableState.columns(
16+
(state) => [state.columns, state.alterColumnLabel]
17+
);
18+
const updateDataAfterLabelChange = table.options.meta.tableState.data(
19+
(state) => state.updateDataAfterLabelChange
20+
);
21+
const ddbbConfig = table.options.meta.tableState.configState(
22+
(state) => state.ddbbConfig
1823
);
1924
/** Header props */
2025
const {
@@ -122,14 +127,14 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
122127
table.setColumnSizing(updateSizeWithNewKey);
123128

124129
setkeyState(newKey);
125-
dispatch({
126-
type: ActionTypes.UPDATE_COLUMN_LABEL,
127-
columnId: column.id,
128-
accessorKey: newKey,
129-
newKey: newKey,
130-
label: labelState,
131-
state: table.options.meta.tableState,
132-
});
130+
updateDataAfterLabelChange(
131+
column.columnDef as TableColumn,
132+
labelState,
133+
columns,
134+
ddbbConfig
135+
);
136+
137+
alterColumnLabel(column.columnDef as TableColumn, labelState);
133138
}
134139

135140
function handleKeyDown(e: any) {

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,50 +87,6 @@ export function databaseReducer(state: TableDataType, action: any) {
8787
},
8888
});
8989

90-
/**
91-
* Check if the given option cell is a candidate for moving the file into a subfolder
92-
*/
93-
case ActionTypes.UPDATE_OPTION_CELL:
94-
// check if this column is configured as a group folder
95-
if (dbconfig.group_folder_column === action.key) {
96-
moveFile(`${state.view.file.parent.path}/${action.value}`, action);
97-
action.row[
98-
MetadataColumns.FILE
99-
] = `[[${state.view.file.parent.path}/${action.value}/${action.file.name}|${action.file.basename}]]`;
100-
// Check if action.value is a valid folder name
101-
const auxPath =
102-
action.value !== ""
103-
? `${state.view.file.parent.path}/${action.value}/${action.file.name}`
104-
: `${state.view.file.parent.path}/${action.file.name}`;
105-
106-
action.row.original.__note__ = new NoteInfo({
107-
...action.row,
108-
file: {
109-
path: auxPath,
110-
},
111-
});
112-
// Update original cell value
113-
const update_option_cell_index = state.view.columns.findIndex(
114-
(column) => column.id === action.columnId
115-
);
116-
const update_option_cell_column_key =
117-
state.view.columns[update_option_cell_index].key;
118-
return update(state, {
119-
view: {
120-
rows: {
121-
[action.row.index]: {
122-
$merge: {
123-
[MetadataColumns.FILE]: action.row[MetadataColumns.FILE],
124-
note: action.row.original.__note__,
125-
[update_option_cell_column_key]: action.value,
126-
},
127-
},
128-
},
129-
},
130-
});
131-
}
132-
break;
133-
13490
/**
13591
* Enable reset
13692
*/

src/helpers/Constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { DatabaseSettings } from "cdm/SettingsModel";
55
/** Table Actions */
66
export const ActionTypes = Object.freeze({
77
UPDATE_COLUMN_LABEL: 'update_column_label',
8-
UPDATE_OPTION_CELL: 'update_option_cell',
98
ENABLE_RESET: 'enable_reset',
109
});
1110

src/stateManagement/useColumnsStore.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
TableColumnsTemplate,
1111
} from "helpers/Constants";
1212
import { obtainUniqueOptionValues } from "helpers/SelectHelper";
13+
import { dbTrim } from "helpers/StylesHelper";
1314
import create from "zustand";
1415

1516
const useColumnsStore = (view: DatabaseView) => {
@@ -139,6 +140,21 @@ const useColumnsStore = (view: DatabaseView) => {
139140
}
140141
return { columns: updater.columns };
141142
}),
143+
alterColumnLabel: (column: TableColumn, label: string) =>
144+
set((updater) => {
145+
const labelIndex = updater.columns.findIndex(
146+
(col: TableColumn) => col.id === column.id
147+
);
148+
const newKey = dbTrim(label);
149+
updater.columns[labelIndex].label = label;
150+
updater.columns[labelIndex].id = newKey;
151+
updater.columns[labelIndex].key = newKey;
152+
updater.columns[labelIndex].accessorKey = newKey;
153+
154+
// Update configuration & row files on disk
155+
view.diskConfig.updateColumnKey(column.id, newKey, label);
156+
return { columns: updater.columns };
157+
}),
142158
}));
143159
};
144160
/**

src/stateManagement/useDataStore.ts

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LocalSettings } from "cdm/SettingsModel";
44
import { DataState } from "cdm/TableStateInterface";
55
import { DatabaseView } from "DatabaseView";
66
import { MetadataColumns, UpdateRowOptions } from "helpers/Constants";
7+
import { dbTrim } from "helpers/StylesHelper";
78
import { moveFile, updateRowFileProxy } from "helpers/VaultManagement";
89
import { DateTime } from "luxon";
910
import { Literal } from "obsidian-dataview";
@@ -102,6 +103,32 @@ const useDataStore = (view: DatabaseView) => {
102103
return { rows: [...state.rows.slice(0, rowIndex), row, ...state.rows.slice(rowIndex + 1)] };
103104
}
104105
),
106+
updateDataAfterLabelChange: (column: TableColumn, label: string, columns: TableColumn[], ddbbConfig: LocalSettings) => set((state) => {
107+
const newKey = dbTrim(label);
108+
// Save on disk
109+
Promise.all(
110+
state.rows.map(async (row: RowDataType) => {
111+
updateRowFileProxy(
112+
row.__note__.getFile(),
113+
column.id,
114+
newKey,
115+
columns,
116+
ddbbConfig,
117+
UpdateRowOptions.COLUMN_KEY
118+
);
119+
}));
120+
121+
// Save on memory
122+
const alterRows = state.rows.map((row) => {
123+
row[newKey] = row[column.id];
124+
delete row[column.id];
125+
return row;
126+
});
127+
128+
return { rows: alterRows };
129+
}
130+
),
131+
105132
parseDataOfColumn: (column: TableColumn, input: string, ddbbConfig: LocalSettings) => {
106133
set((updater) => {
107134
const parsedRows = updater.rows.map((row) => ({
@@ -126,62 +153,67 @@ const useDataStore = (view: DatabaseView) => {
126153
return { rows: newRows };
127154
}),
128155
}),
156+
129157
);
130158
}
131159
export default useDataStore;
132-
133160
/**
134-
*
135-
* dispatch({
136-
type: ActionTypes.UPDATE_OPTION_CELL,
137-
file: note.getFile(),
138-
key: tableColumn.key,
139-
value: option.label,
140-
row: row,
141-
columnId: column.id,
142-
state: table.options.meta,
143-
});
144-
145-
146-
147-
* case ActionTypes.UPDATE_OPTION_CELL:
148-
// check if this column is configured as a group folder
149-
if (dbconfig.group_folder_column === action.key) {
150-
moveFile(`${state.view.file.parent.path}/${action.value}`, action);
151-
action.row[
152-
MetadataColumns.FILE
153-
] = `[[${state.view.file.parent.path}/${action.value}/${action.file.name}|${action.file.basename}]]`;
154-
// Check if action.value is a valid folder name
155-
const auxPath =
156-
action.value !== ""
157-
? `${state.view.file.parent.path}/${action.value}/${action.file.name}`
158-
: `${state.view.file.parent.path}/${action.file.name}`;
161+
* case ActionTypes.UPDATE_COLUMN_LABEL:
162+
const update_column_label_index = state.view.columns.findIndex(
163+
(column: any) => column.id === action.columnId
164+
);
159165
160-
action.row.original.__note__ = new NoteInfo({
161-
...action.row,
162-
file: {
163-
path: auxPath,
164-
},
165-
});
166-
// Update original cell value
167-
const update_option_cell_index = state.view.columns.findIndex(
168-
(column) => column.id === action.columnId
169-
);
170-
const update_option_cell_column_key =
171-
state.view.columns[update_option_cell_index].key;
172-
return update(state, {
173-
view: {
174-
rows: {
175-
[action.row.index]: {
176-
$merge: {
177-
[MetadataColumns.FILE]: action.row[MetadataColumns.FILE],
178-
note: action.row.original.__note__,
179-
[update_option_cell_column_key]: action.value,
180-
},
166+
// Update configuration & row files on disk
167+
state.view.diskConfig.updateColumnKey(
168+
action.columnId,
169+
action.newKey,
170+
action.label
171+
);
172+
// Promise.all(
173+
// state.view.rows.map(async (row: RowDataType) => {
174+
// await updateRowFileProxy(
175+
// row.__note__.getFile(),
176+
// action.columnId,
177+
// action.newKey,
178+
// action.state,
179+
// UpdateRowOptions.COLUMN_KEY
180+
// );
181+
// })
182+
// );
183+
return update(state, {
184+
skipReset: { $set: true },
185+
// Modify column visually with the new label
186+
view: {
187+
columns: {
188+
$set: [
189+
...state.view.columns.slice(0, update_column_label_index),
190+
{
191+
...state.view.columns[update_column_label_index],
192+
label: action.label,
193+
id: action.newKey,
194+
key: action.newKey,
195+
accessorKey: action.newKey,
181196
},
197+
...state.view.columns.slice(
198+
update_column_label_index + 1,
199+
state.view.columns.length
200+
),
201+
],
202+
},
203+
// Modify data visually with the new key
204+
rows: {
205+
$set: state.view.rows.map((row: RowDataType) => {
206+
row[action.newKey] = row[action.columnId];
207+
delete row[action.columnId];
208+
return row;
209+
}),
210+
},
211+
// Update view yaml state
212+
diskConfig: {
213+
yaml: {
214+
$set: state.view.diskConfig.yaml,
182215
},
183216
},
184-
});
185-
}
186-
break;
217+
},
218+
});
187219
*/

0 commit comments

Comments
 (0)