webapp/src/components/Views/AllTopics.tsx

146 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-10-05 15:56:59 +00:00
import { createEffect, createMemo, 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-09-30 14:22:33 +00:00
import { useAuthStore } from '../../stores/auth'
2022-11-09 19:02:12 +00:00
import styles from '../../styles/AllTopics.module.scss'
import cardStyles from '../Topic/Card.module.scss'
import { clsx } from 'clsx'
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[]
}
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>()
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-09-30 14:22:33 +00:00
const { session } = useAuthStore()
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')
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
return (
2022-11-09 19:02:12 +00:00
<div class={styles.allTopicsPage}>
2022-09-28 20:16:44 +00:00
<Show when={sortedTopics().length > 0}>
2022-09-14 11:28:43 +00:00
<div class="wide-container">
<div class="shift-content">
<div class="row">
2022-11-09 19:02:12 +00:00
<div class={clsx(styles.pageHeader, 'col-lg-9')}>
2022-09-14 11:28:43 +00:00
<h1>{t('Topics')}</h1>
2022-11-09 19:02:12 +00:00
<div class="col-lg-10">
<p>{t('Subscribe what you like to tune your personal feed')}</p>
</div>
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-09-14 11:28:43 +00:00
<div class="row">
<div class="col">
2022-11-09 19:02:12 +00:00
<div class="row">
<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>
2022-09-09 11:53:35 +00:00
2022-10-05 15:56:59 +00:00
<Show
2022-10-21 18:17:04 +00:00
when={searchParams().by === 'title'}
2022-10-05 15:56:59 +00:00
fallback={() => (
2022-11-09 19:02:12 +00:00
<div class={cardStyles.stats}>
2022-10-05 15:56:59 +00:00
<For each={sortedTopics()}>
{(topic) => (
<TopicCard topic={topic} compact={false} subscribed={subscribed(topic.slug)} />
)}
</For>
</div>
)}
>
<For each={sortedKeys()}>
{(letter) => (
2022-11-09 19:02:12 +00:00
<div class={clsx(styles.group, 'group')}>
2022-10-05 15:56:59 +00:00
<h2>{letter}</h2>
<div class="container">
<div class="row">
<For each={byLetter()[letter]}>
{(topic) => (
2022-11-09 19:02:12 +00:00
<div class={clsx(styles.topic, 'topic col-sm-6 col-md-3')}>
2022-10-05 15:56:59 +00:00
<div class="topic-title">
<a href={`/topic/${topic.slug}`}>{topic.title}</a>
</div>
</div>
)}
</For>
</div>
</div>
</div>
2022-09-09 11:53:35 +00:00
)}
2022-09-14 11:28:43 +00:00
</For>
2022-10-05 15:56:59 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
2022-09-14 11:28:43 +00:00
</div>
</Show>
</div>
2022-09-09 11:53:35 +00:00
)
}