webapp/src/components/Views/AllTopics/AllTopics.tsx

205 lines
7.6 KiB
TypeScript
Raw Normal View History

2024-02-08 09:11:52 +00:00
import type { Topic } from '../../../graphql/schema/core.gen'
2023-02-17 09:21:02 +00:00
import { Meta } from '@solidjs/meta'
2022-11-09 19:02:12 +00:00
import { clsx } from 'clsx'
2024-05-07 00:05:04 +00:00
import { For, Show, createEffect, createMemo, createSignal } from 'solid-js'
2024-02-08 09:11:52 +00:00
import { useLocalize } from '../../../context/localize'
2024-05-06 23:44:25 +00:00
import { useTopics } from '../../../context/topics'
2024-02-08 09:11:52 +00:00
import { useRouter } from '../../../stores/router'
import { capitalize } from '../../../utils/capitalize'
import { dummyFilter } from '../../../utils/dummyFilter'
import { getImageUrl } from '../../../utils/getImageUrl'
import { scrollHandler } from '../../../utils/scroll'
import { Loading } from '../../_shared/Loading'
import { SearchField } from '../../_shared/SearchField'
import { TopicBadge } from '../../Topic/TopicBadge'
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
type Props = {
2022-09-22 09:37:49 +00:00
topics: Topic[]
isLoaded: boolean
2022-09-22 09:37:49 +00:00
}
2024-02-29 20:46:15 +00:00
export const PAGE_SIZE = 20
2024-02-08 09:11:52 +00:00
export const AllTopics = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
const { searchParams, changeSearchParams } = useRouter<AllTopicsPageSearchParams>()
const [limit, setLimit] = createSignal(PAGE_SIZE)
2023-12-08 11:49:50 +00:00
const ALPHABET =
lang() === 'ru' ? [...'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#'] : [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ#']
2024-05-07 00:05:04 +00:00
const { sortedTopics, setTopicsSort } = useTopics()
2022-09-29 17:37:21 +00:00
2023-11-02 17:43:22 +00:00
createEffect(() => {
2022-11-25 05:54:19 +00:00
if (!searchParams().by) {
changeSearchParams({
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) => {
2023-12-15 13:45:34 +00:00
let letter = lang() === 'en' ? topic.slug[0].toUpperCase() : topic.title[0].toUpperCase()
if (/[^ËА-яё]/.test(letter) && lang() === 'ru') letter = '#'
2023-12-08 11:49:50 +00:00
if (/[^A-z]/.test(letter) && lang() === 'en') 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 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-18 col-xl-15">
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
const ogImage = getImageUrl('production/image/logo_image.png')
const ogTitle = t('Themes and plots')
const description = t(
'Thematic table of contents of the magazine. Here you can find all the topics that the community authors wrote about',
)
2022-09-09 11:53:35 +00:00
return (
<div class={clsx(styles.allTopicsPage, 'wide-container')}>
<Meta name="descprition" content={description} />
<Meta name="keywords" content={t('keywords')} />
<Meta name="og:type" content="article" />
<Meta name="og:title" content={ogTitle} />
<Meta name="og:image" content={ogImage} />
<Meta name="twitter:image" content={ogImage} />
<Meta name="og:description" content={description} />
<Meta name="twitter:card" content="summary_large_image" />
<Meta name="twitter:title" content={ogTitle} />
<Meta name="twitter:description" content={description} />
<Show when={props.isLoaded} fallback={<Loading />}>
<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-18 col-xl-15">
<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>
<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}`}>
{lang() === 'en'
? capitalize(topic.slug.replaceAll('-', ' '))
: topic.title}
</a>
<span class={styles.articlesCounter}>{topic.stat.shouts}</span>
</div>
)}
</For>
</div>
2022-11-14 21:58:33 +00:00
</div>
</div>
2022-10-05 15:56:59 +00:00
</div>
)}
</For>
</Show>
<Show when={searchParams().by && searchParams().by !== 'title'}>
<div class="row">
<div class="col-lg-18 col-xl-15 py-4">
<For each={filteredResults().slice(0, limit())}>
{(topic) => (
<>
2024-03-15 15:24:33 +00:00
<TopicBadge topic={topic} showStat={true} />
</>
)}
</For>
2022-11-11 10:22:07 +00:00
</div>
2023-05-17 20:27:24 +00:00
</div>
</Show>
2023-03-10 17:42:48 +00:00
<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>
2023-03-10 17:42:48 +00:00
</Show>
</div>
2023-03-10 17:42:48 +00:00
</div>
</Show>
2022-09-14 11:28:43 +00:00
</div>
2022-09-09 11:53:35 +00:00
)
}