@liberfi.io/hooks 0.1.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,789 @@
1
+ import React, { createContext, useLayoutEffect, useEffect, useContext, useState, useCallback, useRef, useMemo } from 'react';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { useQuery, useMutation } from '@tanstack/react-query';
4
+ import useConstant from 'use-constant';
5
+ export { default as useConstant } from 'use-constant';
6
+ import { SimpleDI, EventEmitter } from '@liberfi.io/core';
7
+ export * from 'use-debounce';
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
12
+
13
+ // src/version.ts
14
+ if (typeof window !== "undefined") {
15
+ window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};
16
+ window.__LIBERFI_VERSION__["@liberfi.io/hooks"] = "0.1.0";
17
+ }
18
+ var version_default = "0.1.0";
19
+ var APIClientContext = createContext(
20
+ {}
21
+ );
22
+ function APIClientProvider({
23
+ client,
24
+ subscribeClient,
25
+ children
26
+ }) {
27
+ return /* @__PURE__ */ jsx(APIClientContext.Provider, { value: { client, subscribeClient }, children });
28
+ }
29
+ function useAPIClient() {
30
+ const context = useContext(APIClientContext);
31
+ if (!context) {
32
+ throw new Error("useAPIClient must be used within an APIClientProvider");
33
+ }
34
+ return context;
35
+ }
36
+ function finalStretchTokensQueryKey(params) {
37
+ return [
38
+ "finalStretchTokens",
39
+ params.chain,
40
+ params.sortBy ?? "",
41
+ params.sortDirection ?? "",
42
+ JSON.stringify((params.keywords ?? []).sort()),
43
+ JSON.stringify((params.excludeKeywords ?? []).sort()),
44
+ JSON.stringify(params.filters ?? [])
45
+ ];
46
+ }
47
+ async function fetchFinalStretchTokens(client, { chain, ...options }) {
48
+ return await client.getFinalStretchTokens(chain, options);
49
+ }
50
+ function useFinalStretchTokensQuery(params, options = {}) {
51
+ const { client } = useAPIClient();
52
+ return useQuery({
53
+ queryKey: finalStretchTokensQueryKey(params),
54
+ queryFn: async () => fetchFinalStretchTokens(client, params),
55
+ ...options
56
+ });
57
+ }
58
+ function migratedTokensQueryKey(params) {
59
+ return [
60
+ "migratedTokens",
61
+ params.chain,
62
+ params.sortBy ?? "",
63
+ params.sortDirection ?? "",
64
+ JSON.stringify((params.keywords ?? []).sort()),
65
+ JSON.stringify((params.excludeKeywords ?? []).sort()),
66
+ JSON.stringify(params.filters ?? [])
67
+ ];
68
+ }
69
+ async function fetchMigratedTokens(client, { chain, ...options }) {
70
+ return await client.getMigratedTokens(chain, options);
71
+ }
72
+ function useMigratedTokensQuery(params, options = {}) {
73
+ const { client } = useAPIClient();
74
+ return useQuery({
75
+ queryKey: migratedTokensQueryKey(params),
76
+ queryFn: async () => fetchMigratedTokens(client, params),
77
+ ...options
78
+ });
79
+ }
80
+ function newTokensQueryKey(params) {
81
+ return [
82
+ "newTokens",
83
+ params.chain,
84
+ params.sortBy ?? "",
85
+ params.sortDirection ?? "",
86
+ JSON.stringify((params.keywords ?? []).sort()),
87
+ JSON.stringify((params.excludeKeywords ?? []).sort()),
88
+ JSON.stringify(params.filters ?? [])
89
+ ];
90
+ }
91
+ async function fetchNewTokens(client, { chain, ...options }) {
92
+ return await client.getNewTokens(chain, options);
93
+ }
94
+ function useNewTokensQuery(params, options = {}) {
95
+ const { client } = useAPIClient();
96
+ return useQuery({
97
+ queryKey: newTokensQueryKey(params),
98
+ queryFn: async () => fetchNewTokens(client, params),
99
+ ...options
100
+ });
101
+ }
102
+ async function fetchPresignedUploadUrl(client) {
103
+ return await client.getPresignedUploadUrl();
104
+ }
105
+ function usePresignedUploadUrlQuery(options = {}) {
106
+ const { client } = useAPIClient();
107
+ return useQuery({
108
+ queryKey: ["presignedUploadUrl"],
109
+ queryFn: async () => fetchPresignedUploadUrl(client),
110
+ staleTime: 0,
111
+ ...options
112
+ });
113
+ }
114
+ function searchTokensQueryKey(params) {
115
+ return [
116
+ "searchTokens",
117
+ params.cursor ?? "",
118
+ params.limit ? `${params.limit}` : "",
119
+ params.direction ?? "",
120
+ JSON.stringify((params.chains ?? []).sort()),
121
+ params.keyword ?? "",
122
+ JSON.stringify(params.filters ?? []),
123
+ params.sortBy ?? "",
124
+ params.sortDirection ?? ""
125
+ ];
126
+ }
127
+ async function fetchSearchTokens(client, params) {
128
+ return await client.searchTokens(params);
129
+ }
130
+ function useSearchTokensQuery(params, options = {}) {
131
+ const { client } = useAPIClient();
132
+ return useQuery({
133
+ queryKey: searchTokensQueryKey(params),
134
+ queryFn: async () => fetchSearchTokens(client, params),
135
+ ...options
136
+ });
137
+ }
138
+ async function sendTx(client, params) {
139
+ return await client.sendTx(params);
140
+ }
141
+ function useSendTxMutation(options = {}) {
142
+ const { client } = useAPIClient();
143
+ return useMutation({
144
+ mutationFn: async (params) => sendTx(client, params),
145
+ ...options
146
+ });
147
+ }
148
+ function stockTokensQueryKey(params) {
149
+ return [
150
+ "stockTokens",
151
+ params.chain,
152
+ params.sortBy ?? "",
153
+ params.sortDirection ?? "",
154
+ JSON.stringify((params.keywords ?? []).sort()),
155
+ JSON.stringify((params.excludeKeywords ?? []).sort()),
156
+ JSON.stringify(params.filters ?? [])
157
+ ];
158
+ }
159
+ async function fetchStockTokens(client, { chain, ...options }) {
160
+ return await client.getStockTokens(chain, options);
161
+ }
162
+ function useStockTokensQuery(params, options = {}) {
163
+ const { client } = useAPIClient();
164
+ return useQuery({
165
+ queryKey: stockTokensQueryKey(params),
166
+ queryFn: async () => fetchStockTokens(client, params),
167
+ ...options
168
+ });
169
+ }
170
+ function swapRouteQueryKey(params) {
171
+ return [
172
+ "swapRoute",
173
+ params.chain,
174
+ params.userAddress,
175
+ params.input,
176
+ params.output,
177
+ params.mode,
178
+ params.amount,
179
+ params.slippage ? `${params.slippage}` : "",
180
+ params.priorityFee ?? "",
181
+ params.tipFee ?? "",
182
+ params.isAntiMev !== void 0 ? `${params.isAntiMev}` : ""
183
+ ];
184
+ }
185
+ async function fetchSwapRoute(client, params) {
186
+ return await client.swapRoute(params);
187
+ }
188
+ function useSwapRouteQuery(params, options = {}) {
189
+ const { client } = useAPIClient();
190
+ return useQuery({
191
+ queryKey: swapRouteQueryKey(params),
192
+ queryFn: async () => fetchSwapRoute(client, params),
193
+ ...options
194
+ });
195
+ }
196
+ function tokenCandlesQueryKey(params) {
197
+ return [
198
+ "tokenCandles",
199
+ params.chain,
200
+ params.address,
201
+ params.resolution,
202
+ params.after?.toString() ?? "",
203
+ params.before?.toString() ?? "",
204
+ params.limit ? `${params.limit}` : ""
205
+ ];
206
+ }
207
+ async function fetchTokenCandles(client, { chain, address, resolution, ...options }) {
208
+ return await client.getTokenCandles(chain, address, resolution, options);
209
+ }
210
+ function useTokenCandlesQuery(params, options = {}) {
211
+ const { client } = useAPIClient();
212
+ return useQuery({
213
+ queryKey: tokenCandlesQueryKey(params),
214
+ queryFn: async () => fetchTokenCandles(client, params),
215
+ ...options
216
+ });
217
+ }
218
+ function tokenHoldersQueryKey(params) {
219
+ return [
220
+ "tokenHolders",
221
+ params.chain,
222
+ params.address,
223
+ params.cursor ?? "",
224
+ params.limit ? `${params.limit}` : "",
225
+ params.direction ?? ""
226
+ ];
227
+ }
228
+ async function fetchTokenHolders(client, { chain, address, ...options }) {
229
+ return await client.getTokenHolders(chain, address, options);
230
+ }
231
+ function useTokenHoldersQuery(params, options = {}) {
232
+ const { client } = useAPIClient();
233
+ return useQuery({
234
+ queryKey: tokenHoldersQueryKey(params),
235
+ queryFn: async () => fetchTokenHolders(client, params),
236
+ ...options
237
+ });
238
+ }
239
+ function tokenMarketDataQueryKey(params) {
240
+ return ["tokenMarketData", params.chain, params.address];
241
+ }
242
+ async function fetchTokenMarketData(client, { chain, address }) {
243
+ return await client.getTokenMarketData(chain, address);
244
+ }
245
+ function useTokenMarketDataQuery(params, options = {}) {
246
+ const { client } = useAPIClient();
247
+ return useQuery({
248
+ queryKey: tokenMarketDataQueryKey(params),
249
+ queryFn: async () => fetchTokenMarketData(client, params),
250
+ ...options
251
+ });
252
+ }
253
+ function tokenQueryKey(params) {
254
+ return ["token", params.chain, params.address];
255
+ }
256
+ async function fetchToken(client, { chain, address }) {
257
+ return await client.getToken(chain, address);
258
+ }
259
+ function useTokenQuery(params, options = {}) {
260
+ const { client } = useAPIClient();
261
+ return useQuery({
262
+ queryKey: tokenQueryKey(params),
263
+ queryFn: async () => fetchToken(client, params),
264
+ ...options
265
+ });
266
+ }
267
+ function tokenSecurityQueryKey(params) {
268
+ return ["tokenSecurity", params.chain, params.address];
269
+ }
270
+ async function fetchTokenSecurity(client, { chain, address }) {
271
+ return await client.getTokenSecurity(chain, address);
272
+ }
273
+ function useTokenSecurityQuery(params, options = {}) {
274
+ const { client } = useAPIClient();
275
+ return useQuery({
276
+ queryKey: tokenSecurityQueryKey(params),
277
+ queryFn: async () => fetchTokenSecurity(client, params),
278
+ ...options
279
+ });
280
+ }
281
+ function tokensQueryKey(params) {
282
+ return ["tokens", params.chain, params.addresses.sort().join(",")];
283
+ }
284
+ async function fetchTokens(client, { chain, addresses }) {
285
+ return await client.getTokens(chain, addresses);
286
+ }
287
+ function useTokensQuery(params, options = {}) {
288
+ const { client } = useAPIClient();
289
+ return useQuery({
290
+ queryKey: tokensQueryKey(params),
291
+ queryFn: async () => fetchTokens(client, params),
292
+ ...options
293
+ });
294
+ }
295
+ function tokenStatsQueryKey(params) {
296
+ return ["tokenStats", params.chain, params.address];
297
+ }
298
+ async function fetchTokenStats(client, { chain, address }) {
299
+ return await client.getTokenStats(chain, address);
300
+ }
301
+ function useTokenStatsQuery(params, options = {}) {
302
+ const { client } = useAPIClient();
303
+ return useQuery({
304
+ queryKey: tokenStatsQueryKey(params),
305
+ queryFn: async () => fetchTokenStats(client, params),
306
+ ...options
307
+ });
308
+ }
309
+ function tokenTradeActivitiesQueryKey(params) {
310
+ return [
311
+ "tokenTradeActivities",
312
+ params.chain,
313
+ params.address,
314
+ params.before?.toString() ?? "",
315
+ params.after?.toString() ?? "",
316
+ params.beforeBlockHeight ? `${params.beforeBlockHeight}` : "",
317
+ params.afterBlockHeight ? `${params.afterBlockHeight}` : "",
318
+ params.type ?? "",
319
+ params.poolAddress ?? ""
320
+ ];
321
+ }
322
+ async function fetchTokenTradeActivities(client, { chain, address, ...options }) {
323
+ return await client.getTokenTradeActivities(chain, address, options);
324
+ }
325
+ function useTokenTradeActivitiesQuery(params, options = {}) {
326
+ const { client } = useAPIClient();
327
+ return useQuery({
328
+ queryKey: tokenTradeActivitiesQueryKey(params),
329
+ queryFn: async () => fetchTokenTradeActivities(client, params),
330
+ ...options
331
+ });
332
+ }
333
+ function trendingTokensQueryKey(params) {
334
+ return [
335
+ "trendingTokens",
336
+ params.chain,
337
+ params.resolution,
338
+ params.sortBy ?? "",
339
+ params.sortDirection ?? "",
340
+ JSON.stringify((params.keywords ?? []).sort()),
341
+ JSON.stringify((params.excludeKeywords ?? []).sort()),
342
+ JSON.stringify(params.filters ?? [])
343
+ ];
344
+ }
345
+ async function fetchTrendingTokens(client, { chain, resolution, ...options }) {
346
+ return await client.getTrendingTokens(chain, resolution, options);
347
+ }
348
+ function useTrendingTokensQuery(params, options = {}) {
349
+ const { client } = useAPIClient();
350
+ return useQuery({
351
+ queryKey: trendingTokensQueryKey(params),
352
+ queryFn: async () => fetchTrendingTokens(client, params),
353
+ ...options
354
+ });
355
+ }
356
+ function txSuccessQueryKey(params) {
357
+ return [
358
+ "txSuccess",
359
+ params.chain,
360
+ params.txHash,
361
+ params.timeout ? `${params.timeout}` : ""
362
+ ];
363
+ }
364
+ async function fetchTxSuccess(client, { chain, txHash, timeout }) {
365
+ return await client.checkTxSuccess(chain, txHash, timeout);
366
+ }
367
+ function useTxSuccessQuery(params, options = {}) {
368
+ const { client } = useAPIClient();
369
+ return useQuery({
370
+ queryKey: txSuccessQueryKey(params),
371
+ queryFn: async () => fetchTxSuccess(client, params),
372
+ ...options
373
+ });
374
+ }
375
+ function walletPortfoliosQueryKey(params) {
376
+ return ["walletPortfolios", params.chain, params.address];
377
+ }
378
+ async function fetchWalletPortfolios(client, { chain, address }) {
379
+ return await client.getWalletPortfolios(chain, address);
380
+ }
381
+ function useWalletPortfoliosQuery(params, options = {}) {
382
+ const { client } = useAPIClient();
383
+ return useQuery({
384
+ queryKey: walletPortfoliosQueryKey(params),
385
+ queryFn: async () => fetchWalletPortfolios(client, params),
386
+ ...options
387
+ });
388
+ }
389
+ function walletTradeActivitiesQueryKey(params) {
390
+ return [
391
+ "walletTradeActivities",
392
+ params.chain,
393
+ params.address,
394
+ params.before?.toString() ?? "",
395
+ params.after?.toString() ?? "",
396
+ params.beforeBlockHeight ? `${params.beforeBlockHeight}` : "",
397
+ params.afterBlockHeight ? `${params.afterBlockHeight}` : "",
398
+ params.type ?? "",
399
+ params.poolAddress ?? ""
400
+ ];
401
+ }
402
+ async function fetchWalletTradeActivities(client, { chain, address, ...options }) {
403
+ return await client.getWalletTradeActivities(chain, address, options);
404
+ }
405
+ function useWalletTradeActivitiesQuery(params, options = {}) {
406
+ const { client } = useAPIClient();
407
+ return useQuery({
408
+ queryKey: walletTradeActivitiesQueryKey(params),
409
+ queryFn: async () => fetchWalletTradeActivities(client, params),
410
+ ...options
411
+ });
412
+ }
413
+ var WalletConnectorContext = createContext({});
414
+
415
+ // src/wallet/useWalletConnector.ts
416
+ function useWalletConnector() {
417
+ const context = useContext(WalletConnectorContext);
418
+ if (!context) {
419
+ throw new Error(
420
+ "useWalletConnector must be used within a WalletConnectorProvider"
421
+ );
422
+ }
423
+ return context;
424
+ }
425
+ function useWallets() {
426
+ const context = useContext(WalletConnectorContext);
427
+ if (!context) {
428
+ throw new Error("useWallets must be used within a WalletConnectorProvider");
429
+ }
430
+ const { wallets } = context;
431
+ return wallets;
432
+ }
433
+ function WalletConnectorProvider({
434
+ children,
435
+ ...value
436
+ }) {
437
+ return /* @__PURE__ */ jsx(WalletConnectorContext.Provider, { value, children });
438
+ }
439
+ var useBoolean = (initialValue = false) => {
440
+ const [value, setValue] = useState(initialValue);
441
+ const setTrue = useCallback(() => setValue(true), []);
442
+ const setFalse = useCallback(() => setValue(false), []);
443
+ const toggle = useCallback(() => setValue((v) => !v), []);
444
+ return [value, { setTrue, setFalse, toggle }];
445
+ };
446
+ var useSafeLayoutEffect = globalThis?.document ? useLayoutEffect : useEffect;
447
+
448
+ // src/useCallbackRef.ts
449
+ function useCallbackRef(fn, deps = []) {
450
+ const ref = useRef(fn);
451
+ useSafeLayoutEffect(() => {
452
+ ref.current = fn;
453
+ });
454
+ return useCallback(((...args) => ref.current?.(...args)), deps);
455
+ }
456
+ function useIsMounted() {
457
+ const isMounted = useRef(false);
458
+ useEffect(() => {
459
+ isMounted.current = true;
460
+ return () => {
461
+ isMounted.current = false;
462
+ };
463
+ }, []);
464
+ return useCallback(() => isMounted.current, []);
465
+ }
466
+ var initialSize = {
467
+ width: void 0,
468
+ height: void 0
469
+ };
470
+ function useResizeObserver(options) {
471
+ const { ref, box = "content-box" } = options;
472
+ const [{ width, height }, setSize] = useState(initialSize);
473
+ const isMounted = useIsMounted();
474
+ const previousSize = useRef({ ...initialSize });
475
+ const onResize = useRef(void 0);
476
+ onResize.current = options.onResize;
477
+ useEffect(() => {
478
+ if (!ref.current) return;
479
+ if (typeof window === "undefined" || !("ResizeObserver" in window)) return;
480
+ const observer = new ResizeObserver(([entry]) => {
481
+ const boxProp = box === "border-box" ? "borderBoxSize" : box === "device-pixel-content-box" ? "devicePixelContentBoxSize" : "contentBoxSize";
482
+ const newWidth = extractSize(entry, boxProp, "inlineSize");
483
+ const newHeight = extractSize(entry, boxProp, "blockSize");
484
+ const hasChanged = previousSize.current.width !== newWidth || previousSize.current.height !== newHeight;
485
+ if (hasChanged) {
486
+ const newSize = { width: newWidth, height: newHeight };
487
+ previousSize.current.width = newWidth;
488
+ previousSize.current.height = newHeight;
489
+ if (onResize.current) {
490
+ onResize.current(newSize);
491
+ } else {
492
+ if (isMounted()) {
493
+ setSize(newSize);
494
+ }
495
+ }
496
+ }
497
+ });
498
+ observer.observe(ref.current, { box });
499
+ return () => {
500
+ observer.disconnect();
501
+ };
502
+ }, [box, ref, isMounted]);
503
+ return { width, height };
504
+ }
505
+ function extractSize(entry, box, sizeType) {
506
+ if (!entry[box]) {
507
+ if (box === "contentBoxSize") {
508
+ return entry.contentRect[sizeType === "inlineSize" ? "width" : "height"];
509
+ }
510
+ return void 0;
511
+ }
512
+ const boxSize = Array.isArray(entry[box]) ? entry[box][0] : entry[box];
513
+ return boxSize[sizeType];
514
+ }
515
+ var _GlobalTimer = class _GlobalTimer {
516
+ constructor() {
517
+ /** setTimeout id */
518
+ __publicField(this, "timeoutId", null);
519
+ /** subscription map */
520
+ __publicField(this, "subscribers", /* @__PURE__ */ new Map());
521
+ /** is timer running */
522
+ __publicField(this, "isRunning", false);
523
+ /** internal update interval for smoothness */
524
+ __publicField(this, "internalInterval", 200);
525
+ }
526
+ /**
527
+ * Get the singleton instance of the timer
528
+ * @returns Timer instance
529
+ */
530
+ static getInstance() {
531
+ if (!_GlobalTimer.instance) {
532
+ _GlobalTimer.instance = new _GlobalTimer();
533
+ }
534
+ return _GlobalTimer.instance;
535
+ }
536
+ /**
537
+ * Subscribe to the timer
538
+ * @param callback tick callback
539
+ * @param interval interval in milliseconds to execute the callback
540
+ * @returns unsubscribe function
541
+ */
542
+ subscribe(callback, interval = 1e3) {
543
+ this.subscribers.set(callback, {
544
+ interval,
545
+ lastExecuted: Date.now()
546
+ });
547
+ if (!this.isRunning) {
548
+ this.start();
549
+ }
550
+ return () => {
551
+ this.subscribers.delete(callback);
552
+ if (this.subscribers.size === 0) {
553
+ this.stop();
554
+ }
555
+ };
556
+ }
557
+ start() {
558
+ if (this.isRunning) return;
559
+ this.isRunning = true;
560
+ this.scheduleNext();
561
+ }
562
+ scheduleNext() {
563
+ this.timeoutId = setTimeout(() => {
564
+ const now = Date.now();
565
+ this.subscribers.forEach((config, callback) => {
566
+ if (now - config.lastExecuted >= config.interval) {
567
+ callback({ delta: now - config.lastExecuted, now });
568
+ config.lastExecuted = now;
569
+ }
570
+ });
571
+ if (this.isRunning) {
572
+ this.scheduleNext();
573
+ }
574
+ }, this.internalInterval);
575
+ }
576
+ stop() {
577
+ this.isRunning = false;
578
+ if (this.timeoutId) {
579
+ clearTimeout(this.timeoutId);
580
+ this.timeoutId = null;
581
+ }
582
+ }
583
+ };
584
+ /** singleton instance */
585
+ __publicField(_GlobalTimer, "instance");
586
+ var GlobalTimer = _GlobalTimer;
587
+ function useTick(callback, interval = 1e3) {
588
+ const callbackRef = useCallbackRef(callback);
589
+ useEffect(
590
+ () => GlobalTimer.getInstance().subscribe(callbackRef, interval),
591
+ [interval]
592
+ );
593
+ }
594
+ function useTickAge(birthday = Date.now(), interval = 1e3) {
595
+ const birthdayRef = useRef(
596
+ birthday instanceof Date ? birthday.getTime() : birthday
597
+ );
598
+ birthdayRef.current = birthday instanceof Date ? birthday.getTime() : birthday;
599
+ const [age, setAge] = useState(Math.max(0, Date.now() - birthdayRef.current));
600
+ const tickAge = useCallback(
601
+ ({ now }) => setAge(Math.max(0, now - birthdayRef.current)),
602
+ []
603
+ );
604
+ useTick(tickAge, interval);
605
+ return age;
606
+ }
607
+ var useUpdatedRef = (val) => {
608
+ const latestRef = useRef(val);
609
+ latestRef.current = val;
610
+ return latestRef;
611
+ };
612
+ var useMemoizedFn = (fn) => {
613
+ const fnRef = useRef(fn);
614
+ fnRef.current = useMemo(() => fn, [fn]);
615
+ const memoizedFn = useRef(void 0);
616
+ if (!memoizedFn.current) {
617
+ memoizedFn.current = function(...args) {
618
+ return fnRef.current.apply(this, args);
619
+ };
620
+ }
621
+ return memoizedFn.current;
622
+ };
623
+ var useAudioPlayer = (src, options = {}) => {
624
+ const { volume = 1, loop, autoPlay } = options;
625
+ const audioRef = useRef(null);
626
+ const [status, setStatus] = useState("idle");
627
+ const onPlay = () => {
628
+ setStatus("play");
629
+ };
630
+ const onPlaying = () => {
631
+ setStatus("playing");
632
+ };
633
+ const onPause = () => {
634
+ setStatus("paused");
635
+ };
636
+ const onEnded = () => {
637
+ setStatus("ended");
638
+ };
639
+ const onError = () => {
640
+ setStatus("error");
641
+ };
642
+ const element = useMemo(() => {
643
+ return React.createElement("audio", {
644
+ controls: false,
645
+ ref: audioRef,
646
+ autoPlay,
647
+ src,
648
+ style: { display: "none" },
649
+ onPlay,
650
+ onPlaying,
651
+ onPause,
652
+ onEnded,
653
+ onError
654
+ });
655
+ }, [autoPlay, src, onPlay, onPlaying, onPause, onEnded, onError]);
656
+ useEffect(() => {
657
+ const el = audioRef.current;
658
+ if (!el) {
659
+ return;
660
+ }
661
+ el.loop = loop ?? false;
662
+ el.volume = Math.max(0, Math.min(1, volume));
663
+ return () => {
664
+ audioRef.current?.pause();
665
+ audioRef.current = null;
666
+ };
667
+ }, [loop, volume]);
668
+ return [element, audioRef, status];
669
+ };
670
+ var useEventEmitter = () => {
671
+ return useConstant(() => {
672
+ let ee = SimpleDI.get("EE");
673
+ if (!ee) {
674
+ ee = new EventEmitter();
675
+ SimpleDI.registerByName("EE", ee);
676
+ }
677
+ return ee;
678
+ });
679
+ };
680
+
681
+ // src/utils/json.ts
682
+ function parseJSON(value) {
683
+ try {
684
+ return value === "undefined" ? void 0 : JSON.parse(value ?? "");
685
+ } catch {
686
+ return void 0;
687
+ }
688
+ }
689
+
690
+ // src/useSessionStorage.ts
691
+ function useSessionStorage(key, initialValue) {
692
+ const readValue = useCallback(() => {
693
+ if (typeof window === "undefined") {
694
+ return initialValue;
695
+ }
696
+ try {
697
+ const item = window.sessionStorage.getItem(key);
698
+ return item ? parseJSON(item) : initialValue;
699
+ } catch (error) {
700
+ return initialValue;
701
+ }
702
+ }, [initialValue, key]);
703
+ const [storedValue, setStoredValue] = useState(readValue);
704
+ const setValue = (value) => {
705
+ try {
706
+ const newValue = value instanceof Function ? value(storedValue) : value;
707
+ window.sessionStorage.setItem(key, JSON.stringify(newValue));
708
+ setStoredValue(newValue);
709
+ } catch (error) {
710
+ }
711
+ };
712
+ useEffect(() => {
713
+ setStoredValue(readValue());
714
+ }, []);
715
+ useEffect(() => {
716
+ if (typeof window == "undefined") {
717
+ return;
718
+ }
719
+ window.addEventListener?.("storage", handleStorageChange);
720
+ return () => {
721
+ window.removeEventListener?.("storage", handleStorageChange);
722
+ };
723
+ });
724
+ const handleStorageChange = useCallback(
725
+ (event) => {
726
+ if (event?.key && event.key !== key) {
727
+ return;
728
+ }
729
+ setStoredValue(readValue());
730
+ },
731
+ [key, readValue]
732
+ );
733
+ return [storedValue, setValue];
734
+ }
735
+ function useLocalStorage(key, initialValue, options = {
736
+ parseJSON
737
+ }) {
738
+ const readValue = useCallback(() => {
739
+ if (typeof window === "undefined") {
740
+ return initialValue;
741
+ }
742
+ try {
743
+ const item = window.localStorage.getItem(key);
744
+ return item ? options.parseJSON(item) : initialValue;
745
+ } catch (error) {
746
+ return initialValue;
747
+ }
748
+ }, [initialValue, key]);
749
+ const [storedValue, setStoredValue] = useState(readValue);
750
+ const setValue = useCallback(
751
+ (value) => {
752
+ try {
753
+ const newValue = value instanceof Function ? value(storedValue) : value;
754
+ window.localStorage.setItem(key, JSON.stringify(newValue));
755
+ window.dispatchEvent(new Event("storage"));
756
+ setStoredValue(() => newValue);
757
+ } catch (error) {
758
+ }
759
+ },
760
+ [storedValue]
761
+ );
762
+ useEffect(() => {
763
+ setStoredValue(readValue());
764
+ }, []);
765
+ useEffect(() => {
766
+ const handleStorageChange = (event) => {
767
+ if (event?.key && event.key !== key) {
768
+ return;
769
+ }
770
+ setStoredValue(readValue());
771
+ };
772
+ window?.addEventListener?.("storage", handleStorageChange);
773
+ return () => {
774
+ window?.removeEventListener?.("storage", handleStorageChange);
775
+ };
776
+ }, [key]);
777
+ return [storedValue, setValue];
778
+ }
779
+ var useSimpleDI = () => {
780
+ return {
781
+ get: SimpleDI.get,
782
+ // getOr: SimpleDI.getOr<T>(name, SimpleDI.get<T>(name)),
783
+ register: SimpleDI.register
784
+ };
785
+ };
786
+
787
+ export { APIClientContext, APIClientProvider, WalletConnectorContext, WalletConnectorProvider, fetchFinalStretchTokens, fetchMigratedTokens, fetchNewTokens, fetchPresignedUploadUrl, fetchSearchTokens, fetchStockTokens, fetchSwapRoute, fetchToken, fetchTokenCandles, fetchTokenHolders, fetchTokenMarketData, fetchTokenSecurity, fetchTokenStats, fetchTokenTradeActivities, fetchTokens, fetchTrendingTokens, fetchTxSuccess, fetchWalletPortfolios, fetchWalletTradeActivities, finalStretchTokensQueryKey, migratedTokensQueryKey, newTokensQueryKey, parseJSON, searchTokensQueryKey, sendTx, stockTokensQueryKey, swapRouteQueryKey, tokenCandlesQueryKey, tokenHoldersQueryKey, tokenMarketDataQueryKey, tokenQueryKey, tokenSecurityQueryKey, tokenStatsQueryKey, tokenTradeActivitiesQueryKey, tokensQueryKey, trendingTokensQueryKey, txSuccessQueryKey, useAPIClient, useAudioPlayer, useBoolean, useCallbackRef, useEventEmitter, useFinalStretchTokensQuery, useIsMounted, useLocalStorage, useMemoizedFn, useMigratedTokensQuery, useNewTokensQuery, usePresignedUploadUrlQuery, useResizeObserver, useSafeLayoutEffect, useSearchTokensQuery, useSendTxMutation, useSessionStorage, useSimpleDI, useStockTokensQuery, useSwapRouteQuery, useTick, useTickAge, useTokenCandlesQuery, useTokenHoldersQuery, useTokenMarketDataQuery, useTokenQuery, useTokenSecurityQuery, useTokenStatsQuery, useTokenTradeActivitiesQuery, useTokensQuery, useTrendingTokensQuery, useTxSuccessQuery, useUpdatedRef, useWalletConnector, useWalletPortfoliosQuery, useWalletTradeActivitiesQuery, useWallets, version_default as version, walletPortfoliosQueryKey, walletTradeActivitiesQueryKey };
788
+ //# sourceMappingURL=index.mjs.map
789
+ //# sourceMappingURL=index.mjs.map