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