@roochnetwork/rooch-sdk-kit
Version:
Rooch SDK Kit
387 lines (355 loc) • 15.2 kB
JavaScript
// src/components/session-modal/views/WhatIsASessionView.tsx
import { useEffect as useEffect2, useState as useState3 } from "react";
import { toShortStr } from "@roochnetwork/rooch-sdk";
// src/components/ui/Text.tsx
import { Slot } from "@radix-ui/react-slot";
import clsx from "clsx";
import { forwardRef } from "react";
// src/components/ui/Text.css.ts
import { createRuntimeFn as _7a468 } from "@vanilla-extract/recipes/createRuntimeFn";
var textVariants = _7a468({ defaultClassName: "Text__1utg4wj0", variantClassNames: { size: { sm: "Text_textVariants_size_sm__1utg4wj1" }, weight: { normal: "Text_textVariants_weight_normal__1utg4wj2", medium: "Text_textVariants_weight_medium__1utg4wj3", bold: "Text_textVariants_weight_bold__1utg4wj4" }, color: { muted: "Text_textVariants_color_muted__1utg4wj5", danger: "Text_textVariants_color_danger__1utg4wj6", warning: "Text_textVariants_color_warning__1utg4wj7" }, mono: { true: "Text_textVariants_mono_true__1utg4wj8" } }, defaultVariants: { size: "sm", weight: "normal" }, compoundVariants: [] });
// src/components/ui/Text.tsx
import { jsx } from "react/jsx-runtime";
var Text = forwardRef(
({
children,
className,
asChild = false,
as: Tag = "div",
size,
weight,
color,
mono,
...textProps
}, forwardedRef) => {
return /* @__PURE__ */ jsx(
Slot,
{
...textProps,
ref: forwardedRef,
className: clsx(textVariants({ size, weight, color, mono }), className),
children: asChild ? children : /* @__PURE__ */ jsx(Tag, { children })
}
);
}
);
Text.displayName = "Text";
// src/components/ui/Button.tsx
import { Slot as Slot2 } from "@radix-ui/react-slot";
import clsx2 from "clsx";
import { forwardRef as forwardRef2 } from "react";
// src/components/ui/Button.css.ts
import { createRuntimeFn as _7a4682 } from "@vanilla-extract/recipes/createRuntimeFn";
var buttonVariants = _7a4682({ defaultClassName: "Button_buttonVariants__1vdhm2g0", variantClassNames: { variant: { primary: "Button_buttonVariants_variant_primary__1vdhm2g1", outline: "Button_buttonVariants_variant_outline__1vdhm2g2" }, size: { md: "Button_buttonVariants_size_md__1vdhm2g3", lg: "Button_buttonVariants_size_lg__1vdhm2g4" } }, defaultVariants: { variant: "primary", size: "md" }, compoundVariants: [] });
// src/components/ui/Button.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
var Button = forwardRef2(
({ className, variant, size, asChild = false, ...props }, forwardedRef) => {
const Comp = asChild ? Slot2 : "button";
return /* @__PURE__ */ jsx2(
Comp,
{
...props,
className: clsx2(buttonVariants({ variant, size }), className),
ref: forwardedRef
}
);
}
);
Button.displayName = "Button";
// src/components/ui/Heading.tsx
import { Slot as Slot3 } from "@radix-ui/react-slot";
import clsx3 from "clsx";
import { forwardRef as forwardRef3 } from "react";
// src/components/ui/Heading.css.ts
import { createRuntimeFn as _7a4683 } from "@vanilla-extract/recipes/createRuntimeFn";
var headingVariants = _7a4683({ defaultClassName: "Heading__1v58k1t0", variantClassNames: { size: { sm: "Heading_headingVariants_size_sm__1v58k1t1", md: "Heading_headingVariants_size_md__1v58k1t2", lg: "Heading_headingVariants_size_lg__1v58k1t3", xl: "Heading_headingVariants_size_xl__1v58k1t4" }, weight: { normal: "Heading_headingVariants_weight_normal__1v58k1t5", bold: "Heading_headingVariants_weight_bold__1v58k1t6" }, truncate: { true: "Heading_headingVariants_truncate_true__1v58k1t7" } }, defaultVariants: { size: "lg", weight: "bold" }, compoundVariants: [] });
// src/components/ui/Heading.tsx
import { jsx as jsx3 } from "react/jsx-runtime";
var Heading = forwardRef3(
({
children,
className,
asChild = false,
as: Tag = "h1",
size,
weight,
truncate,
...headingProps
}, forwardedRef) => {
return /* @__PURE__ */ jsx3(
Slot3,
{
...headingProps,
ref: forwardedRef,
className: clsx3(headingVariants({ size, weight, truncate }), className),
children: asChild ? children : /* @__PURE__ */ jsx3(Tag, { children })
}
);
}
);
Heading.displayName = "Heading";
// src/components/session-modal/views/WhatIsASessionView.css.ts
var actionButtonContainer = "WhatIsASessionView_actionButtonContainer__9cleo57";
var createSessionContainer = "WhatIsASessionView_createSessionContainer__9cleo53";
var createSessionContent = "WhatIsASessionView_createSessionContent__9cleo54";
var createSessionScopeContent = "WhatIsASessionView_createSessionScopeContent__9cleo55";
var createSessionStatus = "WhatIsASessionView_createSessionStatus__9cleo56";
var moreInfo = "WhatIsASessionView_moreInfo__9cleo58";
var sessionItemContent = "WhatIsASessionView_sessionItemContent__9cleo59";
var whatContent = "WhatIsASessionView_whatContent__9cleo51";
var whatMoreContent = "WhatIsASessionView_whatMoreContent__9cleo52";
// src/components/ProgressProvider.tsx
import { createContext, useCallback, useContext, useState } from "react";
import { jsx as jsx4, jsxs } from "react/jsx-runtime";
var ProgressProviderContext = createContext(null);
var useProgress = () => {
const ctx = useContext(ProgressProviderContext);
if (!ctx) {
throw new Error("useSubscribeToError must be used within a GlobalProvider");
}
return ctx;
};
// src/hooks/client/useRoochContext.ts
import { useContext as useContext5 } from "react";
// src/provider/clientProvider.tsx
import { createContext as createContext5, useMemo, useState as useState2 } from "react";
import {
getRoochNodeUrl,
RoochClient
} from "@roochnetwork/rooch-sdk";
// src/hooks/useSessionsStore.ts
import { useContext as useContext4 } from "react";
import { useStore as useStore2 } from "zustand";
// src/provider/roochProvider.tsx
import { createContext as createContext3, useRef } from "react";
// src/provider/globalProvider.tsx
import { createContext as createContext2, useContext as useContext2 } from "react";
import { jsx as jsx5 } from "react/jsx-runtime";
var GlobalContext = createContext2(null);
var useTriggerError = () => {
const ctx = useContext2(GlobalContext);
if (!ctx) {
throw new Error("useTriggerError must be used within a GlobalProvider");
}
return ctx.triggerError;
};
// src/provider/roochProvider.tsx
import { jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
var RoochContext = createContext3(null);
// src/provider/walletProvider.tsx
import { useCallback as useCallback2, createContext as createContext4, useEffect, useRef as useRef2 } from "react";
import { BitcoinAddress } from "@roochnetwork/rooch-sdk";
// src/hooks/wallet/useWalletStore.ts
import { useContext as useContext3 } from "react";
import { useStore } from "zustand";
function useWalletStore(selector) {
const store = useContext3(WalletContext);
if (!store) {
throw new Error("Could not find WalletContext. Ensure that you have set up the WalletProvider.");
}
return useStore(store, selector);
}
// src/provider/walletProvider.tsx
import { jsx as jsx7 } from "react/jsx-runtime";
var WalletContext = createContext4(null);
// src/hooks/useSessionsStore.ts
function useSessionStore(selector) {
const store = useContext4(RoochContext);
if (!store) {
throw new Error(
"Could not find RoochSessionContext. Ensure that you have set up the RoochClientProvider."
);
}
return useStore2(store, selector);
}
// src/provider/clientProvider.tsx
import { jsx as jsx8 } from "react/jsx-runtime";
var ClientContext = createContext5(null);
var DEFAULT_NETWORKS = {
localnet: { url: getRoochNodeUrl("localnet") }
};
// src/hooks/client/useRoochContext.ts
function useRoochContext() {
const context = useContext5(ClientContext);
if (!context) {
throw new Error(
"Could not find RoochClientContext. Ensure that you have set up the RoochClientProvider."
);
}
return context;
}
// src/hooks/client/useRoochClient.ts
function useRoochClient() {
return useRoochContext().client;
}
// src/constants/walletMutationKeys.ts
function formMutationKeyFn(baseEntity) {
return function mutationKeyFn(additionalKeys = []) {
return [{ ...walletMutationKeys.all, baseEntity }, ...additionalKeys];
};
}
var walletMutationKeys = {
all: { baseScope: "wallet" },
connectWallet: formMutationKeyFn("connect-wallet"),
autoConnectWallet: formMutationKeyFn("auto-connect-wallet"),
switchAccount: formMutationKeyFn("switch-account"),
createSessionKey: formMutationKeyFn("create-session-key")
};
// src/hooks/wallet/useCurrentWallet.ts
function useCurrentWallet() {
const currentWallet = useWalletStore((state) => state.currentWallet);
const connectionStatus = useWalletStore((state) => state.connectionStatus);
switch (connectionStatus) {
case "connecting":
return {
status: connectionStatus,
wallet: currentWallet,
isDisconnected: false,
isConnecting: true,
isConnected: false
};
case "disconnected":
return {
status: connectionStatus,
wallet: currentWallet,
isDisconnected: true,
isConnecting: false,
isConnected: false
};
case "connected": {
return {
status: connectionStatus,
wallet: currentWallet,
isDisconnected: false,
isConnecting: false,
isConnected: true
};
}
}
}
// src/hooks/useCreateSession.ts
import { useMutation } from "@tanstack/react-query";
// src/error/walletErrors.ts
var WalletNotConnectedError = class extends Error {
};
// src/hooks/useCreateSession.ts
function useCreateSessionKey({
mutationKey,
...mutationOptions
} = {}) {
const client = useRoochClient();
const { wallet } = useCurrentWallet();
const setCurrentSession = useSessionStore((state) => state.setCurrentSession);
const triggerError = useTriggerError();
return useMutation({
mutationKey: walletMutationKeys.createSessionKey(mutationKey),
mutationFn: async (args) => {
const signer = args.signer || wallet;
if (!signer) {
throw new WalletNotConnectedError("No wallet is connected.");
}
try {
const sessionAccount = await client.createSession({
signer,
sessionArgs: args
});
setCurrentSession(sessionAccount);
return sessionAccount;
} catch (error) {
if ("code" in error && "message" in error) {
triggerError(error);
}
throw error;
}
},
...mutationOptions
});
}
// src/utils/time.ts
import dayjs from "dayjs";
function getUTCOffset() {
const date = /* @__PURE__ */ new Date();
const offset = -date.getTimezoneOffset();
const hours = Math.floor(offset / 60);
const minutes = offset % 60;
return `UTC ${hours >= 0 ? "+" : "-"}${hours}:${minutes < 10 ? "0" : ""}${minutes}`;
}
var unix2str = (input) => {
const timestampInSeconds = input > 1e12 ? input / 1e3 : input;
return `${dayjs.unix(timestampInSeconds).format("MMM DD, YYYY HH:mm:ss")}`;
};
// src/components/session-modal/views/WhatIsASessionView.tsx
import { jsx as jsx9, jsxs as jsxs3 } from "react/jsx-runtime";
function WhatIsASessionView({ getTitle }) {
const { start, finish } = useProgress();
const { mutateAsync, isError } = useCreateSessionKey();
const _sessionConf = useSessionStore((state) => state.sessionConf);
const [model, setModel] = useState3("what");
useEffect2(() => {
getTitle(model === "what" ? "What is a session" : "Create session");
}, [getTitle, model]);
const createSession = () => {
if (!_sessionConf) {
return;
}
setModel("create");
start();
mutateAsync({ ..._sessionConf }).finally(() => finish());
};
return model === "what" ? /* @__PURE__ */ jsxs3("div", { className: whatContent, children: [
/* @__PURE__ */ jsx9(Heading, { as: "h2", children: "What is a Session" }),
/* @__PURE__ */ jsxs3("div", { className: whatContent, children: [
/* @__PURE__ */ jsx9(Text, { weight: "medium", color: "muted", children: "Rooch's Session Key is a temporary key that facilitates users to interact with the chain." }),
/* @__PURE__ */ jsx9(Text, { weight: "medium", color: "muted", children: "When interacting with Rooch applications, each application generates a session key. It has an expiration time and will become invalid if there is no interaction for a long time." }),
/* @__PURE__ */ jsx9("div", { className: whatMoreContent, children: /* @__PURE__ */ jsx9(Text, { className: moreInfo, children: /* @__PURE__ */ jsx9(
"a",
{
href: "https://rooch.network/learn/core-concepts/accounts/session-key",
target: "_blank",
rel: "noreferrer",
children: "More Info"
}
) }) })
] }),
_sessionConf && /* @__PURE__ */ jsx9("div", { className: actionButtonContainer, children: /* @__PURE__ */ jsx9(Button, { type: "button", variant: "outline", onClick: createSession, children: "Create" }) })
] }) : /* @__PURE__ */ jsxs3("div", { className: createSessionContainer, children: [
/* @__PURE__ */ jsx9(Heading, { as: "h2", children: "Info" }),
/* @__PURE__ */ jsxs3("div", { className: createSessionContent, children: [
/* @__PURE__ */ jsx9(Info, { name: "App name", value: _sessionConf.appName }),
/* @__PURE__ */ jsx9(Info, { name: "App url", value: _sessionConf.appUrl }),
/* @__PURE__ */ jsx9(
Info,
{
name: "Expiration Interval",
value: _sessionConf.maxInactiveInterval ? _sessionConf.maxInactiveInterval * 1e3 + Date.now() : 0
}
),
/* @__PURE__ */ jsxs3("div", { className: createSessionScopeContent, children: [
/* @__PURE__ */ jsx9(Heading, { as: "h3", size: "sm", weight: "normal", children: "Scope" }),
_sessionConf.scopes.slice(0, 3).map(
(item) => typeof item === "string" ? item : `${item.address}::${item.module}::${item.function}`
).sort((a, b) => b.length - a.length).map((item) => {
const _tmp = item.split("::");
return /* @__PURE__ */ jsx9(Text, { children: _tmp[0].length > 3 ? toShortStr(item, {
start: 12,
end: 10 + _tmp[1].length + _tmp[2].length
}) : item }, item);
})
] }),
/* @__PURE__ */ jsx9("div", { className: createSessionStatus, children: isError ? /* @__PURE__ */ jsx9(Text, { color: "danger", children: "Create failed" }) : /* @__PURE__ */ jsx9(Text, { color: "muted", children: "Confirm sign in the wallet..." }) }),
isError ? /* @__PURE__ */ jsx9("div", { className: actionButtonContainer, children: /* @__PURE__ */ jsx9(Button, { type: "button", variant: "outline", onClick: createSession, children: "Retry Create" }) }) : null
] })
] });
}
function Info({ name, value }) {
return /* @__PURE__ */ jsxs3("div", { className: sessionItemContent, children: [
/* @__PURE__ */ jsx9(Text, { color: "danger", style: { width: "130px" }, children: name }),
/* @__PURE__ */ jsx9(Text, { color: "muted", children: typeof value === "number" ? `${unix2str(value)} (${getUTCOffset()})` : value })
] });
}
export {
Info,
WhatIsASessionView
};
//# sourceMappingURL=WhatIsASessionView.js.map