import './account.css'; import { useEffect, useRef, useState } from 'preact/hooks'; import { useHotkeys } from 'react-hotkeys-hook'; import { api } from '../utils/api'; import emojifyText from '../utils/emojify-text'; import enhanceContent from '../utils/enhance-content'; import handleContentLinks from '../utils/handle-content-links'; import niceDateTime from '../utils/nice-date-time'; import shortenNumber from '../utils/shorten-number'; import states, { hideAllModals } from '../utils/states'; import store from '../utils/store'; import AccountBlock from './account-block'; import Avatar from './avatar'; import Icon from './icon'; import Link from './link'; function Account({ account, instance: propInstance, onClose }) { const { masto, instance, authenticated } = api({ instance: propInstance }); const [uiState, setUIState] = useState('default'); const isString = typeof account === 'string'; const [info, setInfo] = useState(isString ? null : account); useEffect(() => { if (isString) { setUIState('loading'); (async () => { try { const info = await masto.v1.accounts.lookup({ acct: account, skip_webfinger: false, }); setInfo(info); setUIState('default'); } catch (e) { try { const result = await masto.v2.search({ q: account, type: 'accounts', limit: 1, resolve: authenticated, }); if (result.accounts.length) { setInfo(result.accounts[0]); setUIState('default'); return; } setInfo(null); setUIState('error'); } catch (err) { console.error(err); setInfo(null); setUIState('error'); } } })(); } else { setInfo(account); } }, [account]); const { acct, avatar, avatarStatic, bot, createdAt, displayName, emojis, fields, followersCount, followingCount, group, header, headerStatic, id, lastStatusAt, locked, note, statusesCount, url, username, } = info || {}; const escRef = useHotkeys('esc', onClose, [onClose]); return (
{uiState === 'error' && (

Unable to load account.

Go to account page

)} {uiState === 'loading' ? ( <>

████████ ███████

███████████████ ███████████████

██ Posts ██ Following ██ Followers

) : ( info && ( <>
{bot && ( <> Automated )}
{fields?.length > 0 && ( )}

{ hideAllModals(); }} > Posts
{shortenNumber(statusesCount)} {' '} Following
{shortenNumber(followingCount)} {' '}
Followers
{shortenNumber(followersCount)} {' '}
{!!createdAt && ( Joined
)}

) )}
); } function RelatedActions({ info, instance, authenticated }) { if (!info) return null; const { masto: currentMasto, instance: currentInstance, authenticated: currentAuthenticated, } = api(); const sameInstance = instance === currentInstance; const [relationshipUIState, setRelationshipUIState] = useState('default'); const [relationship, setRelationship] = useState(null); const [familiarFollowers, setFamiliarFollowers] = useState([]); const { id, locked } = info; const accountID = useRef(id); const { following, showingReblogs, notifying, followedBy, blocking, blockedBy, muting, mutingNotifications, requested, domainBlocking, endorsed, } = relationship || {}; useEffect(() => { if (info) { const currentAccount = store.session.get('currentAccount'); let currentID; (async () => { if (sameInstance && authenticated) { currentID = id; } else if (!sameInstance && currentAuthenticated) { // Grab this account from my logged-in instance const acctHasInstance = info.acct.includes('@'); try { const results = await currentMasto.v2.search({ q: acctHasInstance ? info.acct : `${info.username}@${instance}`, type: 'accounts', limit: 1, resolve: true, }); console.log('🥏 Fetched account from logged-in instance', results); currentID = results.accounts[0].id; } catch (e) { console.error(e); } } if (!currentID) return; if (currentAccount === currentID) { // It's myself! return; } accountID.current = currentID; setRelationshipUIState('loading'); setFamiliarFollowers([]); const fetchRelationships = currentMasto.v1.accounts.fetchRelationships([ currentID, ]); const fetchFamiliarFollowers = currentMasto.v1.accounts.fetchFamiliarFollowers(currentID); try { const relationships = await fetchRelationships; console.log('fetched relationship', relationships); if (relationships.length) { const relationship = relationships[0]; setRelationship(relationship); if (!relationship.following) { try { const followers = await fetchFamiliarFollowers; console.log('fetched familiar followers', followers); setFamiliarFollowers(followers[0].accounts.slice(0, 10)); } catch (e) { console.error(e); } } } setRelationshipUIState('default'); } catch (e) { console.error(e); setRelationshipUIState('error'); } })(); } }, [info, authenticated]); return ( <> {familiarFollowers?.length > 0 && (

Common followers{' '} {familiarFollowers.map((follower) => ( { e.preventDefault(); states.showAccount = { account: follower, instance, }; }} > ))}

)}

{followedBy ? Following you : }{' '} {relationshipUIState !== 'loading' && relationship && ( )}

); } export default Account;