UNPKG

@roochnetwork/rooch-sdk-kit

Version:
134 lines (121 loc) 4.07 kB
// src/hooks/useRemoveSession.ts import { useMutation } from "@tanstack/react-query"; // src/constants/roochMutationKeys.ts function formMutationKeyFn(baseEntity) { return function mutationKeyFn(additionalKeys = []) { return [{ ...roochMutationKeys.all, baseEntity }, ...additionalKeys]; }; } var roochMutationKeys = { all: { baseScope: "rooch" }, addNetwork: formMutationKeyFn("add-network"), switchNetwork: formMutationKeyFn("switch-network"), removeNetwork: formMutationKeyFn("remove-network"), removeSession: formMutationKeyFn("remove-session"), transferObject: formMutationKeyFn("transfer-object"), transferCoin: formMutationKeyFn("transfer-coin"), signAndExecuteTransaction: formMutationKeyFn("sign-and-execute-transaction") }; // src/hooks/client/useRoochContext.ts import { useContext as useContext2 } from "react"; // src/provider/clientProvider.tsx import { createContext as createContext2, useMemo, useState } from "react"; import { getRoochNodeUrl, RoochClient } from "@roochnetwork/rooch-sdk"; // src/hooks/useSessionsStore.ts import { useContext } from "react"; import { useStore } from "zustand"; // src/provider/roochProvider.tsx import { createContext, useRef } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; var RoochContext = createContext(null); // src/hooks/useSessionsStore.ts function useSessionStore(selector) { const store = useContext(RoochContext); if (!store) { throw new Error( "Could not find RoochSessionContext. Ensure that you have set up the RoochClientProvider." ); } return useStore(store, selector); } // src/provider/clientProvider.tsx import { jsx as jsx2 } from "react/jsx-runtime"; var ClientContext = createContext2(null); var DEFAULT_NETWORKS = { localnet: { url: getRoochNodeUrl("localnet") } }; // src/hooks/client/useRoochContext.ts function useRoochContext() { const context = useContext2(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/hooks/useCurrentSession.ts function useCurrentSession() { return useSessionStore((state) => state.currentSession); } // src/hooks/useSessions.ts function useSessions() { return useSessionStore( (state) => state.sessions.sort((a, b) => b.getCreateTime() - a.getCreateTime()) ); } // src/hooks/useRemoveSession.ts function useRemoveSession({ mutationKey, ...mutationOptions } = {}) { const sessionsKeys = useSessions(); const removeSession = useSessionStore((state) => state.removeSession); const setCurrentSession = useSessionStore((state) => state.setCurrentSession); const currentSession = useCurrentSession(); const client = useRoochClient(); return useMutation({ mutationKey: roochMutationKeys.removeSession(mutationKey), mutationFn: async (args) => { try { if (!currentSession) { return; } const result = await client.removeSession({ authKey: args.authKey, signer: currentSession }); if (result) { let cacheSession = sessionsKeys.find( (item) => item.getAuthKey() === args.authKey ); if (cacheSession) { removeSession(cacheSession); if (cacheSession.getAuthKey() === currentSession?.getAuthKey()) { const substitute = sessionsKeys.filter((item) => item.getAuthKey() !== cacheSession.getAuthKey()).sort((a, b) => b.getCreateTime() - a.getCreateTime()).filter((item) => !item.isSessionExpired()); if (substitute.length > 0) { setCurrentSession(substitute[0]); } else { setCurrentSession(void 0); } } } } } catch (e) { throw e; } }, ...mutationOptions }); } export { useRemoveSession }; //# sourceMappingURL=useRemoveSession.js.map