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

38 lines
1.1 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) => {
2023-06-20 14:39:01 +02:00
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();
2023-06-20 14:39:01 +02:00
if(accounts.length === 0) {
console.log("No accounts available");
return;
}
2023-06-16 13:48:40 +02:00
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;