Files
5chan/src/hooks/useAnonModeRef.js
T

32 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-06-17 21:55:20 +02:00
import { useEffect } from "react";
2023-06-26 21:55:26 +02:00
import { setActiveAccount, useAccount } from "@plebbit/plebbit-react-hooks";
2023-06-17 21:55:20 +02:00
import useAnonModeStore from "./stores/useAnonModeStore";
const useAnonModeRef = (threadCidRef, execute) => {
2023-06-26 22:07:32 +02:00
const account = useAccount();
2023-06-17 21:55:20 +02:00
const { anonymousMode } = useAnonModeStore();
useEffect(() => {
const handleAnonMode = async () => {
2023-06-26 21:55:26 +02:00
let storedSigners = JSON.parse(localStorage.getItem('storedSigners')) || {};
2023-06-17 21:55:20 +02:00
if (!anonymousMode) return;
2023-06-26 21:55:26 +02:00
if (!storedSigners[threadCidRef] && execute) {
2023-06-26 22:07:32 +02:00
const signer = await account?.plebbit.createSigner();
2023-06-26 21:55:26 +02:00
storedSigners[threadCidRef] = signer.privateKey;
2023-06-17 21:55:20 +02:00
2023-06-26 21:55:26 +02:00
localStorage.setItem('storedSigners', JSON.stringify(storedSigners));
2023-06-17 21:55:20 +02:00
} else {
2023-06-26 21:55:26 +02:00
const signerPrivateKey = storedSigners[threadCidRef];
2023-06-26 22:07:32 +02:00
const signer = await account?.plebbit.createSigner({privateKey: signerPrivateKey});
2023-06-26 21:55:26 +02:00
await setActiveAccount(account, {signer});
2023-06-17 21:55:20 +02:00
}
}
handleAnonMode();
2023-06-26 21:55:26 +02:00
}, [threadCidRef, execute, account, anonymousMode]);
2023-06-17 21:55:20 +02:00
return;
}
export default useAnonModeRef;