@orderly.network/ui-connector 2.8.1-alpha.0 → 2.8.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -9,18 +9,912 @@ var jsxRuntime = require('react/jsx-runtime');
9
9
  var reactApp = require('@orderly.network/react-app');
10
10
  var uiChainSelector = require('@orderly.network/ui-chain-selector');
11
11
 
12
- var ne=e=>{let{title:t,description:n,showDivider:r}=e;return jsxRuntime.jsxs(ui.Box,{position:"relative",className:"oui-pl-8",children:[jsxRuntime.jsxs(ui.Box,{children:[jsxRuntime.jsx(ui.Text,{as:"div",intensity:98,size:"sm",children:t}),jsxRuntime.jsx(ui.Text,{as:"div",intensity:54,size:"2xs",children:n})]}),jsxRuntime.jsx("div",{className:"oui-absolute oui-left-0 oui-top-1 oui-z-10",children:jsxRuntime.jsx(Ee,{...e})}),r&&jsxRuntime.jsx(ui.Box,{position:"absolute",left:12,top:23,bottom:-21,zIndex:0,children:jsxRuntime.jsx(ui.Divider,{lineStyle:"dashed",direction:"vertical",intensity:16,className:"oui-h-full"})})]})},Ee=e=>{let{active:t,isLoading:n,isCompleted:r}=e;return n?jsxRuntime.jsx(ui.Spinner,{size:"sm",className:"oui-ml-1"}):r?jsxRuntime.jsx(ui.CheckedCircleFillIcon,{opacity:1,className:"oui-text-primary"}):jsxRuntime.jsx(Pe,{active:!!t})},Pe=({active:e,className:t})=>jsxRuntime.jsx("div",{className:ui.cn("oui-w-[8.3px] oui-h-[8.3px] oui-rounded-full oui-ml-2 oui-mt-1",t,e?"oui-bg-primary-light":"oui-bg-base-2")});var Z=e=>{let{initAccountState:t=types.AccountStatusEnum.NotConnected}=e,[n,r]=react.useState(true),o=hooks.useEventEmitter(),{t:i}=i18n.useTranslation(),{disconnect:g,namespace:u}=hooks.useWalletConnector(),{state:m,account:s}=hooks.useAccount(),[a,p]=react.useState(t),[l,k]=react.useState(0),[x,h]=react.useState(false),[B,T]=react.useState(false),{ledgerWallet:N,setLedgerAddress:xe}=hooks.useStorageLedgerAddress(),C=react.useRef(0),[M]=hooks.useLocalStorage("orderly-first-show-wallet-connector-dialog",void 0);react.useEffect(()=>()=>{localStorage.setItem("orderly-first-show-wallet-connector-dialog","1");},[]),react.useEffect(()=>{p(m.status);},[m]);let O=react.useMemo(()=>{let d=[];return t<types.AccountStatusEnum.SignedIn&&d.push({key:"signIn",title:i("connector.createAccount"),description:i("connector.createAccount.description")}),t<types.AccountStatusEnum.EnableTrading&&d.push({key:"enableTrading",title:i("connector.enableTrading"),description:i("connector.enableTrading.description")}),d},[t,i]);react.useEffect(()=>{if(u!=types.ChainNamespace.solana){T(false);return}if(!N){T(true);return}if(N&&s.address&&!N.includes(s.address)){T(true);return}T(false);},[u,s.address,N]);let j=()=>(h(true),e.enableTrading(n).then(async d=>{C.current++,h(false),k(y=>y+1);try{await e.enableTradingComplted?.();}catch{}typeof e.onCompleted=="function"?e.onCompleted():typeof e.close=="function"&&e.close();},d=>{if(h(false),d!==-1){if(d.message.indexOf("Signing off chain messages with Ledger is not yet supported")!==-1){o.emit("wallet:sign-message-with-ledger-error",{message:d.message,userAddress:s.address});return}ui.toast.error(ie(d));}}).catch(d=>{h(false);})),Se=async()=>{localStorage.removeItem("orderly_link_device"),g({label:a.connectWallet?.name}).then(()=>{s.disconnect(),typeof e.close=="function"&&e.close();});},Ne=()=>(h(true),e.signIn().then(d=>{k(y=>y+1),j();},d=>{if(h(false),d!==-1){if(d.message.indexOf("Signing off chain messages with Ledger is not yet supported")!==-1){o.emit("wallet:sign-message-with-ledger-error",{message:d.message,userAddress:s.address});return}ui.toast.error(ie(d));}}).catch(d=>{h(false);}));return jsxRuntime.jsxs(ui.Box,{id:"oui-wallet-connect-dialog-content",className:"oui-font-semibold",children:[jsxRuntime.jsx(ui.Text,{intensity:54,size:"xs",children:i("connector.expired")}),jsxRuntime.jsx(ui.Box,{p:4,my:6,intensity:600,r:"lg",className:"oui-space-y-5",position:"relative",children:O.map((d,y)=>{let We=y===O.length-1;return jsxRuntime.jsx(ne,{title:d.title,description:d.description,isCompleted:l>y,active:l===y,isLoading:x&&l===y,showDivider:!We},d.key)})}),e.showRefCodeInput&&O.length==2&&jsxRuntime.jsx(Ze,{...e}),M&&jsxRuntime.jsxs(ui.Flex,{justify:"between",itemAlign:"center",children:[jsxRuntime.jsx(Ke,{}),jsxRuntime.jsx(ui.Switch,{color:"primary",checked:n,onCheckedChange:r,disabled:x,className:"data-[state=checked]:oui-bg-primary-darken"})]}),jsxRuntime.jsx(ui.Flex,{justify:"center",mt:8,className:"oui-w-full",children:jsxRuntime.jsx(ui.Box,{className:"oui-w-full",children:jsxRuntime.jsx(Ue,{state:a,signIn:Ne,enableTrading:j,loading:x,disabled:a>=types.AccountStatusEnum.EnableTrading,showLedgerButton:B})})}),a>types.AccountStatusEnum.NotConnected&&jsxRuntime.jsxs(ui.Flex,{justify:"center",mt:4,gap:1,className:"oui-w-full oui-cursor-pointer",onClick:Se,children:[jsxRuntime.jsx(Ve,{}),jsxRuntime.jsx(ui.Text,{className:"oui-text-base-contrast-80 oui-text-sm",children:i("connector.disconnectWallet")})]})]})},Ve=()=>jsxRuntime.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"18",height:"18",viewBox:"0 0 18 18",fill:"none",children:jsxRuntime.jsx("path",{d:"M2.24219 5.24316C2.24219 3.58641 3.58536 2.24316 5.24219 2.24316H8.24219C9.89894 2.24316 11.2422 3.58641 11.2422 5.24316C11.2422 5.65716 10.9062 5.99316 10.4922 5.99316C10.0782 5.99316 9.74219 5.65716 9.74219 5.24316C9.74219 4.41441 9.07094 3.74316 8.24219 3.74316H5.24219C4.41374 3.74316 3.74219 4.41441 3.74219 5.24316V12.7432C3.74219 13.5719 4.41374 14.2432 5.24219 14.2432H8.24219C9.07094 14.2432 9.74219 13.5719 9.74219 12.7432C9.74219 12.3292 10.0782 11.9932 10.4922 11.9932C10.9062 11.9932 11.2422 12.3292 11.2422 12.7432C11.2422 14.3999 9.89894 15.7432 8.24219 15.7432H5.24219C3.58536 15.7432 2.24219 14.3999 2.24219 12.7432V5.24316ZM7.49219 8.99316C7.49219 8.57916 7.82819 8.24316 8.24219 8.24316H13.9144L12.4377 6.74316L13.4922 5.68866L16.2814 8.45391C16.5739 8.74716 16.5739 9.23915 16.2814 9.5324L13.4922 12.2977L12.4377 11.2432L13.9144 9.74316H8.24219C7.82819 9.74316 7.49219 9.40716 7.49219 8.99316Z",fill:"white",fillOpacity:"0.8"})}),Ue=({state:e,signIn:t,enableTrading:n,loading:r,disabled:o,showLedgerButton:i})=>{let{t:g}=i18n.useTranslation();return e<=types.AccountStatusEnum.NotSignedIn?jsxRuntime.jsxs(ui.Flex,{direction:"column",gap:3,className:"oui-w-full",children:[jsxRuntime.jsx(ui.Button,{fullWidth:true,onClick:()=>t(),loading:r,disabled:o,children:g("connector.createAccount")}),i&&jsxRuntime.jsx(ae,{onClick:()=>t(),content:g("connector.createAccountWithLedger")})]}):jsxRuntime.jsxs(ui.Flex,{direction:"column",gap:3,className:"oui-w-full",children:[jsxRuntime.jsx(ui.Button,{fullWidth:true,onClick:()=>n(),loading:r,disabled:o,children:g("connector.enableTrading")}),i&&jsxRuntime.jsx(ae,{onClick:()=>n(),disabled:o,content:g("connector.enableTradingWithLedger")})]})},ae=({onClick:e,disabled:t,content:n})=>{let{t:r}=i18n.useTranslation(),{state:o}=hooks.useAccount(),i=o.address,{setLedgerAddress:g}=hooks.useStorageLedgerAddress();return i?jsxRuntime.jsx(ui.Button,{variant:"outlined",color:"primary",fullWidth:true,onClick:()=>{g(i),e();},disabled:t,className:"oui-w-full",children:n}):null},Ze=e=>{let{t}=i18n.useTranslation();return jsxRuntime.jsx(ui.TextField,{placeholder:t("connector.referralCode.placeholder"),fullWidth:true,label:"",value:e.refCode,onChange:n=>{let r=n.target.value.toUpperCase().replace(/[^A-Z0-9]/g,"");e.setRefCode(r);},classNames:{label:"oui-text-base-contrast-54 oui-text-xs",input:"placeholder:oui-text-base-contrast-20 placeholder:oui-text-sm"},formatters:[ui.inputFormatter.createRegexInputFormatter(n=>String(n).replace(/[a-z]/g,r=>r.toUpperCase())),ui.inputFormatter.createRegexInputFormatter(/[^A-Z0-9]/g)],onClear:()=>{e.setRefCode("");},autoComplete:"off",helpText:e.helpText,className:"oui-mb-4",color:e.helpText?"danger":void 0})},Ke=()=>{let{t:e}=i18n.useTranslation(),t=()=>{window.innerWidth>768||ui.modal.alert({title:e("connector.rememberMe"),message:jsxRuntime.jsx("span",{className:"oui-text-2xs oui-text-base-contrast/60",children:e("connector.rememberMe.description")})});};return jsxRuntime.jsx(ui.Tooltip,{content:e("connector.rememberMe.description"),className:"oui-max-w-[300px]",children:jsxRuntime.jsx("button",{onClick:t,children:jsxRuntime.jsx(ui.Text,{intensity:54,size:"xs",className:"oui-underline oui-underline-offset-4 oui-decoration-dashed oui-decoration-base-contrast-36",children:e("connector.rememberMe")})})})};function ie(e){Object.keys(e).forEach(n=>{});let t=i18n.i18n.t("connector.somethingWentWrong");return e.toString().includes("rejected")&&(t=i18n.i18n.t("connector.userRejected")),ui.capitalizeFirstLetter(t)??t}var K=()=>{let{account:e,state:t,createOrderlyKey:n,createAccount:r}=hooks.useAccount(),[o,i]=react.useState(""),[g,u]=react.useState(""),{t:m}=i18n.useTranslation(),{trigger:s}=hooks.useLazyQuery(`/v1/public/referral/verify_ref_code?referral_code=${o}`);react.useEffect(()=>{let C=localStorage.getItem("referral_code");C!=null&&i(C);},[]);let{referral_code:a,isLoading:p}=hooks.useGetReferralCode(e.accountId),[l,{error:k,isMutating:x}]=hooks.useMutation("/v1/referral/bind","POST");react.useEffect(()=>{o.length===0&&u("");},[o]);let h=()=>{ui.toast.success(m("connector.walletConnected")),o.length>=4&&o.length<=10&&l({referral_code:o}).finally(()=>{localStorage.removeItem("referral_code");});},B=async()=>{if(o.length===0)return Promise.resolve(void 0);if(o.length>0&&(o.length<4||o.length>10))return Promise.resolve(m("connector.referralCode.invalid"));let{exist:C}=await s();return C===false?Promise.resolve(m("connector.referralCode.notExist")):Promise.resolve(void 0)},T=(a?.length||0)===0&&!p,N=async()=>{if(T){let C=await B();if(typeof C<"u")return u(C),Promise.reject(-1)}return u(""),r()};return {enableTrading:async C=>{if(T){let M=await B();if(typeof M<"u")return u(M),Promise.reject(-1)}return u(""),n(C)},initAccountState:t.status,signIn:N,enableTradingComplted:h,refCode:o,setRefCode:i,helpText:g,showRefCodeInput:T}};var D="walletConnector",F="walletConnectorSheet",Q=e=>{let t=K();return jsxRuntime.jsx(Z,{...t,...e})};ui.registerSimpleDialog(D,Q,{size:"sm",title:()=>i18n.i18n.t("connector.connectWallet")});ui.registerSimpleSheet(F,Q,{title:()=>i18n.i18n.t("connector.connectWallet")});var A=e=>{let{status:t,buttonProps:n,fallback:r,descriptions:o,classNames:i,networkId:g,id:u,bridgeLessOnly:m}=e,{t:s}=i18n.useTranslation(),{state:a}=hooks.useAccount(),{wrongNetwork:p,disabledConnect:l}=reactApp.useAppContext(),k=react.useMemo(()=>t===void 0?a.status===types.AccountStatusEnum.EnableTradingWithoutConnected?types.AccountStatusEnum.EnableTradingWithoutConnected:types.AccountStatusEnum.EnableTrading:t,[t,a.status]),x={connectWallet:s("connector.connectWallet"),switchChain:s("connector.wrongNetwork"),enableTrading:s("connector.enableTrading"),signin:s("connector.createAccount"),...e.labels},h=react.useMemo(()=>typeof r<"u"?r({validating:a.validating,status:a.status,wrongNetwork:p}):a.validating&&!l?jsxRuntime.jsx(R,{angle:45,disabled:true,loading:true,description:o?.connectWallet,id:u,type:"button",...n,children:x.connectWallet}):jsxRuntime.jsx(gt,{bridgeLessOnly:m,status:a.status,buttonProps:{...n,id:u,type:"button"},wrongNetwork:p,networkId:e.networkId,labels:x,descriptions:o,disabledConnect:l}),[a.status,a.validating,n,p]);return jsxRuntime.jsx(ui.Either,{value:a.status>=k&&!p&&!l,left:h,children:e.children})},dt=()=>{let{t:e}=i18n.useTranslation(),{state:t}=hooks.useAccount();return t.status<types.AccountStatusEnum.SignedIn?jsxRuntime.jsx(ui.Text,{children:e("connector.createAccount")}):t.status<types.AccountStatusEnum.EnableTrading?jsxRuntime.jsx(ui.Text,{children:e("connector.enableTrading")}):jsxRuntime.jsx(ui.Text,{children:e("connector.connectWallet")})},gt=e=>{let{buttonProps:t,labels:n,descriptions:r}=e,{t:o}=i18n.useTranslation(),{connectWallet:i}=reactApp.useAppContext(),{account:g}=hooks.useAccount(),{isMobile:u}=ui.useScreen(),m=hooks.useMediaQuery(types.MEDIA_TABLET),s=()=>{ui.modal.show(m?F:D,{title:jsxRuntime.jsx(dt,{})}).then(l=>{},l=>{});},a=async()=>{let l=await i();l&&(l.wrongNetwork?p():(l?.status??types.AccountStatusEnum.NotConnected)<types.AccountStatusEnum.EnableTrading&&s());},p=()=>{g.once("validate:end",l=>{l<types.AccountStatusEnum.EnableTrading?s():ui.toast.success(o("connector.walletConnected"));}),ui.modal.show(u?uiChainSelector.ChainSelectorSheetId:uiChainSelector.ChainSelectorDialogId,{networkId:e.networkId,bridgeLessOnly:e.bridgeLessOnly}).then(l=>{l.wrongNetwork||e.status>=types.AccountStatusEnum.Connected&&(e.status<types.AccountStatusEnum.EnableTrading?s():ui.toast.success(o("connector.walletConnected")));},l=>{});};return e.wrongNetwork&&!e.disabledConnect?jsxRuntime.jsx(R,{color:"warning",onClick:()=>{p();},description:r?.switchChain,...t,children:n.switchChain}):e.status<=types.AccountStatusEnum.NotConnected||e.disabledConnect?jsxRuntime.jsx(R,{size:"lg",onClick:()=>{a();},variant:e.disabledConnect?void 0:"gradient",angle:45,description:r?.connectWallet,disabled:e.disabledConnect,...t,children:n.connectWallet}):e.status<=types.AccountStatusEnum.NotSignedIn?jsxRuntime.jsx(R,{size:"lg",onClick:()=>{s();},angle:45,description:r?.signin,...t,children:n.signin}):jsxRuntime.jsx(R,{size:"lg",description:r?.enableTrading,...t,onClick:()=>s(),children:n.enableTrading})};A.displayName="AuthGuard";var R=e=>{let{description:t,...n}=e;return jsxRuntime.jsxs(ui.Flex,{direction:"column",children:[jsxRuntime.jsx(ui.Button,{...n}),!!t&&jsxRuntime.jsx(ui.Box,{mt:4,className:"oui-leading-none",style:{lineHeight:0},children:jsxRuntime.jsx(ui.Text,{size:"2xs",intensity:36,children:t})})]})};var St=e=>{let{status:t,labels:n,description:r,dataSource:o,children:i,...g}=e,{state:u}=hooks.useAccount(),{wrongNetwork:m,disabledConnect:s}=reactApp.useAppContext(),a=react.useMemo(()=>t===void 0?u.status===types.AccountStatusEnum.EnableTradingWithoutConnected?types.AccountStatusEnum.EnableTradingWithoutConnected:types.AccountStatusEnum.EnableTrading:t,[t,u.status]),p=reactApp.useDataTap(o,{accountStatus:a}),l=m||s||u.status<a||e.ignoreLoadingCheck;return jsxRuntime.jsx(ui.DataTable,{dataSource:p,ignoreLoadingCheck:l,emptyView:jsxRuntime.jsx(Nt,{status:a,description:r,labels:n,className:e.classNames?.authGuardDescription,visible:!u.validating}),manualPagination:true,...g,children:i})},Nt=e=>{let{t}=i18n.useTranslation(),r={...{connectWallet:t("connector.trade.connectWallet.tooltip"),switchChain:t("connector.wrongNetwork.tooltip"),enableTrading:t("connector.trade.enableTrading.tooltip"),signin:t("connector.trade.createAccount.tooltip")},...e.description};return e.visible?jsxRuntime.jsx(ui.Flex,{py:8,children:jsxRuntime.jsx(A,{status:e.status,labels:e.labels,descriptions:r,buttonProps:{size:"md"},children:jsxRuntime.jsx(ui.ExtensionSlot,{position:ui.ExtensionPositionEnum.EmptyDataIdentifier})})}):null};var he=e=>{let{t}=i18n.useTranslation(),{hint:n={connectWallet:t("connector.trade.connectWallet.tooltip"),signIn:t("connector.trade.createAccount.tooltip"),enableTrading:t("connector.trade.enableTrading.tooltip"),wrongNetwork:t("connector.wrongNetwork.tooltip")},status:r}=e;return jsxRuntime.jsx(ui.Box,{my:8,children:jsxRuntime.jsx(A,{status:r,descriptions:{...n,switchChain:n.wrongNetwork},buttonProps:{size:"md"},children:e.children||jsxRuntime.jsx(ui.EmptyDataState,{})})})};he.displayName="AuthGuardEmpty";var ye=e=>{let{t}=i18n.useTranslation(),{opactiy:n=90,tooltip:r={connectWallet:t("connector.setUp.connectWallet.tooltip"),signIn:t("connector.setUp.createAccount.tooltip"),enableTrading:t("connector.setUp.enableTrading.tooltip"),wrongNetwork:t("connector.wrongNetwork.tooltip")}}=e,[o,i]=react.useState(false),{state:g}=hooks.useAccount(),u=true,{wrongNetwork:m}=reactApp.useAppContext(),s=react.useMemo(()=>{if(m)return r?.wrongNetwork;switch(g.status){case types.AccountStatusEnum.NotConnected:return r?.connectWallet;case types.AccountStatusEnum.NotSignedIn:return r?.signIn;case types.AccountStatusEnum.DisabledTrading:return r?.enableTrading;case types.AccountStatusEnum.EnableTrading:return "";default:return e.content}},[e.content,g,u,r]),a=react.useMemo(()=>{switch(g.status){case types.AccountStatusEnum.NotConnected:case types.AccountStatusEnum.NotSignedIn:return n;case types.AccountStatusEnum.EnableTrading:return;default:return}},[e.opactiy,g,u]);return jsxRuntime.jsx(ui.Tooltip,{open:s?o:false,onOpenChange:i,content:s,className:"oui-text-2xs",align:e.align,alignOffset:e.alignOffset,side:e.side,sideOffset:e.sideOffset,children:jsxRuntime.jsx("div",{style:{opacity:a},children:e.children})})};ye.displayName="AuthGuardTooltip";var Te=(o=>(o[o.WrongNetwork=0]="WrongNetwork",o[o.ConnectWallet=1]="ConnectWallet",o[o.CreateAccount=2]="CreateAccount",o[o.EnableTrading=3]="EnableTrading",o))(Te||{}),Dt=()=>{let{state:e}=hooks.useAccount(),{wrongNetwork:t,disabledConnect:n}=reactApp.useAppContext();return react.useMemo(()=>t&&!n?0:e.status===types.AccountStatusEnum.EnableTradingWithoutConnected?3:e.status<=types.AccountStatusEnum.NotConnected||n?1:e.status<=types.AccountStatusEnum.NotSignedIn?2:3,[e.status,t,n])};
12
+ // src/component/walletConnectorContent.tsx
13
+ var StepItem = (props) => {
14
+ const { title, description, showDivider } = props;
15
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Box, { position: "relative", className: "oui-pl-8", children: [
16
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Box, { children: [
17
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { as: "div", intensity: 98, size: "sm", children: title }),
18
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { as: "div", intensity: 54, size: "2xs", children: description })
19
+ ] }),
20
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "oui-absolute oui-left-0 oui-top-1 oui-z-10", children: /* @__PURE__ */ jsxRuntime.jsx(Identifier, { ...props }) }),
21
+ showDivider && /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { position: "absolute", left: 12, top: 23, bottom: -21, zIndex: 0, children: /* @__PURE__ */ jsxRuntime.jsx(
22
+ ui.Divider,
23
+ {
24
+ lineStyle: "dashed",
25
+ direction: "vertical",
26
+ intensity: 16,
27
+ className: "oui-h-full"
28
+ }
29
+ ) })
30
+ ] });
31
+ };
32
+ var Identifier = (props) => {
33
+ const { active, isLoading, isCompleted } = props;
34
+ if (isLoading) {
35
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, { size: "sm", className: "oui-ml-1" });
36
+ }
37
+ if (isCompleted) {
38
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.CheckedCircleFillIcon, { opacity: 1, className: "oui-text-primary" });
39
+ }
40
+ return /* @__PURE__ */ jsxRuntime.jsx(Dot, { active: !!active });
41
+ };
42
+ var Dot = ({
43
+ active,
44
+ className
45
+ }) => {
46
+ return /* @__PURE__ */ jsxRuntime.jsx(
47
+ "div",
48
+ {
49
+ className: ui.cn(
50
+ "oui-w-[8.3px] oui-h-[8.3px] oui-rounded-full oui-ml-2 oui-mt-1",
51
+ className,
52
+ active ? "oui-bg-primary-light" : "oui-bg-base-2"
53
+ )
54
+ }
55
+ );
56
+ };
57
+ var WalletConnectContent = (props) => {
58
+ const { initAccountState = types.AccountStatusEnum.NotConnected } = props;
59
+ const [remember, setRemember] = react.useState(true);
60
+ const ee = hooks.useEventEmitter();
61
+ const { t } = i18n.useTranslation();
62
+ const { disconnect, namespace } = hooks.useWalletConnector();
63
+ const { state: accountState, account } = hooks.useAccount();
64
+ const [state, setState] = react.useState(initAccountState);
65
+ const [activeStep, setActiveStep] = react.useState(0);
66
+ const [loading, setLoading] = react.useState(false);
67
+ const [showLedgerButton, setShowLedgerButton] = react.useState(false);
68
+ const { ledgerWallet, setLedgerAddress } = hooks.useStorageLedgerAddress();
69
+ const handleRef = react.useRef(0);
70
+ const [firstShowDialog] = hooks.useLocalStorage(
71
+ "orderly-first-show-wallet-connector-dialog",
72
+ void 0
73
+ );
74
+ react.useEffect(() => {
75
+ return () => {
76
+ localStorage.setItem("orderly-first-show-wallet-connector-dialog", "1");
77
+ };
78
+ }, []);
79
+ react.useEffect(() => {
80
+ setState(accountState.status);
81
+ }, [accountState]);
82
+ const steps = react.useMemo(() => {
83
+ const steps2 = [];
84
+ if (initAccountState < types.AccountStatusEnum.SignedIn) {
85
+ steps2.push({
86
+ key: "signIn",
87
+ title: t("connector.createAccount"),
88
+ description: t("connector.createAccount.description")
89
+ });
90
+ }
91
+ if (initAccountState < types.AccountStatusEnum.EnableTrading) {
92
+ steps2.push({
93
+ key: "enableTrading",
94
+ title: t("connector.enableTrading"),
95
+ description: t("connector.enableTrading.description")
96
+ });
97
+ }
98
+ return steps2;
99
+ }, [initAccountState, t]);
100
+ react.useEffect(() => {
101
+ if (namespace != types.ChainNamespace.solana) {
102
+ setShowLedgerButton(false);
103
+ return;
104
+ }
105
+ if (!ledgerWallet) {
106
+ setShowLedgerButton(true);
107
+ return;
108
+ }
109
+ if (ledgerWallet && account.address) {
110
+ if (!ledgerWallet.includes(account.address)) {
111
+ setShowLedgerButton(true);
112
+ return;
113
+ }
114
+ }
115
+ setShowLedgerButton(false);
116
+ }, [namespace, account.address, ledgerWallet]);
117
+ const onEnableTrading = () => {
118
+ setLoading(true);
119
+ return props.enableTrading(remember).then(
120
+ async (res) => {
121
+ handleRef.current++;
122
+ setLoading(false);
123
+ setActiveStep((step) => step + 1);
124
+ try {
125
+ await props.enableTradingComplted?.();
126
+ } catch (e) {
127
+ }
128
+ if (typeof props.onCompleted === "function") {
129
+ props.onCompleted();
130
+ } else if (typeof props.close === "function") {
131
+ props.close();
132
+ }
133
+ },
134
+ (reject) => {
135
+ setLoading(false);
136
+ if (reject === -1) {
137
+ return;
138
+ }
139
+ if (reject.message.indexOf(
140
+ "Signing off chain messages with Ledger is not yet supported"
141
+ ) !== -1) {
142
+ ee.emit("wallet:sign-message-with-ledger-error", {
143
+ message: reject.message,
144
+ userAddress: account.address
145
+ });
146
+ return;
147
+ }
148
+ ui.toast.error(paseErrorMsg(reject));
149
+ }
150
+ ).catch((e) => {
151
+ setLoading(false);
152
+ });
153
+ };
154
+ const onDisconnect = async () => {
155
+ localStorage.removeItem("orderly_link_device");
156
+ disconnect({
157
+ label: state.connectWallet?.name
158
+ }).then(() => {
159
+ account.disconnect();
160
+ if (typeof props.close === "function") {
161
+ props.close();
162
+ }
163
+ });
164
+ };
165
+ const onSignIn = () => {
166
+ setLoading(true);
167
+ return props.signIn().then(
168
+ (res) => {
169
+ setActiveStep((step) => step + 1);
170
+ onEnableTrading();
171
+ },
172
+ (reject) => {
173
+ setLoading(false);
174
+ if (reject === -1) {
175
+ return;
176
+ }
177
+ if (reject.message.indexOf(
178
+ "Signing off chain messages with Ledger is not yet supported"
179
+ ) !== -1) {
180
+ ee.emit("wallet:sign-message-with-ledger-error", {
181
+ message: reject.message,
182
+ userAddress: account.address
183
+ });
184
+ return;
185
+ }
186
+ ui.toast.error(paseErrorMsg(reject));
187
+ }
188
+ ).catch((e) => {
189
+ setLoading(false);
190
+ });
191
+ };
192
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Box, { id: "oui-wallet-connect-dialog-content", className: "oui-font-semibold", children: [
193
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { intensity: 54, size: "xs", children: t("connector.expired") }),
194
+ /* @__PURE__ */ jsxRuntime.jsx(
195
+ ui.Box,
196
+ {
197
+ p: 4,
198
+ my: 6,
199
+ intensity: 600,
200
+ r: "lg",
201
+ className: "oui-space-y-5",
202
+ position: "relative",
203
+ children: steps.map((step, index) => {
204
+ const isLast = index === steps.length - 1;
205
+ return /* @__PURE__ */ jsxRuntime.jsx(
206
+ StepItem,
207
+ {
208
+ title: step.title,
209
+ description: step.description,
210
+ isCompleted: activeStep > index,
211
+ active: activeStep === index,
212
+ isLoading: loading && activeStep === index,
213
+ showDivider: !isLast
214
+ },
215
+ step.key
216
+ );
217
+ })
218
+ }
219
+ ),
220
+ props.showRefCodeInput && steps.length == 2 && /* @__PURE__ */ jsxRuntime.jsx(ReferralCode, { ...props }),
221
+ firstShowDialog && /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { justify: "between", itemAlign: "center", children: [
222
+ /* @__PURE__ */ jsxRuntime.jsx(RememberMe, {}),
223
+ /* @__PURE__ */ jsxRuntime.jsx(
224
+ ui.Switch,
225
+ {
226
+ color: "primary",
227
+ checked: remember,
228
+ onCheckedChange: setRemember,
229
+ disabled: loading,
230
+ className: "data-[state=checked]:oui-bg-primary-darken"
231
+ }
232
+ )
233
+ ] }),
234
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Flex, { justify: "center", mt: 8, className: "oui-w-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { className: "oui-w-full", children: /* @__PURE__ */ jsxRuntime.jsx(
235
+ ActionButton,
236
+ {
237
+ state,
238
+ signIn: onSignIn,
239
+ enableTrading: onEnableTrading,
240
+ loading,
241
+ disabled: state >= types.AccountStatusEnum.EnableTrading,
242
+ showLedgerButton
243
+ }
244
+ ) }) }),
245
+ state > types.AccountStatusEnum.NotConnected && /* @__PURE__ */ jsxRuntime.jsxs(
246
+ ui.Flex,
247
+ {
248
+ justify: "center",
249
+ mt: 4,
250
+ gap: 1,
251
+ className: "oui-w-full oui-cursor-pointer",
252
+ onClick: onDisconnect,
253
+ children: [
254
+ /* @__PURE__ */ jsxRuntime.jsx(DisconnectIcon, {}),
255
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "oui-text-base-contrast-80 oui-text-sm", children: t("connector.disconnectWallet") })
256
+ ]
257
+ }
258
+ )
259
+ ] });
260
+ };
261
+ var DisconnectIcon = () => {
262
+ return /* @__PURE__ */ jsxRuntime.jsx(
263
+ "svg",
264
+ {
265
+ xmlns: "http://www.w3.org/2000/svg",
266
+ width: "18",
267
+ height: "18",
268
+ viewBox: "0 0 18 18",
269
+ fill: "none",
270
+ children: /* @__PURE__ */ jsxRuntime.jsx(
271
+ "path",
272
+ {
273
+ d: "M2.24219 5.24316C2.24219 3.58641 3.58536 2.24316 5.24219 2.24316H8.24219C9.89894 2.24316 11.2422 3.58641 11.2422 5.24316C11.2422 5.65716 10.9062 5.99316 10.4922 5.99316C10.0782 5.99316 9.74219 5.65716 9.74219 5.24316C9.74219 4.41441 9.07094 3.74316 8.24219 3.74316H5.24219C4.41374 3.74316 3.74219 4.41441 3.74219 5.24316V12.7432C3.74219 13.5719 4.41374 14.2432 5.24219 14.2432H8.24219C9.07094 14.2432 9.74219 13.5719 9.74219 12.7432C9.74219 12.3292 10.0782 11.9932 10.4922 11.9932C10.9062 11.9932 11.2422 12.3292 11.2422 12.7432C11.2422 14.3999 9.89894 15.7432 8.24219 15.7432H5.24219C3.58536 15.7432 2.24219 14.3999 2.24219 12.7432V5.24316ZM7.49219 8.99316C7.49219 8.57916 7.82819 8.24316 8.24219 8.24316H13.9144L12.4377 6.74316L13.4922 5.68866L16.2814 8.45391C16.5739 8.74716 16.5739 9.23915 16.2814 9.5324L13.4922 12.2977L12.4377 11.2432L13.9144 9.74316H8.24219C7.82819 9.74316 7.49219 9.40716 7.49219 8.99316Z",
274
+ fill: "white",
275
+ fillOpacity: "0.8"
276
+ }
277
+ )
278
+ }
279
+ );
280
+ };
281
+ var ActionButton = ({
282
+ state,
283
+ signIn,
284
+ enableTrading,
285
+ loading,
286
+ disabled,
287
+ showLedgerButton
288
+ }) => {
289
+ const { t } = i18n.useTranslation();
290
+ if (state <= types.AccountStatusEnum.NotSignedIn) {
291
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { direction: "column", gap: 3, className: "oui-w-full", children: [
292
+ /* @__PURE__ */ jsxRuntime.jsx(
293
+ ui.Button,
294
+ {
295
+ fullWidth: true,
296
+ onClick: () => signIn(),
297
+ loading,
298
+ disabled,
299
+ children: t("connector.createAccount")
300
+ }
301
+ ),
302
+ showLedgerButton && /* @__PURE__ */ jsxRuntime.jsx(
303
+ WithLedgerButton,
304
+ {
305
+ onClick: () => signIn(),
306
+ content: t("connector.createAccountWithLedger")
307
+ }
308
+ )
309
+ ] });
310
+ }
311
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { direction: "column", gap: 3, className: "oui-w-full", children: [
312
+ /* @__PURE__ */ jsxRuntime.jsx(
313
+ ui.Button,
314
+ {
315
+ fullWidth: true,
316
+ onClick: () => enableTrading(),
317
+ loading,
318
+ disabled,
319
+ children: t("connector.enableTrading")
320
+ }
321
+ ),
322
+ showLedgerButton && /* @__PURE__ */ jsxRuntime.jsx(
323
+ WithLedgerButton,
324
+ {
325
+ onClick: () => enableTrading(),
326
+ disabled,
327
+ content: t("connector.enableTradingWithLedger")
328
+ }
329
+ )
330
+ ] });
331
+ };
332
+ var WithLedgerButton = ({
333
+ onClick,
334
+ disabled,
335
+ content
336
+ }) => {
337
+ const { t } = i18n.useTranslation();
338
+ const { state } = hooks.useAccount();
339
+ const address = state.address;
340
+ const { setLedgerAddress } = hooks.useStorageLedgerAddress();
341
+ if (!address) {
342
+ return null;
343
+ }
344
+ return /* @__PURE__ */ jsxRuntime.jsx(
345
+ ui.Button,
346
+ {
347
+ variant: "outlined",
348
+ color: "primary",
349
+ fullWidth: true,
350
+ onClick: () => {
351
+ setLedgerAddress(address);
352
+ onClick();
353
+ },
354
+ disabled,
355
+ className: "oui-w-full",
356
+ children: content
357
+ }
358
+ );
359
+ };
360
+ var ReferralCode = (props) => {
361
+ const { t } = i18n.useTranslation();
362
+ return /* @__PURE__ */ jsxRuntime.jsx(
363
+ ui.TextField,
364
+ {
365
+ placeholder: t("connector.referralCode.placeholder"),
366
+ fullWidth: true,
367
+ label: "",
368
+ value: props.refCode,
369
+ onChange: (e) => {
370
+ const _value = e.target.value.toUpperCase().replace(/[^A-Z0-9]/g, "");
371
+ props.setRefCode(_value);
372
+ },
373
+ classNames: {
374
+ label: "oui-text-base-contrast-54 oui-text-xs",
375
+ input: "placeholder:oui-text-base-contrast-20 placeholder:oui-text-sm"
376
+ },
377
+ formatters: [
378
+ ui.inputFormatter.createRegexInputFormatter((value) => {
379
+ return String(value).replace(
380
+ /[a-z]/g,
381
+ (char) => char.toUpperCase()
382
+ );
383
+ }),
384
+ ui.inputFormatter.createRegexInputFormatter(/[^A-Z0-9]/g)
385
+ ],
386
+ onClear: () => {
387
+ props.setRefCode("");
388
+ },
389
+ autoComplete: "off",
390
+ helpText: props.helpText,
391
+ className: "oui-mb-4",
392
+ color: props.helpText ? "danger" : void 0
393
+ }
394
+ );
395
+ };
396
+ var RememberMe = () => {
397
+ const { t } = i18n.useTranslation();
398
+ const showRememberHint = () => {
399
+ if (window.innerWidth > 768) {
400
+ return;
401
+ }
402
+ ui.modal.alert({
403
+ title: t("connector.rememberMe"),
404
+ message: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "oui-text-2xs oui-text-base-contrast/60", children: t("connector.rememberMe.description") })
405
+ });
406
+ };
407
+ return /* @__PURE__ */ jsxRuntime.jsx(
408
+ ui.Tooltip,
409
+ {
410
+ content: t("connector.rememberMe.description"),
411
+ className: "oui-max-w-[300px]",
412
+ children: /* @__PURE__ */ jsxRuntime.jsx("button", { onClick: showRememberHint, children: /* @__PURE__ */ jsxRuntime.jsx(
413
+ ui.Text,
414
+ {
415
+ intensity: 54,
416
+ size: "xs",
417
+ className: "oui-underline oui-underline-offset-4 oui-decoration-dashed oui-decoration-base-contrast-36",
418
+ children: t("connector.rememberMe")
419
+ }
420
+ ) })
421
+ }
422
+ );
423
+ };
424
+ function paseErrorMsg(reject) {
425
+ Object.keys(reject).forEach((key) => {
426
+ });
427
+ let msg = i18n.i18n.t("connector.somethingWentWrong");
428
+ if (reject.toString().includes("rejected")) {
429
+ msg = i18n.i18n.t("connector.userRejected");
430
+ }
431
+ return ui.capitalizeFirstLetter(msg) ?? msg;
432
+ }
433
+ var useWalletConnectorBuilder = () => {
434
+ const { account, state, createOrderlyKey, createAccount } = hooks.useAccount();
435
+ const [refCode, setRefCode] = react.useState("");
436
+ const [helpText, setHelpText] = react.useState("");
437
+ const { t } = i18n.useTranslation();
438
+ const { trigger: verifyRefCode } = hooks.useLazyQuery(
439
+ `/v1/public/referral/verify_ref_code?referral_code=${refCode}`
440
+ );
441
+ react.useEffect(() => {
442
+ const refCode2 = localStorage.getItem("referral_code");
443
+ if (refCode2 != null) {
444
+ setRefCode(refCode2);
445
+ }
446
+ }, []);
447
+ const { referral_code, isLoading } = hooks.useGetReferralCode(account.accountId);
448
+ const [bindRefCode, { error: updateOrderError, isMutating: updateMutating }] = hooks.useMutation("/v1/referral/bind", "POST");
449
+ react.useEffect(() => {
450
+ if (refCode.length === 0) {
451
+ setHelpText("");
452
+ }
453
+ }, [refCode]);
454
+ const enableTradingComplted = () => {
455
+ ui.toast.success(t("connector.walletConnected"));
456
+ if (refCode.length >= 4 && refCode.length <= 10)
457
+ bindRefCode({ referral_code: refCode }).finally(() => {
458
+ localStorage.removeItem("referral_code");
459
+ });
460
+ };
461
+ const checkRefCode = async () => {
462
+ if (refCode.length === 0) {
463
+ return Promise.resolve(void 0);
464
+ }
465
+ if (refCode.length > 0 && (refCode.length < 4 || refCode.length > 10)) {
466
+ return Promise.resolve(t("connector.referralCode.invalid"));
467
+ }
468
+ const { exist } = await verifyRefCode();
469
+ if (exist === false) {
470
+ return Promise.resolve(t("connector.referralCode.notExist"));
471
+ }
472
+ return Promise.resolve(void 0);
473
+ };
474
+ const showRefCodeInput = (referral_code?.length || 0) === 0 && !isLoading;
475
+ const signIn = async () => {
476
+ if (showRefCodeInput) {
477
+ const info = await checkRefCode();
478
+ if (typeof info !== "undefined") {
479
+ setHelpText(info);
480
+ return Promise.reject(-1);
481
+ }
482
+ }
483
+ setHelpText("");
484
+ return createAccount();
485
+ };
486
+ const enableTrading = async (remember) => {
487
+ if (showRefCodeInput) {
488
+ const info = await checkRefCode();
489
+ if (typeof info !== "undefined") {
490
+ setHelpText(info);
491
+ return Promise.reject(-1);
492
+ }
493
+ }
494
+ setHelpText("");
495
+ return createOrderlyKey(remember);
496
+ };
497
+ return {
498
+ enableTrading,
499
+ initAccountState: state.status,
500
+ signIn,
501
+ enableTradingComplted,
502
+ refCode,
503
+ setRefCode,
504
+ helpText,
505
+ showRefCodeInput
506
+ };
507
+ };
508
+ var WalletConnectorModalId = "walletConnector";
509
+ var WalletConnectorSheetId = "walletConnectorSheet";
510
+ var WalletConnectorWidget = (props) => {
511
+ const state = useWalletConnectorBuilder();
512
+ return /* @__PURE__ */ jsxRuntime.jsx(WalletConnectContent, { ...state, ...props });
513
+ };
514
+ ui.registerSimpleDialog(WalletConnectorModalId, WalletConnectorWidget, {
515
+ size: "sm",
516
+ title: () => i18n.i18n.t("connector.connectWallet")
517
+ });
518
+ ui.registerSimpleSheet(WalletConnectorSheetId, WalletConnectorWidget, {
519
+ title: () => i18n.i18n.t("connector.connectWallet")
520
+ });
521
+ var AuthGuard = (props) => {
522
+ const {
523
+ status,
524
+ buttonProps,
525
+ fallback,
526
+ descriptions,
527
+ classNames,
528
+ networkId,
529
+ id,
530
+ bridgeLessOnly
531
+ // ...rest
532
+ } = props;
533
+ const { t } = i18n.useTranslation();
534
+ const { state } = hooks.useAccount();
535
+ const { wrongNetwork, disabledConnect } = reactApp.useAppContext();
536
+ const _status = react.useMemo(() => {
537
+ if (status === void 0) {
538
+ return state.status === types.AccountStatusEnum.EnableTradingWithoutConnected ? types.AccountStatusEnum.EnableTradingWithoutConnected : types.AccountStatusEnum.EnableTrading;
539
+ }
540
+ return status;
541
+ }, [status, state.status]);
542
+ const labels = {
543
+ connectWallet: t("connector.connectWallet"),
544
+ switchChain: t("connector.wrongNetwork"),
545
+ enableTrading: t("connector.enableTrading"),
546
+ signin: t("connector.createAccount"),
547
+ ...props.labels
548
+ };
549
+ const Left = react.useMemo(() => {
550
+ if (typeof fallback !== "undefined") {
551
+ return fallback({
552
+ validating: state.validating,
553
+ status: state.status,
554
+ wrongNetwork
555
+ });
556
+ }
557
+ if (state.validating && !disabledConnect) {
558
+ return /* @__PURE__ */ jsxRuntime.jsx(
559
+ StatusInfo,
560
+ {
561
+ angle: 45,
562
+ disabled: true,
563
+ loading: true,
564
+ description: descriptions?.connectWallet,
565
+ id,
566
+ type: "button",
567
+ ...buttonProps,
568
+ children: labels.connectWallet
569
+ }
570
+ );
571
+ }
572
+ return /* @__PURE__ */ jsxRuntime.jsx(
573
+ DefaultFallback,
574
+ {
575
+ bridgeLessOnly,
576
+ status: state.status,
577
+ buttonProps: { ...buttonProps, id, type: "button" },
578
+ wrongNetwork,
579
+ networkId: props.networkId,
580
+ labels,
581
+ descriptions,
582
+ disabledConnect
583
+ }
584
+ );
585
+ }, [state.status, state.validating, buttonProps, wrongNetwork]);
586
+ return /* @__PURE__ */ jsxRuntime.jsx(
587
+ ui.Either,
588
+ {
589
+ value: state.status >= _status && !wrongNetwork && !disabledConnect,
590
+ left: Left,
591
+ children: props.children
592
+ }
593
+ );
594
+ };
595
+ var ModalTitle = () => {
596
+ const { t } = i18n.useTranslation();
597
+ const { state } = hooks.useAccount();
598
+ if (state.status < types.AccountStatusEnum.SignedIn) {
599
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { children: t("connector.createAccount") });
600
+ }
601
+ if (state.status < types.AccountStatusEnum.EnableTrading) {
602
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { children: t("connector.enableTrading") });
603
+ }
604
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { children: t("connector.connectWallet") });
605
+ };
606
+ var DefaultFallback = (props) => {
607
+ const { buttonProps, labels, descriptions } = props;
608
+ const { t } = i18n.useTranslation();
609
+ const { connectWallet } = reactApp.useAppContext();
610
+ const { account } = hooks.useAccount();
611
+ const { isMobile } = ui.useScreen();
612
+ const matches = hooks.useMediaQuery(types.MEDIA_TABLET);
613
+ const onConnectOrderly = () => {
614
+ ui.modal.show(matches ? WalletConnectorSheetId : WalletConnectorModalId, {
615
+ title: /* @__PURE__ */ jsxRuntime.jsx(ModalTitle, {})
616
+ }).then(
617
+ (r) => void 0,
618
+ (error) => void 0
619
+ );
620
+ };
621
+ const onConnectWallet = async () => {
622
+ const res = await connectWallet();
623
+ if (!res) {
624
+ return;
625
+ }
626
+ if (res.wrongNetwork) {
627
+ switchChain();
628
+ } else {
629
+ if ((res?.status ?? types.AccountStatusEnum.NotConnected) < types.AccountStatusEnum.EnableTrading) {
630
+ onConnectOrderly();
631
+ }
632
+ }
633
+ };
634
+ const switchChain = () => {
635
+ account.once("validate:end", (status) => {
636
+ if (status < types.AccountStatusEnum.EnableTrading) {
637
+ onConnectOrderly();
638
+ } else {
639
+ ui.toast.success(t("connector.walletConnected"));
640
+ }
641
+ });
642
+ ui.modal.show(
643
+ isMobile ? uiChainSelector.ChainSelectorSheetId : uiChainSelector.ChainSelectorDialogId,
644
+ {
645
+ networkId: props.networkId,
646
+ bridgeLessOnly: props.bridgeLessOnly
647
+ }
648
+ ).then(
649
+ (r) => {
650
+ if (!r.wrongNetwork) {
651
+ if (props.status >= types.AccountStatusEnum.Connected) {
652
+ if (props.status < types.AccountStatusEnum.EnableTrading) {
653
+ onConnectOrderly();
654
+ } else {
655
+ ui.toast.success(t("connector.walletConnected"));
656
+ }
657
+ }
658
+ }
659
+ },
660
+ (error) => void 0
661
+ );
662
+ };
663
+ if (props.wrongNetwork && !props.disabledConnect) {
664
+ return /* @__PURE__ */ jsxRuntime.jsx(
665
+ StatusInfo,
666
+ {
667
+ color: "warning",
668
+ onClick: () => {
669
+ switchChain();
670
+ },
671
+ description: descriptions?.switchChain,
672
+ ...buttonProps,
673
+ children: labels.switchChain
674
+ }
675
+ );
676
+ }
677
+ if (props.status <= types.AccountStatusEnum.NotConnected || props.disabledConnect) {
678
+ return /* @__PURE__ */ jsxRuntime.jsx(
679
+ StatusInfo,
680
+ {
681
+ size: "lg",
682
+ onClick: () => {
683
+ onConnectWallet();
684
+ },
685
+ variant: props.disabledConnect ? void 0 : "gradient",
686
+ angle: 45,
687
+ description: descriptions?.connectWallet,
688
+ disabled: props.disabledConnect,
689
+ ...buttonProps,
690
+ children: labels.connectWallet
691
+ }
692
+ );
693
+ }
694
+ if (props.status <= types.AccountStatusEnum.NotSignedIn) {
695
+ return /* @__PURE__ */ jsxRuntime.jsx(
696
+ StatusInfo,
697
+ {
698
+ size: "lg",
699
+ onClick: () => {
700
+ onConnectOrderly();
701
+ },
702
+ angle: 45,
703
+ description: descriptions?.signin,
704
+ ...buttonProps,
705
+ children: labels.signin
706
+ }
707
+ );
708
+ }
709
+ return /* @__PURE__ */ jsxRuntime.jsx(
710
+ StatusInfo,
711
+ {
712
+ size: "lg",
713
+ description: descriptions?.enableTrading,
714
+ ...buttonProps,
715
+ onClick: () => onConnectOrderly(),
716
+ children: labels.enableTrading
717
+ }
718
+ );
719
+ };
720
+ AuthGuard.displayName = "AuthGuard";
721
+ var StatusInfo = (props) => {
722
+ const { description, ...buttonProps } = props;
723
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Flex, { direction: "column", children: [
724
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { ...buttonProps }),
725
+ !!description && /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { mt: 4, className: "oui-leading-none", style: { lineHeight: 0 }, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "2xs", intensity: 36, children: description }) })
726
+ ] });
727
+ };
728
+ var AuthGuardDataTable = (props) => {
729
+ const {
730
+ status,
731
+ // message,
732
+ labels,
733
+ description,
734
+ dataSource,
735
+ children,
736
+ ...rest
737
+ } = props;
738
+ const { state } = hooks.useAccount();
739
+ const { wrongNetwork, disabledConnect } = reactApp.useAppContext();
740
+ const _status = react.useMemo(() => {
741
+ if (status === void 0) {
742
+ return state.status === types.AccountStatusEnum.EnableTradingWithoutConnected ? types.AccountStatusEnum.EnableTradingWithoutConnected : types.AccountStatusEnum.EnableTrading;
743
+ }
744
+ return status;
745
+ }, [status, state.status]);
746
+ const data = reactApp.useDataTap(dataSource, {
747
+ accountStatus: _status
748
+ });
749
+ const ignoreLoadingCheck = wrongNetwork || disabledConnect || state.status < _status || props.ignoreLoadingCheck;
750
+ return /* @__PURE__ */ jsxRuntime.jsx(
751
+ ui.DataTable,
752
+ {
753
+ dataSource: data,
754
+ ignoreLoadingCheck,
755
+ emptyView: /* @__PURE__ */ jsxRuntime.jsx(
756
+ GuardView,
757
+ {
758
+ status: _status,
759
+ description,
760
+ labels,
761
+ className: props.classNames?.authGuardDescription,
762
+ visible: !state.validating
763
+ }
764
+ ),
765
+ manualPagination: true,
766
+ ...rest,
767
+ children
768
+ }
769
+ );
770
+ };
771
+ var GuardView = (props) => {
772
+ const { t } = i18n.useTranslation();
773
+ const DESCRIPTIONS = {
774
+ connectWallet: t("connector.trade.connectWallet.tooltip"),
775
+ switchChain: t("connector.wrongNetwork.tooltip"),
776
+ enableTrading: t("connector.trade.enableTrading.tooltip"),
777
+ signin: t("connector.trade.createAccount.tooltip")
778
+ };
779
+ const descriptions = { ...DESCRIPTIONS, ...props.description };
780
+ if (!props.visible) {
781
+ return null;
782
+ }
783
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Flex, { py: 8, children: /* @__PURE__ */ jsxRuntime.jsx(
784
+ AuthGuard,
785
+ {
786
+ status: props.status,
787
+ labels: props.labels,
788
+ descriptions,
789
+ buttonProps: {
790
+ size: "md"
791
+ },
792
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.ExtensionSlot, { position: ui.ExtensionPositionEnum.EmptyDataIdentifier })
793
+ }
794
+ ) });
795
+ };
796
+ var AuthGuardEmpty = (props) => {
797
+ const { t } = i18n.useTranslation();
798
+ const {
799
+ hint = {
800
+ connectWallet: t("connector.trade.connectWallet.tooltip"),
801
+ signIn: t("connector.trade.createAccount.tooltip"),
802
+ enableTrading: t("connector.trade.enableTrading.tooltip"),
803
+ wrongNetwork: t("connector.wrongNetwork.tooltip")
804
+ },
805
+ status
806
+ } = props;
807
+ return /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { my: 8, children: /* @__PURE__ */ jsxRuntime.jsx(
808
+ AuthGuard,
809
+ {
810
+ status,
811
+ descriptions: { ...hint, switchChain: hint.wrongNetwork },
812
+ buttonProps: {
813
+ size: "md"
814
+ },
815
+ children: props.children || /* @__PURE__ */ jsxRuntime.jsx(ui.EmptyDataState, {})
816
+ }
817
+ ) });
818
+ };
819
+ AuthGuardEmpty.displayName = "AuthGuardEmpty";
820
+ var AuthGuardTooltip = (props) => {
821
+ const { t } = i18n.useTranslation();
822
+ const {
823
+ opactiy = 90,
824
+ tooltip = {
825
+ connectWallet: t("connector.setUp.connectWallet.tooltip"),
826
+ signIn: t("connector.setUp.createAccount.tooltip"),
827
+ enableTrading: t("connector.setUp.enableTrading.tooltip"),
828
+ wrongNetwork: t("connector.wrongNetwork.tooltip")
829
+ }
830
+ } = props;
831
+ const [open, setOpen] = react.useState(false);
832
+ const { state } = hooks.useAccount();
833
+ const isSupport = true;
834
+ const { wrongNetwork } = reactApp.useAppContext();
835
+ const hint = react.useMemo(() => {
836
+ if (wrongNetwork) {
837
+ return tooltip?.wrongNetwork;
838
+ }
839
+ switch (state.status) {
840
+ case types.AccountStatusEnum.NotConnected:
841
+ return tooltip?.connectWallet;
842
+ case types.AccountStatusEnum.NotSignedIn:
843
+ return tooltip?.signIn;
844
+ case types.AccountStatusEnum.DisabledTrading:
845
+ return tooltip?.enableTrading;
846
+ case types.AccountStatusEnum.EnableTrading: {
847
+ return "";
848
+ }
849
+ default:
850
+ return props.content;
851
+ }
852
+ }, [props.content, state, isSupport, tooltip]);
853
+ const newOpacity = react.useMemo(() => {
854
+ switch (state.status) {
855
+ case types.AccountStatusEnum.NotConnected:
856
+ case types.AccountStatusEnum.NotSignedIn:
857
+ return opactiy;
858
+ case types.AccountStatusEnum.EnableTrading: {
859
+ return void 0;
860
+ }
861
+ default:
862
+ return void 0;
863
+ }
864
+ }, [props.opactiy, state, isSupport]);
865
+ return /* @__PURE__ */ jsxRuntime.jsx(
866
+ ui.Tooltip,
867
+ {
868
+ open: hint ? open : false,
869
+ onOpenChange: setOpen,
870
+ content: hint,
871
+ className: "oui-text-2xs",
872
+ align: props.align,
873
+ alignOffset: props.alignOffset,
874
+ side: props.side,
875
+ sideOffset: props.sideOffset,
876
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { opacity: newOpacity }, children: props.children })
877
+ }
878
+ );
879
+ };
880
+ AuthGuardTooltip.displayName = "AuthGuardTooltip";
881
+ var AuthStatusEnum = /* @__PURE__ */ ((AuthStatusEnum2) => {
882
+ AuthStatusEnum2[AuthStatusEnum2["WrongNetwork"] = 0] = "WrongNetwork";
883
+ AuthStatusEnum2[AuthStatusEnum2["ConnectWallet"] = 1] = "ConnectWallet";
884
+ AuthStatusEnum2[AuthStatusEnum2["CreateAccount"] = 2] = "CreateAccount";
885
+ AuthStatusEnum2[AuthStatusEnum2["EnableTrading"] = 3] = "EnableTrading";
886
+ return AuthStatusEnum2;
887
+ })(AuthStatusEnum || {});
888
+ var useAuthStatus = () => {
889
+ const { state } = hooks.useAccount();
890
+ const { wrongNetwork, disabledConnect } = reactApp.useAppContext();
891
+ return react.useMemo(() => {
892
+ if (wrongNetwork && !disabledConnect) {
893
+ return 0 /* WrongNetwork */;
894
+ }
895
+ if (state.status === types.AccountStatusEnum.EnableTradingWithoutConnected) {
896
+ return 3 /* EnableTrading */;
897
+ }
898
+ if (state.status <= types.AccountStatusEnum.NotConnected || disabledConnect) {
899
+ return 1 /* ConnectWallet */;
900
+ }
901
+ if (state.status <= types.AccountStatusEnum.NotSignedIn) {
902
+ return 2 /* CreateAccount */;
903
+ }
904
+ return 3 /* EnableTrading */;
905
+ }, [state.status, wrongNetwork, disabledConnect]);
906
+ };
13
907
 
14
- exports.AuthGuard = A;
15
- exports.AuthGuardDataTable = St;
16
- exports.AuthGuardEmpty = he;
17
- exports.AuthGuardTooltip = ye;
18
- exports.AuthStatusEnum = Te;
19
- exports.WalletConnectContent = Z;
20
- exports.WalletConnectorModalId = D;
21
- exports.WalletConnectorSheetId = F;
22
- exports.WalletConnectorWidget = Q;
23
- exports.useAuthStatus = Dt;
24
- exports.useWalletConnectorBuilder = K;
908
+ exports.AuthGuard = AuthGuard;
909
+ exports.AuthGuardDataTable = AuthGuardDataTable;
910
+ exports.AuthGuardEmpty = AuthGuardEmpty;
911
+ exports.AuthGuardTooltip = AuthGuardTooltip;
912
+ exports.AuthStatusEnum = AuthStatusEnum;
913
+ exports.WalletConnectContent = WalletConnectContent;
914
+ exports.WalletConnectorModalId = WalletConnectorModalId;
915
+ exports.WalletConnectorSheetId = WalletConnectorSheetId;
916
+ exports.WalletConnectorWidget = WalletConnectorWidget;
917
+ exports.useAuthStatus = useAuthStatus;
918
+ exports.useWalletConnectorBuilder = useWalletConnectorBuilder;
25
919
  //# sourceMappingURL=out.js.map
26
920
  //# sourceMappingURL=index.js.map