import { useSearchParams } from '@solidjs/router' import { clsx } from 'clsx' import { For, Show, createEffect, createMemo, createSignal, on } from 'solid-js' import { AuthorBadge } from '~/components/Author/AuthorBadge' import { Button } from '~/components/_shared/Button' import { InlineLoader } from '~/components/_shared/InlineLoader' import { Loading } from '~/components/_shared/Loading' import { SearchField } from '~/components/_shared/SearchField' import { useAuthors } from '~/context/authors' import { useLocalize } from '~/context/localize' import type { Author } from '~/graphql/schema/core.gen' import { authorLetterReduce, translateAuthor } from '~/intl/translate' import { dummyFilter } from '~/lib/dummyFilter' // import { byFirstChar, byStat } from '~/lib/sort' import { scrollHandler } from '~/utils/scroll' import styles from './AllAuthors.module.scss' import stylesAuthorList from './AuthorsList.module.scss' type Props = { authors: Author[] authorsByFollowers?: Author[] authorsByShouts?: Author[] isLoaded: boolean } export const AUTHORS_PER_PAGE = 20 export const ABC = { ru: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#', en: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ#' } // useAuthors sorted from context, set filter/sort export const AllAuthors = (props: Props) => { const { t, lang } = useLocalize() const alphabet = createMemo(() => ABC[lang()] || ABC['ru']) const [searchParams, ] = useSearchParams<{ by?: string }>() const { authorsSorted, setAuthorsSort, loadAuthors } = useAuthors() const authors = createMemo(() => (searchParams.by || searchParams.by === 'name') ? props.authors : authorsSorted()) const [loading, setLoading] = createSignal(false) // filter const [searchQuery, setSearchQuery] = createSignal('') const [filteredAuthors, setFilteredAuthors] = createSignal([]) createEffect( () => authors() && setFilteredAuthors((_prev: Author[]) => dummyFilter(authors(), searchQuery(), lang()) as Author[]) ) // sort by // onMount(() => !searchParams?.by && changeSearchParams({ by: 'name' })) createEffect(on(() => searchParams?.by || 'name', setAuthorsSort || ((_) => null), {})) // store by first char const byLetterFiltered = createMemo<{ [letter: string]: Author[] }>(() => { if (!(filteredAuthors()?.length > 0)) return {} console.debug('[components.AllAuthors] update byLetterFiltered', filteredAuthors()?.length) return ( filteredAuthors()?.reduce( (acc, author: Author) => authorLetterReduce(acc, author, lang()), {} as { [letter: string]: Author[] } ) || {} ) }) const sortedKeys = createMemo(() => { const keys = Object.keys(byLetterFiltered() || {}) keys.sort() const fk = keys.shift() || '' fk && keys.push(fk) return keys }) const fetchAuthors = async (queryType: string, page: number) => { try { console.debug('[components.AuthorsList] fetching authors...') 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 = () => { const by = searchParams?.by if (!by || by === 'name') return const nextPage = currentPage()[by as 'followers' | 'shouts'] + 1 fetchAuthors(by, nextPage).then(() => setCurrentPage({ ...currentPage(), [by]: nextPage })) } const TabNavigator = () => (

{t('Authors')}

{t('Subscribe who you like to tune your personal feed')}

) const AbcNavigator = () => ( ) const AbcAuthorsList = () => ( {(letter) => (

{letter}

{(author) => (
{translateAuthor(author, lang())} {author.stat?.shouts || 0}
)}
)}
) const AuthorsSortedList = () => (
{(author) => (
)}
0}>
) return ( <> }>
}>
) }