phanpy/src/components/account-block.jsx

144 lines
3.3 KiB
React
Raw Normal View History

2023-02-12 14:29:03 +03:00
import './account-block.css';
import { useNavigate } from 'react-router-dom';
import enhanceContent from '../utils/enhance-content';
import niceDateTime from '../utils/nice-date-time';
import shortenNumber from '../utils/shorten-number';
2023-02-12 14:29:03 +03:00
import states from '../utils/states';
import Avatar from './avatar';
import EmojiText from './emoji-text';
import Icon from './icon';
2023-02-12 14:29:03 +03:00
function AccountBlock({
skeleton,
account,
avatarSize = 'xl',
instance,
external,
internal,
2023-02-12 14:29:03 +03:00
onClick,
showActivity = false,
showStats = false,
2023-02-12 14:29:03 +03:00
}) {
if (skeleton) {
return (
<div class="account-block skeleton">
<Avatar size={avatarSize} />
<span>
<b></b>
<br />
<span class="account-block-acct">@</span>
2023-02-12 14:29:03 +03:00
</span>
</div>
);
}
const navigate = useNavigate();
const {
id,
acct,
avatar,
avatarStatic,
displayName,
username,
emojis,
url,
statusesCount,
lastStatusAt,
2023-04-10 19:26:43 +03:00
bot,
fields,
note,
} = account;
const [_, acct1, acct2] = acct.match(/([^@]+)(@.+)/i) || [, acct];
2023-02-12 14:29:03 +03:00
const verifiedField = fields?.find((f) => !!f.verifiedAt && !!f.value);
2023-02-12 14:29:03 +03:00
return (
<a
class="account-block"
href={url}
target={external ? '_blank' : null}
title={`@${acct}`}
onClick={(e) => {
if (external) return;
e.preventDefault();
if (onClick) return onClick(e);
if (internal) {
navigate(`/${instance}/a/${id}`);
} else {
states.showAccount = {
account,
instance,
};
}
2023-02-12 14:29:03 +03:00
}}
>
2023-04-10 19:26:43 +03:00
<Avatar url={avatar} size={avatarSize} squircle={bot} />
2023-02-12 14:29:03 +03:00
<span>
{displayName ? (
<b>
<EmojiText text={displayName} emojis={emojis} />
</b>
2023-02-12 14:29:03 +03:00
) : (
<b>{username}</b>
)}
<br />
<span class="account-block-acct">
@{acct1}
<wbr />
{acct2}
</span>
{showActivity && (
<>
<br />
<small class="last-status-at insignificant">
Posts: {statusesCount}
{!!lastStatusAt && (
<>
{' '}
&middot; Last posted:{' '}
{niceDateTime(lastStatusAt, {
hideTime: true,
})}
</>
)}
</small>
</>
)}
{showStats && (
<div class="account-block-stats">
<div
class="short-desc"
dangerouslySetInnerHTML={{
__html: enhanceContent(note, { emojis }),
}}
/>
{bot && (
<>
<span class="tag">
<Icon icon="bot" /> Automated
</span>
</>
)}
{!!verifiedField && (
<span class="verified-field ib">
<Icon icon="check-circle" size="s" />{' '}
<span
dangerouslySetInnerHTML={{
__html: enhanceContent(verifiedField.value, { emojis }),
}}
/>
</span>
)}
</div>
)}
2023-02-12 14:29:03 +03:00
</span>
</a>
);
}
export default AccountBlock;