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 21:55:26 +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) {
|
|
|
|
|
const signer = await account.plebbit.createSigner();
|
|
|
|
|
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];
|
|
|
|
|
const signer = await account.plebbit.createSigner({privateKey: signerPrivateKey});
|
|
|
|
|
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;
|