2023-10-04 19:04:09 +00:00
|
|
|
import { clsx } from 'clsx'
|
2023-12-29 06:39:16 +00:00
|
|
|
import { createEffect, createMemo, createSignal, Show } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2024-01-11 15:56:32 +00:00
|
|
|
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'
|
2023-10-04 19:04:09 +00:00
|
|
|
import { follow, unfollow } from '../../../stores/zine/common'
|
2024-01-13 14:26:21 +00:00
|
|
|
import { capitalize } from '../../../utils/capitalize'
|
2023-10-27 18:50:13 +00:00
|
|
|
import { getImageUrl } from '../../../utils/getImageUrl'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { Button } from '../../_shared/Button'
|
|
|
|
import { CheckButton } from '../../_shared/CheckButton'
|
|
|
|
|
|
|
|
import styles from './TopicBadge.module.scss'
|
2023-10-04 19:04:09 +00:00
|
|
|
type Props = {
|
|
|
|
topic: Topic
|
|
|
|
minimizeSubscribeButton?: 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)
|
2023-12-29 06:39:16 +00:00
|
|
|
const [isSubscribing, setIsSubscribing] = createSignal(false)
|
|
|
|
createEffect(() => {
|
|
|
|
setIsMobileView(!mediaMatches.sm)
|
|
|
|
})
|
2023-10-04 19:04:09 +00:00
|
|
|
const {
|
2023-10-19 15:05:22 +00:00
|
|
|
subscriptions,
|
2024-01-11 17:40:06 +00:00
|
|
|
actions: { loadSubscriptions },
|
2023-10-04 19:04:09 +00:00
|
|
|
} = useSession()
|
|
|
|
|
2023-10-19 15:05:22 +00:00
|
|
|
const subscribed = createMemo(() =>
|
2023-11-14 15:10:00 +00:00
|
|
|
subscriptions().topics.some((topic) => topic.slug === props.topic.slug),
|
2023-10-19 15:05:22 +00:00
|
|
|
)
|
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)
|
|
|
|
}
|
|
|
|
|
2024-01-04 07:14:57 +00:00
|
|
|
const title = () =>
|
|
|
|
lang() === 'en' ? capitalize(props.topic.slug.replaceAll('-', ' ')) : props.topic.title
|
|
|
|
|
2023-10-04 19:04:09 +00:00
|
|
|
return (
|
|
|
|
<div class={styles.TopicBadge}>
|
2024-01-11 17:40:06 +00:00
|
|
|
<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 })}')`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<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 })}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<div class={clsx('text-truncate', styles.description)}>{props.topic.body}</div>
|
|
|
|
</Show>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class={styles.actions}>
|
2023-10-05 21:43:53 +00:00
|
|
|
<Show
|
2024-01-11 17:40:06 +00:00
|
|
|
when={!props.minimizeSubscribeButton}
|
2023-10-05 21:43:53 +00:00
|
|
|
fallback={
|
2024-01-11 17:40:06 +00:00
|
|
|
<CheckButton text={t('Follow')} checked={subscribed()} onClick={() => subscribe(!subscribed)} />
|
2023-10-05 21:43:53 +00:00
|
|
|
}
|
|
|
|
>
|
2023-10-04 19:04:09 +00:00
|
|
|
<Show
|
2024-01-11 17:40:06 +00:00
|
|
|
when={subscribed()}
|
2023-10-04 19:04:09 +00:00
|
|
|
fallback={
|
|
|
|
<Button
|
2024-01-11 17:40:06 +00:00
|
|
|
variant="primary"
|
2023-10-04 19:04:09 +00:00
|
|
|
size="S"
|
2024-01-11 17:40:06 +00:00
|
|
|
value={isSubscribing() ? t('subscribing...') : t('Subscribe')}
|
|
|
|
onClick={() => subscribe(true)}
|
2023-10-09 21:22:06 +00:00
|
|
|
class={styles.subscribeButton}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
2024-01-11 17:40:06 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
onClick={() => subscribe(false)}
|
|
|
|
variant="bordered"
|
|
|
|
size="S"
|
|
|
|
value={t('Following')}
|
|
|
|
class={styles.subscribeButton}
|
|
|
|
/>
|
2023-10-04 19:04:09 +00:00
|
|
|
</Show>
|
2024-01-11 17:40:06 +00:00
|
|
|
</Show>
|
|
|
|
</div>
|
2023-10-04 19:04:09 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|