import * as React from 'react'; import { Link } from 'react-router-dom'; import HoverRefWrapper from 'soapbox/components/hover_ref_wrapper'; import VerificationBadge from 'soapbox/components/verification_badge'; import ActionButton from 'soapbox/features/ui/components/action_button'; import { useAppSelector, useOnScreen } from 'soapbox/hooks'; import { getAcct } from 'soapbox/utils/accounts'; import { displayFqn } from 'soapbox/utils/state'; import RelativeTimestamp from './relative_timestamp'; import { Avatar, HStack, IconButton, Text } from './ui'; import type { Account as AccountEntity } from 'soapbox/types/entities'; interface IProfilePopper { condition: boolean, wrapper: (children: any) => React.ReactElement } const ProfilePopper: React.FC = ({ condition, wrapper, children }): any => condition ? wrapper(children) : children; interface IAccount { account: AccountEntity, action?: React.ReactElement, actionAlignment?: 'center' | 'top', actionIcon?: string, actionTitle?: string, avatarSize?: number, hidden?: boolean, hideActions?: boolean, id?: string, onActionClick?: (account: any) => void, showProfileHoverCard?: boolean, timestamp?: string | Date, timestampUrl?: string, withRelationship?: boolean, } const Account = ({ account, action, actionIcon, actionTitle, actionAlignment = 'center', avatarSize = 42, hidden = false, hideActions = false, onActionClick, showProfileHoverCard = true, timestamp, timestampUrl, withRelationship = true, }: IAccount) => { const overflowRef = React.useRef(null); const actionRef = React.useRef(null); // @ts-ignore const isOnScreen = useOnScreen(overflowRef); const [style, setStyle] = React.useState({ visibility: 'hidden' }); const me = useAppSelector((state) => state.me); const username = useAppSelector((state) => account ? getAcct(account, displayFqn(state)) : null); const handleAction = () => { // @ts-ignore onActionClick(account); }; const renderAction = () => { if (action) { return action; } if (hideActions) { return null; } if (onActionClick && actionIcon) { return ( ); } if (account.get('id') !== me && account.get('relationship', null) !== null) { return ; } return null; }; React.useEffect(() => { const style: React.CSSProperties = {}; const actionWidth = actionRef.current?.clientWidth || 0; if (overflowRef.current) { style.maxWidth = overflowRef.current.clientWidth - 30 - avatarSize - actionWidth; } else { style.visibility = 'hidden'; } setStyle(style); }, [isOnScreen, overflowRef, actionRef]); if (!account) { return null; } if (hidden) { return ( <> {account.get('display_name')} {account.get('username')} ); } const LinkEl: any = showProfileHoverCard ? Link : 'div'; return (
{children}} > event.stopPropagation()} >
{children}} > event.stopPropagation()} >
{account.get('verified') && }
@{username} {(timestamp) ? ( <> · {timestampUrl ? ( ) : ( )} ) : null}
{withRelationship ? renderAction() : null}
); }; export default Account;