grist-widget-sdk 0.3.27 → 0.5.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.
Files changed (88) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/README.md +79 -208
  3. package/dist/app.cjs +1336 -4415
  4. package/dist/app.cjs.map +1 -1
  5. package/dist/app.d.cts +6 -103
  6. package/dist/app.d.ts +6 -103
  7. package/dist/app.js +5 -7
  8. package/dist/chunk-4ZGVKGDF.js +204 -0
  9. package/dist/chunk-4ZGVKGDF.js.map +1 -0
  10. package/dist/{chunk-6Y2IONGJ.js → chunk-LSALXIGM.js} +191 -20
  11. package/dist/chunk-LSALXIGM.js.map +1 -0
  12. package/dist/chunk-MAPAWVQC.js +1898 -0
  13. package/dist/chunk-MAPAWVQC.js.map +1 -0
  14. package/dist/{chunk-YQNMIL33.js → chunk-OXA2VITC.js} +532 -181
  15. package/dist/chunk-OXA2VITC.js.map +1 -0
  16. package/dist/define-widget-B1FIvI-h.d.cts +433 -0
  17. package/dist/define-widget-B9gEtJP0.d.ts +433 -0
  18. package/dist/emulator/testing.cjs +3341 -6512
  19. package/dist/emulator/testing.cjs.map +1 -1
  20. package/dist/emulator/testing.d.cts +29 -14
  21. package/dist/emulator/testing.d.ts +29 -14
  22. package/dist/emulator/testing.js +78 -72
  23. package/dist/emulator/testing.js.map +1 -1
  24. package/dist/emulator.cjs +63 -249
  25. package/dist/emulator.cjs.map +1 -1
  26. package/dist/emulator.d.cts +6 -8
  27. package/dist/emulator.d.ts +6 -8
  28. package/dist/emulator.js +5 -11
  29. package/dist/index.cjs +1442 -4862
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +81 -260
  32. package/dist/index.d.ts +81 -260
  33. package/dist/index.js +12 -158
  34. package/dist/mount-grist-widget-CkB-3iG_.d.ts +137 -0
  35. package/dist/mount-grist-widget-Dtelqao_.d.cts +137 -0
  36. package/dist/{inline-DpcT8tV9.d.ts → templates-CGmRIQBV.d.cts} +89 -35
  37. package/dist/{inline-C7DkvuET.d.cts → templates-Cq94qOcf.d.ts} +89 -35
  38. package/dist/{types-D3JxUCfL.d.cts → types-DBStn0Dk.d.cts} +2 -26
  39. package/dist/{types-D3JxUCfL.d.ts → types-DBStn0Dk.d.ts} +2 -26
  40. package/package.json +8 -37
  41. package/dist/advanced.cjs +0 -2799
  42. package/dist/advanced.cjs.map +0 -1
  43. package/dist/advanced.d.cts +0 -822
  44. package/dist/advanced.d.ts +0 -822
  45. package/dist/advanced.js +0 -872
  46. package/dist/advanced.js.map +0 -1
  47. package/dist/chunk-22U7WAY3.js +0 -463
  48. package/dist/chunk-22U7WAY3.js.map +0 -1
  49. package/dist/chunk-64SVRWW7.js +0 -758
  50. package/dist/chunk-64SVRWW7.js.map +0 -1
  51. package/dist/chunk-6Y2IONGJ.js.map +0 -1
  52. package/dist/chunk-B4AKVSWV.js +0 -2362
  53. package/dist/chunk-B4AKVSWV.js.map +0 -1
  54. package/dist/chunk-ELGVOHFD.js +0 -32
  55. package/dist/chunk-ELGVOHFD.js.map +0 -1
  56. package/dist/chunk-GTTD5HPO.js +0 -36
  57. package/dist/chunk-GTTD5HPO.js.map +0 -1
  58. package/dist/chunk-NO6FYX33.js +0 -2084
  59. package/dist/chunk-NO6FYX33.js.map +0 -1
  60. package/dist/chunk-OPTOYD2Y.js +0 -277
  61. package/dist/chunk-OPTOYD2Y.js.map +0 -1
  62. package/dist/chunk-PM3B4WYV.js +0 -12
  63. package/dist/chunk-PM3B4WYV.js.map +0 -1
  64. package/dist/chunk-REV5AUXD.js +0 -25
  65. package/dist/chunk-REV5AUXD.js.map +0 -1
  66. package/dist/chunk-YQNMIL33.js.map +0 -1
  67. package/dist/create-grist-emulator-BCqp1BF9.d.ts +0 -81
  68. package/dist/create-grist-emulator-DBM8OSIR.d.cts +0 -81
  69. package/dist/emulator/devtools.cjs +0 -1404
  70. package/dist/emulator/devtools.cjs.map +0 -1
  71. package/dist/emulator/devtools.d.cts +0 -48
  72. package/dist/emulator/devtools.d.ts +0 -48
  73. package/dist/emulator/devtools.js +0 -506
  74. package/dist/emulator/devtools.js.map +0 -1
  75. package/dist/emulator/transports.cjs +0 -1132
  76. package/dist/emulator/transports.cjs.map +0 -1
  77. package/dist/emulator/transports.d.cts +0 -38
  78. package/dist/emulator/transports.d.ts +0 -38
  79. package/dist/emulator/transports.js +0 -12
  80. package/dist/emulator/transports.js.map +0 -1
  81. package/dist/templates-8k2LKoO-.d.cts +0 -24
  82. package/dist/templates-DIwi_mvI.d.ts +0 -24
  83. package/dist/use-grist-reads-DUMvJgV7.d.ts +0 -480
  84. package/dist/use-grist-reads-DpjVuI0H.d.cts +0 -480
  85. package/dist/use-grist-types-DdOLEJXN.d.cts +0 -499
  86. package/dist/use-grist-types-kwxoFeub.d.ts +0 -499
  87. package/dist/use-grist-widget-options-from-context-CGOlQ5Vc.d.cts +0 -43
  88. package/dist/use-grist-widget-options-from-context-CGOlQ5Vc.d.ts +0 -43
package/dist/advanced.js DELETED
@@ -1,872 +0,0 @@
1
- import {
2
- GristReadsContext,
3
- createGristHandshakeManager,
4
- deriveCapabilities,
5
- deriveErrorMessage,
6
- deriveStatus,
7
- ensureGristReady,
8
- fetchWithGristAuth,
9
- getGristAccessToken,
10
- initialGristSnapshot,
11
- newRecordEvent,
12
- optionsEvent,
13
- recordEvent,
14
- recordsEvent,
15
- resetGristReadySingleton,
16
- useGristCore,
17
- useGristManagerOptional,
18
- useGristReady,
19
- useGristWidgetOptionsFromContext,
20
- useRpcHeartbeatCoalesce
21
- } from "./chunk-NO6FYX33.js";
22
- import {
23
- tableDataToRows
24
- } from "./chunk-OPTOYD2Y.js";
25
- import {
26
- gristAddRecordAction,
27
- gristBulkAddRecordAction,
28
- gristBulkRemoveRecordAction,
29
- gristBulkUpdateRecordAction,
30
- gristRemoveRecordAction,
31
- gristUpdateRecordAction
32
- } from "./chunk-64SVRWW7.js";
33
-
34
- // src/sdk/low-level-hooks/use-grist-availability.ts
35
- function useGristAvailability(options) {
36
- const { isAvailable } = useGristCore(options);
37
- return isAvailable;
38
- }
39
-
40
- // src/sdk/low-level-hooks/use-grist-handshake.ts
41
- import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
42
- function useGristHandshake(options = {}) {
43
- const optionsRef = useRef(options);
44
- optionsRef.current = options;
45
- const managerRef = useRef(null);
46
- if (managerRef.current === null) {
47
- managerRef.current = createGristHandshakeManager({
48
- ...options,
49
- negotiate: {
50
- readyImpl: (payload) => ensureGristReady(payload),
51
- ...options.negotiate
52
- },
53
- onBeforeReload: () => {
54
- resetGristReadySingleton();
55
- options.onBeforeReload?.();
56
- }
57
- });
58
- }
59
- const manager = managerRef.current;
60
- useEffect(() => {
61
- manager.start();
62
- return () => {
63
- manager.stop();
64
- };
65
- }, [manager]);
66
- const snapshot = useSyncExternalStore(
67
- manager.subscribe,
68
- manager.getSnapshot,
69
- initialGristSnapshot
70
- );
71
- const projected = useMemo(
72
- () => ({
73
- status: deriveStatus(snapshot),
74
- error: deriveErrorMessage(snapshot),
75
- capabilities: deriveCapabilities(snapshot)
76
- }),
77
- [snapshot]
78
- );
79
- return useMemo(
80
- () => ({
81
- snapshot,
82
- status: projected.status,
83
- error: projected.error,
84
- capabilities: projected.capabilities,
85
- reload: manager.reload,
86
- restart: manager.restart
87
- }),
88
- [snapshot, projected, manager]
89
- );
90
- }
91
- function useGristCapabilities(options = {}) {
92
- return useGristHandshake(options).capabilities;
93
- }
94
-
95
- // src/sdk/low-level-hooks/use-ambient-grist-handshake.ts
96
- import { useMemo as useMemo2, useSyncExternalStore as useSyncExternalStore2 } from "react";
97
- var noopSubscribe = () => () => {
98
- };
99
- var getInitialSnapshot = () => initialGristSnapshot();
100
- function useAmbientGristHandshake() {
101
- const manager = useGristManagerOptional();
102
- const snapshot = useSyncExternalStore2(
103
- manager?.subscribe ?? noopSubscribe,
104
- manager?.getSnapshot ?? getInitialSnapshot,
105
- getInitialSnapshot
106
- );
107
- return useMemo2(() => {
108
- if (!manager) return null;
109
- return {
110
- snapshot,
111
- status: deriveStatus(snapshot),
112
- error: deriveErrorMessage(snapshot),
113
- capabilities: deriveCapabilities(snapshot),
114
- reload: manager.reload,
115
- restart: manager.restart
116
- };
117
- }, [manager, snapshot]);
118
- }
119
-
120
- // src/sdk/components/grist-handshake-provider.tsx
121
- import {
122
- createContext,
123
- useContext,
124
- useEffect as useEffect2,
125
- useMemo as useMemo3,
126
- useRef as useRef2,
127
- useSyncExternalStore as useSyncExternalStore3
128
- } from "react";
129
- import { jsx } from "react/jsx-runtime";
130
- var GristHandshakeReactContext = createContext(null);
131
- function GristHandshakeProvider({ options, children }) {
132
- const managerRef = useRef2(null);
133
- if (managerRef.current === null) {
134
- managerRef.current = createGristHandshakeManager({
135
- ...options,
136
- negotiate: {
137
- readyImpl: (payload) => ensureGristReady(payload),
138
- ...options?.negotiate
139
- },
140
- onBeforeReload: () => {
141
- resetGristReadySingleton();
142
- options?.onBeforeReload?.();
143
- }
144
- });
145
- }
146
- const manager = managerRef.current;
147
- useEffect2(() => {
148
- manager.start();
149
- return () => {
150
- manager.stop();
151
- };
152
- }, [manager]);
153
- const snapshot = useSyncExternalStore3(
154
- manager.subscribe,
155
- manager.getSnapshot,
156
- initialGristSnapshot
157
- );
158
- const value = useMemo3(
159
- () => ({
160
- snapshot,
161
- status: deriveStatus(snapshot),
162
- error: deriveErrorMessage(snapshot),
163
- capabilities: deriveCapabilities(snapshot),
164
- reload: manager.reload,
165
- restart: manager.restart,
166
- recordRpcSuccess: manager.recordRpcSuccess,
167
- recordRpcFailure: manager.recordRpcFailure
168
- }),
169
- [snapshot, manager]
170
- );
171
- return /* @__PURE__ */ jsx(GristHandshakeReactContext.Provider, { value, children });
172
- }
173
- function useGristHandshakeContext() {
174
- const ctx = useContext(GristHandshakeReactContext);
175
- if (!ctx) {
176
- throw new Error(
177
- "useGristHandshakeContext() must be used inside <GristHandshakeProvider>"
178
- );
179
- }
180
- return ctx;
181
- }
182
- function useGristHandshakeContextOptional() {
183
- return useContext(GristHandshakeReactContext);
184
- }
185
-
186
- // src/sdk/low-level-hooks/use-grist-record.ts
187
- import { useEffect as useEffect3, useRef as useRef3, useState } from "react";
188
- function useGristRecord(cb, options) {
189
- const cbRef = useRef3(cb);
190
- useEffect3(() => {
191
- cbRef.current = cb;
192
- }, [cb]);
193
- const [mappings, setMappings] = useState(null);
194
- const { docApi, error: readyError } = useGristReady({
195
- requiredAccess: options?.requiredAccess ?? "read table",
196
- columns: options?.columns,
197
- allowSelectBy: options?.allowSelectBy,
198
- hasCustomOptions: options?.hasCustomOptions,
199
- onEditOptions: options?.onEditOptions
200
- });
201
- useEffect3(() => {
202
- if (!docApi) return;
203
- return recordEvent.subscribe(({ record, mappings: nextMappings }) => {
204
- setMappings(nextMappings);
205
- cbRef.current(record, nextMappings);
206
- });
207
- }, [docApi]);
208
- return {
209
- loading: docApi === null && !readyError,
210
- error: readyError,
211
- mappings
212
- };
213
- }
214
-
215
- // src/sdk/low-level-hooks/use-grist-records.ts
216
- import { useEffect as useEffect4, useRef as useRef4, useState as useState2 } from "react";
217
- function useGristRecords(cb, options) {
218
- const cbRef = useRef4(cb);
219
- useEffect4(() => {
220
- cbRef.current = cb;
221
- }, [cb]);
222
- const [mappings, setMappings] = useState2(null);
223
- const { docApi, error: readyError } = useGristReady({
224
- requiredAccess: options?.requiredAccess ?? "read table",
225
- columns: options?.columns,
226
- allowSelectBy: options?.allowSelectBy,
227
- hasCustomOptions: options?.hasCustomOptions,
228
- onEditOptions: options?.onEditOptions
229
- });
230
- useEffect4(() => {
231
- if (!docApi) return;
232
- return recordsEvent.subscribe(({ records, mappings: nextMappings }) => {
233
- setMappings(nextMappings);
234
- cbRef.current(records, nextMappings);
235
- });
236
- }, [docApi]);
237
- return {
238
- loading: docApi === null && !readyError,
239
- error: readyError,
240
- mappings
241
- };
242
- }
243
-
244
- // src/sdk/low-level-hooks/use-grist-new-record.ts
245
- import { useEffect as useEffect5, useRef as useRef5, useState as useState3 } from "react";
246
- function useGristNewRecord(options) {
247
- const { docApi, error: readyError } = useGristReady({
248
- requiredAccess: options?.requiredAccess ?? "read table",
249
- columns: options?.columns,
250
- allowSelectBy: options?.allowSelectBy,
251
- hasCustomOptions: options?.hasCustomOptions,
252
- onEditOptions: options?.onEditOptions
253
- });
254
- const [isNewRecord, setIsNewRecord] = useState3(false);
255
- const [mappings, setMappings] = useState3(null);
256
- const subscribedRef = useRef5(false);
257
- useEffect5(() => {
258
- if (!docApi || subscribedRef.current) return;
259
- if (typeof grist.onNewRecord !== "function") return;
260
- subscribedRef.current = true;
261
- return newRecordEvent.subscribe(({ mappings: nextMappings }) => {
262
- setIsNewRecord(true);
263
- setMappings(nextMappings);
264
- });
265
- }, [docApi]);
266
- return {
267
- isNewRecord,
268
- mappings,
269
- loading: docApi === null && !readyError,
270
- error: readyError
271
- };
272
- }
273
-
274
- // src/sdk/mid-level-hooks/use-grist-actions.ts
275
- import { useCallback, useState as useState4 } from "react";
276
- function useGristActions(options) {
277
- const requiredAccess = options?.requiredAccess ?? "full";
278
- const { docApi, error: readyError } = useGristReady({
279
- requiredAccess,
280
- columns: options?.columns,
281
- allowSelectBy: options?.allowSelectBy,
282
- hasCustomOptions: options?.hasCustomOptions,
283
- onEditOptions: options?.onEditOptions
284
- });
285
- const [status, setStatus] = useState4("idle");
286
- const [actionError, setActionError] = useState4(null);
287
- const coalesceRpc = useRpcHeartbeatCoalesce();
288
- const apply = useCallback(
289
- async (actions, applyOptions) => {
290
- if (!docApi) throw new Error("Grist docApi is not ready");
291
- setStatus("running");
292
- setActionError(null);
293
- try {
294
- const out = await coalesceRpc(() => docApi.applyUserActions(actions, applyOptions));
295
- setStatus("idle");
296
- return out;
297
- } catch (err) {
298
- const message = err instanceof Error ? err.message : String(err);
299
- setActionError(message);
300
- setStatus("error");
301
- throw err;
302
- }
303
- },
304
- [docApi, coalesceRpc]
305
- );
306
- const updateRecord = useCallback(
307
- (tableId, recordId, patch, applyOptions) => apply([gristUpdateRecordAction(tableId, recordId, patch)], applyOptions),
308
- [apply]
309
- );
310
- const addRecord = useCallback(
311
- (tableId, values, applyOptions) => apply([gristAddRecordAction(tableId, values)], applyOptions),
312
- [apply]
313
- );
314
- const removeRecord = useCallback(
315
- (tableId, recordId, applyOptions) => apply([gristRemoveRecordAction(tableId, recordId)], applyOptions),
316
- [apply]
317
- );
318
- const bulkAddRecord = useCallback(
319
- (tableId, values, applyOptions) => apply([gristBulkAddRecordAction(tableId, values)], applyOptions),
320
- [apply]
321
- );
322
- const bulkUpdateRecord = useCallback(
323
- (tableId, rowIds, columnarPatch, applyOptions) => apply([gristBulkUpdateRecordAction(tableId, rowIds, columnarPatch)], applyOptions),
324
- [apply]
325
- );
326
- const bulkRemoveRecord = useCallback(
327
- (tableId, rowIds, applyOptions) => apply([gristBulkRemoveRecordAction(tableId, rowIds)], applyOptions),
328
- [apply]
329
- );
330
- return {
331
- status: readyError ? "error" : status,
332
- error: readyError ?? actionError,
333
- apply,
334
- updateRecord,
335
- addRecord,
336
- removeRecord,
337
- bulkAddRecord,
338
- bulkUpdateRecord,
339
- bulkRemoveRecord
340
- };
341
- }
342
-
343
- // src/sdk/mid-level-hooks/use-grist-mapped-record.ts
344
- import { useCallback as useCallback2, useEffect as useEffect6, useState as useState5 } from "react";
345
- function manualMapBack(patch, mappings) {
346
- if (!mappings) return { ...patch };
347
- const out = {};
348
- for (const [name, value] of Object.entries(patch)) {
349
- const target = mappings[name];
350
- if (Array.isArray(target)) {
351
- if (typeof console !== "undefined") {
352
- console.warn(`[lib/grist] mapBack: cannot reverse-map allowMultiple field "${name}" without grist.mapColumnNamesBack`);
353
- }
354
- continue;
355
- }
356
- out[target ?? name] = value;
357
- }
358
- return out;
359
- }
360
- function useGristMappedRecord(options) {
361
- const requiredAccess = options?.requiredAccess ?? "read table";
362
- const { docApi, error: readyError } = useGristReady({
363
- requiredAccess,
364
- columns: options?.columns,
365
- allowSelectBy: options?.allowSelectBy,
366
- hasCustomOptions: options?.hasCustomOptions,
367
- onEditOptions: options?.onEditOptions
368
- });
369
- const [rawRecord, setRawRecord] = useState5(null);
370
- const [mappedRecord, setMappedRecord] = useState5(null);
371
- const [mappings, setMappings] = useState5(null);
372
- useEffect6(() => {
373
- if (!docApi) return;
374
- return recordEvent.subscribe(({ record, mappings: nextMappings }) => {
375
- setRawRecord(record);
376
- setMappings(nextMappings);
377
- if (!record) {
378
- setMappedRecord(null);
379
- return;
380
- }
381
- if (typeof grist.mapColumnNames === "function") {
382
- const mapped = grist.mapColumnNames(record);
383
- setMappedRecord(
384
- mapped !== null && mapped !== void 0 ? mapped : null
385
- );
386
- return;
387
- }
388
- setMappedRecord(record);
389
- });
390
- }, [docApi]);
391
- const mapBack = useCallback2(
392
- (patch) => {
393
- if (typeof grist.mapColumnNamesBack === "function") {
394
- return grist.mapColumnNamesBack(patch);
395
- }
396
- return manualMapBack(patch, mappings);
397
- },
398
- [mappings]
399
- );
400
- const resolveColumnId = useCallback2(
401
- (logicalName) => {
402
- const value = mappings?.[logicalName];
403
- if (Array.isArray(value)) return void 0;
404
- return value ?? void 0;
405
- },
406
- [mappings]
407
- );
408
- return {
409
- rawRecord,
410
- mappedRecord,
411
- mappings,
412
- loading: docApi === null && !readyError,
413
- error: readyError,
414
- mapBack,
415
- resolveColumnId
416
- };
417
- }
418
-
419
- // src/sdk/mid-level-hooks/use-grist-table-ops.ts
420
- import { useCallback as useCallback3, useState as useState6 } from "react";
421
- function resolveOps(tableId) {
422
- if (tableId === "selected") {
423
- if (!grist.selectedTable) throw new Error("grist.selectedTable is not available");
424
- return grist.selectedTable;
425
- }
426
- if (typeof grist.getTable !== "function") {
427
- throw new Error("grist.getTable is not available in this Grist version");
428
- }
429
- return grist.getTable(tableId);
430
- }
431
- function useGristTableOps(options) {
432
- const requiredAccess = options?.requiredAccess ?? "full";
433
- const tableIdOption = options?.tableId ?? "selected";
434
- const { docApi, error: readyError } = useGristReady({
435
- requiredAccess,
436
- columns: options?.columns,
437
- allowSelectBy: options?.allowSelectBy,
438
- hasCustomOptions: options?.hasCustomOptions,
439
- onEditOptions: options?.onEditOptions
440
- });
441
- const [status, setStatus] = useState6("idle");
442
- const [actionError, setActionError] = useState6(null);
443
- const coalesceRpc = useRpcHeartbeatCoalesce();
444
- let ops = null;
445
- let resolveError = null;
446
- if (docApi) {
447
- try {
448
- ops = resolveOps(tableIdOption);
449
- } catch (err) {
450
- resolveError = err instanceof Error ? err.message : String(err);
451
- }
452
- }
453
- const wrap = useCallback3(
454
- async (run) => {
455
- if (!ops) throw new Error("Grist table operations are not ready");
456
- setStatus("running");
457
- setActionError(null);
458
- try {
459
- const out = await coalesceRpc(() => run(ops));
460
- setStatus("idle");
461
- return out;
462
- } catch (err) {
463
- const message = err instanceof Error ? err.message : String(err);
464
- setActionError(message);
465
- setStatus("error");
466
- throw err;
467
- }
468
- },
469
- [ops, coalesceRpc]
470
- );
471
- const getTableId = useCallback3(async () => {
472
- if (!ops) throw new Error("Grist table operations are not ready");
473
- return ops.getTableId();
474
- }, [ops]);
475
- const create = useCallback3(
476
- (records, opOptions) => wrap((o) => o.create(records, opOptions)),
477
- [wrap]
478
- );
479
- const update = useCallback3(
480
- (records, opOptions) => wrap((o) => o.update(records, opOptions)),
481
- [wrap]
482
- );
483
- const upsert = useCallback3(
484
- (records, opOptions) => wrap((o) => o.upsert(records, opOptions)),
485
- [wrap]
486
- );
487
- const destroy = useCallback3(
488
- (recordIds) => wrap((o) => o.destroy(recordIds)),
489
- [wrap]
490
- );
491
- return {
492
- status: readyError || resolveError ? "error" : status,
493
- error: readyError ?? resolveError ?? actionError,
494
- getTableId,
495
- create,
496
- update,
497
- upsert,
498
- destroy
499
- };
500
- }
501
-
502
- // src/sdk/mid-level-hooks/use-grist-rows-from-table.ts
503
- import { useCallback as useCallback4, useContext as useContext2, useEffect as useEffect7, useRef as useRef6, useState as useState7 } from "react";
504
- function useGristRowsFromTable(options) {
505
- const requiredAccess = options.requiredAccess ?? "read table";
506
- const { docApi, error: readyError } = useGristReady({
507
- requiredAccess,
508
- columns: options.columns,
509
- allowSelectBy: options.allowSelectBy,
510
- hasCustomOptions: options.hasCustomOptions,
511
- onEditOptions: options.onEditOptions
512
- });
513
- const providerFetchTable = useContext2(GristReadsContext)?.fetchTable;
514
- const [rows, setRows] = useState7(null);
515
- const [loading, setLoading] = useState7(false);
516
- const [fetchError, setFetchError] = useState7(null);
517
- const transformRef = useRef6(options.transform);
518
- transformRef.current = options.transform;
519
- const coalesceRpc = useRpcHeartbeatCoalesce();
520
- const refresh = useCallback4(async () => {
521
- if (!docApi) return;
522
- setLoading(true);
523
- setFetchError(null);
524
- try {
525
- const data = providerFetchTable ? await providerFetchTable(options.tableId) : await coalesceRpc(
526
- () => docApi.fetchTable(options.tableId)
527
- );
528
- const raw = tableDataToRows(data);
529
- const t = transformRef.current;
530
- setRows(t ? raw.map((r) => t(r)) : raw);
531
- } catch (err) {
532
- setFetchError(err instanceof Error ? err.message : String(err));
533
- setRows(null);
534
- } finally {
535
- setLoading(false);
536
- }
537
- }, [docApi, providerFetchTable, options.tableId, coalesceRpc]);
538
- useEffect7(() => {
539
- if (docApi) void refresh();
540
- }, [docApi, refresh]);
541
- useEffect7(() => {
542
- if (!docApi || !options.pollIntervalMs || options.pollIntervalMs <= 0) return;
543
- const id = window.setInterval(() => void refresh(), options.pollIntervalMs);
544
- return () => window.clearInterval(id);
545
- }, [docApi, options.pollIntervalMs, refresh]);
546
- return {
547
- rows,
548
- loading,
549
- error: readyError ?? fetchError,
550
- refresh
551
- };
552
- }
553
-
554
- // src/sdk/mid-level-hooks/use-grist-access-token.ts
555
- import { useCallback as useCallback5, useEffect as useEffect8, useState as useState8 } from "react";
556
- function useGristAccessToken(options) {
557
- const requiredAccess = options?.requiredAccess ?? "full";
558
- const readOnlyDefault = options?.readOnly ?? false;
559
- const prefetch = options?.prefetch ?? false;
560
- const { docApi, error: readyError } = useGristReady({
561
- requiredAccess,
562
- columns: options?.columns,
563
- allowSelectBy: options?.allowSelectBy,
564
- hasCustomOptions: options?.hasCustomOptions,
565
- onEditOptions: options?.onEditOptions
566
- });
567
- const [token, setToken] = useState8(null);
568
- const [loading, setLoading] = useState8(false);
569
- const [error, setError] = useState8(null);
570
- const refresh = useCallback5(
571
- async (overrides) => {
572
- if (!docApi) throw new Error("Grist is not ready");
573
- setLoading(true);
574
- setError(null);
575
- try {
576
- const next = await getGristAccessToken(docApi, { readOnly: overrides?.readOnly ?? readOnlyDefault });
577
- setToken(next);
578
- return next;
579
- } catch (err) {
580
- const message = err instanceof Error ? err.message : String(err);
581
- setError(message);
582
- throw err;
583
- } finally {
584
- setLoading(false);
585
- }
586
- },
587
- [docApi, readOnlyDefault]
588
- );
589
- const fetchFn = useCallback5(
590
- (path, init) => {
591
- if (!docApi) throw new Error("Grist is not ready");
592
- return fetchWithGristAuth(docApi, path, { readOnly: readOnlyDefault, ...init });
593
- },
594
- [docApi, readOnlyDefault]
595
- );
596
- useEffect8(() => {
597
- if (docApi && prefetch) void refresh();
598
- }, [docApi, prefetch, refresh]);
599
- return {
600
- token,
601
- loading,
602
- error: readyError ?? error,
603
- refresh,
604
- fetch: fetchFn
605
- };
606
- }
607
-
608
- // src/sdk/mid-level-hooks/use-grist-section.ts
609
- import { useCallback as useCallback6, useEffect as useEffect9, useState as useState9 } from "react";
610
- function useGristSection(options) {
611
- const { docApi, error: readyError } = useGristReady({
612
- requiredAccess: options?.requiredAccess ?? "read table",
613
- columns: options?.columns,
614
- allowSelectBy: options?.allowSelectBy,
615
- hasCustomOptions: options?.hasCustomOptions,
616
- onEditOptions: options?.onEditOptions
617
- });
618
- const [mappings, setMappings] = useState9(null);
619
- const [loading, setLoading] = useState9(false);
620
- const [error, setError] = useState9(null);
621
- const refresh = useCallback6(async () => {
622
- if (!docApi) return;
623
- if (!grist.sectionApi?.mappings) {
624
- setError("grist.sectionApi.mappings is not available in this Grist version");
625
- return;
626
- }
627
- setLoading(true);
628
- setError(null);
629
- try {
630
- const next = await grist.sectionApi.mappings();
631
- setMappings(next);
632
- } catch (err) {
633
- setError(err instanceof Error ? err.message : String(err));
634
- } finally {
635
- setLoading(false);
636
- }
637
- }, [docApi]);
638
- const configure = useCallback6(async (next) => {
639
- if (!grist.sectionApi?.configure) {
640
- throw new Error("grist.sectionApi.configure is not available in this Grist version");
641
- }
642
- await grist.sectionApi.configure(next);
643
- }, []);
644
- useEffect9(() => {
645
- if (docApi) void refresh();
646
- }, [docApi, refresh]);
647
- return {
648
- mappings,
649
- loading,
650
- error: readyError ?? error,
651
- refresh,
652
- configure
653
- };
654
- }
655
-
656
- // src/sdk/mid-level-hooks/use-grist-widget-options.ts
657
- import { useCallback as useCallback7, useEffect as useEffect10, useMemo as useMemo4, useRef as useRef7, useState as useState10 } from "react";
658
- function mergeWithDefaults(defaults, stored, namespace) {
659
- if (stored === null || stored === void 0) return { ...defaults };
660
- const source = namespace ? stored[namespace] : stored;
661
- if (!source || typeof source !== "object") return { ...defaults };
662
- return { ...defaults, ...source };
663
- }
664
- function useGristWidgetOptions(config) {
665
- const requiredAccess = config.requiredAccess ?? "read table";
666
- const debounceMs = config.debounceMs ?? 250;
667
- const hasCustomOptions = config.hasCustomOptions ?? true;
668
- const { docApi, error: readyError } = useGristReady({
669
- requiredAccess,
670
- columns: config.columns,
671
- allowSelectBy: config.allowSelectBy,
672
- hasCustomOptions,
673
- onEditOptions: config.onEditOptions
674
- });
675
- const defaultsRef = useRef7(config.defaults);
676
- useEffect10(() => {
677
- defaultsRef.current = config.defaults;
678
- }, [config.defaults]);
679
- const [options, setOptionsState] = useState10(() => ({ ...config.defaults }));
680
- const [loading, setLoading] = useState10(true);
681
- const [error, setError] = useState10(null);
682
- const lastWrittenRef = useRef7(null);
683
- const debounceRef = useRef7(null);
684
- useEffect10(() => {
685
- if (!docApi) return;
686
- return optionsEvent.subscribe(({ options: stored }) => {
687
- const merged = mergeWithDefaults(defaultsRef.current, stored, config.namespace);
688
- lastWrittenRef.current = merged;
689
- setOptionsState(merged);
690
- setLoading(false);
691
- });
692
- }, [docApi, config.namespace]);
693
- const flushWrite = useCallback7(
694
- async (value) => {
695
- try {
696
- if (config.namespace) {
697
- await grist.setOption(config.namespace, value);
698
- } else {
699
- await grist.setOptions({ ...value });
700
- }
701
- lastWrittenRef.current = value;
702
- } catch (err) {
703
- setError(err instanceof Error ? err.message : String(err));
704
- }
705
- },
706
- [config.namespace]
707
- );
708
- const scheduleWrite = useCallback7(
709
- (value) => {
710
- if (debounceRef.current) window.clearTimeout(debounceRef.current);
711
- debounceRef.current = window.setTimeout(() => {
712
- debounceRef.current = null;
713
- void flushWrite(value);
714
- }, debounceMs);
715
- },
716
- [debounceMs, flushWrite]
717
- );
718
- useEffect10(() => {
719
- return () => {
720
- if (debounceRef.current) window.clearTimeout(debounceRef.current);
721
- };
722
- }, []);
723
- const setOptionsApi = useCallback7(
724
- (next) => {
725
- setOptionsState(next);
726
- scheduleWrite(next);
727
- },
728
- [scheduleWrite]
729
- );
730
- const patchOptionsApi = useCallback7(
731
- (patch) => {
732
- setOptionsState((prev) => {
733
- const next = { ...prev, ...patch };
734
- scheduleWrite(next);
735
- return next;
736
- });
737
- },
738
- [scheduleWrite]
739
- );
740
- const reset = useCallback7(async () => {
741
- if (debounceRef.current) {
742
- window.clearTimeout(debounceRef.current);
743
- debounceRef.current = null;
744
- }
745
- const next = { ...defaultsRef.current };
746
- setOptionsState(next);
747
- await flushWrite(next);
748
- }, [flushWrite]);
749
- return useMemo4(
750
- () => ({
751
- options,
752
- loading,
753
- error: readyError ?? error,
754
- setOptions: setOptionsApi,
755
- patchOptions: patchOptionsApi,
756
- reset
757
- }),
758
- [options, loading, readyError, error, setOptionsApi, patchOptionsApi, reset]
759
- );
760
- }
761
-
762
- // src/sdk/use-grist-table.ts
763
- import { useCallback as useCallback8, useEffect as useEffect11, useState as useState11 } from "react";
764
- function isCurrentMode(option) {
765
- return option === void 0 || option === "current";
766
- }
767
- function useGristTable(options) {
768
- const requiredAccess = options?.requiredAccess ?? "read table";
769
- const tableIdOption = options?.tableId ?? "current";
770
- const { docApi, error: readyError } = useGristReady({
771
- requiredAccess,
772
- columns: options?.columns,
773
- allowSelectBy: options?.allowSelectBy,
774
- hasCustomOptions: options?.hasCustomOptions,
775
- onEditOptions: options?.onEditOptions
776
- });
777
- const [tableId, setTableId] = useState11(null);
778
- const [loading, setLoading] = useState11(false);
779
- const [error, setError] = useState11(null);
780
- const fetchTable = useCallback8(
781
- async (targetTableId) => {
782
- if (!docApi) throw new Error("Grist is not ready");
783
- return docApi.fetchTable(targetTableId);
784
- },
785
- [docApi]
786
- );
787
- const listTables = useCallback8(async () => {
788
- if (!docApi) throw new Error("Grist is not ready");
789
- const out = await Promise.resolve(docApi.listTables());
790
- return [...out];
791
- }, [docApi]);
792
- const getDocName = useCallback8(async () => {
793
- if (!docApi) throw new Error("Grist is not ready");
794
- return String(await Promise.resolve(docApi.getDocName()));
795
- }, [docApi]);
796
- const fetchSelectedTable = useCallback8(
797
- async (fetchOptions) => {
798
- if (!docApi) throw new Error("Grist is not ready");
799
- const impl = docApi.fetchSelectedTable ?? grist.fetchSelectedTable;
800
- if (!impl) throw new Error("fetchSelectedTable is not available in this Grist version");
801
- const merged = { keepEncoded: false, format: "rows", ...fetchOptions };
802
- return await impl(merged);
803
- },
804
- [docApi]
805
- );
806
- const fetchSelectedRecord = useCallback8(
807
- async (rowId, fetchOptions) => {
808
- if (!docApi) throw new Error("Grist is not ready");
809
- const impl = docApi.fetchSelectedRecord ?? grist.fetchSelectedRecord;
810
- if (!impl) throw new Error("fetchSelectedRecord is not available in this Grist version");
811
- const merged = { keepEncoded: false, format: "rows", ...fetchOptions };
812
- return await impl(rowId, merged);
813
- },
814
- [docApi]
815
- );
816
- const refresh = useCallback8(async () => {
817
- if (!docApi) return;
818
- if (!isCurrentMode(tableIdOption)) {
819
- setTableId(tableIdOption);
820
- setError(null);
821
- return;
822
- }
823
- setLoading(true);
824
- setError(null);
825
- try {
826
- const next = await grist.selectedTable?.getTableId();
827
- setTableId(next ?? null);
828
- } catch (err) {
829
- setTableId(null);
830
- setError(err instanceof Error ? err.message : String(err));
831
- } finally {
832
- setLoading(false);
833
- }
834
- }, [docApi, tableIdOption]);
835
- useEffect11(() => {
836
- if (docApi) void refresh();
837
- }, [docApi, refresh]);
838
- return {
839
- tableId,
840
- loading,
841
- error: readyError ?? error,
842
- refresh,
843
- fetchTable,
844
- listTables,
845
- getDocName,
846
- fetchSelectedTable,
847
- fetchSelectedRecord
848
- };
849
- }
850
- export {
851
- GristHandshakeProvider,
852
- useAmbientGristHandshake,
853
- useGristAccessToken,
854
- useGristActions,
855
- useGristAvailability,
856
- useGristCapabilities,
857
- useGristHandshake,
858
- useGristHandshakeContext,
859
- useGristHandshakeContextOptional,
860
- useGristMappedRecord,
861
- useGristNewRecord,
862
- useGristReady,
863
- useGristRecord,
864
- useGristRecords,
865
- useGristRowsFromTable,
866
- useGristSection,
867
- useGristTable,
868
- useGristTableOps,
869
- useGristWidgetOptions,
870
- useGristWidgetOptionsFromContext
871
- };
872
- //# sourceMappingURL=advanced.js.map