mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
implement signers in anon mode
This commit is contained in:
+14
-24
@@ -1,38 +1,28 @@
|
||||
import { useEffect } from "react";
|
||||
import { createAccount, setActiveAccount, useAccounts } from "@plebbit/plebbit-react-hooks";
|
||||
import { useAccount } from "@plebbit/plebbit-react-hooks";
|
||||
import useAnonModeStore from "./stores/useAnonModeStore";
|
||||
|
||||
const useAnonMode = (threadCid, execute) => {
|
||||
const { accounts } = useAccounts();
|
||||
const { account } = useAccount();
|
||||
const { anonymousMode } = useAnonModeStore();
|
||||
|
||||
useEffect(() => {
|
||||
const handleAnonMode = async () => {
|
||||
let storedAccounts = JSON.parse(localStorage.getItem('storedAccounts')) || {};
|
||||
useEffect(() => {
|
||||
const handleAnonMode = async () => {
|
||||
let storedSigners = JSON.parse(localStorage.getItem('storedSigners')) || {};
|
||||
|
||||
if (!anonymousMode) return;
|
||||
|
||||
if (!storedAccounts[threadCid] && execute) {
|
||||
await createAccount();
|
||||
|
||||
if(accounts.length === 0) {
|
||||
console.log("No accounts available");
|
||||
return;
|
||||
}
|
||||
|
||||
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]);
|
||||
if (!anonymousMode) {
|
||||
if (execute && storedSigners[threadCid]) {
|
||||
const signerPrivateKey = storedSigners[threadCid];
|
||||
await account.plebbit.createSigner({privateKey: signerPrivateKey});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleAnonMode();
|
||||
}, [threadCid, execute, account, anonymousMode]);
|
||||
|
||||
handleAnonMode();
|
||||
}, [threadCid, execute, accounts, anonymousMode]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
export default useAnonMode;
|
||||
+11
-11
@@ -1,31 +1,31 @@
|
||||
import { useEffect } from "react";
|
||||
import { createAccount, setActiveAccount, useAccounts } from "@plebbit/plebbit-react-hooks";
|
||||
import { setActiveAccount, useAccount } from "@plebbit/plebbit-react-hooks";
|
||||
import useAnonModeStore from "./stores/useAnonModeStore";
|
||||
|
||||
const useAnonModeRef = (threadCidRef, execute) => {
|
||||
const {accounts} = useAccounts();
|
||||
const { account } = useAccount();
|
||||
const { anonymousMode } = useAnonModeStore();
|
||||
|
||||
useEffect(() => {
|
||||
const handleAnonMode = async () => {
|
||||
let storedAccounts = JSON.parse(localStorage.getItem('storedAccounts')) || {};
|
||||
let storedSigners = JSON.parse(localStorage.getItem('storedSigners')) || {};
|
||||
|
||||
if (!anonymousMode) return;
|
||||
|
||||
if (!storedAccounts[threadCidRef.current] && execute) {
|
||||
await createAccount();
|
||||
const lastAccount = accounts[accounts.length - 1];
|
||||
await setActiveAccount(lastAccount.name);
|
||||
storedAccounts[threadCidRef.current] = lastAccount.name;
|
||||
if (!storedSigners[threadCidRef] && execute) {
|
||||
const signer = await account.plebbit.createSigner();
|
||||
storedSigners[threadCidRef] = signer.privateKey;
|
||||
|
||||
localStorage.setItem('storedAccounts', JSON.stringify(storedAccounts));
|
||||
localStorage.setItem('storedSigners', JSON.stringify(storedSigners));
|
||||
} else {
|
||||
await setActiveAccount(storedAccounts[threadCidRef.current]);
|
||||
const signerPrivateKey = storedSigners[threadCidRef];
|
||||
const signer = await account.plebbit.createSigner({privateKey: signerPrivateKey});
|
||||
await setActiveAccount(account, {signer});
|
||||
}
|
||||
}
|
||||
|
||||
handleAnonMode();
|
||||
}, [threadCidRef, execute, accounts, anonymousMode]);
|
||||
}, [threadCidRef, execute, account, anonymousMode]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user