diff --git a/src/components/AccountList/index.tsx b/src/components/AccountList/index.tsx index 3db97207..2385c397 100644 --- a/src/components/AccountList/index.tsx +++ b/src/components/AccountList/index.tsx @@ -1,10 +1,11 @@ import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' import { isSameAccount } from '@/lib/account' import { formatPubkey } from '@/lib/pubkey' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import { TAccountPointer, TSignerType } from '@/types' -import { Loader } from 'lucide-react' +import { Loader, Trash2 } from 'lucide-react' import { useState } from 'react' import { SimpleUserAvatar } from '../UserAvatar' import { SimpleUsername } from '../Username' @@ -16,7 +17,7 @@ export default function AccountList({ className?: string afterSwitch: () => void }) { - const { accounts, account, switchAccount } = useNostr() + const { accounts, account, switchAccount, removeAccount } = useNostr() const [switchingAccount, setSwitchingAccount] = useState(null) return ( @@ -46,8 +47,21 @@ export default function AccountList({ -
- +
+
+ +
+
{switchingAccount && isSameAccount(act, switchingAccount) && ( diff --git a/src/providers/NostrProvider/index.tsx b/src/providers/NostrProvider/index.tsx index 2674b5ce..75061c42 100644 --- a/src/providers/NostrProvider/index.tsx +++ b/src/providers/NostrProvider/index.tsx @@ -80,6 +80,9 @@ export const useNostr = () => { export function NostrProvider({ children }: { children: React.ReactNode }) { const { t } = useTranslation() + const [accounts, setAccounts] = useState( + storage.getAccounts().map((act) => ({ pubkey: act.pubkey, signerType: act.signerType })) + ) const [account, setAccount] = useState(null) const [nsec, setNsec] = useState(null) const [ncryptsec, setNcryptsec] = useState(null) @@ -300,7 +303,8 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { } const login = (signer: ISigner, act: TAccount) => { - storage.addAccount(act) + const newAccounts = storage.addAccount(act) + setAccounts(newAccounts) storage.switchAccount(act) setAccount({ pubkey: act.pubkey, signerType: act.signerType }) setSigner(signer) @@ -308,7 +312,8 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { } const removeAccount = (act: TAccountPointer) => { - storage.removeAccount(act) + const newAccounts = storage.removeAccount(act) + setAccounts(newAccounts) if (account?.pubkey === act.pubkey) { setAccount(null) setSigner(null) @@ -652,9 +657,7 @@ export function NostrProvider({ children }: { children: React.ReactNode }) { favoriteRelaysEvent, notificationsSeenAt, account, - accounts: storage - .getAccounts() - .map((act) => ({ pubkey: act.pubkey, signerType: act.signerType })), + accounts, nsec, ncryptsec, switchAccount, diff --git a/src/services/local-storage.service.ts b/src/services/local-storage.service.ts index bce065cd..f5154e04 100644 --- a/src/services/local-storage.service.ts +++ b/src/services/local-storage.service.ts @@ -187,12 +187,13 @@ class LocalStorageService { this.accounts.push(account) } window.localStorage.setItem(StorageKey.ACCOUNTS, JSON.stringify(this.accounts)) - return account + return this.accounts } removeAccount(account: TAccount) { this.accounts = this.accounts.filter((act) => !isSameAccount(act, account)) window.localStorage.setItem(StorageKey.ACCOUNTS, JSON.stringify(this.accounts)) + return this.accounts } switchAccount(account: TAccount | null) {