2024-08-13 10:26:23 +03:00
|
|
|
import { t } from '@lingui/macro';
|
2023-10-19 11:06:55 +03:00
|
|
|
import { useEffect } from 'preact/hooks';
|
2023-03-11 09:05:56 +03:00
|
|
|
|
|
|
|
import { api } from '../utils/api';
|
2023-03-11 16:33:55 +03:00
|
|
|
import states from '../utils/states';
|
2023-10-19 11:06:55 +03:00
|
|
|
import useLocationChange from '../utils/useLocationChange';
|
2023-03-11 09:05:56 +03:00
|
|
|
|
|
|
|
import AccountInfo from './account-info';
|
2023-04-20 11:10:57 +03:00
|
|
|
import Icon from './icon';
|
2023-03-11 09:05:56 +03:00
|
|
|
|
|
|
|
function AccountSheet({ account, instance: propInstance, onClose }) {
|
|
|
|
const { masto, instance, authenticated } = api({ instance: propInstance });
|
|
|
|
const isString = typeof account === 'string';
|
|
|
|
|
2023-03-11 16:33:55 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isString) {
|
|
|
|
states.accounts[`${account.id}@${instance}`] = account;
|
|
|
|
}
|
|
|
|
}, [account]);
|
|
|
|
|
2023-10-19 11:06:55 +03:00
|
|
|
useLocationChange(onClose);
|
2023-10-19 05:15:54 +03:00
|
|
|
|
2023-03-11 09:05:56 +03:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class="sheet"
|
2023-10-19 05:15:54 +03:00
|
|
|
// onClick={(e) => {
|
|
|
|
// const accountBlock = e.target.closest('.account-block');
|
|
|
|
// if (accountBlock) {
|
|
|
|
// onClose({
|
|
|
|
// destination: 'account-statuses',
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }}
|
2023-03-11 09:05:56 +03:00
|
|
|
>
|
2023-04-20 11:10:57 +03:00
|
|
|
{!!onClose && (
|
|
|
|
<button type="button" class="sheet-close outer" onClick={onClose}>
|
2024-08-13 10:26:23 +03:00
|
|
|
<Icon icon="x" alt={t`Close`} />
|
2023-04-20 11:10:57 +03:00
|
|
|
</button>
|
|
|
|
)}
|
2023-03-11 09:05:56 +03:00
|
|
|
<AccountInfo
|
|
|
|
instance={instance}
|
|
|
|
authenticated={authenticated}
|
|
|
|
account={account}
|
|
|
|
fetchAccount={async () => {
|
|
|
|
if (isString) {
|
|
|
|
try {
|
|
|
|
const info = await masto.v1.accounts.lookup({
|
|
|
|
acct: account,
|
|
|
|
skip_webfinger: false,
|
|
|
|
});
|
|
|
|
return info;
|
|
|
|
} catch (e) {
|
2023-10-12 07:48:09 +03:00
|
|
|
const result = await masto.v2.search.fetch({
|
2023-03-11 09:05:56 +03:00
|
|
|
q: account,
|
|
|
|
type: 'accounts',
|
|
|
|
limit: 1,
|
|
|
|
resolve: authenticated,
|
|
|
|
});
|
|
|
|
if (result.accounts.length) {
|
|
|
|
return result.accounts[0];
|
2023-09-10 04:13:00 +03:00
|
|
|
} else if (/https?:\/\/[^/]+\/@/.test(account)) {
|
2024-06-14 03:34:50 +03:00
|
|
|
const accountURL = URL.parse(account);
|
2024-01-12 09:47:59 +03:00
|
|
|
const { hostname, pathname } = accountURL;
|
|
|
|
const acct =
|
|
|
|
pathname.replace(/^\//, '').replace(/\/$/, '') +
|
|
|
|
'@' +
|
|
|
|
hostname;
|
2023-10-12 07:48:09 +03:00
|
|
|
const result = await masto.v2.search.fetch({
|
2023-09-10 04:13:00 +03:00
|
|
|
q: acct,
|
|
|
|
type: 'accounts',
|
|
|
|
limit: 1,
|
|
|
|
resolve: authenticated,
|
|
|
|
});
|
|
|
|
if (result.accounts.length) {
|
|
|
|
return result.accounts[0];
|
|
|
|
}
|
2023-03-11 09:05:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AccountSheet;
|