webapp/src/components/Topic/TopicBadge/TopicBadge.tsx

132 lines
4.3 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2024-02-08 09:11:52 +00:00
import { Show, createEffect, createSignal, on } from 'solid-js'
2024-01-31 12:34:15 +00:00
import { useFollowing } from '../../../context/following'
import { useLocalize } from '../../../context/localize'
2024-01-11 17:40:06 +00:00
import { useMediaQuery } from '../../../context/mediaQuery'
import { useSession } from '../../../context/session'
import { FollowingEntity, Topic } from '../../../graphql/schema/core.gen'
2024-01-13 14:26:21 +00:00
import { capitalize } from '../../../utils/capitalize'
import { getImageUrl } from '../../../utils/getImageUrl'
import { Button } from '../../_shared/Button'
import { CheckButton } from '../../_shared/CheckButton'
2024-02-08 09:11:52 +00:00
import { FollowedInfo } from '../../../pages/types'
import styles from './TopicBadge.module.scss'
2024-01-31 12:34:15 +00:00
type Props = {
topic: Topic
minimizeSubscribeButton?: boolean
2024-02-08 09:11:52 +00:00
isFollowed?: FollowedInfo
showStat?: boolean
}
export const TopicBadge = (props: Props) => {
2024-01-11 17:40:06 +00:00
const { t, lang } = useLocalize()
const { mediaMatches } = useMediaQuery()
const [isMobileView, setIsMobileView] = createSignal(false)
2024-02-04 17:40:15 +00:00
const { requireAuthentication } = useSession()
2024-01-31 12:34:15 +00:00
const { setFollowing, loading: subLoading } = useFollowing()
2024-02-08 09:11:52 +00:00
const [isFollowed, setIsFollowed] = createSignal<boolean>()
2024-01-31 12:34:15 +00:00
const handleFollowClick = () => {
2024-02-08 09:11:52 +00:00
const value = !isFollowed()
2024-01-31 12:34:15 +00:00
requireAuthentication(() => {
2024-02-08 09:11:52 +00:00
setIsFollowed(value)
2024-01-31 12:34:15 +00:00
setFollowing(FollowingEntity.Topic, props.topic.slug, value)
}, 'subscribe')
}
2024-01-31 12:34:15 +00:00
createEffect(() => {
setIsMobileView(!mediaMatches.sm)
})
2024-02-08 09:11:52 +00:00
createEffect(
on(
() => props.isFollowed,
() => {
setIsFollowed(props.isFollowed.value)
},
),
)
const title = () =>
lang() === 'en' ? capitalize(props.topic.slug.replaceAll('-', ' ')) : props.topic.title
return (
<div class={styles.TopicBadge}>
2024-02-08 09:11:52 +00:00
<div class={styles.content}>
<div class={styles.basicInfo}>
<a
href={`/topic/${props.topic.slug}`}
class={clsx(styles.picture, {
[styles.withImage]: props.topic.pic,
[styles.smallSize]: isMobileView(),
})}
style={
props.topic.pic && {
'background-image': `url('${getImageUrl(props.topic.pic, { width: 40, height: 40 })}')`,
}
2024-01-11 17:40:06 +00:00
}
2024-02-08 09:11:52 +00:00
/>
<a href={`/topic/${props.topic.slug}`} class={styles.info}>
<span class={styles.title}>{title()}</span>
<Show
when={props.topic.body}
fallback={
<div class={styles.description}>
{t('PublicationsWithCount', { count: props.topic?.stat?.shouts ?? 0 })}
2024-02-08 09:11:52 +00:00
</div>
}
>
<div class={clsx('text-truncate', styles.description)}>{props.topic.body}</div>
</Show>
</a>
</div>
<div class={styles.actions}>
2024-01-11 17:40:06 +00:00
<Show
2024-02-08 09:11:52 +00:00
when={!props.minimizeSubscribeButton}
2024-01-11 17:40:06 +00:00
fallback={
2024-02-08 09:11:52 +00:00
<CheckButton text={t('Follow')} checked={Boolean(isFollowed())} onClick={handleFollowClick} />
2024-01-11 17:40:06 +00:00
}
>
2024-02-08 09:11:52 +00:00
<Show
when={isFollowed()}
fallback={
<Button
variant="primary"
size="S"
value={subLoading() ? t('subscribing...') : t('Subscribe')}
onClick={handleFollowClick}
class={styles.subscribeButton}
/>
}
>
<Button
2024-01-31 12:34:15 +00:00
onClick={handleFollowClick}
2024-02-08 09:11:52 +00:00
variant="bordered"
size="S"
value={t('Following')}
2023-10-09 21:22:06 +00:00
class={styles.subscribeButton}
/>
2024-02-08 09:11:52 +00:00
</Show>
</Show>
2024-02-08 09:11:52 +00:00
</div>
</div>
<div class={styles.stats}>
2024-03-06 12:55:33 +00:00
<span class={styles.statsItem}>{t('shoutsWithCount', { count: props.topic?.stat?.shouts })}</span>
<span class={styles.statsItem}>{t('authorsWithCount', { count: props.topic?.stat?.authors })}</span>
2024-02-08 09:11:52 +00:00
<span class={styles.statsItem}>
2024-03-06 12:55:33 +00:00
{t('FollowersWithCount', { count: props.topic?.stat?.followers })}
2024-02-08 09:11:52 +00:00
</span>
2024-03-06 12:36:12 +00:00
<Show when={props.topic?.stat?.comments}>
2024-03-06 12:55:33 +00:00
<span class={styles.statsItem}>
{t('CommentsWithCount', { count: props.topic?.stat?.comments ?? 0 })}
</span>
2024-03-06 12:36:12 +00:00
</Show>
2024-01-11 17:40:06 +00:00
</div>
2024-03-06 12:55:33 +00:00
</div>
)
}