phanpy/src/components/name-text.jsx

88 lines
2 KiB
React
Raw Normal View History

2022-12-10 09:14:48 +00:00
import './name-text.css';
import emojifyText from '../utils/emojify-text';
import states from '../utils/states';
import Avatar from './avatar';
function NameText({
account,
instance,
showAvatar,
showAcct,
short,
external,
onClick,
}) {
const { acct, avatar, avatarStatic, id, url, displayName, emojis } = account;
let { username } = account;
2022-12-10 09:14:48 +00:00
const displayNameWithEmoji = emojifyText(displayName, emojis);
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
.replace(/\s+/g, '') // E.g. "My name" === "myname"
.replace(/[^a-z0-9]/gi, ''); // Remove non-alphanumeric characters
if (
2023-02-15 13:40:58 +00:00
!short &&
(trimmedUsername === trimmedDisplayName ||
trimmedUsername === shortenedDisplayName)
) {
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}
2022-12-10 09:14:48 +00:00
title={`@${acct}`}
onClick={(e) => {
if (external) return;
e.preventDefault();
if (onClick) return onClick(e);
states.showAccount = {
account,
instance,
};
2022-12-10 09:14:48 +00:00
}}
>
{showAvatar && (
<>
2023-03-15 11:30:53 +00:00
<Avatar url={avatarStatic || avatar} />{' '}
2022-12-10 09:14:48 +00:00
</>
)}
{displayName && !short ? (
<>
<b
dangerouslySetInnerHTML={{
__html: displayNameWithEmoji,
}}
/>
{!showAcct && username && (
2022-12-10 09:14:48 +00:00
<>
{' '}
<i>@{username}</i>
</>
)}
</>
) : short ? (
<i>@{username}</i>
) : (
<b>@{username}</b>
)}
{showAcct && (
<>
<br />
<i>@{acct}</i>
</>
)}
</a>
);
2022-12-16 05:27:04 +00:00
}
export default NameText;