webapp/src/components/Views/AllTopics.tsx

152 lines
5.0 KiB
TypeScript
Raw Normal View History

import { createEffect, createMemo, createSignal, For, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import type { Topic } from '../../graphql/types.gen'
2022-09-22 09:37:49 +00:00
import { Icon } from '../Nav/Icon'
2022-09-09 11:53:35 +00:00
import { t } from '../../utils/intl'
2022-10-21 18:17:04 +00:00
import { setTopicsSort, useTopicsStore } from '../../stores/zine/topics'
2022-09-22 09:37:49 +00:00
import { handleClientRouteLinkClick, useRouter } from '../../stores/router'
2022-09-09 11:53:35 +00:00
import { TopicCard } from '../Topic/Card'
2022-11-09 19:02:12 +00:00
import styles from '../../styles/AllTopics.module.scss'
import { clsx } from 'clsx'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
type AllTopicsPageSearchParams = {
by: 'shouts' | 'authors' | 'title' | ''
}
2022-09-14 11:28:43 +00:00
2022-09-29 17:37:21 +00:00
type AllTopicsViewProps = {
2022-09-22 09:37:49 +00:00
topics: Topic[]
}
const PAGE_SIZE = 20
2022-09-29 17:37:21 +00:00
export const AllTopicsView = (props: AllTopicsViewProps) => {
2022-10-21 18:17:04 +00:00
const { searchParams, changeSearchParam } = useRouter<AllTopicsPageSearchParams>()
const [limit, setLimit] = createSignal(PAGE_SIZE)
2022-09-14 11:28:43 +00:00
2022-09-28 20:16:44 +00:00
const { sortedTopics } = useTopicsStore({
2022-09-22 09:37:49 +00:00
topics: props.topics,
2022-10-21 18:17:04 +00:00
sortBy: searchParams().by || 'shouts'
2022-09-22 09:37:49 +00:00
})
2022-09-29 17:37:21 +00:00
2022-11-14 10:02:08 +00:00
const { session } = useSession()
2022-09-14 11:28:43 +00:00
2022-09-09 11:53:35 +00:00
createEffect(() => {
2022-10-21 18:17:04 +00:00
setTopicsSort(searchParams().by || 'shouts')
setLimit(PAGE_SIZE)
2022-09-22 09:37:49 +00:00
})
2022-10-05 15:56:59 +00:00
const byLetter = createMemo<{ [letter: string]: Topic[] }>(() => {
return sortedTopics().reduce((acc, topic) => {
2022-10-05 17:26:47 +00:00
const letter = topic.title[0].toUpperCase()
2022-10-05 15:56:59 +00:00
if (!acc[letter]) {
acc[letter] = []
}
acc[letter].push(topic)
return acc
}, {} as { [letter: string]: Topic[] })
})
const sortedKeys = createMemo<string[]>(() => {
const keys = Object.keys(byLetter())
keys.sort()
return keys
})
2022-10-04 12:16:07 +00:00
const subscribed = (s) => Boolean(session()?.news?.topics && session()?.news?.topics?.includes(s || ''))
2022-09-09 11:53:35 +00:00
const showMore = () => setLimit((oldLimit) => oldLimit + PAGE_SIZE)
2022-09-09 11:53:35 +00:00
return (
2022-11-11 10:22:07 +00:00
<div class={clsx(styles.allTopicsPage, 'container')}>
2022-09-28 20:16:44 +00:00
<Show when={sortedTopics().length > 0}>
2022-11-11 10:22:07 +00:00
<div class="shift-content">
<div class="row">
<div class={clsx(styles.pageHeader, 'col-lg-9')}>
<h1>{t('Topics')}</h1>
<div class="col-lg-10">
<p>{t('Subscribe what you like to tune your personal feed')}</p>
2022-09-09 11:53:35 +00:00
</div>
2022-09-14 11:28:43 +00:00
</div>
2022-09-09 11:53:35 +00:00
2022-11-11 10:22:07 +00:00
<div class="col-12">
<ul class={clsx(styles.viewSwitcher, 'view-switcher col-lg-10')}>
<li classList={{ selected: searchParams().by === 'shouts' || !searchParams().by }}>
<a href="/topics?by=shouts" onClick={handleClientRouteLinkClick}>
{t('By shouts')}
</a>
</li>
<li classList={{ selected: searchParams().by === 'authors' }}>
<a href="/topics?by=authors" onClick={handleClientRouteLinkClick}>
{t('By authors')}
</a>
</li>
<li classList={{ selected: searchParams().by === 'title' }}>
<a
href="/topics?by=title"
onClick={(ev) => {
// just an example
ev.preventDefault()
changeSearchParam('by', 'title')
}}
>
{t('By alphabet')}
</a>
</li>
<li class="view-switcher__search">
<a href="/topic/search">
<Icon name="search" />
{t('Search topic')}
</a>
</li>
</ul>
</div>
</div>
2022-09-09 11:53:35 +00:00
2022-11-11 10:22:07 +00:00
<Show
when={searchParams().by === 'title'}
fallback={() => (
<>
<For each={sortedTopics().slice(0, limit())}>
{(topic) => (
<TopicCard topic={topic} compact={false} subscribed={subscribed(topic.slug)} />
)}
</For>
<Show when={sortedTopics().length > limit()}>
<div class={styles.loadMoreContainer}>
<button class={clsx('button', styles.loadMoreButton)} onClick={showMore}>
{t('More')}
</button>
</div>
</Show>
</>
2022-11-11 10:22:07 +00:00
)}
>
<For each={sortedKeys()}>
{(letter) => (
<div class={clsx(styles.group, 'group')}>
<h2>{letter}</h2>
<div class="container">
<div class="row">
<For each={byLetter()[letter]}>
2022-10-05 15:56:59 +00:00
{(topic) => (
2022-11-11 10:22:07 +00:00
<div class={clsx(styles.topic, 'topic col-sm-6 col-md-3')}>
<div class="topic-title">
<a href={`/topic/${topic.slug}`}>{topic.title}</a>
</div>
</div>
2022-10-05 15:56:59 +00:00
)}
</For>
</div>
2022-11-11 10:22:07 +00:00
</div>
</div>
)}
</For>
</Show>
2022-09-14 11:28:43 +00:00
</div>
</Show>
</div>
2022-09-09 11:53:35 +00:00
)
}