import { useSearchParams } from '@solidjs/router' import { clsx } from 'clsx' import { For, Show, createEffect, createMemo, createSignal } 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[] isLoaded: boolean } export const AUTHORS_PER_PAGE = 20 export const ABC = { ru: 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#', en: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ#' } export const AllAuthors = (props: Props) => { const { t, lang } = useLocalize() const alphabet = createMemo(() => ABC[lang()] || ABC['ru']) const [searchParams, changeSearchParams] = useSearchParams<{ by?: string }>() const { authorsSorted, setAuthorsSort, loadAuthors } = useAuthors() const [loading, setLoading] = createSignal(false) const [searchQuery, setSearchQuery] = createSignal('') const [filteredAuthors, setFilteredAuthors] = createSignal([]) createEffect(() => { // Load all authors initially fetchAuthors(searchParams.by || 'name', 0) }) /* const authors = createMemo(() => { let sortedAuthors = [...props.authors] sortedAuthors = authorsSorted() if (!searchParams.by || searchParams.by === 'name') { sortedAuthors = sortedAuthors.sort(byFirstChar) } else if (searchParams.by === 'shouts') { sortedAuthors = sortedAuthors.sort(byStat('shouts')) } else if (searchParams.by === 'followers') { sortedAuthors = sortedAuthors.sort(byStat('followers')) } return sortedAuthors }) */ const authors = createMemo(() => { let sortedAuthors: Author[] = [] if (!searchParams.by || searchParams.by === 'name') { sortedAuthors = [...props.authors].sort(byFirstChar) } else { sortedAuthors = authorsSorted().sort(byStat(searchParams.by || 'shouts')) } return sortedAuthors }) createEffect(() => { setFilteredAuthors(dummyFilter(authors(), searchQuery(), lang()) as Author[]) }) const byLetterFiltered = createMemo<{ [letter: string]: Author[] }>(() => { if (!(filteredAuthors()?.length > 0)) return {} 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 { 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 as 'followers' | 'shouts' | undefined if (!by) return const nextPage = currentPage()[by] + 1 fetchAuthors(by, nextPage).then(() => setCurrentPage({ ...currentPage(), [by]: nextPage })) } const TabNavigator = () => ( ) const AbcNavigator = () => ( ) const AbcAuthorsList = () => ( {(letter) => (

{letter}

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