webapp/src/components/Views/AllAuthors/AllAuthors.tsx

244 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-07-03 17:38:43 +00:00
import { useSearchParams } from '@solidjs/router'
import { clsx } from 'clsx'
2024-08-27 17:17:24 +00:00
import { For, Show, createEffect, createMemo, createSignal, on } from 'solid-js'
2024-07-09 09:13:13 +00:00
import { AuthorBadge } from '~/components/Author/AuthorBadge'
import { Button } from '~/components/_shared/Button'
2024-07-21 02:17:42 +00:00
import { InlineLoader } from '~/components/_shared/InlineLoader'
import { Loading } from '~/components/_shared/Loading'
import { SearchField } from '~/components/_shared/SearchField'
2024-07-05 19:40:54 +00:00
import { useAuthors } from '~/context/authors'
import { useLocalize } from '~/context/localize'
import type { Author } from '~/graphql/schema/core.gen'
2024-08-30 13:45:17 +00:00
import { dummyFilter } from '~/intl/dummyFilter'
2024-07-05 14:08:12 +00:00
import { authorLetterReduce, translateAuthor } from '~/intl/translate'
2024-08-27 17:17:24 +00:00
// import { byFirstChar, byStat } from '~/lib/sort'
import { scrollHandler } from '~/utils/scroll'
import styles from './AllAuthors.module.scss'
2024-07-09 09:13:13 +00:00
import stylesAuthorList from './AuthorsList.module.scss'
type Props = {
authors: Author[]
2024-07-26 15:49:15 +00:00
authorsByFollowers?: Author[]
authorsByShouts?: Author[]
isLoaded: boolean
}
2024-07-03 07:02:46 +00:00
export const AUTHORS_PER_PAGE = 20
export const ABC = {
ru: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#',
en: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ#'
}
2024-08-27 17:17:24 +00:00
// useAuthors sorted from context, set filter/sort
export const AllAuthors = (props: Props) => {
const { t, lang } = useLocalize()
2024-07-03 07:02:46 +00:00
const alphabet = createMemo(() => ABC[lang()] || ABC['ru'])
2024-08-29 14:40:31 +00:00
const [searchParams] = useSearchParams<{ by?: string }>()
2024-07-09 09:13:13 +00:00
const { authorsSorted, setAuthorsSort, loadAuthors } = useAuthors()
2024-08-29 14:40:31 +00:00
const authors = createMemo(() =>
searchParams.by || searchParams.by === 'name' ? props.authors : authorsSorted()
)
2024-07-09 09:13:13 +00:00
const [loading, setLoading] = createSignal<boolean>(false)
2024-07-26 15:49:15 +00:00
// filter
const [searchQuery, setSearchQuery] = createSignal('')
const [filteredAuthors, setFilteredAuthors] = createSignal<Author[]>([])
createEffect(
2024-08-27 17:17:24 +00:00
() =>
authors() &&
setFilteredAuthors((_prev: Author[]) => dummyFilter(authors(), searchQuery(), lang()) as Author[])
2024-07-26 15:49:15 +00:00
)
2024-08-27 17:17:24 +00:00
// sort by
// onMount(() => !searchParams?.by && changeSearchParams({ by: 'name' }))
createEffect(on(() => searchParams?.by || 'name', setAuthorsSort || ((_) => null), {}))
2024-07-26 15:49:15 +00:00
// store by first char
const byLetterFiltered = createMemo<{ [letter: string]: Author[] }>(() => {
2024-07-09 09:13:13 +00:00
if (!(filteredAuthors()?.length > 0)) return {}
2024-07-26 15:49:15 +00:00
console.debug('[components.AllAuthors] update byLetterFiltered', filteredAuthors()?.length)
2024-08-27 17:17:24 +00:00
return (
filteredAuthors()?.reduce(
(acc, author: Author) => authorLetterReduce(acc, author, lang()),
{} as { [letter: string]: Author[] }
) || {}
)
})
const sortedKeys = createMemo<string[]>(() => {
2024-07-05 08:12:17 +00:00
const keys = Object.keys(byLetterFiltered() || {})
keys.sort()
2024-06-24 17:50:27 +00:00
const fk = keys.shift() || ''
fk && keys.push(fk)
return keys
})
2024-07-09 09:13:13 +00:00
const fetchAuthors = async (queryType: string, page: number) => {
try {
2024-07-26 15:49:15 +00:00
console.debug('[components.AuthorsList] fetching authors...')
2024-07-09 09:13:13 +00:00
setLoading(true)
setAuthorsSort?.(queryType)
const offset = AUTHORS_PER_PAGE * page
await loadAuthors({
by: { order: queryType },
limit: AUTHORS_PER_PAGE,
offset
})
} catch (error) {
console.error('[components.AuthorsList] error fetching authors:', error)
} finally {
setLoading(false)
}
}
const [currentPage, setCurrentPage] = createSignal<{ followers: number; shouts: number }>({
followers: 0,
shouts: 0
})
const loadMoreAuthors = () => {
2024-08-27 17:17:24 +00:00
const by = searchParams?.by
if (!by || by === 'name') return
const nextPage = currentPage()[by as 'followers' | 'shouts'] + 1
2024-07-12 11:19:58 +00:00
fetchAuthors(by, nextPage).then(() => setCurrentPage({ ...currentPage(), [by]: nextPage }))
2024-07-09 09:13:13 +00:00
}
2024-07-09 09:13:13 +00:00
const TabNavigator = () => (
<div class="row">
<div class="col-lg-20 col-xl-18">
<h1>{t('Authors')}</h1>
<p>{t('Subscribe who you like to tune your personal feed')}</p>
<ul class={clsx(styles.viewSwitcher, 'view-switcher')}>
<li
class={clsx({
2024-07-26 15:49:15 +00:00
['view-switcher__item--selected']: !searchParams?.by || searchParams?.by === 'shouts'
2024-07-09 09:13:13 +00:00
})}
>
2024-08-27 17:17:24 +00:00
<a href="/author?by=shouts">{t('By shouts')}</a>
2024-07-09 09:13:13 +00:00
</li>
<li
class={clsx({
['view-switcher__item--selected']: searchParams?.by === 'followers'
})}
>
2024-08-27 17:17:24 +00:00
<a href="/author?by=followers">{t('By popularity')}</a>
2024-07-09 09:13:13 +00:00
</li>
<li
class={clsx({
2024-07-26 15:49:15 +00:00
['view-switcher__item--selected']: searchParams?.by === 'name'
2024-07-09 09:13:13 +00:00
})}
>
2024-08-27 17:17:24 +00:00
<a href="/author?by=name">{t('By name')}</a>
2024-07-09 09:13:13 +00:00
</li>
<Show when={searchParams?.by === 'name'}>
<li class="view-switcher__search">
<SearchField onChange={(value) => setSearchQuery(value)} />
</li>
</Show>
</ul>
</div>
</div>
)
const AbcNavigator = () => (
<div class="row">
<div class="col-lg-20 col-xl-18">
<ul class={clsx('nodash', styles.alphabet)}>
<For each={[...(alphabet() || [])]}>
{(letter, index) => (
<li>
<Show when={letter in byLetterFiltered()} fallback={letter}>
<a
href={`/author?by=name#letter-${index()}`}
onClick={(event) => {
event.preventDefault()
scrollHandler(`letter-${index()}`)
}}
>
{letter}
</a>
</Show>
2024-07-09 09:13:13 +00:00
</li>
)}
</For>
</ul>
</div>
</div>
)
2024-07-09 09:13:13 +00:00
const AbcAuthorsList = () => (
<For each={sortedKeys() || []}>
{(letter) => (
<div class={clsx(styles.group, 'group')}>
<h2 id={`letter-${alphabet()?.indexOf(letter) || ''}`}>{letter}</h2>
<div class="container">
<div class="row">
2024-07-09 09:13:13 +00:00
<div class="col-lg-20">
<div class="row">
<For each={byLetterFiltered()?.[letter] || []}>
{(author) => (
<div class={clsx(styles.topic, 'topic col-sm-12 col-md-8')}>
<div class="topic-title">
2024-07-15 23:11:01 +00:00
<a href={`/@${author.slug}`}>{translateAuthor(author, lang())}</a>
2024-07-09 09:13:13 +00:00
<Show when={author.stat?.shouts || 0}>
<span class={styles.articlesCounter}>{author.stat?.shouts || 0}</span>
</Show>
</div>
</div>
)}
</For>
2024-07-09 09:13:13 +00:00
</div>
</div>
</div>
2024-07-09 09:13:13 +00:00
</div>
</div>
)}
</For>
)
const AuthorsSortedList = () => (
<div class={clsx(stylesAuthorList.AuthorsList)}>
2024-08-27 17:17:24 +00:00
<For each={authorsSorted?.()}>
2024-07-09 09:13:13 +00:00
{(author) => (
<div class="row">
<div class="col-lg-20 col-xl-18">
<AuthorBadge author={author} />
</div>
</div>
)}
</For>
<div class="row">
<div class="col-lg-20 col-xl-18">
<div class={stylesAuthorList.action}>
2024-08-29 14:40:31 +00:00
<Show
when={
searchParams.by !== 'name' &&
!searchParams.by &&
!loading() &&
((authorsSorted?.() || []).length || 0) > 0
}
>
2024-07-09 09:13:13 +00:00
<Button value={t('Load more')} onClick={loadMoreAuthors} aria-live="polite" />
</Show>
<Show when={loading()}>
<InlineLoader />
</Show>
</div>
</div>
</div>
</div>
)
return (
<>
<Show when={props.isLoaded} fallback={<Loading />}>
<div class="offset-md-5">
<TabNavigator />
2024-07-26 15:49:15 +00:00
<Show when={searchParams?.by === 'name'} fallback={<AuthorsSortedList />}>
2024-07-09 09:13:13 +00:00
<AbcNavigator />
<AbcAuthorsList />
</Show>
</div>
</Show>
2024-07-09 09:13:13 +00:00
</>
)
}