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

32 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-06-16 13:48:40 +02:00
import { useEffect } from "react";
import { createAccount, setActiveAccount, useAccounts } from "@plebbit/plebbit-react-hooks";
2023-06-16 15:28:03 +02:00
import useAnonModeStore from "./stores/useAnonModeStore";
2023-06-16 13:48:40 +02:00
const useAnonMode = (threadCid, execute) => {
const {accounts} = useAccounts();
2023-06-16 15:28:03 +02:00
const { anonymousMode } = useAnonModeStore();
2023-06-16 13:48:40 +02:00
useEffect(() => {
const handleAnonMode = async () => {
let storedAccounts = JSON.parse(localStorage.getItem('storedAccounts')) || {};
2023-06-16 15:28:03 +02:00
if (!anonymousMode) return;
2023-06-16 13:48:40 +02:00
if (!storedAccounts[threadCid] && execute) {
await createAccount();
const lastAccount = accounts[accounts.length - 1];
await setActiveAccount(lastAccount.name);
storedAccounts[threadCid] = lastAccount.name;
localStorage.setItem('storedAccounts', JSON.stringify(storedAccounts));
} else {
await setActiveAccount(storedAccounts[threadCid]);
}
}
handleAnonMode();
2023-06-16 15:28:03 +02:00
}, [threadCid, execute, accounts, anonymousMode]);
2023-06-16 13:48:40 +02:00
return;
}
export default useAnonMode;