phanpy/src/components/name-text.jsx

111 wiersze
2.8 KiB
React
Czysty Zwykły widok Historia

2022-12-10 09:14:48 +00:00
import './name-text.css';
import { memo } from 'preact/compat';
2022-12-10 09:14:48 +00:00
import states from '../utils/states';
import Avatar from './avatar';
import EmojiText from './emoji-text';
2022-12-10 09:14:48 +00:00
2023-12-21 16:26:29 +00:00
const nameCollator = new Intl.Collator('en', {
sensitivity: 'base',
});
function NameText({
account,
instance,
showAvatar,
showAcct,
short,
external,
onClick,
}) {
2023-04-10 16:26:43 +00:00
const { acct, avatar, avatarStatic, id, url, displayName, emojis, bot } =
account;
let { username } = account;
2023-07-16 02:36:33 +00:00
const [_, acct1, acct2] = acct.match(/([^@]+)(@.+)/i) || [, acct];
2022-12-10 09:14:48 +00:00
const trimmedUsername = username.toLowerCase().trim();
const trimmedDisplayName = (displayName || '').toLowerCase().trim();
2023-02-15 13:40:58 +00:00
const shortenedDisplayName = trimmedDisplayName
.replace(/(\:(\w|\+|\-)+\:)(?=|[\!\.\?]|$)/g, '') // Remove shortcodes, regex from https://regex101.com/r/iE9uV0/1
2023-10-04 13:23:21 +00:00
.replace(/\s+/g, ''); // E.g. "My name" === "myname"
const shortenedAlphaNumericDisplayName = shortenedDisplayName.replace(
/[^a-z0-9]/gi,
'',
); // Remove non-alphanumeric characters
if (
2023-02-15 13:40:58 +00:00
!short &&
(trimmedUsername === trimmedDisplayName ||
2023-10-04 13:23:21 +00:00
trimmedUsername === shortenedDisplayName ||
trimmedUsername === shortenedAlphaNumericDisplayName ||
2023-12-21 16:26:29 +00:00
nameCollator.compare(trimmedUsername, shortenedDisplayName) === 0)
) {
username = null;
}
2022-12-10 09:14:48 +00:00
return (
<a
class={`name-text ${showAcct ? 'show-acct' : ''} ${short ? 'short' : ''}`}
2022-12-10 09:14:48 +00:00
href={url}
2022-12-15 11:45:25 +00:00
target={external ? '_blank' : null}
2024-03-18 03:48:15 +00:00
title={
displayName
? `${displayName} (${acct2 ? '' : '@'}${acct})`
: `${acct2 ? '' : '@'}${acct}`
}
2022-12-10 09:14:48 +00:00
onClick={(e) => {
if (external) return;
e.preventDefault();
e.stopPropagation();
if (onClick) return onClick(e);
states.showAccount = {
account,
instance,
};
2022-12-10 09:14:48 +00:00
}}
>
{showAvatar && (
<>
2023-04-10 16:26:43 +00:00
<Avatar url={avatarStatic || avatar} squircle={bot} />{' '}
2022-12-10 09:14:48 +00:00
</>
)}
{displayName && !short ? (
<>
<b>
<EmojiText text={displayName} emojis={emojis} />
</b>
{!showAcct && username && (
2022-12-10 09:14:48 +00:00
<>
{' '}
<i>@{username}</i>
</>
)}
</>
) : short ? (
<i>{username}</i>
2022-12-10 09:14:48 +00:00
) : (
<b>{username}</b>
2022-12-10 09:14:48 +00:00
)}
{showAcct && (
<>
<br />
2023-07-16 02:36:33 +00:00
<i>
2024-03-18 03:48:15 +00:00
{acct2 ? '' : '@'}
{acct1}
{!!acct2 && <span class="ib">{acct2}</span>}
2023-07-16 02:36:33 +00:00
</i>
2022-12-10 09:14:48 +00:00
</>
)}
</a>
);
2022-12-16 05:27:04 +00:00
}
2024-01-25 13:28:41 +00:00
export default memo(NameText, (oldProps, newProps) => {
// Only care about account.id, the other props usually don't change
const { account } = oldProps;
const { account: newAccount } = newProps;
return account?.acct === newAccount?.acct;
});