import { createEffect, createMemo, createSignal, For, Show } from 'solid-js' import type { Topic } from '../../graphql/types.gen' import { t } from '../../utils/intl' 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 { locale } from '../../stores/ui' import { translit } from '../../utils/ru2en' import styles from '../../styles/AllTopics.module.scss' import { SearchField } from '../_shared/SearchField' type AllTopicsPageSearchParams = { by: 'shouts' | 'authors' | 'title' | '' } type AllTopicsViewProps = { topics: Topic[] } const PAGE_SIZE = 20 export const AllTopicsView = (props: AllTopicsViewProps) => { const { searchParams, changeSearchParam } = useRouter() const [limit, setLimit] = createSignal(PAGE_SIZE) const ALPHABET = [ '#', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я' ] const { sortedTopics } = useTopicsStore({ topics: props.topics, sortBy: searchParams().by || 'shouts' }) const { session } = useSession() createEffect(() => { setTopicsSort(searchParams().by || 'shouts') setLimit(PAGE_SIZE) }) const byLetter = createMemo<{ [letter: string]: Topic[] }>(() => { return sortedTopics().reduce((acc, topic) => { let letter = topic.title[0].toUpperCase() if (!/[А-я]/i.test(letter) && locale() === '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() return keys }) const subscribed = (s) => Boolean(session()?.news?.topics && session()?.news?.topics?.includes(s || '')) const showMore = () => setLimit((oldLimit) => oldLimit + PAGE_SIZE) const [searchResults, setSearchResults] = createSignal([]) // eslint-disable-next-line sonarjs/cognitive-complexity const searchTopics = (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 ttt: Topic[] = [] sortedTopics().forEach((topic) => { let flag = false topic.slug.split('-').forEach((w) => { if (w.startsWith(q)) flag = true }) if (!flag) { let wrds: string = topic.title.toLowerCase() if (locale() === 'ru') wrds = translit(wrds, 'ru') wrds.split(' ').forEach((w: string) => { if (w.startsWith(q)) flag = true }) } if (flag && !ttt.includes(topic)) ttt.push(topic) }) setSearchResults((sr: Topic[]) => [...sr, ...ttt]) changeSearchParam('by', '') } } const AllTopicsHead = () => (

{t('Topics')}

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

) return (
0 || searchResults().length > 0}>
    {(letter, index) => (
  • {letter} {letter}
  • )}
{(letter, index) => (

{letter}

{(topic) => ( )}
)}
1}> {(topic) => ( )} {(topic) => ( )} {(topic) => ( )} limit()}>
) }