webapp/src/components/Views/AllTopics.tsx

202 lines
7.1 KiB
TypeScript
Raw Normal View History

2023-11-04 15:37:28 +00:00
import { createEffect, createMemo, createSignal, For, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import type { Topic } from '../../graphql/types.gen'
2023-02-17 09:21:02 +00:00
2022-10-21 18:17:04 +00:00
import { setTopicsSort, useTopicsStore } from '../../stores/zine/topics'
import { 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 { clsx } from 'clsx'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2022-11-18 18:33:31 +00:00
import { SearchField } from '../_shared/SearchField'
2022-11-19 08:16:00 +00:00
import { scrollHandler } from '../../utils/scroll'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import { dummyFilter } from '../../utils/dummyFilter'
2022-09-09 11:53:35 +00:00
import styles from './AllTopics.module.scss'
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
const ALPHABET = [...'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#']
2022-09-29 17:37:21 +00:00
export const AllTopicsView = (props: AllTopicsViewProps) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
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
const { subscriptions } = useSession()
2022-09-14 11:28:43 +00:00
2023-11-02 17:43:22 +00:00
createEffect(() => {
2022-11-25 05:54:19 +00:00
if (!searchParams().by) {
changeSearchParam({
by: 'shouts'
})
2022-11-25 05:54:19 +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) => {
let letter = topic.title[0].toUpperCase()
if (/[^ËА-яё]/.test(letter) && lang() === 'ru') letter = '#'
if (!acc[letter]) acc[letter] = []
acc[letter].push(topic)
return acc
},
{} as { [letter: string]: Topic[] }
)
2022-10-05 15:56:59 +00:00
})
const sortedKeys = createMemo<string[]>(() => {
const keys = Object.keys(byLetter())
keys.sort()
keys.push(keys.shift())
2022-10-05 15:56:59 +00:00
return keys
})
const subscribed = (topicSlug: string) => subscriptions().topics.some((topic) => topic.slug === topicSlug)
2022-09-09 11:53:35 +00:00
const showMore = () => setLimit((oldLimit) => oldLimit + PAGE_SIZE)
const [searchQuery, setSearchQuery] = createSignal('')
const filteredResults = createMemo(() => {
return dummyFilter(sortedTopics(), searchQuery(), lang())
})
2022-11-17 20:08:12 +00:00
const AllTopicsHead = () => (
2022-11-20 21:23:12 +00:00
<div class="row">
<div class="col-lg-20 col-xl-18">
2022-11-20 21:23:12 +00:00
<h1>{t('Topics')}</h1>
<p>{t('Subscribe what you like to tune your personal feed')}</p>
2023-05-22 22:01:04 +00:00
<ul class="view-switcher">
<li classList={{ 'view-switcher__item--selected': searchParams().by === 'shouts' }}>
2022-11-20 21:23:12 +00:00
<a href="/topics?by=shouts">{t('By shouts')}</a>
</li>
2023-05-22 22:01:04 +00:00
<li classList={{ 'view-switcher__item--selected': searchParams().by === 'authors' }}>
2022-11-20 21:23:12 +00:00
<a href="/topics?by=authors">{t('By authors')}</a>
</li>
2023-05-22 22:01:04 +00:00
<li classList={{ 'view-switcher__item--selected': searchParams().by === 'title' }}>
2022-11-20 21:23:12 +00:00
<a href="/topics?by=title">{t('By title')}</a>
</li>
<Show when={searchParams().by !== 'title'}>
<li class="view-switcher__search">
<SearchField onChange={(value) => setSearchQuery(value)} />
</li>
</Show>
2022-11-20 21:23:12 +00:00
</ul>
</div>
2022-11-17 20:08:12 +00:00
</div>
)
2022-11-19 08:09:52 +00:00
2022-09-09 11:53:35 +00:00
return (
<div class={clsx(styles.allTopicsPage, 'wide-container')}>
2023-03-10 17:42:48 +00:00
<div class="row">
<div class="col-md-19 offset-md-5">
<AllTopicsHead />
<Show when={filteredResults().length > 0}>
<Show when={searchParams().by === 'title'}>
<div class="col-lg-20 col-xl-18">
<ul class={clsx('nodash', styles.alphabet)}>
<For each={ALPHABET}>
{(letter, index) => (
<li>
<Show when={letter in byLetter()} fallback={letter}>
<a
href={`/topics?by=title#letter-${index()}`}
onClick={(event) => {
event.preventDefault()
scrollHandler(`letter-${index()}`)
}}
>
{letter}
</a>
</Show>
</li>
)}
</For>
</ul>
</div>
<For each={sortedKeys()}>
{(letter) => (
<div class={clsx(styles.group, 'group')}>
<h2 id={`letter-${ALPHABET.indexOf(letter)}`}>{letter}</h2>
2023-07-18 21:50:27 +00:00
<div class="row">
<div class="col-lg-20">
<div class="row">
<For each={byLetter()[letter]}>
{(topic) => (
<div class={clsx(styles.topic, 'topic col-sm-12 col-md-8')}>
<a href={`/topic/${topic.slug}`}>{topic.title}</a>
<span class={styles.articlesCounter}>{topic.stat.shouts}</span>
</div>
)}
</For>
2022-11-14 21:58:33 +00:00
</div>
</div>
2022-10-05 15:56:59 +00:00
</div>
2022-11-11 10:22:07 +00:00
</div>
2023-03-10 17:42:48 +00:00
)}
</For>
</Show>
<Show when={searchParams().by && searchParams().by !== 'title'}>
2023-05-17 20:27:24 +00:00
<div class="row">
2023-05-17 21:45:53 +00:00
<div class="col-lg-20 col-xl-18">
2023-05-17 20:27:24 +00:00
<For each={filteredResults().slice(0, limit())}>
{(topic) => (
<>
<TopicCard
topic={topic as Topic}
2023-05-17 20:27:24 +00:00
compact={false}
subscribed={subscribed(topic.slug)}
showPublications={true}
showDescription={true}
/>
<div class={styles.stats}>
<span class={styles.statsItem}>
{t('shoutsWithCount', { count: topic.stat.shouts })}
</span>
<span class={styles.statsItem}>
{t('authorsWithCount', { count: topic.stat.authors })}
</span>
<span class={styles.statsItem}>
{t('followersWithCount', { count: topic.stat.followers })}
</span>
</div>
2023-05-17 20:27:24 +00:00
</>
)}
</For>
</div>
</div>
2023-03-10 17:42:48 +00:00
</Show>
<Show when={filteredResults().length > limit() && searchParams().by !== 'title'}>
<div class={clsx(styles.loadMoreContainer, 'col-24 col-md-20 col-lg-14 offset-md-2')}>
<button class={clsx('button', styles.loadMoreButton)} onClick={showMore}>
{t('Load more')}
</button>
</div>
</Show>
2022-11-11 10:22:07 +00:00
</Show>
2023-03-10 17:42:48 +00:00
</div>
2022-11-17 20:08:12 +00:00
</div>
2022-09-14 11:28:43 +00:00
</div>
2022-09-09 11:53:35 +00:00
)
}