2023-07-16 04:35:54 +02:00
|
|
|
import './accounts.css';
|
2023-03-07 17:32:33 +01:00
|
|
|
|
2023-10-23 02:42:40 +02:00
|
|
|
import { useAutoAnimate } from '@formkit/auto-animate/preact';
|
2023-03-23 02:51:52 +01:00
|
|
|
import { Menu, MenuDivider, MenuItem } from '@szhsin/react-menu';
|
2023-10-22 17:09:04 +02:00
|
|
|
import { useReducer } from 'preact/hooks';
|
2023-03-07 17:32:33 +01:00
|
|
|
|
|
|
|
import Avatar from '../components/avatar';
|
|
|
|
import Icon from '../components/icon';
|
|
|
|
import Link from '../components/link';
|
2023-11-18 14:11:07 +01:00
|
|
|
import Menu2 from '../components/menu2';
|
2023-07-17 15:01:00 +02:00
|
|
|
import MenuConfirm from '../components/menu-confirm';
|
2023-03-07 17:32:33 +01:00
|
|
|
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 [_, reload] = useReducer((x) => x + 1, 0);
|
2023-10-23 02:42:40 +02:00
|
|
|
const [accountsListParent] = useAutoAnimate();
|
2023-03-07 17:32:33 +01:00
|
|
|
|
|
|
|
return (
|
2023-07-16 04:35:54 +02:00
|
|
|
<div id="accounts-container" class="sheet" tabIndex="-1">
|
2023-04-20 10:10:57 +02:00
|
|
|
{!!onClose && (
|
|
|
|
<button type="button" class="sheet-close" onClick={onClose}>
|
|
|
|
<Icon icon="x" />
|
|
|
|
</button>
|
|
|
|
)}
|
2023-03-07 17:32:33 +01:00
|
|
|
<header class="header-grid">
|
|
|
|
<h2>Accounts</h2>
|
|
|
|
</header>
|
|
|
|
<main>
|
|
|
|
<section>
|
2023-10-23 02:42:40 +02:00
|
|
|
<ul class="accounts-list" ref={accountsListParent}>
|
2023-03-07 17:32:33 +01:00
|
|
|
{accounts.map((account, i) => {
|
|
|
|
const isCurrent = account.info.id === currentAccount;
|
2023-10-22 17:09:04 +02:00
|
|
|
const isDefault = i === 0; // first account is always default
|
2023-03-07 17:32:33 +01:00
|
|
|
return (
|
2023-10-22 17:09:04 +02:00
|
|
|
<li key={account.info.id}>
|
2023-03-07 17:32:33 +01:00
|
|
|
<div>
|
|
|
|
{moreThanOneAccount && (
|
|
|
|
<span class={`current ${isCurrent ? 'is-current' : ''}`}>
|
|
|
|
<Icon icon="check-circle" alt="Current" />
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
<Avatar
|
|
|
|
url={account.info.avatarStatic}
|
|
|
|
size="xxl"
|
|
|
|
onDblClick={async () => {
|
|
|
|
if (isCurrent) {
|
|
|
|
try {
|
2023-10-12 06:48:09 +02:00
|
|
|
const info = await masto.v1.accounts
|
|
|
|
.$select(account.info.id)
|
|
|
|
.fetch();
|
2023-03-07 17:32:33 +01:00
|
|
|
console.log('fetched account info', info);
|
|
|
|
account.info = info;
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
reload();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<NameText
|
2023-07-14 08:46:57 +02:00
|
|
|
account={
|
|
|
|
moreThanOneAccount
|
|
|
|
? {
|
|
|
|
...account.info,
|
|
|
|
acct: /@/.test(account.info.acct)
|
|
|
|
? account.info.acct
|
|
|
|
: `${account.info.acct}@${account.instanceURL}`,
|
|
|
|
}
|
|
|
|
: account.info
|
|
|
|
}
|
2023-03-07 17:32:33 +01:00
|
|
|
showAcct
|
|
|
|
onClick={() => {
|
2023-03-23 02:51:52 +01:00
|
|
|
if (isCurrent) {
|
|
|
|
states.showAccount = `${account.info.username}@${account.instanceURL}`;
|
|
|
|
} else {
|
|
|
|
store.session.set('currentAccount', account.info.id);
|
|
|
|
location.reload();
|
|
|
|
}
|
2023-03-07 17:32:33 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="actions">
|
|
|
|
{isDefault && moreThanOneAccount && (
|
|
|
|
<>
|
|
|
|
<span class="tag">Default</span>{' '}
|
|
|
|
</>
|
|
|
|
)}
|
2023-11-18 14:11:07 +01:00
|
|
|
<Menu2
|
2023-03-07 17:32:33 +01:00
|
|
|
align="end"
|
|
|
|
menuButton={
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
title="More"
|
|
|
|
class="plain more-button"
|
|
|
|
>
|
|
|
|
<Icon icon="more" size="l" alt="More" />
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
>
|
2023-03-23 02:51:52 +01:00
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
states.showAccount = `${account.info.username}@${account.instanceURL}`;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon icon="user" />
|
|
|
|
<span>View profile…</span>
|
|
|
|
</MenuItem>
|
|
|
|
<MenuDivider />
|
2023-03-07 17:32:33 +01:00
|
|
|
{moreThanOneAccount && (
|
|
|
|
<MenuItem
|
|
|
|
disabled={isDefault}
|
|
|
|
onClick={() => {
|
|
|
|
// Move account to the top of the list
|
|
|
|
accounts.splice(i, 1);
|
|
|
|
accounts.unshift(account);
|
|
|
|
store.local.setJSON('accounts', accounts);
|
2023-10-22 17:09:04 +02:00
|
|
|
reload();
|
2023-03-07 17:32:33 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon icon="check-circle" />
|
|
|
|
<span>Set as default</span>
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
2023-07-17 15:01:00 +02:00
|
|
|
<MenuConfirm
|
|
|
|
subMenu
|
|
|
|
confirmLabel={
|
|
|
|
<>
|
|
|
|
<Icon icon="exit" />
|
|
|
|
<span>Log out @{account.info.acct}?</span>
|
|
|
|
</>
|
|
|
|
}
|
2023-03-07 17:32:33 +01:00
|
|
|
disabled={!isCurrent}
|
2023-07-17 15:01:00 +02:00
|
|
|
menuItemClassName="danger"
|
2023-03-07 17:32:33 +01:00
|
|
|
onClick={() => {
|
2023-07-17 15:01:00 +02:00
|
|
|
// const yes = confirm('Log out?');
|
|
|
|
// if (!yes) return;
|
2023-03-07 17:32:33 +01:00
|
|
|
accounts.splice(i, 1);
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
// location.reload();
|
2023-08-30 11:46:22 +02:00
|
|
|
location.href = location.pathname || '/';
|
2023-03-07 17:32:33 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon icon="exit" />
|
2023-03-23 02:51:52 +01:00
|
|
|
<span>Log out…</span>
|
2023-07-17 15:01:00 +02:00
|
|
|
</MenuConfirm>
|
2023-11-18 14:11:07 +01:00
|
|
|
</Menu2>
|
2023-03-07 17:32:33 +01:00
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
2023-03-23 02:51:52 +01:00
|
|
|
<p>
|
|
|
|
<Link to="/login" class="button plain2" onClick={onClose}>
|
|
|
|
<Icon icon="plus" /> <span>Add an existing account</span>
|
|
|
|
</Link>
|
|
|
|
</p>
|
2023-03-07 17:32:33 +01:00
|
|
|
{moreThanOneAccount && (
|
|
|
|
<p>
|
|
|
|
<small>
|
|
|
|
Note: <i>Default</i> account will always be used for first load.
|
|
|
|
Switched accounts will persist during the session.
|
|
|
|
</small>
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</section>
|
|
|
|
</main>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Accounts;
|