2023-10-04 19:04:09 +00:00
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import styles from './TopicBadge.module.scss'
|
|
|
|
import { FollowingEntity, Topic } from '../../../graphql/types.gen'
|
|
|
|
import { createMemo, createSignal, Show } from 'solid-js'
|
|
|
|
import { Button } from '../../_shared/Button'
|
|
|
|
import { useSession } from '../../../context/session'
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
|
|
|
import { follow, unfollow } from '../../../stores/zine/common'
|
|
|
|
import { CheckButton } from '../../_shared/CheckButton'
|
2023-10-27 18:50:13 +00:00
|
|
|
import { getImageUrl } from '../../../utils/getImageUrl'
|
2023-10-04 19:04:09 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
topic: Topic
|
|
|
|
minimizeSubscribeButton?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TopicBadge = (props: Props) => {
|
|
|
|
const [isSubscribing, setIsSubscribing] = createSignal(false)
|
|
|
|
const { t } = useLocalize()
|
|
|
|
const {
|
|
|
|
isAuthenticated,
|
2023-10-19 15:05:22 +00:00
|
|
|
subscriptions,
|
|
|
|
actions: { loadSubscriptions }
|
2023-10-04 19:04:09 +00:00
|
|
|
} = useSession()
|
|
|
|
|
2023-10-19 15:05:22 +00:00
|
|
|
const subscribed = createMemo(() =>
|
|
|
|
subscriptions().topics.some((topic) => topic.slug === props.topic.slug)
|
|
|
|
)
|
2023-10-04 19:04:09 +00:00
|
|
|
|
|
|
|
const subscribe = async (really = true) => {
|
|
|
|
setIsSubscribing(true)
|
|
|
|
|
|
|
|
await (really
|
|
|
|
? follow({ what: FollowingEntity.Topic, slug: props.topic.slug })
|
|
|
|
: unfollow({ what: FollowingEntity.Topic, slug: props.topic.slug }))
|
|
|
|
|
2023-10-19 15:05:22 +00:00
|
|
|
await loadSubscriptions()
|
2023-10-04 19:04:09 +00:00
|
|
|
setIsSubscribing(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={styles.TopicBadge}>
|
|
|
|
<a
|
|
|
|
href={`/topic/${props.topic.slug}`}
|
|
|
|
class={clsx(styles.picture, { [styles.withImage]: props.topic.pic })}
|
2023-10-27 18:50:13 +00:00
|
|
|
style={
|
|
|
|
props.topic.pic && {
|
|
|
|
'background-image': `url('${getImageUrl(props.topic.pic, { width: 40, height: 40 })}')`
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
<a href={`/topic/${props.topic.slug}`} class={styles.info}>
|
2023-10-05 21:43:53 +00:00
|
|
|
<span class={styles.title}>{props.topic.title}</span>
|
|
|
|
<Show
|
|
|
|
when={props.topic.body}
|
|
|
|
fallback={
|
|
|
|
<div class={styles.description}>
|
2023-10-09 09:14:41 +00:00
|
|
|
{t('PublicationsWithCount', { count: props.topic.stat.shouts ?? 0 })}
|
2023-10-05 21:43:53 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
2023-10-04 19:04:09 +00:00
|
|
|
<div class={clsx('text-truncate', styles.description)}>{props.topic.body}</div>
|
|
|
|
</Show>
|
|
|
|
</a>
|
|
|
|
<Show when={isAuthenticated()}>
|
|
|
|
<div class={styles.actions}>
|
|
|
|
<Show
|
|
|
|
when={!props.minimizeSubscribeButton}
|
|
|
|
fallback={
|
|
|
|
<CheckButton
|
|
|
|
text={t('Follow')}
|
|
|
|
checked={subscribed()}
|
|
|
|
onClick={() => subscribe(!subscribed)}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Show
|
|
|
|
when={subscribed()}
|
|
|
|
fallback={
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
size="S"
|
2023-11-02 22:02:11 +00:00
|
|
|
value={isSubscribing() ? t('subscribing...') : t('Subscribe')}
|
2023-10-04 19:04:09 +00:00
|
|
|
onClick={() => subscribe(true)}
|
2023-10-09 21:22:06 +00:00
|
|
|
class={styles.subscribeButton}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
onClick={() => subscribe(false)}
|
2023-10-09 21:22:06 +00:00
|
|
|
variant="bordered"
|
2023-10-04 19:04:09 +00:00
|
|
|
size="S"
|
2023-10-16 09:54:14 +00:00
|
|
|
value={t('Following')}
|
2023-10-09 21:22:06 +00:00
|
|
|
class={styles.subscribeButton}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
</Show>
|
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|