webapp/src/components/Views/AllTopics.tsx

205 lines
6.7 KiB
TypeScript
Raw Normal View History

import { createEffect, createMemo, createSignal, For, onMount, 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-17 20:08:12 +00:00
import { translit } from '../../utils/ru2en'
import styles from '../../styles/AllTopics.module.scss'
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'
import { StatMetrics } from '../_shared/StatMetrics'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
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
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
2022-11-14 10:02:08 +00:00
const { session } = useSession()
2022-09-14 11:28:43 +00:00
2022-11-25 05:54:19 +00:00
onMount(() => {
if (!searchParams().by) {
changeSearchParam('by', 'shouts')
}
})
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-11-17 17:20:19 +00:00
let letter = topic.title[0].toUpperCase()
2023-02-17 09:21:02 +00:00
if (/[^ËА-яё]/.test(letter) && lang() === 'ru') letter = '#'
2022-11-17 17:20:19 +00:00
if (!acc[letter]) acc[letter] = []
2022-10-05 15:56:59 +00:00
acc[letter].push(topic)
return acc
}, {} as { [letter: string]: Topic[] })
})
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
})
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)
const [searchQuery, setSearchQuery] = createSignal('')
const filteredResults = createMemo(() => {
/* very stupid filter by string algorithm with no deps */
let q = searchQuery().toLowerCase()
if (q.length === 0) {
return sortedTopics()
}
2023-02-17 09:21:02 +00:00
if (lang() === 'ru') {
q = translit(q)
2022-11-17 20:08:12 +00:00
}
return sortedTopics().filter((topic) => {
if (topic.slug.split('-').some((w) => w.startsWith(q))) {
return true
}
let title = topic.title.toLowerCase()
2023-02-17 09:21:02 +00:00
if (lang() === 'ru') {
title = translit(title)
}
return title.split(' ').some((word) => word.startsWith(q))
})
})
2022-11-17 20:08:12 +00:00
const AllTopicsHead = () => (
2022-11-20 21:23:12 +00:00
<div class="row">
2023-02-17 09:21:02 +00:00
<div class={clsx('col-lg-10 col-xl-9')}>
2022-11-20 21:23:12 +00:00
<h1>{t('Topics')}</h1>
<p>{t('Subscribe what you like to tune your personal feed')}</p>
<ul class={clsx(styles.viewSwitcher, 'view-switcher')}>
<li classList={{ selected: searchParams().by === 'shouts' }}>
<a href="/topics?by=shouts">{t('By shouts')}</a>
</li>
<li classList={{ selected: searchParams().by === 'authors' }}>
<a href="/topics?by=authors">{t('By authors')}</a>
</li>
<li classList={{ selected: searchParams().by === 'title' }}>
<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')}>
2022-11-17 20:08:12 +00:00
<div class="shift-content">
2022-11-18 18:33:31 +00:00
<AllTopicsHead />
<Show when={filteredResults().length > 0}>
2022-11-17 20:08:12 +00:00
<Show when={searchParams().by === 'title'}>
<div class="col-lg-10 col-xl-9">
<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>
2022-11-18 18:33:31 +00:00
</div>
2022-11-11 10:22:07 +00:00
<For each={sortedKeys()}>
{(letter) => (
2022-11-11 10:22:07 +00:00
<div class={clsx(styles.group, 'group')}>
<h2 id={`letter-${ALPHABET.indexOf(letter)}`}>{letter}</h2>
2022-11-11 10:22:07 +00:00
<div class="container">
<div class="row">
2022-11-14 21:58:33 +00:00
<div class="col-lg-10">
<div class="row">
<For each={byLetter()[letter]}>
{(topic) => (
<div class={clsx(styles.topic, 'topic col-sm-6 col-md-4')}>
<a href={`/topic/${topic.slug}`}>{topic.title}</a>
<span class={styles.articlesCounter}>{topic.stat.shouts}</span>
2022-11-14 21:58:33 +00:00
</div>
)}
</For>
</div>
</div>
2022-10-05 15:56:59 +00:00
</div>
2022-11-11 10:22:07 +00:00
</div>
</div>
)}
</For>
</Show>
2022-11-17 20:08:12 +00:00
2022-11-19 08:09:52 +00:00
<Show when={searchParams().by && searchParams().by !== 'title'}>
<For each={filteredResults().slice(0, limit())}>
2022-11-17 20:08:12 +00:00
{(topic) => (
<>
<TopicCard
topic={topic}
compact={false}
subscribed={subscribed(topic.slug)}
showPublications={true}
/>
<StatMetrics fields={['shouts', 'authors', 'followers']} stat={topic.stat} />
</>
2022-11-17 20:08:12 +00:00
)}
</For>
</Show>
<Show when={filteredResults().length > limit() && searchParams().by !== 'title'}>
<div class={clsx(styles.loadMoreContainer, 'col-12 col-md-10 offset-md-1')}>
<button class={clsx('button', styles.loadMoreButton)} onClick={showMore}>
{t('Load more')}
</button>
2022-11-17 20:08:12 +00:00
</div>
</Show>
</Show>
</div>
2022-09-14 11:28:43 +00:00
</div>
2022-09-09 11:53:35 +00:00
)
}