@constructive-io/sheets 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +188 -0
- package/dist/advanced.cjs +5477 -0
- package/dist/advanced.cjs.map +1 -0
- package/dist/advanced.d.cts +83 -0
- package/dist/advanced.d.ts +83 -0
- package/dist/advanced.js +34 -0
- package/dist/advanced.js.map +1 -0
- package/dist/auth.cjs +1396 -0
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +60 -0
- package/dist/auth.d.ts +60 -0
- package/dist/auth.js +19 -0
- package/dist/auth.js.map +1 -0
- package/dist/chunk-4PGPVMZB.js +670 -0
- package/dist/chunk-4PGPVMZB.js.map +1 -0
- package/dist/chunk-FNVHSOQQ.js +1314 -0
- package/dist/chunk-FNVHSOQQ.js.map +1 -0
- package/dist/chunk-GGECS2BP.js +17 -0
- package/dist/chunk-GGECS2BP.js.map +1 -0
- package/dist/chunk-GLVZSPV2.js +43 -0
- package/dist/chunk-GLVZSPV2.js.map +1 -0
- package/dist/chunk-H4JJTMP5.js +160 -0
- package/dist/chunk-H4JJTMP5.js.map +1 -0
- package/dist/chunk-ODUBS2NL.js +100 -0
- package/dist/chunk-ODUBS2NL.js.map +1 -0
- package/dist/chunk-PZL2BCWF.js +168 -0
- package/dist/chunk-PZL2BCWF.js.map +1 -0
- package/dist/chunk-TU7ATZ4X.js +4494 -0
- package/dist/chunk-TU7ATZ4X.js.map +1 -0
- package/dist/chunk-X7DHTSOX.js +21 -0
- package/dist/chunk-X7DHTSOX.js.map +1 -0
- package/dist/chunk-YDQQA46V.js +13 -0
- package/dist/chunk-YDQQA46V.js.map +1 -0
- package/dist/date-editor-CXKKGNL6.js +361 -0
- package/dist/date-editor-CXKKGNL6.js.map +1 -0
- package/dist/editor-props-M6Dh0P9f.d.cts +245 -0
- package/dist/editor-props-M6Dh0P9f.d.ts +245 -0
- package/dist/index.cjs +15861 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1650 -0
- package/dist/index.d.ts +1650 -0
- package/dist/index.js +8470 -0
- package/dist/index.js.map +1 -0
- package/dist/map-picker-VTKDS6CM.js +290 -0
- package/dist/map-picker-VTKDS6CM.js.map +1 -0
- package/dist/sheets-context-CojEFlaY.d.cts +214 -0
- package/dist/sheets-context-Da55TTR9.d.ts +214 -0
- package/dist/testing.cjs +1113 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +45 -0
- package/dist/testing.d.ts +45 -0
- package/dist/testing.js +269 -0
- package/dist/testing.js.map +1 -0
- package/package.json +144 -0
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/context/sheets-context.ts
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
var SheetsContext = createContext(null);
|
|
6
|
+
function useSheetsContext() {
|
|
7
|
+
const ctx = useContext(SheetsContext);
|
|
8
|
+
if (!ctx) {
|
|
9
|
+
throw new Error("useSheetsContext must be used within a <SheetsProvider>");
|
|
10
|
+
}
|
|
11
|
+
return ctx;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/store/draft-rows-slice.ts
|
|
15
|
+
function parseDraftTableKey(tableKey) {
|
|
16
|
+
const parts = tableKey.split("::");
|
|
17
|
+
if (parts.length === 2) {
|
|
18
|
+
const [databaseId, tableName] = parts;
|
|
19
|
+
if (!databaseId || !tableName) return null;
|
|
20
|
+
return { databaseId, tableName };
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
function createDraftRowsSlice(set) {
|
|
25
|
+
return {
|
|
26
|
+
draftRowsByTable: {},
|
|
27
|
+
createDraftRow: (args) => {
|
|
28
|
+
const { tableKey, columnOrder, fieldMetaByKey, relationInfoByKey, metaVersion } = args;
|
|
29
|
+
const effectiveColumnOrder = ensureColumnOrder(columnOrder, fieldMetaByKey, relationInfoByKey);
|
|
30
|
+
const template = buildTemplate(effectiveColumnOrder, relationInfoByKey);
|
|
31
|
+
const draftRowId = `draft:${generateDraftSuffix()}`;
|
|
32
|
+
const values = initializeValuesFromTemplate(template, draftRowId);
|
|
33
|
+
set((state) => {
|
|
34
|
+
const existing = state.draftRowsByTable[tableKey];
|
|
35
|
+
const nextOrder = existing ? [...existing.order, draftRowId] : [draftRowId];
|
|
36
|
+
const nextMap = {
|
|
37
|
+
...existing?.map ?? {},
|
|
38
|
+
[draftRowId]: {
|
|
39
|
+
id: draftRowId,
|
|
40
|
+
values,
|
|
41
|
+
status: "idle",
|
|
42
|
+
errors: null,
|
|
43
|
+
createdAt: Date.now(),
|
|
44
|
+
metaVersion
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
draftRowsByTable: {
|
|
49
|
+
...state.draftRowsByTable,
|
|
50
|
+
[tableKey]: {
|
|
51
|
+
order: nextOrder,
|
|
52
|
+
map: nextMap,
|
|
53
|
+
template,
|
|
54
|
+
metaVersion,
|
|
55
|
+
columnOrder: [...effectiveColumnOrder]
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
return draftRowId;
|
|
61
|
+
},
|
|
62
|
+
updateDraftCell: ({ tableKey, draftRowId, columnKey, value, extraValues }) => {
|
|
63
|
+
set((state) => {
|
|
64
|
+
const tableState = state.draftRowsByTable[tableKey];
|
|
65
|
+
if (!tableState) return state;
|
|
66
|
+
const existingRow = tableState.map[draftRowId];
|
|
67
|
+
if (!existingRow) return state;
|
|
68
|
+
let hasValueChanges = !deepEqual(existingRow.values[columnKey], value);
|
|
69
|
+
if (!hasValueChanges && extraValues) {
|
|
70
|
+
for (const [key, extraValue] of Object.entries(extraValues)) {
|
|
71
|
+
if (!deepEqual(existingRow.values[key], extraValue)) {
|
|
72
|
+
hasValueChanges = true;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const shouldResetErrors = existingRow.status === "error" || existingRow.errors !== null;
|
|
78
|
+
if (!hasValueChanges && !shouldResetErrors) return state;
|
|
79
|
+
const updatedValues = hasValueChanges ? { ...existingRow.values, [columnKey]: deepClone(value) } : existingRow.values;
|
|
80
|
+
if (hasValueChanges && extraValues) {
|
|
81
|
+
for (const [key, extraValue] of Object.entries(extraValues)) {
|
|
82
|
+
updatedValues[key] = deepClone(extraValue);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const nextMap = {
|
|
86
|
+
...tableState.map,
|
|
87
|
+
[draftRowId]: {
|
|
88
|
+
...existingRow,
|
|
89
|
+
values: updatedValues,
|
|
90
|
+
status: existingRow.status === "error" ? "idle" : existingRow.status,
|
|
91
|
+
errors: null
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
draftRowsByTable: {
|
|
96
|
+
...state.draftRowsByTable,
|
|
97
|
+
[tableKey]: {
|
|
98
|
+
...tableState,
|
|
99
|
+
map: nextMap
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
removeDraftRow: (tableKey, draftRowId) => {
|
|
106
|
+
set((state) => {
|
|
107
|
+
const tableState = state.draftRowsByTable[tableKey];
|
|
108
|
+
if (!tableState) return state;
|
|
109
|
+
if (!tableState.map[draftRowId]) return state;
|
|
110
|
+
const nextOrder = tableState.order.filter((id) => id !== draftRowId);
|
|
111
|
+
const nextMap = { ...tableState.map };
|
|
112
|
+
delete nextMap[draftRowId];
|
|
113
|
+
const nextTableState = nextOrder.length ? { ...tableState, order: nextOrder, map: nextMap } : void 0;
|
|
114
|
+
const nextByTable = { ...state.draftRowsByTable };
|
|
115
|
+
if (nextTableState) {
|
|
116
|
+
nextByTable[tableKey] = nextTableState;
|
|
117
|
+
} else {
|
|
118
|
+
delete nextByTable[tableKey];
|
|
119
|
+
}
|
|
120
|
+
return { draftRowsByTable: nextByTable };
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
clearDraftRowsForTable: (tableKey) => {
|
|
124
|
+
set((state) => {
|
|
125
|
+
if (!state.draftRowsByTable[tableKey]) return state;
|
|
126
|
+
const nextByTable = { ...state.draftRowsByTable };
|
|
127
|
+
delete nextByTable[tableKey];
|
|
128
|
+
return { draftRowsByTable: nextByTable };
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
clearDraftRowsForDatabase: (databaseId) => {
|
|
132
|
+
set((state) => {
|
|
133
|
+
if (!databaseId || !Object.keys(state.draftRowsByTable).length) return state;
|
|
134
|
+
let changed = false;
|
|
135
|
+
const nextByTable = { ...state.draftRowsByTable };
|
|
136
|
+
for (const tableKey of Object.keys(nextByTable)) {
|
|
137
|
+
const parsed = parseDraftTableKey(tableKey);
|
|
138
|
+
const matchesDatabase = parsed ? parsed.databaseId === databaseId : tableKey.includes(`${databaseId}::`);
|
|
139
|
+
if (matchesDatabase) {
|
|
140
|
+
delete nextByTable[tableKey];
|
|
141
|
+
changed = true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return changed ? { draftRowsByTable: nextByTable } : state;
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
clearAllDraftRows: () => {
|
|
148
|
+
set((state) => {
|
|
149
|
+
if (!Object.keys(state.draftRowsByTable).length) return state;
|
|
150
|
+
return { draftRowsByTable: {} };
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
syncDraftRowsWithMeta: (args) => {
|
|
154
|
+
const { tableKey, columnOrder, fieldMetaByKey, relationInfoByKey, metaVersion } = args;
|
|
155
|
+
set((state) => {
|
|
156
|
+
const tableState = state.draftRowsByTable[tableKey];
|
|
157
|
+
const effectiveColumnOrder = ensureColumnOrder(columnOrder, fieldMetaByKey, relationInfoByKey);
|
|
158
|
+
const template = buildTemplate(effectiveColumnOrder, relationInfoByKey);
|
|
159
|
+
if (!tableState) {
|
|
160
|
+
return state;
|
|
161
|
+
}
|
|
162
|
+
if (tableState.order.length === 0) {
|
|
163
|
+
const nextByTable = { ...state.draftRowsByTable };
|
|
164
|
+
delete nextByTable[tableKey];
|
|
165
|
+
return { draftRowsByTable: nextByTable };
|
|
166
|
+
}
|
|
167
|
+
const isSameMeta = tableState.metaVersion === metaVersion && shallowEqualArrays(tableState.columnOrder, effectiveColumnOrder);
|
|
168
|
+
if (isSameMeta) return state;
|
|
169
|
+
const nextMap = {};
|
|
170
|
+
for (const draftId of tableState.order) {
|
|
171
|
+
const row = tableState.map[draftId];
|
|
172
|
+
if (!row) continue;
|
|
173
|
+
const mergedValues = initializeValuesFromTemplate(template, draftId);
|
|
174
|
+
for (const key of Object.keys(row.values)) {
|
|
175
|
+
if (Object.prototype.hasOwnProperty.call(template, key)) {
|
|
176
|
+
mergedValues[key] = deepClone(row.values[key]);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
nextMap[draftId] = {
|
|
180
|
+
...row,
|
|
181
|
+
values: mergedValues,
|
|
182
|
+
metaVersion,
|
|
183
|
+
status: row.status === "error" ? "idle" : row.status,
|
|
184
|
+
errors: row.status === "error" ? null : row.errors
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
draftRowsByTable: {
|
|
189
|
+
...state.draftRowsByTable,
|
|
190
|
+
[tableKey]: {
|
|
191
|
+
order: [...tableState.order],
|
|
192
|
+
map: nextMap,
|
|
193
|
+
template,
|
|
194
|
+
metaVersion,
|
|
195
|
+
columnOrder: [...effectiveColumnOrder]
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
setDraftRowStatus: ({ tableKey, draftRowId, status, errors }) => {
|
|
202
|
+
set((state) => {
|
|
203
|
+
const tableState = state.draftRowsByTable[tableKey];
|
|
204
|
+
if (!tableState) return state;
|
|
205
|
+
const row = tableState.map[draftRowId];
|
|
206
|
+
if (!row) return state;
|
|
207
|
+
const nextErrors = errors ?? null;
|
|
208
|
+
if (row.status === status && shallowEqualRecords(row.errors, nextErrors)) return state;
|
|
209
|
+
const nextMap = {
|
|
210
|
+
...tableState.map,
|
|
211
|
+
[draftRowId]: { ...row, status, errors: nextErrors }
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
draftRowsByTable: {
|
|
215
|
+
...state.draftRowsByTable,
|
|
216
|
+
[tableKey]: { ...tableState, map: nextMap }
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function ensureColumnOrder(columnOrder, fieldMetaByKey, relationInfoByKey) {
|
|
224
|
+
if (columnOrder.length > 0) return [...columnOrder];
|
|
225
|
+
const keys = /* @__PURE__ */ new Set();
|
|
226
|
+
Object.keys(fieldMetaByKey).forEach((key) => {
|
|
227
|
+
if (key) keys.add(key);
|
|
228
|
+
});
|
|
229
|
+
if (relationInfoByKey) {
|
|
230
|
+
Object.keys(relationInfoByKey).forEach((key) => {
|
|
231
|
+
if (key) keys.add(key);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return Array.from(keys).sort((a, b) => a.localeCompare(b));
|
|
235
|
+
}
|
|
236
|
+
function buildTemplate(columnOrder, relationInfoByKey) {
|
|
237
|
+
const template = {};
|
|
238
|
+
for (const column of columnOrder) {
|
|
239
|
+
if (!column) continue;
|
|
240
|
+
const relationInfo = relationInfoByKey?.[column];
|
|
241
|
+
if (relationInfo && (relationInfo.kind === "hasMany" || relationInfo.kind === "manyToMany")) {
|
|
242
|
+
template[column] = [];
|
|
243
|
+
} else {
|
|
244
|
+
template[column] = null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return template;
|
|
248
|
+
}
|
|
249
|
+
function generateDraftSuffix() {
|
|
250
|
+
const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
|
|
251
|
+
if (cryptoObj && typeof cryptoObj.getRandomValues === "function") {
|
|
252
|
+
const buffer = new Uint32Array(1);
|
|
253
|
+
cryptoObj.getRandomValues(buffer);
|
|
254
|
+
return buffer[0].toString(36).slice(0, 8);
|
|
255
|
+
}
|
|
256
|
+
return Math.random().toString(36).slice(2, 10);
|
|
257
|
+
}
|
|
258
|
+
function initializeValuesFromTemplate(template, draftRowId) {
|
|
259
|
+
const result = {};
|
|
260
|
+
for (const [key, value] of Object.entries(template)) {
|
|
261
|
+
result[key] = deepClone(value);
|
|
262
|
+
}
|
|
263
|
+
if (!result.id) result.id = draftRowId;
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
function deepClone(value) {
|
|
267
|
+
if (Array.isArray(value)) return value.map((item) => deepClone(item));
|
|
268
|
+
if (value instanceof Date) return new Date(value.getTime());
|
|
269
|
+
if (value && typeof value === "object") {
|
|
270
|
+
const result = {};
|
|
271
|
+
for (const [key, val] of Object.entries(value)) {
|
|
272
|
+
result[key] = deepClone(val);
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
function deepEqual(a, b) {
|
|
279
|
+
if (Object.is(a, b)) return true;
|
|
280
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
281
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
282
|
+
if (a.length !== b.length) return false;
|
|
283
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
284
|
+
if (!deepEqual(a[i], b[i])) return false;
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
289
|
+
const keysA = Object.keys(a);
|
|
290
|
+
const keysB = Object.keys(b);
|
|
291
|
+
if (keysA.length !== keysB.length) return false;
|
|
292
|
+
for (const key of keysA) {
|
|
293
|
+
if (!(key in b)) return false;
|
|
294
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
function shallowEqualRecords(a, b) {
|
|
301
|
+
if (a === b) return true;
|
|
302
|
+
if (!a || !b) return false;
|
|
303
|
+
const keysA = Object.keys(a);
|
|
304
|
+
const keysB = Object.keys(b);
|
|
305
|
+
if (keysA.length !== keysB.length) return false;
|
|
306
|
+
for (const key of keysA) {
|
|
307
|
+
if (a[key] !== b[key]) return false;
|
|
308
|
+
}
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
function shallowEqualArrays(a, b) {
|
|
312
|
+
if (a.length !== b.length) return false;
|
|
313
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
314
|
+
if (a[i] !== b[i]) return false;
|
|
315
|
+
}
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
function computeDraftMetaSignature(columnOrder, fieldMetaByKey, relationInfoByKey) {
|
|
319
|
+
const signatureParts = columnOrder.map((column) => {
|
|
320
|
+
const typeMeta = fieldMetaByKey[column]?.type;
|
|
321
|
+
const relationMeta = relationInfoByKey?.[column];
|
|
322
|
+
const typePart = typeMeta ? [
|
|
323
|
+
typeMeta.gqlType ?? "",
|
|
324
|
+
typeMeta.isArray ? "1" : "0",
|
|
325
|
+
typeMeta.pgAlias ?? "",
|
|
326
|
+
typeMeta.pgType ?? "",
|
|
327
|
+
typeMeta.subtype ?? ""
|
|
328
|
+
].join(":") : "";
|
|
329
|
+
const relationPart = relationMeta ? [relationMeta.kind ?? "", relationMeta.foreignKeyField ?? "", relationMeta.relationField ?? ""].join(":") : "";
|
|
330
|
+
return `${column}|${typePart}|${relationPart}`;
|
|
331
|
+
});
|
|
332
|
+
return hashString(signatureParts.join("||"));
|
|
333
|
+
}
|
|
334
|
+
function hashString(input) {
|
|
335
|
+
let hash = 0;
|
|
336
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
337
|
+
hash = (hash << 5) - hash + input.charCodeAt(i);
|
|
338
|
+
hash |= 0;
|
|
339
|
+
}
|
|
340
|
+
return hash.toString(16);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/store/relation-info-slice.ts
|
|
344
|
+
import { pluralize } from "inflekt";
|
|
345
|
+
import { cleanTable, pgFieldToCamelCase as pgFieldToCamelCase2 } from "@constructive-io/data";
|
|
346
|
+
|
|
347
|
+
// src/utils/relation-utils.ts
|
|
348
|
+
import { singularize } from "inflekt";
|
|
349
|
+
import { pgFieldToCamelCase } from "@constructive-io/data";
|
|
350
|
+
function codecNameToTableName(name) {
|
|
351
|
+
if (!name) return name;
|
|
352
|
+
const singular = singularize(name);
|
|
353
|
+
return singular.charAt(0).toUpperCase() + singular.slice(1);
|
|
354
|
+
}
|
|
355
|
+
function getAllRelationFields(relations, metaTables) {
|
|
356
|
+
if (!relations) return [];
|
|
357
|
+
const out = [];
|
|
358
|
+
const push = (fieldName) => {
|
|
359
|
+
if (fieldName) out.push(fieldName);
|
|
360
|
+
};
|
|
361
|
+
relations.belongsTo?.forEach((rel) => push(rel?.fieldName));
|
|
362
|
+
relations.hasOne?.forEach((rel) => push(rel?.fieldName));
|
|
363
|
+
const junctionNames = /* @__PURE__ */ new Set();
|
|
364
|
+
relations.manyToMany?.forEach((rel) => {
|
|
365
|
+
const jtName = rel?.junctionTable?.name;
|
|
366
|
+
if (!jtName) return;
|
|
367
|
+
if (metaTables) {
|
|
368
|
+
const leftFk = rel?.junctionLeftKeyAttributes?.[0]?.name;
|
|
369
|
+
const rightFk = rel?.junctionRightKeyAttributes?.[0]?.name;
|
|
370
|
+
const leftFkCamel = leftFk ? pgFieldToCamelCase(leftFk) : void 0;
|
|
371
|
+
const rightFkCamel = rightFk ? pgFieldToCamelCase(rightFk) : void 0;
|
|
372
|
+
const jtTableName = codecNameToTableName(jtName);
|
|
373
|
+
if (!isJunctionTablePure(jtTableName, leftFkCamel, rightFkCamel, metaTables)) return;
|
|
374
|
+
}
|
|
375
|
+
junctionNames.add(jtName);
|
|
376
|
+
});
|
|
377
|
+
relations.hasMany?.forEach((rel) => {
|
|
378
|
+
const refName = rel?.referencedBy?.name;
|
|
379
|
+
if (refName && junctionNames.has(refName)) return;
|
|
380
|
+
push(rel?.fieldName);
|
|
381
|
+
});
|
|
382
|
+
const seenM2N = /* @__PURE__ */ new Map();
|
|
383
|
+
relations.manyToMany?.forEach((rel) => {
|
|
384
|
+
const target = rel?.rightTable?.name;
|
|
385
|
+
if (!target) return;
|
|
386
|
+
if (metaTables) {
|
|
387
|
+
const jtName = rel?.junctionTable?.name;
|
|
388
|
+
if (jtName) {
|
|
389
|
+
const leftFk = rel?.junctionLeftKeyAttributes?.[0]?.name;
|
|
390
|
+
const rightFk = rel?.junctionRightKeyAttributes?.[0]?.name;
|
|
391
|
+
const leftFkCamel = leftFk ? pgFieldToCamelCase(leftFk) : void 0;
|
|
392
|
+
const rightFkCamel = rightFk ? pgFieldToCamelCase(rightFk) : void 0;
|
|
393
|
+
const jtTableName = codecNameToTableName(jtName);
|
|
394
|
+
if (!isJunctionTablePure(jtTableName, leftFkCamel, rightFkCamel, metaTables)) return;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
seenM2N.set(target, rel);
|
|
398
|
+
});
|
|
399
|
+
seenM2N.forEach((rel) => push(rel?.fieldName));
|
|
400
|
+
return Array.from(new Set(out));
|
|
401
|
+
}
|
|
402
|
+
function isJunctionTablePure(junctionTableName, leftFkField, rightFkField, metaTables) {
|
|
403
|
+
if (!leftFkField || !rightFkField) return true;
|
|
404
|
+
const junctionMeta = metaTables.find((t) => t?.name === junctionTableName);
|
|
405
|
+
if (!junctionMeta?.fields) return true;
|
|
406
|
+
const exemptFields = /* @__PURE__ */ new Set(["id", leftFkField, rightFkField]);
|
|
407
|
+
for (const field of junctionMeta.fields) {
|
|
408
|
+
if (!field?.name) continue;
|
|
409
|
+
const camelName = pgFieldToCamelCase(field.name);
|
|
410
|
+
if (exemptFields.has(camelName)) continue;
|
|
411
|
+
if (field.isNotNull && !field.hasDefault) return false;
|
|
412
|
+
}
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
function getJunctionTableNames(relations, metaTables) {
|
|
416
|
+
const names = /* @__PURE__ */ new Set();
|
|
417
|
+
for (const rel of relations.manyToMany) {
|
|
418
|
+
if (!rel.junctionTable) continue;
|
|
419
|
+
if (metaTables) {
|
|
420
|
+
const leftFk = rel.junctionLeftKeyFields?.[0];
|
|
421
|
+
const rightFk = rel.junctionRightKeyFields?.[0];
|
|
422
|
+
if (!isJunctionTablePure(rel.junctionTable, leftFk, rightFk, metaTables)) continue;
|
|
423
|
+
}
|
|
424
|
+
names.add(rel.junctionTable);
|
|
425
|
+
}
|
|
426
|
+
return names;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/store/relation-info-slice.ts
|
|
430
|
+
function toRelationInfoMap(info) {
|
|
431
|
+
return new Map(Object.entries(info));
|
|
432
|
+
}
|
|
433
|
+
function setTableRelationInfo(set, tableName, info) {
|
|
434
|
+
set((state) => ({
|
|
435
|
+
relationInfoCache: { ...state.relationInfoCache, [tableName]: info },
|
|
436
|
+
relationInfoMapCache: { ...state.relationInfoMapCache, [tableName]: toRelationInfoMap(info) }
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
function createRelationInfoSlice(set, get) {
|
|
440
|
+
return {
|
|
441
|
+
relationInfoCache: {},
|
|
442
|
+
relationInfoMapCache: {},
|
|
443
|
+
setRelationInfoForTable: (tableName, info) => setTableRelationInfo(set, tableName, info),
|
|
444
|
+
getRelationInfoForTable: (tableName) => get().relationInfoCache[tableName],
|
|
445
|
+
getRelationInfoMapForTable: (tableName) => get().relationInfoMapCache[tableName],
|
|
446
|
+
clearRelationInfoCache: () => set({ relationInfoCache: {}, relationInfoMapCache: {} }),
|
|
447
|
+
rebuildRelationInfo: (tableName, meta) => {
|
|
448
|
+
try {
|
|
449
|
+
const info = buildRelationInfoFromMeta(tableName, meta);
|
|
450
|
+
if (!info) return;
|
|
451
|
+
setTableRelationInfo(set, tableName, info);
|
|
452
|
+
} catch {
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
ensureRelationInfo: (tableName, meta) => {
|
|
456
|
+
const existing = get().relationInfoCache[tableName];
|
|
457
|
+
if (existing) return existing;
|
|
458
|
+
const info = buildRelationInfoFromMeta(tableName, meta) || {};
|
|
459
|
+
if (Object.keys(info).length) {
|
|
460
|
+
setTableRelationInfo(set, tableName, info);
|
|
461
|
+
}
|
|
462
|
+
return info;
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
function toCleanTables(meta) {
|
|
467
|
+
const tables = meta?._meta?.tables ?? [];
|
|
468
|
+
return tables.filter((table) => table != null).map((table) => cleanTable(table));
|
|
469
|
+
}
|
|
470
|
+
function firstFieldName(fields) {
|
|
471
|
+
return fields[0]?.name;
|
|
472
|
+
}
|
|
473
|
+
function findForeignKeyInRelatedTable(currentTableName, relatedTableName, allTables, rawMetaTables) {
|
|
474
|
+
const relatedTable = allTables.find((t) => t.name === relatedTableName);
|
|
475
|
+
if (relatedTable) {
|
|
476
|
+
const belongsTo = relatedTable.relations.belongsTo.find(
|
|
477
|
+
(rel) => rel.referencesTable === currentTableName
|
|
478
|
+
);
|
|
479
|
+
if (belongsTo?.keys?.[0]?.name) return belongsTo.keys[0].name;
|
|
480
|
+
}
|
|
481
|
+
const rawRelated = rawMetaTables.find((t) => t.name === relatedTableName);
|
|
482
|
+
if (rawRelated) {
|
|
483
|
+
const v5Fks = Array.isArray(rawRelated.constraints) ? [] : rawRelated.constraints?.foreignKey;
|
|
484
|
+
const fks = v5Fks ?? rawRelated.foreignKeyConstraints ?? [];
|
|
485
|
+
for (const fk of fks) {
|
|
486
|
+
if (!fk) continue;
|
|
487
|
+
const refName = fk.refTable?.name;
|
|
488
|
+
if (refName && refName.toLowerCase() === currentTableName.toLowerCase()) {
|
|
489
|
+
const fieldName = fk.fields?.[0]?.name;
|
|
490
|
+
if (fieldName) return pgFieldToCamelCase2(fieldName);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return void 0;
|
|
495
|
+
}
|
|
496
|
+
function buildRelationInfoFromMeta(tableName, meta) {
|
|
497
|
+
const tables = toCleanTables(meta);
|
|
498
|
+
const table = tables.find((candidate) => candidate.name === tableName);
|
|
499
|
+
if (!table) return null;
|
|
500
|
+
const rawMetaTables = (meta?._meta?.tables ?? []).filter(
|
|
501
|
+
(t) => t != null
|
|
502
|
+
);
|
|
503
|
+
const PRIORITY_FIELDS = [
|
|
504
|
+
"displayName",
|
|
505
|
+
"fullName",
|
|
506
|
+
"preferredName",
|
|
507
|
+
"nickname",
|
|
508
|
+
"firstName",
|
|
509
|
+
"lastName",
|
|
510
|
+
"givenName",
|
|
511
|
+
"familyName",
|
|
512
|
+
"username",
|
|
513
|
+
"handle",
|
|
514
|
+
"email",
|
|
515
|
+
"phone",
|
|
516
|
+
"title",
|
|
517
|
+
"label",
|
|
518
|
+
"slug",
|
|
519
|
+
"code",
|
|
520
|
+
"name"
|
|
521
|
+
];
|
|
522
|
+
const NAME_PATTERN = /name/i;
|
|
523
|
+
const computeCandidates = (relatedTableName) => {
|
|
524
|
+
if (!relatedTableName) return [];
|
|
525
|
+
const related = tables.find((candidate) => candidate.name === relatedTableName);
|
|
526
|
+
if (!related) return [];
|
|
527
|
+
const fieldNamesSet = new Set(related.fields.map((field) => field.name));
|
|
528
|
+
const explicit = PRIORITY_FIELDS.filter((k) => fieldNamesSet.has(k));
|
|
529
|
+
const explicitSet = new Set(explicit);
|
|
530
|
+
const nameLike = [];
|
|
531
|
+
for (const name of fieldNamesSet) {
|
|
532
|
+
if (NAME_PATTERN.test(name) && !explicitSet.has(name)) {
|
|
533
|
+
nameLike.push(name);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
nameLike.sort((a, b) => a.length - b.length);
|
|
537
|
+
return Array.from(/* @__PURE__ */ new Set([...explicit, ...nameLike]));
|
|
538
|
+
};
|
|
539
|
+
const info = {};
|
|
540
|
+
table.relations.belongsTo.forEach((relation) => {
|
|
541
|
+
const displayCandidates = computeCandidates(relation.referencesTable);
|
|
542
|
+
const foreignKeys = relation.keys.map((key) => key.name).filter(Boolean);
|
|
543
|
+
const primaryForeignKey = foreignKeys[0];
|
|
544
|
+
if (relation.fieldName) {
|
|
545
|
+
info[relation.fieldName] = {
|
|
546
|
+
kind: "belongsTo",
|
|
547
|
+
relatedTable: relation.referencesTable,
|
|
548
|
+
displayCandidates,
|
|
549
|
+
relationField: relation.fieldName,
|
|
550
|
+
foreignKeyField: primaryForeignKey
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
foreignKeys.forEach((fk) => {
|
|
554
|
+
if (!fk) return;
|
|
555
|
+
info[fk] = {
|
|
556
|
+
kind: "belongsTo",
|
|
557
|
+
relatedTable: relation.referencesTable,
|
|
558
|
+
displayCandidates,
|
|
559
|
+
relationField: relation.fieldName ?? void 0,
|
|
560
|
+
foreignKeyField: fk
|
|
561
|
+
};
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
table.relations.hasOne.forEach((relation) => {
|
|
565
|
+
if (relation.fieldName) {
|
|
566
|
+
const fkInRelated = findForeignKeyInRelatedTable(tableName, relation.referencedByTable, tables, rawMetaTables);
|
|
567
|
+
info[relation.fieldName] = {
|
|
568
|
+
kind: "hasOne",
|
|
569
|
+
relatedTable: relation.referencedByTable,
|
|
570
|
+
displayCandidates: computeCandidates(relation.referencedByTable),
|
|
571
|
+
relationField: relation.fieldName,
|
|
572
|
+
foreignKeyField: fkInRelated
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
const junctionTableNames = getJunctionTableNames(table.relations, rawMetaTables);
|
|
577
|
+
table.relations.hasMany.forEach((relation) => {
|
|
578
|
+
if (!relation.fieldName) return;
|
|
579
|
+
if (junctionTableNames.has(relation.referencedByTable)) return;
|
|
580
|
+
const fkInRelated = findForeignKeyInRelatedTable(tableName, relation.referencedByTable, tables, rawMetaTables);
|
|
581
|
+
info[relation.fieldName] = {
|
|
582
|
+
kind: "hasMany",
|
|
583
|
+
relatedTable: relation.referencedByTable,
|
|
584
|
+
displayCandidates: computeCandidates(relation.referencedByTable),
|
|
585
|
+
relationField: relation.fieldName,
|
|
586
|
+
foreignKeyField: fkInRelated
|
|
587
|
+
};
|
|
588
|
+
});
|
|
589
|
+
table.relations.manyToMany.forEach((relation) => {
|
|
590
|
+
if (relation.fieldName) {
|
|
591
|
+
let junctionLeftKeyField = relation.junctionLeftKeyFields?.[0];
|
|
592
|
+
let junctionRightKeyField = relation.junctionRightKeyFields?.[0];
|
|
593
|
+
if (!junctionLeftKeyField || !junctionRightKeyField) {
|
|
594
|
+
const junctionTableName = relation.junctionTable;
|
|
595
|
+
const junctionTable = junctionTableName ? tables.find((candidate) => candidate.name === junctionTableName) : void 0;
|
|
596
|
+
const junctionBelongsTo = junctionTable?.relations.belongsTo ?? [];
|
|
597
|
+
if (!junctionLeftKeyField) {
|
|
598
|
+
const leftRelation = junctionBelongsTo.find((entry) => entry.referencesTable === tableName);
|
|
599
|
+
junctionLeftKeyField = firstFieldName(leftRelation?.keys ?? []);
|
|
600
|
+
}
|
|
601
|
+
if (!junctionRightKeyField) {
|
|
602
|
+
const rightRelation = junctionBelongsTo.find((entry) => entry.referencesTable === relation.rightTable);
|
|
603
|
+
junctionRightKeyField = firstFieldName(rightRelation?.keys ?? []);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
if (!isJunctionTablePure(relation.junctionTable, junctionLeftKeyField, junctionRightKeyField, rawMetaTables)) {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
info[relation.fieldName] = {
|
|
610
|
+
kind: "manyToMany",
|
|
611
|
+
relatedTable: relation.rightTable,
|
|
612
|
+
displayCandidates: computeCandidates(relation.rightTable),
|
|
613
|
+
relationField: relation.fieldName,
|
|
614
|
+
junctionTable: relation.junctionTable,
|
|
615
|
+
junctionLeftKeyField,
|
|
616
|
+
junctionRightKeyField,
|
|
617
|
+
displayName: pluralize(relation.rightTable)
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
return info;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/store/sheets-store.ts
|
|
625
|
+
import { createContext as createContext2, useContext as useContext2 } from "react";
|
|
626
|
+
import { createStore, useStore as useZustandStore } from "zustand";
|
|
627
|
+
function createSheetsStore() {
|
|
628
|
+
return createStore((set, get) => ({
|
|
629
|
+
// Draft rows
|
|
630
|
+
...createDraftRowsSlice(set),
|
|
631
|
+
// Relation info cache
|
|
632
|
+
...createRelationInfoSlice(set, get),
|
|
633
|
+
// Auth state (standalone mode)
|
|
634
|
+
isAuthenticated: false,
|
|
635
|
+
accessToken: null,
|
|
636
|
+
expiresAt: null,
|
|
637
|
+
setAuthenticated: (token, expiresAt) => set({ isAuthenticated: true, accessToken: token, expiresAt }),
|
|
638
|
+
setUnauthenticated: () => set({ isAuthenticated: false, accessToken: null, expiresAt: null })
|
|
639
|
+
}));
|
|
640
|
+
}
|
|
641
|
+
var SheetsStoreContext = createContext2(null);
|
|
642
|
+
function useSheetsStore(selector) {
|
|
643
|
+
const store = useContext2(SheetsStoreContext);
|
|
644
|
+
if (!store) {
|
|
645
|
+
throw new Error("useSheetsStore must be used within a <SheetsProvider>");
|
|
646
|
+
}
|
|
647
|
+
return useZustandStore(store, selector);
|
|
648
|
+
}
|
|
649
|
+
function useSheetsStoreApi() {
|
|
650
|
+
const store = useContext2(SheetsStoreContext);
|
|
651
|
+
if (!store) {
|
|
652
|
+
throw new Error("useSheetsStoreApi must be used within a <SheetsProvider>");
|
|
653
|
+
}
|
|
654
|
+
return store;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
export {
|
|
658
|
+
SheetsContext,
|
|
659
|
+
useSheetsContext,
|
|
660
|
+
computeDraftMetaSignature,
|
|
661
|
+
getAllRelationFields,
|
|
662
|
+
isJunctionTablePure,
|
|
663
|
+
getJunctionTableNames,
|
|
664
|
+
buildRelationInfoFromMeta,
|
|
665
|
+
createSheetsStore,
|
|
666
|
+
SheetsStoreContext,
|
|
667
|
+
useSheetsStore,
|
|
668
|
+
useSheetsStoreApi
|
|
669
|
+
};
|
|
670
|
+
//# sourceMappingURL=chunk-4PGPVMZB.js.map
|