InteractionCounter: refactor with <button>

This commit is contained in:
Alex Gleason 2022-11-20 16:14:50 -06:00
parent b9a3f7ec13
commit de3678c272
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -68,23 +68,17 @@ const StatusInteractionBar: React.FC<IStatusInteractionBar> = ({ status }): JSX.
const getReposts = () => { const getReposts = () => {
if (status.reblogs_count) { if (status.reblogs_count) {
return ( return (
<button <InteractionCounter count={status.reblogs_count} onClick={handleOpenReblogsModal}>
type='button'
onClick={handleOpenReblogsModal}
className='text-gray-600 dark:text-gray-700 hover:underline'
>
<InteractionCounter count={status.reblogs_count}>
<FormattedMessage <FormattedMessage
id='status.interactions.reblogs' id='status.interactions.reblogs'
defaultMessage='{count, plural, one {Repost} other {Reposts}}' defaultMessage='{count, plural, one {Repost} other {Reposts}}'
values={{ count: status.reblogs_count }} values={{ count: status.reblogs_count }}
/> />
</InteractionCounter> </InteractionCounter>
</button>
); );
} }
return ''; return null;
}; };
const handleOpenFavouritesModal: React.EventHandler<React.MouseEvent<HTMLButtonElement>> = (e) => { const handleOpenFavouritesModal: React.EventHandler<React.MouseEvent<HTMLButtonElement>> = (e) => {
@ -97,29 +91,17 @@ const StatusInteractionBar: React.FC<IStatusInteractionBar> = ({ status }): JSX.
const getFavourites = () => { const getFavourites = () => {
if (status.favourites_count) { if (status.favourites_count) {
return ( return (
<button <InteractionCounter count={status.favourites_count} onClick={features.exposableReactions ? handleOpenFavouritesModal : undefined}>
type='button'
onClick={features.exposableReactions ? handleOpenFavouritesModal : undefined}
className={
classNames({
'text-gray-600 dark:text-gray-700 hover:underline': true,
'hover:underline': features.exposableReactions,
'cursor-default': !features.exposableReactions,
})
}
>
<InteractionCounter count={status.favourites_count}>
<FormattedMessage <FormattedMessage
id='status.interactions.favourites' id='status.interactions.favourites'
defaultMessage='{count, plural, one {Like} other {Likes}}' defaultMessage='{count, plural, one {Like} other {Likes}}'
values={{ count: status.favourites_count }} values={{ count: status.favourites_count }}
/> />
</InteractionCounter> </InteractionCounter>
</button>
); );
} }
return ''; return null;
}; };
const handleOpenReactionsModal = () => { const handleOpenReactionsModal = () => {
@ -136,19 +118,9 @@ const StatusInteractionBar: React.FC<IStatusInteractionBar> = ({ status }): JSX.
acc + cur.get('count') acc + cur.get('count')
), 0); ), 0);
if (count) {
return ( return (
<button <InteractionCounter count={count} onClick={features.exposableReactions ? handleOpenReactionsModal : undefined}>
type='button'
onClick={features.exposableReactions ? handleOpenReactionsModal : undefined}
className={
classNames({
'text-gray-600 dark:text-gray-700': true,
'hover:underline': features.exposableReactions,
'cursor-default': !features.exposableReactions,
})
}
>
<InteractionCounter count={count}>
<HStack space={0.5} alignItems='center'> <HStack space={0.5} alignItems='center'>
{emojiReacts.take(3).map((e, i) => { {emojiReacts.take(3).map((e, i) => {
return ( return (
@ -161,27 +133,42 @@ const StatusInteractionBar: React.FC<IStatusInteractionBar> = ({ status }): JSX.
})} })}
</HStack> </HStack>
</InteractionCounter> </InteractionCounter>
</button>
); );
}
return null;
}; };
return ( return (
<HStack space={3}> <HStack space={3}>
{getReposts()} {getReposts()}
{getFavourites()} {getFavourites()}
{features.emojiReacts ? getEmojiReacts() : null} {features.emojiReacts && getEmojiReacts()}
</HStack> </HStack>
); );
}; };
interface IInteractionCounter { interface IInteractionCounter {
count: number, count: number,
onClick?: React.MouseEventHandler<HTMLButtonElement>,
children: React.ReactNode, children: React.ReactNode,
} }
/** InteractionCounter component. */ const InteractionCounter: React.FC<IInteractionCounter> = ({ count, onClick, children }) => {
const InteractionCounter: React.FC<IInteractionCounter> = ({ count, children }) => { const features = useFeatures();
return ( return (
<button
type='button'
onClick={onClick}
className={
classNames({
'text-gray-600 dark:text-gray-700': true,
'hover:underline': features.exposableReactions,
'cursor-default': !features.exposableReactions,
})
}
>
<HStack space={1} alignItems='center'> <HStack space={1} alignItems='center'>
<Text theme='primary' weight='bold'> <Text theme='primary' weight='bold'>
<FormattedNumber value={count} /> <FormattedNumber value={count} />
@ -191,6 +178,7 @@ const InteractionCounter: React.FC<IInteractionCounter> = ({ count, children })
{children} {children}
</Text> </Text>
</HStack> </HStack>
</button>
); );
}; };