@kakadu/components 3.2.3 → 3.2.4
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("../jsx-runtime-BB_1_6y_.js"),M=require("@kuma-ui/core"),a=require("react"),
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("../jsx-runtime-BB_1_6y_.js"),M=require("@kuma-ui/core"),a=require("react"),q=require("../theme.js"),R=require("../icons/icon.js"),f=require("./popover.js"),P=require("./atom.js"),h=require("./text.js"),g=require("./hover-gradient.js"),I=require("./click-ripples.js"),N=require("./separator.js"),S=require("./decorators.js"),E=new Set;function y(){const[e,n]=a.useState(!1),r=a.useCallback(s=>{n(s);for(const i of E)i(s)},[]);return a.useEffect(()=>{const s=i=>{n(i)};return E.add(s),()=>{E.delete(s)}},[]),a.useMemo(()=>({isKeyboardNavigating:e,setIsKeyboardNavigating:r}),[e,r])}function _({label:e,iconLeft:n,iconRight:r,href:s,shouldCloseOnClick:i=!0,decorators:v=[],submenu:x,className:u,onClick:o,...j}){const{isVisible:l,isFocused:c,close:p}=f.usePopoverContext(),{isKeyboardNavigating:d}=y(),m=a.useCallback(w=>{c&&(o==null||o(w),x?x.show():i&&(p==null||p()))},[c,o,x,i,p]),k=a.useMemo(()=>t.jsxRuntimeExports.jsxs(P.default,{as:s?"a":"button",...j,type:s?void 0:"button",role:"menuitem",href:s,disabled:s?void 0:!l,className:M.cx(q.buttonResetStyles,"kakadu-components-3114599304",d?"kakadu-components-4096376656":"kakadu-components-2554182777",u),onClick:m,children:[t.jsxRuntimeExports.jsxs(h.Span,{className:"kakadu-components-1109353535",children:[n?t.jsxRuntimeExports.jsx(R.default,{width:12,height:12,type:n}):null,t.jsxRuntimeExports.jsx("span",{className:"kakadu-components-913703148",children:e}),r?t.jsxRuntimeExports.jsx(R.default,{width:12,height:12,type:r}):null]}),d?null:t.jsxRuntimeExports.jsxs(t.jsxRuntimeExports.Fragment,{children:[t.jsxRuntimeExports.jsx(g.default,{size:150,className:g.normalBlendStyles}),t.jsxRuntimeExports.jsx(I.default,{size:100})]})]}),[s,j,l,d,u,m,n,e,r]);return t.jsxRuntimeExports.jsx(S.default,{decorators:v,children:x?t.jsxRuntimeExports.jsx(b,{menu:x.popoverMenu,className:"kakadu-components-1861176543",children:k}):k})}function K(e){if("type"in e){if(e.type==="separator")return t.jsxRuntimeExports.jsx(N.default,{});if(e.type==="group")return t.jsxRuntimeExports.jsx(h.Span,{className:"kakadu-components-1499037363",children:e.label})}return t.jsxRuntimeExports.jsx(_,{...e})}function C({items:e}){const{isFocused:n}=f.usePopoverContext(),[r,s]=a.useState([]),{setIsKeyboardNavigating:i}=y(),v=a.useCallback(u=>{u&&s(o=>o.includes(u)?o:[...o,u])},[]),x=a.useCallback(u=>{n&&(u.currentTarget.focus(),i(!1))},[n,i]);return a.useEffect(()=>{if(!n)return;const u=o=>{const j=r.filter(m=>m.isConnected),l=j.length;if(l===0)return;let c;const p=document.activeElement;let d=p?j.indexOf(p):-1;switch(d===-1&&(d=0),o.key){case"ArrowDown":{c=(d+1)%l;break}case"ArrowUp":{c=(d-1+l)%l;break}case"Home":{c=0;break}case"End":{c=l-1;break}}if(c!==void 0){o.preventDefault(),o.stopPropagation();const m=j[c];m&&(m.focus(),i(!0))}};return window.addEventListener("keydown",u,{capture:!0}),()=>{window.removeEventListener("keydown",u,{capture:!0})}},[n,r,i]),t.jsxRuntimeExports.jsx(t.jsxRuntimeExports.Fragment,{children:e.map((u,o)=>t.jsxRuntimeExports.jsx(K,{...u,reference:v,onMouseEnter:x},o))})}function F({items:e,...n}){return t.jsxRuntimeExports.jsx(f.default,{...n,as:"nav",role:"menu",innerClassName:"kakadu-components-3600436535",children:t.jsxRuntimeExports.jsx(C,{items:e})})}function z(e){const n=a.useMemo(()=>({items:e}),[e]),{show:r,popover:s}=f.usePopover(F,n);return a.useMemo(()=>({show:r,popoverMenu:s}),[r,s])}function b({menu:e,className:n,children:r}){return t.jsxRuntimeExports.jsxs(f.PopoverContainer,{className:n,children:[r,e]})}exports.default=b;exports.usePopoverMenu=z;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { j as n } from "../jsx-runtime-B4hRZ52C.mjs";
|
|
2
|
-
import { cx as
|
|
3
|
-
import { useMemo as v, useState as
|
|
4
|
-
import { buttonResetStyles as
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import { Span as
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
2
|
+
import { cx as M } from "@kuma-ui/core";
|
|
3
|
+
import { useMemo as v, useState as y, useCallback as x, useEffect as w } from "react";
|
|
4
|
+
import { buttonResetStyles as E } from "../theme.mjs";
|
|
5
|
+
import h from "../icons/icon.mjs";
|
|
6
|
+
import S, { PopoverContainer as K, usePopover as F, usePopoverContext as I } from "./popover.mjs";
|
|
7
|
+
import A from "./atom.mjs";
|
|
8
|
+
import { Span as b } from "./text.mjs";
|
|
9
|
+
import D, { normalBlendStyles as H } from "./hover-gradient.mjs";
|
|
10
|
+
import z from "./click-ripples.mjs";
|
|
11
|
+
import C from "./separator.mjs";
|
|
12
|
+
import L from "./decorators.mjs";
|
|
13
13
|
const j = /* @__PURE__ */ new Set();
|
|
14
14
|
function N() {
|
|
15
|
-
const [e, t] =
|
|
15
|
+
const [e, t] = y(!1), s = x((o) => {
|
|
16
16
|
t(o);
|
|
17
17
|
for (const i of j)
|
|
18
18
|
i(o);
|
|
19
19
|
}, []);
|
|
20
|
-
return
|
|
20
|
+
return w(() => {
|
|
21
21
|
const o = (i) => {
|
|
22
22
|
t(i);
|
|
23
23
|
};
|
|
@@ -29,76 +29,77 @@ function N() {
|
|
|
29
29
|
setIsKeyboardNavigating: s
|
|
30
30
|
}), [e, s]);
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function R({
|
|
33
33
|
label: e,
|
|
34
34
|
iconLeft: t,
|
|
35
35
|
iconRight: s,
|
|
36
36
|
href: o,
|
|
37
37
|
shouldCloseOnClick: i = !0,
|
|
38
38
|
decorators: k = [],
|
|
39
|
-
submenu:
|
|
39
|
+
submenu: m,
|
|
40
40
|
className: a,
|
|
41
41
|
onClick: r,
|
|
42
|
-
...
|
|
42
|
+
...f
|
|
43
43
|
}) {
|
|
44
44
|
const {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
isVisible: u,
|
|
46
|
+
isFocused: c,
|
|
47
|
+
close: l
|
|
48
|
+
} = I(), {
|
|
49
|
+
isKeyboardNavigating: d
|
|
50
|
+
} = N(), p = x((P) => {
|
|
51
|
+
c && (r == null || r(P), m ? m.show() : i && (l == null || l()));
|
|
52
|
+
}, [c, r, m, i, l]), g = v(() => /* @__PURE__ */ n.jsxs(A, { as: o ? "a" : "button", ...f, type: o ? void 0 : "button", role: "menuitem", href: o, disabled: o ? void 0 : !u, className: M(E, "kakadu-components-3114599304", d ? "kakadu-components-4096376656" : "kakadu-components-2554182777", a), onClick: p, children: [
|
|
53
|
+
/* @__PURE__ */ n.jsxs(b, { className: "kakadu-components-1109353535", children: [
|
|
54
|
+
t ? /* @__PURE__ */ n.jsx(h, { width: 12, height: 12, type: t }) : null,
|
|
54
55
|
/* @__PURE__ */ n.jsx("span", { className: "kakadu-components-913703148", children: e }),
|
|
55
|
-
s ? /* @__PURE__ */ n.jsx(
|
|
56
|
+
s ? /* @__PURE__ */ n.jsx(h, { width: 12, height: 12, type: s }) : null
|
|
56
57
|
] }),
|
|
57
|
-
|
|
58
|
-
/* @__PURE__ */ n.jsx(
|
|
59
|
-
/* @__PURE__ */ n.jsx(
|
|
58
|
+
d ? null : /* @__PURE__ */ n.jsxs(n.Fragment, { children: [
|
|
59
|
+
/* @__PURE__ */ n.jsx(D, { size: 150, className: H }),
|
|
60
|
+
/* @__PURE__ */ n.jsx(z, { size: 100 })
|
|
60
61
|
] })
|
|
61
|
-
] }), [f,
|
|
62
|
-
return /* @__PURE__ */ n.jsx(
|
|
62
|
+
] }), [o, f, u, d, a, p, t, e, s]);
|
|
63
|
+
return /* @__PURE__ */ n.jsx(L, { decorators: k, children: m ? /* @__PURE__ */ n.jsx(U, { menu: m.popoverMenu, className: "kakadu-components-1861176543", children: g }) : g });
|
|
63
64
|
}
|
|
64
|
-
function
|
|
65
|
+
function B(e) {
|
|
65
66
|
if ("type" in e) {
|
|
66
67
|
if (e.type === "separator")
|
|
67
|
-
return /* @__PURE__ */ n.jsx(
|
|
68
|
+
return /* @__PURE__ */ n.jsx(C, {});
|
|
68
69
|
if (e.type === "group")
|
|
69
|
-
return /* @__PURE__ */ n.jsx(
|
|
70
|
+
return /* @__PURE__ */ n.jsx(b, { className: "kakadu-components-1499037363", children: e.label });
|
|
70
71
|
}
|
|
71
|
-
return /* @__PURE__ */ n.jsx(
|
|
72
|
+
return /* @__PURE__ */ n.jsx(R, { ...e });
|
|
72
73
|
}
|
|
73
|
-
function
|
|
74
|
+
function G({
|
|
74
75
|
items: e
|
|
75
76
|
}) {
|
|
76
77
|
const {
|
|
77
78
|
isFocused: t
|
|
78
|
-
} =
|
|
79
|
+
} = I(), [s, o] = y([]), {
|
|
79
80
|
setIsKeyboardNavigating: i
|
|
80
81
|
} = N(), k = x((a) => {
|
|
81
82
|
a && o((r) => r.includes(a) ? r : [...r, a]);
|
|
82
|
-
}, []),
|
|
83
|
+
}, []), m = x((a) => {
|
|
83
84
|
t && (a.currentTarget.focus(), i(!1));
|
|
84
85
|
}, [t, i]);
|
|
85
|
-
return
|
|
86
|
+
return w(() => {
|
|
86
87
|
if (!t)
|
|
87
88
|
return;
|
|
88
89
|
const a = (r) => {
|
|
89
|
-
const
|
|
90
|
+
const f = s.filter((p) => p.isConnected), u = f.length;
|
|
90
91
|
if (u === 0)
|
|
91
92
|
return;
|
|
92
93
|
let c;
|
|
93
|
-
const
|
|
94
|
-
let
|
|
95
|
-
switch (
|
|
94
|
+
const l = document.activeElement;
|
|
95
|
+
let d = l ? f.indexOf(l) : -1;
|
|
96
|
+
switch (d === -1 && (d = 0), r.key) {
|
|
96
97
|
case "ArrowDown": {
|
|
97
|
-
c = (
|
|
98
|
+
c = (d + 1) % u;
|
|
98
99
|
break;
|
|
99
100
|
}
|
|
100
101
|
case "ArrowUp": {
|
|
101
|
-
c = (
|
|
102
|
+
c = (d - 1 + u) % u;
|
|
102
103
|
break;
|
|
103
104
|
}
|
|
104
105
|
case "Home": {
|
|
@@ -112,8 +113,8 @@ function B({
|
|
|
112
113
|
}
|
|
113
114
|
if (c !== void 0) {
|
|
114
115
|
r.preventDefault(), r.stopPropagation();
|
|
115
|
-
const
|
|
116
|
-
|
|
116
|
+
const p = f[c];
|
|
117
|
+
p && (p.focus(), i(!0));
|
|
117
118
|
}
|
|
118
119
|
};
|
|
119
120
|
return window.addEventListener("keydown", a, {
|
|
@@ -124,44 +125,44 @@ function B({
|
|
|
124
125
|
});
|
|
125
126
|
};
|
|
126
127
|
}, [t, s, i]), /* @__PURE__ */ n.jsx(n.Fragment, { children: e.map((a, r) => /* @__PURE__ */ n.jsx(
|
|
127
|
-
|
|
128
|
+
B,
|
|
128
129
|
{
|
|
129
130
|
...a,
|
|
130
131
|
reference: k,
|
|
131
|
-
onMouseEnter:
|
|
132
|
+
onMouseEnter: m
|
|
132
133
|
},
|
|
133
134
|
r
|
|
134
135
|
)) });
|
|
135
136
|
}
|
|
136
|
-
function
|
|
137
|
+
function T({
|
|
137
138
|
items: e,
|
|
138
139
|
...t
|
|
139
140
|
}) {
|
|
140
|
-
return /* @__PURE__ */ n.jsx(
|
|
141
|
+
return /* @__PURE__ */ n.jsx(S, { ...t, as: "nav", role: "menu", innerClassName: "kakadu-components-3600436535", children: /* @__PURE__ */ n.jsx(G, { items: e }) });
|
|
141
142
|
}
|
|
142
|
-
function
|
|
143
|
+
function ne(e) {
|
|
143
144
|
const t = v(() => ({
|
|
144
145
|
items: e
|
|
145
146
|
}), [e]), {
|
|
146
147
|
show: s,
|
|
147
148
|
popover: o
|
|
148
|
-
} =
|
|
149
|
+
} = F(T, t);
|
|
149
150
|
return v(() => ({
|
|
150
151
|
show: s,
|
|
151
152
|
popoverMenu: o
|
|
152
153
|
}), [s, o]);
|
|
153
154
|
}
|
|
154
|
-
function
|
|
155
|
+
function U({
|
|
155
156
|
menu: e,
|
|
156
157
|
className: t,
|
|
157
158
|
children: s
|
|
158
159
|
}) {
|
|
159
|
-
return /* @__PURE__ */ n.jsxs(
|
|
160
|
+
return /* @__PURE__ */ n.jsxs(K, { className: t, children: [
|
|
160
161
|
s,
|
|
161
162
|
e
|
|
162
163
|
] });
|
|
163
164
|
}
|
|
164
165
|
export {
|
|
165
|
-
|
|
166
|
-
|
|
166
|
+
U as default,
|
|
167
|
+
ne as usePopoverMenu
|
|
167
168
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("../jsx-runtime-BB_1_6y_.js"),l=require("@kuma-ui/core"),e=require("react"),H=require("../focus-trap-react-CcpTuiJi.js"),g=require("./atom.js"),z=e.createContext({isVisible:!1,isFocused:!1}),{Provider:U}=z;function G(){return e.useContext(z)}function J({as:r,className:s,...o}){return n.jsxRuntimeExports.jsx(g.default,{as:r??g.defaultAtomElementType,...o,className:l.cx("kakadu-components-2845019685",s)})}function K({as:r,isVisible:s,label:o,shouldCloseOnOutsideClick:f=!0,shouldCloseOnEscapePress:c=!0,children:m,className:p,innerClassName:x,onClose:a,...v}){const b=e.useRef(null),F=e.useRef(null),[I,k]=e.useState(!1),[S,B]=e.useState(!1),[u,R]=e.useState(!1),C=e.useCallback(t=>{b.current=t,R(!1)},[]),j=e.useRef(null),M=e.useCallback(t=>{t&&(j.current=t)},[]),W=e.useMemo(()=>({initialFocus:()=>j.current??void 0,onActivate(){var t;(t=j.current)==null||t.focus()
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("../jsx-runtime-BB_1_6y_.js"),l=require("@kuma-ui/core"),e=require("react"),H=require("../focus-trap-react-CcpTuiJi.js"),g=require("./atom.js"),z=e.createContext({isVisible:!1,isFocused:!1}),{Provider:U}=z;function G(){return e.useContext(z)}function J({as:r,className:s,...o}){return n.jsxRuntimeExports.jsx(g.default,{as:r??g.defaultAtomElementType,...o,className:l.cx("kakadu-components-2845019685",s)})}function K({as:r,isVisible:s,label:o,shouldCloseOnOutsideClick:f=!0,shouldCloseOnEscapePress:c=!0,children:m,className:p,innerClassName:x,onClose:a,...v}){const b=e.useRef(null),F=e.useRef(null),[I,k]=e.useState(!1),[S,B]=e.useState(!1),[u,R]=e.useState(!1),C=e.useCallback(t=>{b.current=t,R(!1)},[]),j=e.useRef(null),M=e.useCallback(t=>{t&&(j.current=t)},[]),W=e.useMemo(()=>({initialFocus:()=>j.current??void 0,onActivate(){var t;k(!0),(t=j.current)==null||t.focus()},clickOutsideDeactivates:f,escapeDeactivates:c,onUnpause(){k(!0)},onPause(){k(!1)},onDeactivate(){R(!1),k(!1)}}),[f,c]),[q,N]=e.useState(s??!1),$=e.useMemo(()=>({isVisible:q,isFocused:I,setInitialFocus:M,close:C}),[q,I,M,C]),[i,L]=e.useState("center");return e.useEffect(()=>{const t=F.current;if(t){const w=()=>{const O=t.offsetParent,E=O instanceof HTMLElement?O.getBoundingClientRect():null,y=t.getBoundingClientRect();if(!E){L("center");return}const P=16,D=window.innerWidth-P,T=E.left+E.width/2-y.width/2,V=T+y.width,A=T<P,_=V>D;let d="center";if(A&&_){const h=P-T;d=V-D>h?"right":"left"}else _?d="right":A&&(d="left");L(h=>h===d?h:d)};return w(),window.addEventListener("resize",w),()=>{window.removeEventListener("resize",w)}}},[]),e.useEffect(()=>{s&&(B(!0),N(!0))},[s]),e.useEffect(()=>{if(s){const t=setTimeout(()=>{R(!0)},100);return()=>{clearTimeout(t)}}R(!1)},[s]),e.useEffect(()=>{if(S&&!u){const t=setTimeout(()=>{a==null||a(b.current??void 0),N(!1)},370);return()=>{clearTimeout(t)}}},[S,u,a]),n.jsxRuntimeExports.jsx(g.default,{reference:F,as:r??g.defaultAtomElementType,role:"dialog","aria-hidden":!u,"aria-label":o??"Popover",...v,className:l.cx("kakadu-components-3920007758",i==="left"&&"kakadu-components-611400930",i==="right"&&"kakadu-components-944244368",u&&"kakadu-components-1399012551",p),children:n.jsxRuntimeExports.jsxs("div",{className:l.cx("kakadu-components-1118152266",u&&"kakadu-components-231244945"),children:[n.jsxRuntimeExports.jsx("div",{className:l.cx("kakadu-components-1951789226",i==="left"&&"kakadu-components-1495725644",i==="right"&&"kakadu-components-4235689197")}),n.jsxRuntimeExports.jsx(H.focusTrapReactExports.FocusTrap,{active:u,focusTrapOptions:W,children:n.jsxRuntimeExports.jsxs("div",{className:l.cx("kakadu-components-2488426662",x),children:[n.jsxRuntimeExports.jsx(U,{value:$,children:m}),n.jsxRuntimeExports.jsx("div",{className:l.cx("kakadu-components-3252390100",i==="left"&&"kakadu-components-3754561431",i==="right"&&"kakadu-components-2265262600")})]})})]})})}function Q(r,s){const[o,f]=e.useState(!1),c=e.useRef(void 0),m=e.useCallback(async()=>(f(!0),new Promise(a=>{c.current=a})),[]),p=e.useCallback(a=>{var v;f(!1),(v=c.current)==null||v.call(c,a),c.current=void 0},[]),x=e.useMemo(()=>n.jsxRuntimeExports.jsx(r,{...s,isVisible:o,onClose:p}),[r,s,o,p]);return e.useMemo(()=>({isVisible:o,show:m,popover:x}),[o,m,x])}exports.PopoverContainer=J;exports.default=K;exports.usePopover=Q;exports.usePopoverContext=G;
|
package/build/hooks/use-fetch.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react"),b=new Map,C=new Map,y=new Map;function j(e,s){let t=y.get(e);return t||(t=new Set,y.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&y.delete(e)}}function q(e,s){C.set(e,{timestamp:Date.now(),response:s});const t=y.get(e);if(t)for(const r of t)r(s)}function L(e){const[s,t]=n.useState(!1),[r,l]=n.useState(),[d,h]=n.useState(),g=n.useCallback(async(v,o)=>{try{h(void 0),t(!0);const a=await fetch(v,o),u=e?await e(a):a;return l(u),u}catch(a){const u=a instanceof Error?a:new Error("Unknown error");throw h(u),u}finally{t(!1)}},[e]);return n.useMemo(()=>({isLoading:s,fetch:g,response:r,error:d}),[s,g,r,d])}function J(e){const s=n.useCallback(async t=>{const r=await t.json();return e?e(r):r},[e]);return L(s)}function p(e,s,t,r){const{cacheLifetime:l=1e3*60}=r??{},[d,h]=n.useState(0),[g,v]=n.useState(()=>!!e),o=n.useMemo(()=>e?JSON.stringify({url:e,options:s}):"",[e,s]),a=n.useCallback(()=>{if(!o)return;const c=C.get(o);if(c){if(Date.now()-c.timestamp>l){C.delete(o);return}return c.response}},[o,l]),[u,w]=n.useState(()=>a()),[M,m]=n.useState(),S=n.useMemo(()=>{var c;return((c=s==null?void 0:s.method)==null?void 0:c.toUpperCase())??"GET"},[s]);return n.useEffect(()=>{if(S!=="GET")return;const c=()=>{document.hidden||h(f=>f+1)};return document.addEventListener("visibilitychange",c),()=>{document.removeEventListener("visibilitychange",c)}},[S]),n.useEffect(()=>{if(!e||!o){w(void 0);return}const c=a();c===void 0?(w(void 0),m(void 0)):(w(c),m(void 0));const f=j(o,E=>{w(E),m(void 0)});return()=>{f()}},[e,o,a]),n.useEffect(()=>{if(!e||!o){v(!1);return}if(d<0)return;const c=S==="GET";if(c&&a()!==void 0){v(!1);return}let f=!0;m(void 0),v(!0);let E=c&&b.has(o)?b.get(o):void 0;if(!E){const i=fetch(e,s);b.set(o,i),i.then(()=>{b.delete(o)}),E=i}return E.then(async i=>{if(f){const F=t?await t(i):i;c&&q(o,F),w(F),m(void 0)}}).catch(i=>{f&&m(i)}).finally(()=>{f&&v(!1)}),()=>{f=!1}},[e,
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("react"),b=new Map,C=new Map,y=new Map;function j(e,s){let t=y.get(e);return t||(t=new Set,y.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&y.delete(e)}}function q(e,s){C.set(e,{timestamp:Date.now(),response:s});const t=y.get(e);if(t)for(const r of t)r(s)}function L(e){const[s,t]=n.useState(!1),[r,l]=n.useState(),[d,h]=n.useState(),g=n.useCallback(async(v,o)=>{try{h(void 0),t(!0);const a=await fetch(v,o),u=e?await e(a):a;return l(u),u}catch(a){const u=a instanceof Error?a:new Error("Unknown error");throw h(u),u}finally{t(!1)}},[e]);return n.useMemo(()=>({isLoading:s,fetch:g,response:r,error:d}),[s,g,r,d])}function J(e){const s=n.useCallback(async t=>{const r=await t.json();return e?e(r):r},[e]);return L(s)}function p(e,s,t,r){const{cacheLifetime:l=1e3*60}=r??{},[d,h]=n.useState(0),[g,v]=n.useState(()=>!!e),o=n.useMemo(()=>e?JSON.stringify({url:e,options:s}):"",[e,s]),a=n.useCallback(()=>{if(!o)return;const c=C.get(o);if(c){if(Date.now()-c.timestamp>l){C.delete(o);return}return c.response}},[o,l]),[u,w]=n.useState(()=>a()),[M,m]=n.useState(),S=n.useMemo(()=>{var c;return((c=s==null?void 0:s.method)==null?void 0:c.toUpperCase())??"GET"},[s]);return n.useEffect(()=>{if(S!=="GET")return;const c=()=>{document.hidden||h(f=>f+1)};return document.addEventListener("visibilitychange",c),()=>{document.removeEventListener("visibilitychange",c)}},[S]),n.useEffect(()=>{if(!e||!o){w(void 0);return}const c=a();c===void 0?(w(void 0),m(void 0)):(w(c),m(void 0));const f=j(o,E=>{w(E),m(void 0)});return()=>{f()}},[e,o,a]),n.useEffect(()=>{if(!e||!o){v(!1);return}if(d<0)return;const c=S==="GET";if(c&&a()!==void 0){v(!1);return}let f=!0;m(void 0),v(!0);let E=c&&b.has(o)?b.get(o):void 0;if(!E){const i=fetch(e,s);b.set(o,i),i.then(()=>{b.delete(o)}),E=i}return E.then(async i=>{if(f){const F=t?await t(i):i;c&&q(o,F),w(F),m(void 0)}}).catch(i=>{f&&m(i)}).finally(()=>{f&&v(!1)}),()=>{f=!1}},[e,o,d,S,a,s,t]),n.useMemo(()=>({isLoading:g,response:u,error:M}),[g,u,M])}function k(e,s,t,r){const l=n.useCallback(async d=>{const h=await d.json();return t?t(h):h},[t]);return p(e,s,l,r)}exports.default=p;exports.useFetcher=L;exports.useJsonFetch=k;exports.useJsonFetcher=J;
|