@figma-vars/hooks 1.4.5 → 1.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 (55) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +17 -0
  3. package/dist/index.mjs +838 -0
  4. package/package.json +1 -1
  5. package/dist/figma-vars-hooks.js +0 -838
  6. package/dist/figma-vars-hooks.umd.cjs +0 -17
  7. package/dist/src/api/mutator.test.d.ts +0 -1
  8. package/dist/tests/FigmaVarsProvider.test.d.ts +0 -1
  9. package/dist/tests/api/fetcher.test.d.ts +0 -1
  10. package/dist/tests/api/index.test.d.ts +0 -1
  11. package/dist/tests/api/mutator.test.d.ts +0 -1
  12. package/dist/tests/constants/index.test.d.ts +0 -1
  13. package/dist/tests/contexts/index.test.d.ts +0 -1
  14. package/dist/tests/hooks/index.test.d.ts +0 -1
  15. package/dist/tests/hooks/useBulkUpdateVariables.test.d.ts +0 -1
  16. package/dist/tests/hooks/useCreateVariable.test.d.ts +0 -1
  17. package/dist/tests/hooks/useDeleteVariable.test.d.ts +0 -1
  18. package/dist/tests/hooks/useFigmaToken.test.d.ts +0 -1
  19. package/dist/tests/hooks/useMutation.test.d.ts +0 -1
  20. package/dist/tests/hooks/useUpdateVariable.test.d.ts +0 -1
  21. package/dist/tests/hooks/useVariableCollections.test.d.ts +0 -1
  22. package/dist/tests/hooks/useVariableModes.test.d.ts +0 -1
  23. package/dist/tests/hooks/useVariables.test.d.ts +0 -1
  24. package/dist/tests/index.test.d.ts +0 -1
  25. package/dist/tests/mocks/variables.d.ts +0 -2
  26. package/dist/tests/setup.d.ts +0 -0
  27. package/dist/tests/test-utils.d.ts +0 -10
  28. package/dist/tests/test-utils.test.d.ts +0 -1
  29. package/dist/tests/types.test.d.ts +0 -1
  30. package/dist/tests/utils/filterVariables.test.d.ts +0 -1
  31. package/dist/tests/utils/index.test.d.ts +0 -1
  32. /package/dist/{src/api → api}/fetcher.d.ts +0 -0
  33. /package/dist/{src/api → api}/index.d.ts +0 -0
  34. /package/dist/{src/api → api}/mutator.d.ts +0 -0
  35. /package/dist/{src/constants → constants}/index.d.ts +0 -0
  36. /package/dist/{src/contexts → contexts}/FigmaVarsProvider.d.ts +0 -0
  37. /package/dist/{src/contexts → contexts}/index.d.ts +0 -0
  38. /package/dist/{src/hooks → hooks}/index.d.ts +0 -0
  39. /package/dist/{src/hooks → hooks}/useBulkUpdateVariables.d.ts +0 -0
  40. /package/dist/{src/hooks → hooks}/useCreateVariable.d.ts +0 -0
  41. /package/dist/{src/hooks → hooks}/useDeleteVariable.d.ts +0 -0
  42. /package/dist/{src/hooks → hooks}/useFigmaToken.d.ts +0 -0
  43. /package/dist/{src/hooks → hooks}/useMutation.d.ts +0 -0
  44. /package/dist/{src/hooks → hooks}/useUpdateVariable.d.ts +0 -0
  45. /package/dist/{src/hooks → hooks}/useVariableCollections.d.ts +0 -0
  46. /package/dist/{src/hooks → hooks}/useVariableModes.d.ts +0 -0
  47. /package/dist/{src/hooks → hooks}/useVariables.d.ts +0 -0
  48. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  49. /package/dist/{src/types → types}/contexts.d.ts +0 -0
  50. /package/dist/{src/types → types}/figma.d.ts +0 -0
  51. /package/dist/{src/types → types}/hooks.d.ts +0 -0
  52. /package/dist/{src/types → types}/index.d.ts +0 -0
  53. /package/dist/{src/types → types}/mutations.d.ts +0 -0
  54. /package/dist/{src/utils → utils}/filterVariables.d.ts +0 -0
  55. /package/dist/{src/utils → utils}/index.d.ts +0 -0
@@ -1,838 +0,0 @@
1
- import { jsx as Oe } from "react/jsx-runtime";
2
- import et, { createContext as Zt, useContext as te, useEffect as ve, useLayoutEffect as we, useRef as $, useMemo as ft, useCallback as lt, useDebugValue as Re, useReducer as Te } from "react";
3
- const ee = Zt(
4
- void 0
5
- ), En = ({
6
- children: t,
7
- token: e,
8
- fileKey: n
9
- }) => /* @__PURE__ */ Oe(ee.Provider, { value: { token: e, fileKey: n }, children: t }), nt = () => {
10
- const t = te(ee);
11
- if (t === void 0)
12
- throw new Error(
13
- "useFigmaTokenContext must be used within a FigmaVarsProvider"
14
- );
15
- return t;
16
- };
17
- var at = { exports: {} }, _t = {};
18
- /**
19
- * @license React
20
- * use-sync-external-store-shim.production.js
21
- *
22
- * Copyright (c) Meta Platforms, Inc. and affiliates.
23
- *
24
- * This source code is licensed under the MIT license found in the
25
- * LICENSE file in the root directory of this source tree.
26
- */
27
- var Ht;
28
- function ye() {
29
- if (Ht) return _t;
30
- Ht = 1;
31
- var t = et;
32
- function e(u, d) {
33
- return u === d && (u !== 0 || 1 / u === 1 / d) || u !== u && d !== d;
34
- }
35
- var n = typeof Object.is == "function" ? Object.is : e, r = t.useState, o = t.useEffect, s = t.useLayoutEffect, i = t.useDebugValue;
36
- function E(u, d) {
37
- var c = d(), p = r({ inst: { value: c, getSnapshot: d } }), l = p[0].inst, D = p[1];
38
- return s(
39
- function() {
40
- l.value = c, l.getSnapshot = d, h(l) && D({ inst: l });
41
- },
42
- [u, c, d]
43
- ), o(
44
- function() {
45
- return h(l) && D({ inst: l }), u(function() {
46
- h(l) && D({ inst: l });
47
- });
48
- },
49
- [u]
50
- ), i(c), c;
51
- }
52
- function h(u) {
53
- var d = u.getSnapshot;
54
- u = u.value;
55
- try {
56
- var c = d();
57
- return !n(u, c);
58
- } catch {
59
- return !0;
60
- }
61
- }
62
- function y(u, d) {
63
- return d();
64
- }
65
- var O = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? y : E;
66
- return _t.useSyncExternalStore = t.useSyncExternalStore !== void 0 ? t.useSyncExternalStore : O, _t;
67
- }
68
- var St = {};
69
- /**
70
- * @license React
71
- * use-sync-external-store-shim.development.js
72
- *
73
- * Copyright (c) Meta Platforms, Inc. and affiliates.
74
- *
75
- * This source code is licensed under the MIT license found in the
76
- * LICENSE file in the root directory of this source tree.
77
- */
78
- var kt;
79
- function ge() {
80
- return kt || (kt = 1, process.env.NODE_ENV !== "production" && function() {
81
- function t(c, p) {
82
- return c === p && (c !== 0 || 1 / c === 1 / p) || c !== c && p !== p;
83
- }
84
- function e(c, p) {
85
- O || o.startTransition === void 0 || (O = !0, console.error(
86
- "You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."
87
- ));
88
- var l = p();
89
- if (!u) {
90
- var D = p();
91
- s(l, D) || (console.error(
92
- "The result of getSnapshot should be cached to avoid an infinite loop"
93
- ), u = !0);
94
- }
95
- D = i({
96
- inst: { value: l, getSnapshot: p }
97
- });
98
- var a = D[0].inst, P = D[1];
99
- return h(
100
- function() {
101
- a.value = l, a.getSnapshot = p, n(a) && P({ inst: a });
102
- },
103
- [c, l, p]
104
- ), E(
105
- function() {
106
- return n(a) && P({ inst: a }), c(function() {
107
- n(a) && P({ inst: a });
108
- });
109
- },
110
- [c]
111
- ), y(l), l;
112
- }
113
- function n(c) {
114
- var p = c.getSnapshot;
115
- c = c.value;
116
- try {
117
- var l = p();
118
- return !s(c, l);
119
- } catch {
120
- return !0;
121
- }
122
- }
123
- function r(c, p) {
124
- return p();
125
- }
126
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
127
- var o = et, s = typeof Object.is == "function" ? Object.is : t, i = o.useState, E = o.useEffect, h = o.useLayoutEffect, y = o.useDebugValue, O = !1, u = !1, d = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? r : e;
128
- St.useSyncExternalStore = o.useSyncExternalStore !== void 0 ? o.useSyncExternalStore : d, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
129
- }()), St;
130
- }
131
- var Kt;
132
- function Ae() {
133
- return Kt || (Kt = 1, process.env.NODE_ENV === "production" ? at.exports = ye() : at.exports = ge()), at.exports;
134
- }
135
- var De = Ae();
136
- const ne = 0, re = 1, oe = 2, Jt = 3;
137
- var Yt = Object.prototype.hasOwnProperty;
138
- function gt(t, e) {
139
- var n, r;
140
- if (t === e) return !0;
141
- if (t && e && (n = t.constructor) === e.constructor) {
142
- if (n === Date) return t.getTime() === e.getTime();
143
- if (n === RegExp) return t.toString() === e.toString();
144
- if (n === Array) {
145
- if ((r = t.length) === e.length)
146
- for (; r-- && gt(t[r], e[r]); ) ;
147
- return r === -1;
148
- }
149
- if (!n || typeof t == "object") {
150
- r = 0;
151
- for (n in t)
152
- if (Yt.call(t, n) && ++r && !Yt.call(e, n) || !(n in e) || !gt(t[n], e[n])) return !1;
153
- return Object.keys(e).length === r;
154
- }
155
- }
156
- return t !== t && e !== e;
157
- }
158
- const M = /* @__PURE__ */ new WeakMap(), U = () => {
159
- }, A = (
160
- /*#__NOINLINE__*/
161
- U()
162
- ), At = Object, f = (t) => t === A, N = (t) => typeof t == "function", G = (t, e) => ({
163
- ...t,
164
- ...e
165
- }), se = (t) => N(t.then), Ot = {}, ct = {}, Nt = "undefined", rt = typeof window != Nt, Dt = typeof document != Nt, Ie = rt && "Deno" in window, Le = () => rt && typeof window.requestAnimationFrame != Nt, ie = (t, e) => {
166
- const n = M.get(t);
167
- return [
168
- // Getter
169
- () => !f(e) && t.get(e) || Ot,
170
- // Setter
171
- (r) => {
172
- if (!f(e)) {
173
- const o = t.get(e);
174
- e in ct || (ct[e] = o), n[5](e, G(o, r), o || Ot);
175
- }
176
- },
177
- // Subscriber
178
- n[6],
179
- // Get server cache snapshot
180
- () => !f(e) && e in ct ? ct[e] : !f(e) && t.get(e) || Ot
181
- ];
182
- };
183
- let It = !0;
184
- const Ce = () => It, [Lt, Ct] = rt && window.addEventListener ? [
185
- window.addEventListener.bind(window),
186
- window.removeEventListener.bind(window)
187
- ] : [
188
- U,
189
- U
190
- ], be = () => {
191
- const t = Dt && document.visibilityState;
192
- return f(t) || t !== "hidden";
193
- }, Ve = (t) => (Dt && document.addEventListener("visibilitychange", t), Lt("focus", t), () => {
194
- Dt && document.removeEventListener("visibilitychange", t), Ct("focus", t);
195
- }), Ne = (t) => {
196
- const e = () => {
197
- It = !0, t();
198
- }, n = () => {
199
- It = !1;
200
- };
201
- return Lt("online", e), Lt("offline", n), () => {
202
- Ct("online", e), Ct("offline", n);
203
- };
204
- }, Fe = {
205
- isOnline: Ce,
206
- isVisible: be
207
- }, xe = {
208
- initFocus: Ve,
209
- initReconnect: Ne
210
- }, zt = !et.useId, tt = !rt || Ie, Me = (t) => Le() ? window.requestAnimationFrame(t) : setTimeout(t, 1), vt = tt ? ve : we, wt = typeof navigator < "u" && navigator.connection, Xt = !tt && wt && ([
211
- "slow-2g",
212
- "2g"
213
- ].includes(wt.effectiveType) || wt.saveData), ut = /* @__PURE__ */ new WeakMap(), Rt = (t, e) => At.prototype.toString.call(t) === `[object ${e}]`;
214
- let Pe = 0;
215
- const bt = (t) => {
216
- const e = typeof t, n = Rt(t, "Date"), r = Rt(t, "RegExp"), o = Rt(t, "Object");
217
- let s, i;
218
- if (At(t) === t && !n && !r) {
219
- if (s = ut.get(t), s) return s;
220
- if (s = ++Pe + "~", ut.set(t, s), Array.isArray(t)) {
221
- for (s = "@", i = 0; i < t.length; i++)
222
- s += bt(t[i]) + ",";
223
- ut.set(t, s);
224
- }
225
- if (o) {
226
- s = "#";
227
- const E = At.keys(t).sort();
228
- for (; !f(i = E.pop()); )
229
- f(t[i]) || (s += i + ":" + bt(t[i]) + ",");
230
- ut.set(t, s);
231
- }
232
- } else
233
- s = n ? t.toJSON() : e == "symbol" ? t.toString() : e == "string" ? JSON.stringify(t) : "" + t;
234
- return s;
235
- }, Ft = (t) => {
236
- if (N(t))
237
- try {
238
- t = t();
239
- } catch {
240
- t = "";
241
- }
242
- const e = t;
243
- return t = typeof t == "string" ? t : (Array.isArray(t) ? t.length : t) ? bt(t) : "", [
244
- t,
245
- e
246
- ];
247
- };
248
- let je = 0;
249
- const Vt = () => ++je;
250
- async function ae(...t) {
251
- const [e, n, r, o] = t, s = G({
252
- populateCache: !0,
253
- throwOnError: !0
254
- }, typeof o == "boolean" ? {
255
- revalidate: o
256
- } : o || {});
257
- let i = s.populateCache;
258
- const E = s.rollbackOnError;
259
- let h = s.optimisticData;
260
- const y = (d) => typeof E == "function" ? E(d) : E !== !1, O = s.throwOnError;
261
- if (N(n)) {
262
- const d = n, c = [], p = e.keys();
263
- for (const l of p)
264
- // Skip the special useSWRInfinite and useSWRSubscription keys.
265
- !/^\$(inf|sub)\$/.test(l) && d(e.get(l)._k) && c.push(l);
266
- return Promise.all(c.map(u));
267
- }
268
- return u(n);
269
- async function u(d) {
270
- const [c] = Ft(d);
271
- if (!c) return;
272
- const [p, l] = ie(e, c), [D, a, P, Q] = M.get(e), j = () => {
273
- const C = D[c];
274
- return (N(s.revalidate) ? s.revalidate(p().data, d) : s.revalidate !== !1) && (delete P[c], delete Q[c], C && C[0]) ? C[0](oe).then(() => p().data) : p().data;
275
- };
276
- if (t.length < 3)
277
- return j();
278
- let R = r, I;
279
- const q = Vt();
280
- a[c] = [
281
- q,
282
- 0
283
- ];
284
- const S = !f(h), H = p(), V = H.data, k = H._c, W = f(k) ? V : k;
285
- if (S && (h = N(h) ? h(W, V) : h, l({
286
- data: h,
287
- _c: W
288
- })), N(R))
289
- try {
290
- R = R(W);
291
- } catch (C) {
292
- I = C;
293
- }
294
- if (R && se(R))
295
- if (R = await R.catch((C) => {
296
- I = C;
297
- }), q !== a[c][0]) {
298
- if (I) throw I;
299
- return R;
300
- } else I && S && y(I) && (i = !0, l({
301
- data: W,
302
- _c: A
303
- }));
304
- if (i && !I)
305
- if (N(i)) {
306
- const C = i(R, W);
307
- l({
308
- data: C,
309
- error: A,
310
- _c: A
311
- });
312
- } else
313
- l({
314
- data: R,
315
- error: A,
316
- _c: A
317
- });
318
- if (a[c][1] = Vt(), Promise.resolve(j()).then(() => {
319
- l({
320
- _c: A
321
- });
322
- }), I) {
323
- if (O) throw I;
324
- return;
325
- }
326
- return R;
327
- }
328
- }
329
- const Qt = (t, e) => {
330
- for (const n in t)
331
- t[n][0] && t[n][0](e);
332
- }, Ue = (t, e) => {
333
- if (!M.has(t)) {
334
- const n = G(xe, e), r = /* @__PURE__ */ Object.create(null), o = ae.bind(A, t);
335
- let s = U;
336
- const i = /* @__PURE__ */ Object.create(null), E = (O, u) => {
337
- const d = i[O] || [];
338
- return i[O] = d, d.push(u), () => d.splice(d.indexOf(u), 1);
339
- }, h = (O, u, d) => {
340
- t.set(O, u);
341
- const c = i[O];
342
- if (c)
343
- for (const p of c)
344
- p(u, d);
345
- }, y = () => {
346
- if (!M.has(t) && (M.set(t, [
347
- r,
348
- /* @__PURE__ */ Object.create(null),
349
- /* @__PURE__ */ Object.create(null),
350
- /* @__PURE__ */ Object.create(null),
351
- o,
352
- h,
353
- E
354
- ]), !tt)) {
355
- const O = n.initFocus(setTimeout.bind(A, Qt.bind(A, r, ne))), u = n.initReconnect(setTimeout.bind(A, Qt.bind(A, r, re)));
356
- s = () => {
357
- O && O(), u && u(), M.delete(t);
358
- };
359
- }
360
- };
361
- return y(), [
362
- t,
363
- o,
364
- y,
365
- s
366
- ];
367
- }
368
- return [
369
- t,
370
- M.get(t)[4]
371
- ];
372
- }, Ge = (t, e, n, r, o) => {
373
- const s = n.errorRetryCount, i = o.retryCount, E = ~~((Math.random() + 0.5) * (1 << (i < 8 ? i : 8))) * n.errorRetryInterval;
374
- !f(s) && i > s || setTimeout(r, E, o);
375
- }, We = gt, [ce, Be] = Ue(/* @__PURE__ */ new Map()), $e = G(
376
- {
377
- // events
378
- onLoadingSlow: U,
379
- onSuccess: U,
380
- onError: U,
381
- onErrorRetry: Ge,
382
- onDiscarded: U,
383
- // switches
384
- revalidateOnFocus: !0,
385
- revalidateOnReconnect: !0,
386
- revalidateIfStale: !0,
387
- shouldRetryOnError: !0,
388
- // timeouts
389
- errorRetryInterval: Xt ? 1e4 : 5e3,
390
- focusThrottleInterval: 5 * 1e3,
391
- dedupingInterval: 2 * 1e3,
392
- loadingTimeout: Xt ? 5e3 : 3e3,
393
- // providers
394
- compare: We,
395
- isPaused: () => !1,
396
- cache: ce,
397
- mutate: Be,
398
- fallback: {}
399
- },
400
- // use web preset by default
401
- Fe
402
- ), qe = (t, e) => {
403
- const n = G(t, e);
404
- if (e) {
405
- const { use: r, fallback: o } = t, { use: s, fallback: i } = e;
406
- r && s && (n.use = r.concat(s)), o && i && (n.fallback = G(o, i));
407
- }
408
- return n;
409
- }, He = Zt({}), ke = "$inf$", ue = rt && window.__SWR_DEVTOOLS_USE__, Ke = ue ? window.__SWR_DEVTOOLS_USE__ : [], Je = () => {
410
- ue && (window.__SWR_DEVTOOLS_REACT__ = et);
411
- }, Ye = (t) => N(t[1]) ? [
412
- t[0],
413
- t[1],
414
- t[2] || {}
415
- ] : [
416
- t[0],
417
- null,
418
- (t[1] === null ? t[2] : t[1]) || {}
419
- ], ze = () => G($e, te(He)), Xe = (t) => (e, n, r) => t(e, n && ((...s) => {
420
- const [i] = Ft(e), [, , , E] = M.get(ce);
421
- if (i.startsWith(ke))
422
- return n(...s);
423
- const h = E[i];
424
- return f(h) ? n(...s) : (delete E[i], h);
425
- }), r), Qe = Ke.concat(Xe), Ze = (t) => function(...n) {
426
- const r = ze(), [o, s, i] = Ye(n), E = qe(r, i);
427
- let h = t;
428
- const { use: y } = E, O = (y || []).concat(Qe);
429
- for (let u = O.length; u--; )
430
- h = O[u](h);
431
- return h(o, s || E.fetcher || null, E);
432
- }, tn = (t, e, n) => {
433
- const r = e[t] || (e[t] = []);
434
- return r.push(n), () => {
435
- const o = r.indexOf(n);
436
- o >= 0 && (r[o] = r[r.length - 1], r.pop());
437
- };
438
- };
439
- Je();
440
- const Tt = et.use || // This extra generic is to avoid TypeScript mixing up the generic and JSX sytax
441
- // and emitting an error.
442
- // We assume that this is only for the `use(thenable)` case, not `use(context)`.
443
- // https://github.com/facebook/react/blob/aed00dacfb79d17c53218404c52b1c7aa59c4a89/packages/react-server/src/ReactFizzThenable.js#L45
444
- ((t) => {
445
- switch (t.status) {
446
- case "pending":
447
- throw t;
448
- case "fulfilled":
449
- return t.value;
450
- case "rejected":
451
- throw t.reason;
452
- default:
453
- throw t.status = "pending", t.then((e) => {
454
- t.status = "fulfilled", t.value = e;
455
- }, (e) => {
456
- t.status = "rejected", t.reason = e;
457
- }), t;
458
- }
459
- }), yt = {
460
- dedupe: !0
461
- }, en = (t, e, n) => {
462
- const { cache: r, compare: o, suspense: s, fallbackData: i, revalidateOnMount: E, revalidateIfStale: h, refreshInterval: y, refreshWhenHidden: O, refreshWhenOffline: u, keepPreviousData: d } = n, [c, p, l, D] = M.get(r), [a, P] = Ft(t), Q = $(!1), j = $(!1), R = $(a), I = $(e), q = $(n), S = () => q.current, H = () => S().isVisible() && S().isOnline(), [V, k, W, C] = ie(r, a), K = $({}).current, ot = f(i) ? f(n.fallback) ? A : n.fallback[a] : i, Mt = (_, m) => {
463
- for (const T in K) {
464
- const v = T;
465
- if (v === "data") {
466
- if (!o(_[v], m[v]) && (!f(_[v]) || !o(it, m[v])))
467
- return !1;
468
- } else if (m[v] !== _[v])
469
- return !1;
470
- }
471
- return !0;
472
- }, Pt = ft(() => {
473
- const _ = !a || !e ? !1 : f(E) ? S().isPaused() || s ? !1 : h !== !1 : E, m = (g) => {
474
- const F = G(g);
475
- return delete F._k, _ ? {
476
- isValidating: !0,
477
- isLoading: !0,
478
- ...F
479
- } : F;
480
- }, T = V(), v = C(), b = m(T), z = T === v ? b : m(v);
481
- let w = b;
482
- return [
483
- () => {
484
- const g = m(V());
485
- return Mt(g, w) ? (w.data = g.data, w.isLoading = g.isLoading, w.isValidating = g.isValidating, w.error = g.error, w) : (w = g, g);
486
- },
487
- () => z
488
- ];
489
- }, [
490
- r,
491
- a
492
- ]), J = De.useSyncExternalStore(lt(
493
- (_) => W(a, (m, T) => {
494
- Mt(T, m) || _();
495
- }),
496
- // eslint-disable-next-line react-hooks/exhaustive-deps
497
- [
498
- r,
499
- a
500
- ]
501
- ), Pt[0], Pt[1]), jt = !Q.current, me = c[a] && c[a].length > 0, Y = J.data, B = f(Y) ? ot && se(ot) ? Tt(ot) : ot : Y, st = J.error, pt = $(B), it = d ? f(Y) ? f(pt.current) ? B : pt.current : Y : B, Ut = me && !f(st) ? !1 : jt && !f(E) ? E : S().isPaused() ? !1 : s ? f(B) ? !1 : h : f(B) || h, Gt = !!(a && e && jt && Ut), he = f(J.isValidating) ? Gt : J.isValidating, _e = f(J.isLoading) ? Gt : J.isLoading, Z = lt(
502
- async (_) => {
503
- const m = I.current;
504
- if (!a || !m || j.current || S().isPaused())
505
- return !1;
506
- let T, v, b = !0;
507
- const z = _ || {}, w = !l[a] || !z.dedupe, g = () => zt ? !j.current && a === R.current && Q.current : a === R.current, F = {
508
- isValidating: !1,
509
- isLoading: !1
510
- }, Bt = () => {
511
- k(F);
512
- }, $t = () => {
513
- const L = l[a];
514
- L && L[1] === v && delete l[a];
515
- }, qt = {
516
- isValidating: !0
517
- };
518
- f(V().data) && (qt.isLoading = !0);
519
- try {
520
- if (w && (k(qt), n.loadingTimeout && f(V().data) && setTimeout(() => {
521
- b && g() && S().onLoadingSlow(a, n);
522
- }, n.loadingTimeout), l[a] = [
523
- m(P),
524
- Vt()
525
- ]), [T, v] = l[a], T = await T, w && setTimeout($t, n.dedupingInterval), !l[a] || l[a][1] !== v)
526
- return w && g() && S().onDiscarded(a), !1;
527
- F.error = A;
528
- const L = p[a];
529
- if (!f(L) && // case 1
530
- (v <= L[0] || // case 2
531
- v <= L[1] || // case 3
532
- L[1] === 0))
533
- return Bt(), w && g() && S().onDiscarded(a), !1;
534
- const x = V().data;
535
- F.data = o(x, T) ? x : T, w && g() && S().onSuccess(T, a, n);
536
- } catch (L) {
537
- $t();
538
- const x = S(), { shouldRetryOnError: mt } = x;
539
- x.isPaused() || (F.error = L, w && g() && (x.onError(L, a, x), (mt === !0 || N(mt) && mt(L)) && (!S().revalidateOnFocus || !S().revalidateOnReconnect || H()) && x.onErrorRetry(L, a, x, (Se) => {
540
- const ht = c[a];
541
- ht && ht[0] && ht[0](Jt, Se);
542
- }, {
543
- retryCount: (z.retryCount || 0) + 1,
544
- dedupe: !0
545
- })));
546
- }
547
- return b = !1, Bt(), !0;
548
- },
549
- // `setState` is immutable, and `eventsCallback`, `fnArg`, and
550
- // `keyValidating` are depending on `key`, so we can exclude them from
551
- // the deps array.
552
- //
553
- // FIXME:
554
- // `fn` and `config` might be changed during the lifecycle,
555
- // but they might be changed every render like this.
556
- // `useSWR('key', () => fetch('/api/'), { suspense: true })`
557
- // So we omit the values from the deps array
558
- // even though it might cause unexpected behaviors.
559
- // eslint-disable-next-line react-hooks/exhaustive-deps
560
- [
561
- a,
562
- r
563
- ]
564
- ), Wt = lt(
565
- // Use callback to make sure `keyRef.current` returns latest result every time
566
- (..._) => ae(r, R.current, ..._),
567
- // eslint-disable-next-line react-hooks/exhaustive-deps
568
- []
569
- );
570
- if (vt(() => {
571
- I.current = e, q.current = n, f(Y) || (pt.current = Y);
572
- }), vt(() => {
573
- if (!a) return;
574
- const _ = Z.bind(A, yt);
575
- let m = 0;
576
- S().revalidateOnFocus && (m = Date.now() + S().focusThrottleInterval);
577
- const v = tn(a, c, (b, z = {}) => {
578
- if (b == ne) {
579
- const w = Date.now();
580
- S().revalidateOnFocus && w > m && H() && (m = w + S().focusThrottleInterval, _());
581
- } else if (b == re)
582
- S().revalidateOnReconnect && H() && _();
583
- else {
584
- if (b == oe)
585
- return Z();
586
- if (b == Jt)
587
- return Z(z);
588
- }
589
- });
590
- return j.current = !1, R.current = a, Q.current = !0, k({
591
- _k: P
592
- }), Ut && (f(B) || tt ? _() : Me(_)), () => {
593
- j.current = !0, v();
594
- };
595
- }, [
596
- a
597
- ]), vt(() => {
598
- let _;
599
- function m() {
600
- const v = N(y) ? y(V().data) : y;
601
- v && _ !== -1 && (_ = setTimeout(T, v));
602
- }
603
- function T() {
604
- !V().error && (O || S().isVisible()) && (u || S().isOnline()) ? Z(yt).then(m) : m();
605
- }
606
- return m(), () => {
607
- _ && (clearTimeout(_), _ = -1);
608
- };
609
- }, [
610
- y,
611
- O,
612
- u,
613
- a
614
- ]), Re(it), s && f(B) && a) {
615
- if (!zt && tt)
616
- throw new Error("Fallback data is required when using Suspense in SSR.");
617
- I.current = e, q.current = n, j.current = !1;
618
- const _ = D[a];
619
- if (!f(_)) {
620
- const m = Wt(_);
621
- Tt(m);
622
- }
623
- if (f(st)) {
624
- const m = Z(yt);
625
- f(it) || (m.status = "fulfilled", m.value = !0), Tt(m);
626
- } else
627
- throw st;
628
- }
629
- return {
630
- mutate: Wt,
631
- get data() {
632
- return K.data = !0, it;
633
- },
634
- get error() {
635
- return K.error = !0, st;
636
- },
637
- get isValidating() {
638
- return K.isValidating = !0, he;
639
- },
640
- get isLoading() {
641
- return K.isLoading = !0, _e;
642
- }
643
- };
644
- }, nn = Ze(en), xt = "https://api.figma.com", rn = `${xt}/v1/files`, le = `${xt}/v1/variables`, fe = le, de = (t) => `${le}/${t}`, on = (t) => `${rn}/${t}/variables/local`, sn = "application/json", Ee = "X-FIGMA-TOKEN", X = "A Figma API token is required.", an = "An error occurred while fetching data from the Figma API.";
645
- async function cn(t, e) {
646
- if (!e)
647
- throw new Error(X);
648
- const n = await fetch(t, {
649
- method: "GET",
650
- headers: {
651
- [Ee]: e,
652
- "Content-Type": sn
653
- }
654
- });
655
- if (!n.ok) {
656
- let r = an;
657
- try {
658
- const o = await n.json();
659
- o != null && o.message && (r = o.message);
660
- } catch {
661
- }
662
- throw new Error(r);
663
- }
664
- return n.json();
665
- }
666
- const pe = () => {
667
- const { token: t, fileKey: e } = nt(), n = e ? on(e) : null, { data: r, error: o, isLoading: s, isValidating: i, mutate: E } = nn(
668
- t && n ? [n, t] : null,
669
- cn
670
- );
671
- return {
672
- data: r,
673
- isLoading: s,
674
- isValidating: i,
675
- error: o,
676
- mutate: E
677
- };
678
- }, pn = () => {
679
- const { data: t } = pe(), e = ft(
680
- () => t != null && t.meta ? Object.values(t.meta.variableCollections) : [],
681
- [t]
682
- ), n = ft(
683
- () => t != null && t.meta ? t.meta.variableCollections : {},
684
- [t]
685
- );
686
- return {
687
- collections: e,
688
- collectionsById: n
689
- };
690
- }, mn = () => {
691
- const { data: t } = pe();
692
- return ft(() => {
693
- const e = [], n = {}, r = {};
694
- if (t != null && t.meta)
695
- for (const o of Object.values(
696
- t.meta.variableCollections
697
- )) {
698
- e.push(...o.modes), n[o.id] = o.modes;
699
- for (const s of o.modes)
700
- r[s.modeId] = s;
701
- }
702
- return {
703
- modes: e,
704
- modesByCollectionId: n,
705
- modesById: r
706
- };
707
- }, [t]);
708
- };
709
- function un(t, e) {
710
- switch (e.type) {
711
- case "loading":
712
- return { ...t, status: "loading", error: null };
713
- case "success":
714
- return { ...t, status: "success", data: e.payload };
715
- case "error":
716
- return { ...t, status: "error", error: e.payload };
717
- default:
718
- return t;
719
- }
720
- }
721
- const dt = (t) => {
722
- const e = {
723
- status: "idle",
724
- data: null,
725
- error: null
726
- }, [n, r] = Te(un, e);
727
- return {
728
- mutate: lt(
729
- async (s) => {
730
- r({ type: "loading" });
731
- try {
732
- const i = await t(s);
733
- return r({ type: "success", payload: i }), i;
734
- } catch (i) {
735
- r({ type: "error", payload: i });
736
- return;
737
- }
738
- },
739
- [t]
740
- ),
741
- ...n,
742
- isLoading: n.status === "loading",
743
- isSuccess: n.status === "success",
744
- isError: n.status === "error"
745
- };
746
- };
747
- async function Et(t, e, n, r) {
748
- if (!e)
749
- throw new Error(X);
750
- const s = {
751
- CREATE: "POST",
752
- UPDATE: "PUT",
753
- DELETE: "DELETE"
754
- }[n], i = await fetch(`${xt}${t}`, {
755
- method: s,
756
- headers: {
757
- "Content-Type": "application/json",
758
- [Ee]: e
759
- },
760
- body: r ? JSON.stringify(r) : void 0
761
- });
762
- if (!i.ok) {
763
- const E = await i.json().catch(() => ({}));
764
- throw new Error(
765
- E.err || E.message || "An API error occurred"
766
- );
767
- }
768
- return i.status === 204 || !i.body ? {} : i.json();
769
- }
770
- const hn = () => {
771
- const { token: t } = nt();
772
- return dt(async (n) => {
773
- if (!t)
774
- throw new Error(X);
775
- return await Et(
776
- fe,
777
- t,
778
- "CREATE",
779
- n
780
- );
781
- });
782
- }, _n = () => {
783
- const { token: t } = nt();
784
- return dt(
785
- async ({ variableId: n, payload: r }) => {
786
- if (!t)
787
- throw new Error(X);
788
- const o = de(n);
789
- return await Et(
790
- o,
791
- t,
792
- "UPDATE",
793
- r
794
- );
795
- }
796
- );
797
- }, Sn = () => {
798
- const { token: t } = nt();
799
- return dt(async (n) => {
800
- if (!t)
801
- throw new Error(X);
802
- return await Et(
803
- de(n),
804
- t,
805
- "DELETE",
806
- void 0
807
- );
808
- });
809
- }, On = () => {
810
- const { token: t } = nt();
811
- return dt(async (n) => {
812
- if (!t)
813
- throw new Error(X);
814
- return await Et(
815
- fe,
816
- t,
817
- "CREATE",
818
- n
819
- );
820
- });
821
- };
822
- function vn(t, e) {
823
- return t.filter((n) => {
824
- let r = !0;
825
- return e.resolvedType && (r = r && n.resolvedType === e.resolvedType), e.name && (r = r && n.name.includes(e.name)), r;
826
- });
827
- }
828
- export {
829
- En as FigmaVarsProvider,
830
- vn as filterVariables,
831
- On as useBulkUpdateVariables,
832
- hn as useCreateVariable,
833
- Sn as useDeleteVariable,
834
- _n as useUpdateVariable,
835
- pn as useVariableCollections,
836
- mn as useVariableModes,
837
- pe as useVariables
838
- };