@roochnetwork/rooch-sdk-kit
Version:
Rooch SDK Kit
152 lines (138 loc) • 5.1 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/hooks/useRemoveSession.ts
var useRemoveSession_exports = {};
__export(useRemoveSession_exports, {
useRemoveSession: () => useRemoveSession
});
module.exports = __toCommonJS(useRemoveSession_exports);
var import_react_query = require("@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
var import_react4 = require("react");
// src/provider/clientProvider.tsx
var import_react3 = require("react");
var import_rooch_sdk = require("@roochnetwork/rooch-sdk");
// src/hooks/useSessionsStore.ts
var import_react2 = require("react");
var import_zustand = require("zustand");
// src/provider/roochProvider.tsx
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var RoochContext = (0, import_react.createContext)(null);
// src/hooks/useSessionsStore.ts
function useSessionStore(selector) {
const store = (0, import_react2.useContext)(RoochContext);
if (!store) {
throw new Error(
"Could not find RoochSessionContext. Ensure that you have set up the RoochClientProvider."
);
}
return (0, import_zustand.useStore)(store, selector);
}
// src/provider/clientProvider.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var ClientContext = (0, import_react3.createContext)(null);
var DEFAULT_NETWORKS = {
localnet: { url: (0, import_rooch_sdk.getRoochNodeUrl)("localnet") }
};
// src/hooks/client/useRoochContext.ts
function useRoochContext() {
const context = (0, import_react4.useContext)(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 (0, import_react_query.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
});
}
//# sourceMappingURL=useRemoveSession.js.map