webapp/src/components/Topic/Full.tsx

93 lines
2.9 KiB
TypeScript
Raw Normal View History

import type { Author, Topic } from '~/graphql/schema/core.gen'
2023-02-17 09:21:02 +00:00
2022-11-07 21:07:42 +00:00
import { clsx } from 'clsx'
2024-07-09 09:13:13 +00:00
import { Show, createEffect, createMemo, createSignal } from 'solid-js'
import { useFollowing } from '~/context/following'
import { useLocalize } from '~/context/localize'
import { useSession } from '~/context/session'
import { FollowingEntity } from '~/graphql/schema/core.gen'
import { Button } from '../_shared/Button'
2022-09-09 11:53:35 +00:00
2024-07-09 09:13:13 +00:00
import { capitalize } from '~/utils/capitalize'
2024-06-06 09:04:01 +00:00
import { FollowingCounters } from '../_shared/FollowingCounters/FollowingCounters'
2024-05-10 14:14:06 +00:00
import { Icon } from '../_shared/Icon'
import styles from './Full.module.scss'
2022-09-09 11:53:35 +00:00
type Props = {
topic: Topic
2024-05-18 16:45:36 +00:00
followers?: Author[]
2024-05-18 17:16:45 +00:00
authors?: Author[]
2022-09-09 11:53:35 +00:00
}
export const FullTopic = (props: Props) => {
2024-07-09 09:13:13 +00:00
const { t, lang } = useLocalize()
2024-05-20 11:16:54 +00:00
const { follows, changeFollowing } = useFollowing()
2024-02-04 17:40:15 +00:00
const { requireAuthentication } = useSession()
2024-01-31 12:34:15 +00:00
const [followed, setFollowed] = createSignal()
2024-07-09 17:41:14 +00:00
const title = createMemo(() => {
/* FIXME: use title translation*/
return `#${capitalize(
lang() === 'en'
? props.topic.slug.replace(/-/, ' ')
: props.topic.title || props.topic.slug.replace(/-/, ' '),
true
)}`
})
2024-01-31 12:34:15 +00:00
createEffect(() => {
2024-06-24 17:50:27 +00:00
if (follows?.topics?.length !== 0) {
2024-05-20 11:16:54 +00:00
const items = follows.topics || []
2024-01-31 12:34:15 +00:00
setFollowed(items.some((x: Topic) => x?.slug === props.topic?.slug))
}
})
2024-06-24 17:50:27 +00:00
const handleFollowClick = (_ev?: MouseEvent | undefined) => {
2024-01-31 12:34:15 +00:00
const really = !followed()
setFollowed(really)
requireAuthentication(() => {
2024-05-20 11:16:54 +00:00
changeFollowing(FollowingEntity.Topic, props.topic.slug, really)
}, 'follow')
}
2022-09-09 11:53:35 +00:00
return (
2023-03-10 17:42:48 +00:00
<div class={clsx(styles.topicHeader, 'col-md-16 col-lg-12 offset-md-4 offset-lg-6')}>
2024-07-09 09:13:13 +00:00
<h1>{title()}</h1>
2024-06-24 17:50:27 +00:00
<p class={styles.topicDescription} innerHTML={props.topic?.body || ''} />
2024-05-10 14:14:06 +00:00
<div class={styles.topicDetails}>
<Show when={props.topic?.stat}>
<div class={styles.topicDetailsItem}>
<Icon name="feed-all" class={styles.topicDetailsIcon} />
2024-06-06 09:27:49 +00:00
{t('some posts', {
2024-06-26 08:22:05 +00:00
count: props.topic?.stat?.shouts ?? 0
2024-05-10 14:14:06 +00:00
})}
</div>
</Show>
<FollowingCounters
2024-05-18 17:16:45 +00:00
followers={props.followers}
followersAmount={props.topic?.stat?.followers}
2024-06-06 09:04:01 +00:00
authors={props.authors}
authorsAmount={props.topic?.stat?.authors || props.authors?.length || 0}
2024-05-18 17:16:45 +00:00
/>
2024-05-10 14:14:06 +00:00
</div>
2023-02-17 09:21:02 +00:00
<div class={clsx(styles.topicActions)}>
2024-01-31 12:34:15 +00:00
<Button
variant="primary"
onClick={handleFollowClick}
value={followed() ? t('Unfollow the topic') : t('Follow the topic')}
2024-05-10 14:14:06 +00:00
class={styles.followControl}
2024-01-31 12:34:15 +00:00
/>
2024-07-03 21:25:03 +00:00
<a class={styles.writeControl} href={`/edit/new/?topicId=${props.topic?.id}`}>
{t('Write about the topic')}
</a>
2023-02-17 09:21:02 +00:00
</div>
2023-12-27 22:50:42 +00:00
<Show when={props.topic?.pic}>
2024-06-24 17:50:27 +00:00
<img src={props.topic?.pic || ''} alt={props.topic?.title || ''} />
2022-11-07 21:07:42 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</div>
)
}