2022-12-10 12:14:48 +03:00
|
|
|
import './name-text.css';
|
|
|
|
|
|
|
|
import emojifyText from '../utils/emojify-text';
|
|
|
|
import states from '../utils/states';
|
|
|
|
|
|
|
|
import Avatar from './avatar';
|
|
|
|
|
2023-02-05 19:17:19 +03:00
|
|
|
function NameText({
|
|
|
|
account,
|
|
|
|
instance,
|
|
|
|
showAvatar,
|
|
|
|
showAcct,
|
|
|
|
short,
|
|
|
|
external,
|
|
|
|
onClick,
|
|
|
|
}) {
|
2022-12-20 14:52:55 +03:00
|
|
|
const { acct, avatar, avatarStatic, id, url, displayName, emojis } = account;
|
|
|
|
let { username } = account;
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
const displayNameWithEmoji = emojifyText(displayName, emojis);
|
|
|
|
|
2023-02-15 05:46:29 +03:00
|
|
|
const trimmedUsername = username.toLowerCase().trim();
|
|
|
|
const trimmedDisplayName = (displayName || '').toLowerCase().trim();
|
2023-02-15 16:40:58 +03:00
|
|
|
const shortenedDisplayName = trimmedDisplayName
|
|
|
|
.replace(/(\:(\w|\+|\-)+\:)(?=|[\!\.\?]|$)/g, '') // Remove shortcodes, regex from https://regex101.com/r/iE9uV0/1
|
|
|
|
.replace(/\s+/g, '') // E.g. "My name" === "myname"
|
|
|
|
.replace(/[^a-z0-9]/gi, ''); // Remove non-alphanumeric characters
|
2023-02-15 05:46:29 +03:00
|
|
|
|
2022-12-20 17:00:11 +03:00
|
|
|
if (
|
2023-02-15 16:40:58 +03:00
|
|
|
!short &&
|
|
|
|
(trimmedUsername === trimmedDisplayName ||
|
|
|
|
trimmedUsername === shortenedDisplayName)
|
2022-12-20 17:00:11 +03:00
|
|
|
) {
|
|
|
|
username = null;
|
|
|
|
}
|
2022-12-20 14:52:55 +03:00
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<a
|
|
|
|
class={`name-text ${short ? 'short' : ''}`}
|
|
|
|
href={url}
|
2022-12-15 14:45:25 +03:00
|
|
|
target={external ? '_blank' : null}
|
2022-12-10 12:14:48 +03:00
|
|
|
title={`@${acct}`}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (external) return;
|
|
|
|
e.preventDefault();
|
2022-12-25 18:31:50 +03:00
|
|
|
if (onClick) return onClick(e);
|
2023-02-05 19:17:19 +03:00
|
|
|
states.showAccount = {
|
|
|
|
account,
|
|
|
|
instance,
|
|
|
|
};
|
2022-12-10 12:14:48 +03:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{showAvatar && (
|
|
|
|
<>
|
|
|
|
<Avatar url={avatar} />{' '}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{displayName && !short ? (
|
|
|
|
<>
|
|
|
|
<b
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: displayNameWithEmoji,
|
|
|
|
}}
|
|
|
|
/>
|
2022-12-20 14:52:55 +03:00
|
|
|
{!showAcct && username && (
|
2022-12-10 12:14:48 +03:00
|
|
|
<>
|
|
|
|
{' '}
|
|
|
|
<i>@{username}</i>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : short ? (
|
|
|
|
<i>@{username}</i>
|
|
|
|
) : (
|
|
|
|
<b>@{username}</b>
|
|
|
|
)}
|
|
|
|
{showAcct && (
|
|
|
|
<>
|
|
|
|
<br />
|
|
|
|
<i>@{acct}</i>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</a>
|
|
|
|
);
|
2022-12-16 08:27:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default NameText;
|