import { createEffect, createMemo, createSignal, For, onMount, Show } from 'solid-js' import type { Topic } from '../../graphql/types.gen' import { setTopicsSort, useTopicsStore } from '../../stores/zine/topics' import { useRouter } from '../../stores/router' import { TopicCard } from '../Topic/Card' import { clsx } from 'clsx' import { useSession } from '../../context/session' import { translit } from '../../utils/ru2en' import styles from '../../styles/AllTopics.module.scss' import { SearchField } from '../_shared/SearchField' import { scrollHandler } from '../../utils/scroll' import { StatMetrics } from '../_shared/StatMetrics' import { useLocalize } from '../../context/localize' type AllTopicsPageSearchParams = { by: 'shouts' | 'authors' | 'title' | '' } type AllTopicsViewProps = { topics: Topic[] } const PAGE_SIZE = 20 const ALPHABET = [...'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ#'] export const AllTopicsView = (props: AllTopicsViewProps) => { const { t, lang } = useLocalize() const { searchParams, changeSearchParam } = useRouter() const [limit, setLimit] = createSignal(PAGE_SIZE) const { sortedTopics } = useTopicsStore({ topics: props.topics, sortBy: searchParams().by || 'shouts' }) const { session } = useSession() onMount(() => { if (!searchParams().by) { changeSearchParam('by', 'shouts') } }) createEffect(() => { setTopicsSort(searchParams().by || 'shouts') }) const byLetter = createMemo<{ [letter: string]: Topic[] }>(() => { return sortedTopics().reduce((acc, topic) => { let letter = topic.title[0].toUpperCase() if (/[^ËА-яё]/.test(letter) && lang() === 'ru') letter = '#' if (!acc[letter]) acc[letter] = [] acc[letter].push(topic) return acc }, {} as { [letter: string]: Topic[] }) }) const sortedKeys = createMemo(() => { const keys = Object.keys(byLetter()) keys.sort() keys.push(keys.shift()) return keys }) const subscribed = (s) => Boolean(session()?.news?.topics && session()?.news?.topics?.includes(s || '')) const showMore = () => setLimit((oldLimit) => oldLimit + PAGE_SIZE) const [searchQuery, setSearchQuery] = createSignal('') const filteredResults = createMemo(() => { /* very stupid filter by string algorithm with no deps */ let q = searchQuery().toLowerCase() if (q.length === 0) { return sortedTopics() } if (lang() === 'ru') { q = translit(q) } return sortedTopics().filter((topic) => { if (topic.slug.split('-').some((w) => w.startsWith(q))) { return true } let title = topic.title.toLowerCase() if (lang() === 'ru') { title = translit(title) } return title.split(' ').some((word) => word.startsWith(q)) }) }) const AllTopicsHead = () => (

{t('Topics')}

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

) return (
0}> {(letter) => (

{letter}

{(topic) => (
{topic.title} {topic.stat.shouts}
)}
)}
{(topic) => ( <> )} limit() && searchParams().by !== 'title'}>
) }