import './settings.css'; import { Menu, MenuDivider, MenuItem } from '@szhsin/react-menu'; import { useReducer, useState } from 'preact/hooks'; import Avatar from '../components/avatar'; import Icon from '../components/icon'; import Link from '../components/link'; import NameText from '../components/name-text'; import { api } from '../utils/api'; import states from '../utils/states'; import store from '../utils/store'; function Accounts({ onClose }) { const { masto } = api(); // Accounts const accounts = store.local.getJSON('accounts'); const currentAccount = store.session.get('currentAccount'); const moreThanOneAccount = accounts.length > 1; const [currentDefault, setCurrentDefault] = useState(0); const [_, reload] = useReducer((x) => x + 1, 0); return (

Accounts

    {accounts.map((account, i) => { const isCurrent = account.info.id === currentAccount; const isDefault = i === (currentDefault || 0); return (
  • {moreThanOneAccount && ( )} { if (isCurrent) { try { const info = await masto.v1.accounts.fetch( account.info.id, ); console.log('fetched account info', info); account.info = info; store.local.setJSON('accounts', accounts); reload(); } catch (e) {} } }} /> { if (isCurrent) { states.showAccount = `${account.info.username}@${account.instanceURL}`; } else { store.session.set('currentAccount', account.info.id); location.reload(); } }} />
    {isDefault && moreThanOneAccount && ( <> Default{' '} )} } > { states.showAccount = `${account.info.username}@${account.instanceURL}`; }} > View profile… {moreThanOneAccount && ( { // Move account to the top of the list accounts.splice(i, 1); accounts.unshift(account); store.local.setJSON('accounts', accounts); setCurrentDefault(i); }} > Set as default )} { const yes = confirm('Log out?'); if (!yes) return; accounts.splice(i, 1); store.local.setJSON('accounts', accounts); // location.reload(); location.href = '/'; }} > Log out…
  • ); })}

Add an existing account

{moreThanOneAccount && (

Note: Default account will always be used for first load. Switched accounts will persist during the session.

)}
); } export default Accounts;