webapp/src/components/Views/AllAuthors.tsx

217 lines
7.5 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 { Author } from '../../graphql/types.gen'
import { AuthorCard } from '../Author/Card'
import { t } from '../../utils/intl'
2022-10-21 18:17:04 +00:00
import { useAuthorsStore, setAuthorsSort } from '../../stores/zine/authors'
import { useRouter } from '../../stores/router'
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-11-17 17:20:19 +00:00
import { locale } from '../../stores/ui'
2022-11-19 08:09:52 +00:00
import { translit } from '../../utils/ru2en'
import { SearchField } from '../_shared/SearchField'
2022-11-19 08:16:00 +00:00
import { scrollHandler } from '../../utils/scroll'
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
type AllAuthorsPageSearchParams = {
by: '' | 'name' | 'shouts' | 'rating'
}
type Props = {
authors: Author[]
}
const PAGE_SIZE = 20
2022-11-19 08:16:00 +00:00
const ALPHABET = [...'@АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ']
2022-09-22 09:37:49 +00:00
export const AllAuthorsView = (props: Props) => {
2022-10-05 11:13:15 +00:00
const { sortedAuthors } = useAuthorsStore({ authors: props.authors })
const [limit, setLimit] = createSignal(PAGE_SIZE)
2022-09-30 14:22:33 +00:00
2022-11-14 10:02:08 +00:00
const { session } = useSession()
2022-09-30 14:22:33 +00:00
2022-10-05 15:56:59 +00:00
createEffect(() => {
2022-10-21 18:17:04 +00:00
setAuthorsSort(searchParams().by || 'shouts')
2022-10-05 15:56:59 +00:00
})
2022-10-04 12:16:07 +00:00
const subscribed = (s) => Boolean(session()?.news?.authors && session()?.news?.authors?.includes(s || ''))
2022-09-14 11:28:43 +00:00
2022-11-19 08:09:52 +00:00
const { searchParams, changeSearchParam } = useRouter<AllAuthorsPageSearchParams>()
2022-09-14 11:28:43 +00:00
2022-10-05 15:56:59 +00:00
const byLetter = createMemo<{ [letter: string]: Author[] }>(() => {
return sortedAuthors().reduce((acc, author) => {
2022-11-17 17:20:19 +00:00
let letter = author.name.trim().split(' ').pop().at(0).toUpperCase()
2022-11-20 21:25:59 +00:00
if (!/[А-Я]/i.test(letter) && locale() === '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(author)
return acc
}, {} as { [letter: string]: Author[] })
})
const sortedKeys = createMemo<string[]>(() => {
const keys = Object.keys(byLetter())
keys.sort()
return keys
2022-10-05 11:13:15 +00:00
})
const showMore = () => setLimit((oldLimit) => oldLimit + PAGE_SIZE)
2022-11-19 08:09:52 +00:00
const AllAuthorsHead = () => (
<div class="row">
<div class={clsx(styles.pageHeader, 'col-lg-10 col-xl-9')}>
<h1>{t('Authors')}</h1>
<p>{t('Subscribe who you like to tune your personal feed')}</p>
2022-09-09 11:53:35 +00:00
2022-11-19 08:09:52 +00:00
<ul class={clsx(styles.viewSwitcher, 'view-switcher')}>
<li classList={{ selected: searchParams().by === 'shouts' }}>
<a href="/authors?by=shouts">{t('By shouts')}</a>
</li>
<li classList={{ selected: searchParams().by === 'rating' }}>
<a href="/authors?by=rating">{t('By rating')}</a>
</li>
<li classList={{ selected: !searchParams().by || searchParams().by === 'name' }}>
<a href="/authors?by=name">{t('By name')}</a>
</li>
<li class="view-switcher__search">
<SearchField onChange={searchAuthors} />
</li>
</ul>
</div>
</div>
)
const [searchResults, setSearchResults] = createSignal<Author[]>([])
// eslint-disable-next-line sonarjs/cognitive-complexity
const searchAuthors = (value) => {
/* very stupid search algorithm with no deps */
let q = value.toLowerCase()
if (q.length > 0) {
console.debug(q)
setSearchResults([])
if (locale() === 'ru') q = translit(q, 'ru')
const aaa: Author[] = []
sortedAuthors().forEach((a) => {
let flag = false
a.slug.split('-').forEach((w) => {
if (w.startsWith(q)) flag = true
})
if (!flag) {
let wrds: string = a.name.toLowerCase()
if (locale() === 'ru') wrds = translit(wrds, 'ru')
wrds.split(' ').forEach((w: string) => {
if (w.startsWith(q)) flag = true
})
}
if (flag && !aaa.includes(a)) aaa.push(a)
})
setSearchResults((sr: Author[]) => [...sr, ...aaa])
changeSearchParam('by', '')
}
}
2022-09-09 11:53:35 +00:00
return (
<div class={clsx(styles.allTopicsPage, 'wide-container')}>
2022-11-19 08:09:52 +00:00
<Show when={sortedAuthors().length > 0 || searchResults().length > 0}>
2022-11-11 10:22:07 +00:00
<div class="shift-content">
2022-11-19 08:09:52 +00:00
<AllAuthorsHead />
2022-11-14 21:58:33 +00:00
2022-11-19 08:09:52 +00:00
<Show when={searchParams().by === 'name'}>
<div class="row">
<div class="col-lg-10 col-xl-9">
<ul class={clsx('nodash', styles.alphabet)}>
<For each={ALPHABET}>
{(letter: string, index) => (
<li>
<Show when={letter in byLetter()} fallback={letter}>
<a
href={`/authors?by=name#letter-${index()}`}
onClick={() => scrollHandler(`letter-${index()}`)}
>
{letter}
</a>
</Show>
</li>
)}
</For>
</ul>
2022-11-14 21:58:33 +00:00
</div>
2022-11-19 08:09:52 +00:00
</div>
2022-11-14 21:58:33 +00:00
<For each={sortedKeys()}>
2022-11-19 08:09:52 +00:00
{(letter, index) => (
2022-11-14 21:58:33 +00:00
<div class={clsx(styles.group, 'group')}>
2022-11-19 08:09:52 +00:00
<h2 id={`letter-${index()}`}>{letter}</h2>
2022-11-14 21:58:33 +00:00
<div class="container">
<div class="row">
<div class="col-lg-10">
2022-11-11 10:22:07 +00:00
<div class="row">
<For each={byLetter()[letter]}>
2022-11-19 08:09:52 +00:00
{(author) => (
2022-11-14 21:58:33 +00:00
<div class={clsx(styles.topic, 'topic col-sm-6 col-md-4')}>
2022-11-11 10:22:07 +00:00
<div class="topic-title">
<a href={`/author/${author.slug}`}>{author.name}</a>
2022-11-20 21:23:12 +00:00
<span class={styles.articlesCounter}>{author.stat.shouts}</span>
2022-09-09 11:53:35 +00:00
</div>
2022-11-11 10:22:07 +00:00
</div>
)}
</For>
2022-09-09 11:53:35 +00:00
</div>
</div>
2022-11-11 10:22:07 +00:00
</div>
2022-11-14 21:58:33 +00:00
</div>
</div>
)}
</For>
</Show>
2022-11-19 08:09:52 +00:00
2022-11-20 21:23:12 +00:00
<Show when={searchResults().length > 0}>
<For each={searchResults().slice(0, limit())}>
{(author) => (
<AuthorCard
author={author}
compact={false}
hasLink={true}
subscribed={subscribed(author.slug)}
noSocialButtons={true}
isAuthorsList={true}
truncateBio={true}
/>
)}
</For>
</Show>
<Show when={searchParams().by && searchParams().by !== 'name'}>
2022-11-20 22:19:03 +00:00
<div class={clsx(styles.stats, 'row')}>
2022-11-20 21:23:12 +00:00
<div class="col-lg-10 col-xl-9">
<For each={sortedAuthors().slice(0, limit())}>
{(author) => (
<AuthorCard
author={author}
compact={false}
hasLink={true}
subscribed={subscribed(author.slug)}
noSocialButtons={true}
isAuthorsList={true}
truncateBio={true}
/>
)}
</For>
2022-11-19 08:09:52 +00:00
</div>
2022-11-20 21:23:12 +00:00
</div>
</Show>
<Show when={sortedAuthors().length > limit()}>
<div class="row">
<div class={clsx(styles.loadMoreContainer, 'col-12 col-md-10')}>
<button class={clsx('button', styles.loadMoreButton)} onClick={showMore}>
{t('Load more')}
</button>
2022-11-19 08:09:52 +00:00
</div>
2022-11-20 21:23:12 +00:00
</div>
</Show>
2022-09-09 11:53:35 +00:00
</div>
</Show>
</div>
)
}