1import type { Handle } from '@atcute/lexicons';
2import { writable } from 'svelte/store';
3import { hashColor } from './theme';
4import type { AtprotoDid } from '@atcute/lexicons/syntax';
5
6export type Account = {
7 did: AtprotoDid;
8 handle: Handle | null;
9};
10
11let _accounts: Account[] = [];
12export const accounts = (() => {
13 const raw = localStorage.getItem('accounts');
14 _accounts = raw ? JSON.parse(raw) : [];
15 const store = writable<Account[]>(_accounts);
16 store.subscribe((accounts) => {
17 _accounts = accounts;
18 localStorage.setItem('accounts', JSON.stringify(accounts));
19 });
20 return store;
21})();
22
23export const addAccount = (account: Account): void => {
24 accounts.update((accounts) => [...accounts.filter((a) => a.did !== account.did), account]);
25};
26
27export const loggingIn = {
28 set: (account: Account | null) => {
29 if (!account) {
30 localStorage.removeItem('loggingIn');
31 } else {
32 localStorage.setItem('loggingIn', JSON.stringify(account));
33 }
34 },
35 get: (): Account | null => {
36 const raw = localStorage.getItem('loggingIn');
37 return raw ? JSON.parse(raw) : null;
38 }
39};
40
41export const generateColorForDid = (did: string) => hashColor(did);