@fractalshq/sync 0.1.0 → 0.1.2

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 (43) hide show
  1. package/README.md +43 -11
  2. package/dist/chunk-4VKCXC52.mjs +129 -0
  3. package/dist/chunk-4ZYNBLY5.mjs +129 -0
  4. package/dist/chunk-DUDWBTA2.mjs +0 -0
  5. package/dist/chunk-FBL2T6BA.mjs +249 -0
  6. package/dist/chunk-ZIWHYEXE.mjs +498 -0
  7. package/dist/index.d.mts +1 -3
  8. package/dist/index.d.ts +1 -3
  9. package/dist/index.js +19 -649
  10. package/dist/index.mjs +7 -42
  11. package/dist/v1/core/index.d.mts +28 -0
  12. package/dist/v1/core/index.d.ts +28 -0
  13. package/dist/v1/core/index.js +320 -0
  14. package/dist/v1/core/index.mjs +51 -0
  15. package/dist/v1/index.d.mts +3 -0
  16. package/dist/v1/index.d.ts +3 -0
  17. package/dist/v1/index.js +816 -0
  18. package/dist/v1/index.mjs +63 -0
  19. package/dist/v1/react/index.d.mts +111 -0
  20. package/dist/v1/react/index.d.ts +111 -0
  21. package/dist/v1/react/index.js +552 -0
  22. package/dist/v1/react/index.mjs +330 -0
  23. package/dist/v1/server/index.d.mts +67 -0
  24. package/dist/v1/server/index.d.ts +67 -0
  25. package/dist/v1/server/index.js +540 -0
  26. package/dist/v1/server/index.mjs +15 -0
  27. package/dist/v1/widgets/index.d.mts +2 -0
  28. package/dist/v1/widgets/index.d.ts +2 -0
  29. package/dist/v1/widgets/index.js +18 -0
  30. package/dist/v1/widgets/index.mjs +0 -0
  31. package/dist/v2/core/index.d.mts +5 -21
  32. package/dist/v2/core/index.d.ts +5 -21
  33. package/dist/v2/core/index.js +9 -35
  34. package/dist/v2/core/index.mjs +3 -7
  35. package/dist/v2/index.d.mts +1 -1
  36. package/dist/v2/index.d.ts +1 -1
  37. package/dist/v2/index.js +9 -35
  38. package/dist/v2/index.mjs +4 -7
  39. package/dist/v2/react/index.d.mts +5 -27
  40. package/dist/v2/react/index.d.ts +5 -27
  41. package/dist/v2/react/index.js +26 -108
  42. package/dist/v2/react/index.mjs +20 -76
  43. package/package.json +52 -21
@@ -0,0 +1,552 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __objRest = (source, exclude) => {
24
+ var target = {};
25
+ for (var prop in source)
26
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
27
+ target[prop] = source[prop];
28
+ if (source != null && __getOwnPropSymbols)
29
+ for (var prop of __getOwnPropSymbols(source)) {
30
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
31
+ target[prop] = source[prop];
32
+ }
33
+ return target;
34
+ };
35
+ var __export = (target, all) => {
36
+ for (var name in all)
37
+ __defProp(target, name, { get: all[name], enumerable: true });
38
+ };
39
+ var __copyProps = (to, from, except, desc) => {
40
+ if (from && typeof from === "object" || typeof from === "function") {
41
+ for (let key of __getOwnPropNames(from))
42
+ if (!__hasOwnProp.call(to, key) && key !== except)
43
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
44
+ }
45
+ return to;
46
+ };
47
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
48
+
49
+ // src/v1/react/index.tsx
50
+ var react_exports = {};
51
+ __export(react_exports, {
52
+ SyncProvider: () => SyncProvider,
53
+ useClaimFlow: () => useClaimFlow,
54
+ useClaimHistory: () => useClaimHistory,
55
+ useClaimTransaction: () => useClaimTransaction,
56
+ useClaimableDistributions: () => useClaimableDistributions,
57
+ useCommitClaim: () => useCommitClaim,
58
+ useCommitDistribution: () => useCommitDistribution,
59
+ useDistribution: () => useDistribution,
60
+ useDistributionFlow: () => useDistributionFlow,
61
+ useDistributionTransaction: () => useDistributionTransaction,
62
+ useDistributions: () => useDistributions,
63
+ useRecipientComposer: () => useRecipientComposer,
64
+ useSyncClient: () => useSyncClient
65
+ });
66
+ module.exports = __toCommonJS(react_exports);
67
+ var import_react = require("react");
68
+ var import_react_query = require("@tanstack/react-query");
69
+
70
+ // src/v1/core/constants.ts
71
+ var DEFAULT_RPC_ENDPOINT = process.env.NEXT_PUBLIC_SOLANA_RPC_URL || "https://api.mainnet-beta.solana.com";
72
+
73
+ // src/v1/core/persistence.ts
74
+ var STORAGE_PREFIX = "distribution:persistence-warning:";
75
+ var PERSISTENCE_WARNING_EVENT = "distribution:persistence-warning";
76
+ function isBrowser() {
77
+ return typeof window !== "undefined" && typeof localStorage !== "undefined";
78
+ }
79
+ function persistToStorage(record) {
80
+ if (!isBrowser()) return;
81
+ try {
82
+ localStorage.setItem(`${STORAGE_PREFIX}${record.id}`, JSON.stringify(record));
83
+ } catch (error) {
84
+ console.error("[sync:persistence-warning] Failed to write to localStorage", error);
85
+ }
86
+ }
87
+ function emitWarning(record) {
88
+ if (!isBrowser()) return;
89
+ try {
90
+ window.dispatchEvent(new CustomEvent(PERSISTENCE_WARNING_EVENT, { detail: record }));
91
+ } catch (error) {
92
+ console.error("[sync:persistence-warning] Failed to emit warning event", error);
93
+ }
94
+ }
95
+ function recordPersistenceWarning(input) {
96
+ var _a2;
97
+ if (!isBrowser()) return void 0;
98
+ const record = __spreadProps(__spreadValues({}, input), {
99
+ createdAt: (_a2 = input.createdAt) != null ? _a2 : Date.now()
100
+ });
101
+ persistToStorage(record);
102
+ emitWarning(record);
103
+ return record;
104
+ }
105
+ function handlePersistenceWarningResponse(response) {
106
+ var _a2;
107
+ if (!isBrowser() || !response || typeof response !== "object") return;
108
+ const warning = response.persistenceWarning;
109
+ if (!warning) return;
110
+ const distributionId = warning.distributionId || response.distributionId || "unknown";
111
+ const exportJson = (_a2 = response.exportJson) != null ? _a2 : warning.exportJson;
112
+ const reason = warning.reason || "persist_distribution_failed";
113
+ const message = warning.message || "We were unable to save your distribution draft. Download the JSON to avoid data loss.";
114
+ const key = warning.id || `${distributionId}:${reason}:${Date.now()}`;
115
+ recordPersistenceWarning({
116
+ id: key,
117
+ distributionId,
118
+ reason,
119
+ message,
120
+ detail: warning.detail,
121
+ exportJson
122
+ });
123
+ }
124
+
125
+ // src/v1/core/http.ts
126
+ var FALLBACK_BASE_PATH = "/api/v1/fractals-sync";
127
+ var _a, _b;
128
+ var ENV_BASE_PATH = typeof process !== "undefined" && (((_a = process.env) == null ? void 0 : _a.NEXT_PUBLIC_SYNC_PATH) || ((_b = process.env) == null ? void 0 : _b.NEXT_PUBLIC_FRACTALS_SYNC_PATH)) || void 0;
129
+ var DEFAULT_BASE_PATH = sanitizeBasePath(ENV_BASE_PATH || FALLBACK_BASE_PATH);
130
+ function resolveRequestConfig(options) {
131
+ return {
132
+ basePath: sanitizeBasePath((options == null ? void 0 : options.basePath) || DEFAULT_BASE_PATH),
133
+ fetcher: resolveFetcher(options == null ? void 0 : options.fetcher)
134
+ };
135
+ }
136
+ var SyncReactError = class extends Error {
137
+ constructor(status, code, details) {
138
+ super(code || `Sync request failed with status ${status}`);
139
+ this.status = status;
140
+ this.code = code;
141
+ this.details = details;
142
+ this.name = "SyncReactError";
143
+ }
144
+ };
145
+ async function requestJSON(config, path, init) {
146
+ var _a2;
147
+ const url = buildUrl(config.basePath, path);
148
+ const requestInit = prepareInit(init);
149
+ requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
150
+ const response = await config.fetcher(url, requestInit);
151
+ const payload = await parseResponseBody(response);
152
+ if (!response.ok) {
153
+ const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
154
+ throw new SyncReactError(response.status, errorCode, payload);
155
+ }
156
+ return payload;
157
+ }
158
+ function resolveFetcher(custom) {
159
+ if (custom) return custom;
160
+ if (typeof fetch === "function") return fetch.bind(globalThis);
161
+ throw new Error("Global fetch is not available. Provide a fetcher via Sync configuration.");
162
+ }
163
+ function sanitizeBasePath(path) {
164
+ if (!path) return FALLBACK_BASE_PATH;
165
+ const trimmed = path.trim();
166
+ if (!trimmed || trimmed === "/") return "/";
167
+ return trimmed.replace(/\/+$/, "");
168
+ }
169
+ function buildUrl(basePath, path) {
170
+ if (path && isAbsoluteUrl(path)) return path;
171
+ const normalizedBase = sanitizeBasePath(basePath);
172
+ const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
173
+ if (!normalizedBase || normalizedBase === "/") {
174
+ return suffix ? suffix : normalizedBase;
175
+ }
176
+ return `${normalizedBase}${suffix}`;
177
+ }
178
+ function isAbsoluteUrl(path) {
179
+ return /^https?:\/\//i.test(path);
180
+ }
181
+ function isRelativeUrl(path) {
182
+ return !isAbsoluteUrl(path);
183
+ }
184
+ function prepareInit(init) {
185
+ const finalInit = __spreadValues({}, init);
186
+ if ((init == null ? void 0 : init.headers) instanceof Headers) {
187
+ const cloned = new Headers(init.headers);
188
+ if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
189
+ finalInit.headers = cloned;
190
+ return finalInit;
191
+ }
192
+ const headers = new Headers(init == null ? void 0 : init.headers);
193
+ if (!headers.has("content-type")) {
194
+ headers.set("content-type", "application/json");
195
+ }
196
+ finalInit.headers = headers;
197
+ return finalInit;
198
+ }
199
+ async function parseResponseBody(response) {
200
+ if (response.status === 204) return void 0;
201
+ const text = await response.text();
202
+ if (!text) return void 0;
203
+ try {
204
+ return JSON.parse(text);
205
+ } catch (e) {
206
+ return text;
207
+ }
208
+ }
209
+
210
+ // src/v1/core/api.ts
211
+ async function buildDistributionTransaction(input, options) {
212
+ const config = resolveRequestConfig(options);
213
+ const path = (input == null ? void 0 : input.distributionId) ? `/distributions/${encodeURIComponent(input.distributionId)}/create-transaction` : "/distributions/create-transaction";
214
+ const payload = await requestJSON(config, path, {
215
+ method: "POST",
216
+ body: JSON.stringify(input)
217
+ });
218
+ handlePersistenceWarningResponse(payload);
219
+ return payload;
220
+ }
221
+ async function commitDistributionSignature(input, options) {
222
+ const distributionId = input == null ? void 0 : input.distributionId;
223
+ if (!distributionId) {
224
+ throw new SyncReactError(400, "distribution_id_required");
225
+ }
226
+ const config = resolveRequestConfig(options);
227
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
228
+ return requestJSON(
229
+ config,
230
+ `/distributions/${encodeURIComponent(distributionId)}/commit`,
231
+ {
232
+ method: "POST",
233
+ body: JSON.stringify(body)
234
+ }
235
+ );
236
+ }
237
+
238
+ // src/v1/react/index.tsx
239
+ var SyncContext = (0, import_react.createContext)({ basePath: DEFAULT_BASE_PATH });
240
+ function SyncProvider({ basePath, fetcher, children }) {
241
+ const value = (0, import_react.useMemo)(
242
+ () => ({
243
+ basePath: sanitizeBasePath(basePath || DEFAULT_BASE_PATH),
244
+ fetcher
245
+ }),
246
+ [basePath, fetcher]
247
+ );
248
+ return (0, import_react.createElement)(SyncContext.Provider, { value }, children);
249
+ }
250
+ function useSyncClient() {
251
+ const requestConfig = useSyncRequestConfig();
252
+ return (0, import_react.useMemo)(() => {
253
+ const request = (path, init) => requestJSON(requestConfig, path, init);
254
+ return {
255
+ basePath: requestConfig.basePath,
256
+ request,
257
+ get: (path, init) => request(path, __spreadProps(__spreadValues({}, init), { method: "GET" })),
258
+ post: (path, body, init) => {
259
+ const finalInit = __spreadProps(__spreadValues({}, init), { method: ((init == null ? void 0 : init.method) || "POST").toUpperCase() });
260
+ if (body !== void 0) {
261
+ finalInit.body = typeof body === "string" ? body : JSON.stringify(body);
262
+ }
263
+ return request(path, finalInit);
264
+ }
265
+ };
266
+ }, [requestConfig]);
267
+ }
268
+ function useDistributions(paramsOrOptions, optionsMaybe) {
269
+ var _a2, _b2;
270
+ const client = useSyncClient();
271
+ const { params, options } = resolveDistributionsArgs(paramsOrOptions, optionsMaybe);
272
+ const path = buildDistributionsPath(params);
273
+ return (0, import_react_query.useQuery)(__spreadValues({
274
+ queryKey: ["sync", "distributions", "me", (_a2 = params == null ? void 0 : params.role) != null ? _a2 : "claimant", (_b2 = params == null ? void 0 : params.status) != null ? _b2 : "all"],
275
+ queryFn: () => client.get(path)
276
+ }, options));
277
+ }
278
+ function useDistribution(distributionId, options) {
279
+ var _a2;
280
+ const client = useSyncClient();
281
+ const id = distributionId ? encodeURIComponent(distributionId) : "";
282
+ return (0, import_react_query.useQuery)(__spreadProps(__spreadValues({
283
+ queryKey: ["sync", "distribution", id],
284
+ queryFn: () => client.get(`/distributions/me/${id}`)
285
+ }, options), {
286
+ enabled: Boolean(id) && ((_a2 = options == null ? void 0 : options.enabled) != null ? _a2 : true)
287
+ }));
288
+ }
289
+ function useDistributionTransaction(options) {
290
+ const requestConfig = useSyncRequestConfig();
291
+ return (0, import_react_query.useMutation)(__spreadValues({
292
+ mutationFn: async (input) => {
293
+ if (!input) throw new SyncReactError(400, "distribution_input_required");
294
+ return buildDistributionTransaction(input, requestConfig);
295
+ }
296
+ }, options));
297
+ }
298
+ function useCommitDistribution(options) {
299
+ const requestConfig = useSyncRequestConfig();
300
+ return (0, import_react_query.useMutation)(__spreadValues({
301
+ mutationFn: async (input) => {
302
+ if (!(input == null ? void 0 : input.distributionId)) throw new SyncReactError(400, "distribution_id_required");
303
+ return commitDistributionSignature(input, requestConfig);
304
+ }
305
+ }, options));
306
+ }
307
+ function useDistributionFlow() {
308
+ var _a2, _b2;
309
+ const buildMutation = useDistributionTransaction();
310
+ const commitMutation = useCommitDistribution();
311
+ const [latestPayload, setLatestPayload] = (0, import_react.useState)(null);
312
+ const [latestInput, setLatestInput] = (0, import_react.useState)(null);
313
+ const build = (0, import_react.useCallback)(
314
+ async (input) => {
315
+ const payload = await buildMutation.mutateAsync(input);
316
+ setLatestPayload(payload);
317
+ setLatestInput(cloneDistributionInput(input));
318
+ return payload;
319
+ },
320
+ [buildMutation]
321
+ );
322
+ const doCommit = (0, import_react.useCallback)(
323
+ async (signer, input) => {
324
+ let payload = null;
325
+ if (input) {
326
+ payload = await build(input);
327
+ } else if (latestInput) {
328
+ payload = await build(latestInput);
329
+ } else if (latestPayload) {
330
+ payload = latestPayload;
331
+ }
332
+ if (!payload) {
333
+ throw new SyncReactError(400, "distribution_payload_missing");
334
+ }
335
+ if (!payload.distributionId) {
336
+ throw new SyncReactError(400, "distribution_id_required");
337
+ }
338
+ const signerInput = await signer(payload);
339
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
340
+ throw new SyncReactError(400, "distribution_signature_required");
341
+ }
342
+ const commitPayload = __spreadProps(__spreadValues({}, signerInput), {
343
+ distributionId: payload.distributionId
344
+ });
345
+ if (!commitPayload.exportJson && payload.exportJson) {
346
+ commitPayload.exportJson = payload.exportJson;
347
+ }
348
+ const commitResult = await commitMutation.mutateAsync(commitPayload);
349
+ return { payload, commit: commitResult, signerInput };
350
+ },
351
+ [build, latestInput, latestPayload, commitMutation]
352
+ );
353
+ const reset = (0, import_react.useCallback)(() => {
354
+ setLatestPayload(null);
355
+ setLatestInput(null);
356
+ buildMutation.reset();
357
+ commitMutation.reset();
358
+ }, [buildMutation, commitMutation]);
359
+ const error = (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null;
360
+ const state = (0, import_react.useMemo)(
361
+ () => ({
362
+ latestPayload,
363
+ building: buildMutation.isPending,
364
+ committing: commitMutation.isPending,
365
+ error
366
+ }),
367
+ [latestPayload, buildMutation.isPending, commitMutation.isPending, error]
368
+ );
369
+ return { state, build, commit: doCommit, claim: doCommit, reset };
370
+ }
371
+ function useClaimableDistributions(options) {
372
+ const client = useSyncClient();
373
+ return (0, import_react_query.useQuery)(__spreadValues({
374
+ queryKey: ["sync", "claims", "me"],
375
+ queryFn: () => client.get("/claims/me")
376
+ }, options));
377
+ }
378
+ function useClaimHistory(options) {
379
+ const client = useSyncClient();
380
+ return (0, import_react_query.useQuery)(__spreadValues({
381
+ queryKey: ["sync", "claims", "history"],
382
+ queryFn: () => client.get("/claims/history")
383
+ }, options));
384
+ }
385
+ function useClaimTransaction(distributionId, options) {
386
+ const client = useSyncClient();
387
+ const id = distributionId ? encodeURIComponent(distributionId) : null;
388
+ return (0, import_react_query.useMutation)(__spreadValues({
389
+ mutationFn: async (input = {}) => {
390
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
391
+ return client.post(`/claims/${id}/create-transaction`, input);
392
+ }
393
+ }, options));
394
+ }
395
+ function useCommitClaim(distributionId, options) {
396
+ const client = useSyncClient();
397
+ const id = distributionId ? encodeURIComponent(distributionId) : null;
398
+ return (0, import_react_query.useMutation)(__spreadValues({
399
+ mutationFn: async (input) => {
400
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
401
+ if (!input || !input.signature && !input.transaction && !input.signedTransactionBase64) {
402
+ throw new SyncReactError(400, "transaction_or_signature_required");
403
+ }
404
+ return client.post(`/claims/${id}/commit`, input);
405
+ }
406
+ }, options));
407
+ }
408
+ function useClaimFlow(distributionId) {
409
+ var _a2, _b2;
410
+ const buildMutation = useClaimTransaction(distributionId);
411
+ const commitMutation = useCommitClaim(distributionId);
412
+ const [latestPayload, setLatestPayload] = (0, import_react.useState)(null);
413
+ const build = (0, import_react.useCallback)(
414
+ async (input) => {
415
+ const payload = await buildMutation.mutateAsync(input != null ? input : {});
416
+ setLatestPayload(payload);
417
+ return payload;
418
+ },
419
+ [buildMutation]
420
+ );
421
+ const claim = (0, import_react.useCallback)(
422
+ async (signer, input) => {
423
+ const payload = await build(input);
424
+ const signerInput = await signer(payload);
425
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
426
+ throw new SyncReactError(400, "claim_signature_required");
427
+ }
428
+ const commit = await commitMutation.mutateAsync(signerInput);
429
+ return { payload, commit, signerInput };
430
+ },
431
+ [build, commitMutation]
432
+ );
433
+ const reset = (0, import_react.useCallback)(() => {
434
+ setLatestPayload(null);
435
+ }, []);
436
+ const state = {
437
+ latestPayload,
438
+ building: buildMutation.isPending,
439
+ claiming: commitMutation.isPending,
440
+ error: (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null
441
+ };
442
+ return { state, build, claim, reset };
443
+ }
444
+ function useSyncRequestConfig() {
445
+ const context = (0, import_react.useContext)(SyncContext);
446
+ const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
447
+ const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
448
+ return (0, import_react.useMemo)(() => ({ basePath, fetcher }), [basePath, fetcher]);
449
+ }
450
+ function cloneDistributionInput(input) {
451
+ return __spreadProps(__spreadValues({}, input), {
452
+ recipients: input.recipients.map((recipient) => __spreadValues({}, recipient))
453
+ });
454
+ }
455
+ function resolveDistributionsArgs(paramsOrOptions, optionsMaybe) {
456
+ if (isDistributionsParams(paramsOrOptions)) {
457
+ return { params: paramsOrOptions, options: optionsMaybe };
458
+ }
459
+ return { params: void 0, options: paramsOrOptions };
460
+ }
461
+ function isDistributionsParams(arg) {
462
+ if (!arg || typeof arg !== "object") return false;
463
+ return "role" in arg || "status" in arg;
464
+ }
465
+ function buildDistributionsPath(params) {
466
+ const search = new URLSearchParams();
467
+ if ((params == null ? void 0 : params.role) && params.role !== "claimant") {
468
+ search.set("role", params.role);
469
+ }
470
+ if (params == null ? void 0 : params.status) {
471
+ search.set("status", params.status);
472
+ }
473
+ const qs = search.toString();
474
+ return qs ? `/distributions/me?${qs}` : "/distributions/me";
475
+ }
476
+ function createRecipientRowId() {
477
+ return `recipient-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
478
+ }
479
+ function hydrateRecipientRow(partial) {
480
+ var _a2, _b2;
481
+ return {
482
+ id: createRecipientRowId(),
483
+ address: (_a2 = partial == null ? void 0 : partial.address) != null ? _a2 : "",
484
+ amount: (_b2 = partial == null ? void 0 : partial.amount) != null ? _b2 : ""
485
+ };
486
+ }
487
+ function useRecipientComposer(initial = []) {
488
+ const [rows, setRows] = (0, import_react.useState)(() => {
489
+ if (initial.length === 0) return [hydrateRecipientRow()];
490
+ return initial.map((row) => hydrateRecipientRow(row));
491
+ });
492
+ const addRow = (0, import_react.useCallback)(() => {
493
+ setRows((current) => [...current, hydrateRecipientRow()]);
494
+ }, []);
495
+ const removeRow = (0, import_react.useCallback)((id) => {
496
+ setRows((current) => {
497
+ if (current.length <= 1) return [hydrateRecipientRow()];
498
+ return current.filter((row) => row.id !== id);
499
+ });
500
+ }, []);
501
+ const updateRow = (0, import_react.useCallback)((id, patch) => {
502
+ setRows((current) => current.map((row) => row.id === id ? __spreadValues(__spreadValues({}, row), patch) : row));
503
+ }, []);
504
+ const replaceRows = (0, import_react.useCallback)((incoming) => {
505
+ if (!incoming.length) {
506
+ setRows([hydrateRecipientRow()]);
507
+ return;
508
+ }
509
+ setRows(incoming.map((row) => hydrateRecipientRow(row)));
510
+ }, []);
511
+ const clearRows = (0, import_react.useCallback)(() => {
512
+ setRows([hydrateRecipientRow()]);
513
+ }, []);
514
+ const totals = (0, import_react.useMemo)(() => {
515
+ const normalized = rows.map((row) => ({
516
+ id: row.id,
517
+ address: row.address.trim(),
518
+ amountUi: Number(row.amount) || 0,
519
+ amountRaw: row.amount
520
+ }));
521
+ const totalAmountUi = normalized.reduce((sum, row) => sum + Math.max(0, row.amountUi), 0);
522
+ return {
523
+ count: rows.length,
524
+ totalAmountUi,
525
+ normalized
526
+ };
527
+ }, [rows]);
528
+ return {
529
+ state: { rows, totals },
530
+ addRow,
531
+ removeRow,
532
+ updateRow,
533
+ replaceRows,
534
+ clearRows
535
+ };
536
+ }
537
+ // Annotate the CommonJS export names for ESM import in node:
538
+ 0 && (module.exports = {
539
+ SyncProvider,
540
+ useClaimFlow,
541
+ useClaimHistory,
542
+ useClaimTransaction,
543
+ useClaimableDistributions,
544
+ useCommitClaim,
545
+ useCommitDistribution,
546
+ useDistribution,
547
+ useDistributionFlow,
548
+ useDistributionTransaction,
549
+ useDistributions,
550
+ useRecipientComposer,
551
+ useSyncClient
552
+ });