add LinkedDisplayName component

This commit is contained in:
ChaosExAnima 2025-07-30 16:16:14 +02:00
parent 7f87bd9195
commit 2633692a3d
No known key found for this signature in database
GPG Key ID: 8F2B333100FB6117
2 changed files with 59 additions and 16 deletions

View File

@ -4,7 +4,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite';
import { accountFactoryState } from '@/testing/factories'; import { accountFactoryState } from '@/testing/factories';
import { DisplayName } from '.'; import { DisplayName, LinkedDisplayName } from '.';
type PageProps = Omit<ComponentProps<typeof DisplayName>, 'account'> & { type PageProps = Omit<ComponentProps<typeof DisplayName>, 'account'> & {
name: string; name: string;
@ -67,3 +67,15 @@ export const LocalUser: Story = {
localDomain: '', localDomain: '',
}, },
}; };
export const Linked: Story = {
render({ name, username, loading, ...args }) {
const account = !loading
? accountFactoryState({
display_name: name,
acct: username,
})
: undefined;
return <LinkedDisplayName {...args} account={account} />;
},
};

View File

@ -1,6 +1,9 @@
import type { ComponentPropsWithoutRef, FC } from 'react'; import type { ComponentPropsWithoutRef, FC } from 'react';
import { useMemo } from 'react'; import { useMemo } from 'react';
import type { LinkProps } from 'react-router-dom';
import { Link } from 'react-router-dom';
import { EmojiHTML } from '@/mastodon/features/emoji/emoji_html'; import { EmojiHTML } from '@/mastodon/features/emoji/emoji_html';
import type { Account } from '@/mastodon/models/account'; import type { Account } from '@/mastodon/models/account';
import { isModernEmojiEnabled } from '@/mastodon/utils/environment'; import { isModernEmojiEnabled } from '@/mastodon/utils/environment';
@ -52,31 +55,24 @@ export const DisplayName: FC<Props & ComponentPropsWithoutRef<'span'>> = ({
</span> </span>
); );
} }
const accountName = isModernEmojiEnabled()
? account.get('display_name')
: account.get('display_name_html');
if (simple) { if (simple) {
return ( return (
<EmojiHTML <bdi>
{...props} <EmojiHTML {...props} htmlString={accountName} shallow as='span' />
htmlString={ </bdi>
isModernEmojiEnabled()
? account.get('display_name')
: account.get('display_name_html')
}
shallow
as='span'
/>
); );
} }
return ( return (
<span {...props} className={`display-name ${className}`}> <span {...props} className={`display-name ${className}`}>
<bdi> <bdi>
<EmojiHTML <EmojiHTML
{...props} {...props}
className={`display-name__html ${className}`} className={`display-name__html ${className}`}
htmlString={ htmlString={accountName}
isModernEmojiEnabled()
? account.get('display_name')
: account.get('display_name_html')
}
shallow shallow
as='strong' as='strong'
/> />
@ -85,3 +81,38 @@ export const DisplayName: FC<Props & ComponentPropsWithoutRef<'span'>> = ({
</span> </span>
); );
}; };
export const LinkedDisplayName: FC<
Props & { asProps?: ComponentPropsWithoutRef<'span'> } & Partial<LinkProps>
> = ({
account,
asProps = {},
className,
localDomain,
simple,
noDomain,
...linkProps
}) => {
const displayProps = {
account,
className,
localDomain,
simple,
noDomain,
...asProps,
};
if (!account) {
return <DisplayName {...displayProps} />;
}
return (
<Link
to={`/@${account.acct}`}
title={`@${account.acct}`}
data-hover-card-account={account.id}
{...linkProps}
>
<DisplayName {...displayProps} />
</Link>
);
};