@figma-vars/hooks 1.5.0 → 2.0.0-beta.1

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