webapp/src/components/Views/Topic.tsx

165 lines
5.6 KiB
TypeScript
Raw Normal View History

import { For, Show, createMemo, onMount, createSignal } from 'solid-js'
2022-09-09 11:53:35 +00:00
import type { Shout, Topic } from '../../graphql/types.gen'
import { Row3 } from '../Feed/Row3'
import { Row2 } from '../Feed/Row2'
import { Beside } from '../Feed/Beside'
2022-09-09 11:53:35 +00:00
import { ArticleCard } from '../Feed/Card'
import '../../styles/Topic.scss'
import { FullTopic } from '../Topic/Full'
import { t } from '../../utils/intl'
2022-09-22 09:37:49 +00:00
import { useRouter } from '../../stores/router'
2022-09-13 09:59:04 +00:00
import { useTopicsStore } from '../../stores/zine/topics'
import { loadPublishedArticles, useArticlesStore } from '../../stores/zine/articles'
2022-09-13 09:59:04 +00:00
import { useAuthorsStore } from '../../stores/zine/authors'
import { restoreScrollPosition, saveScrollPosition } from '../../utils/scroll'
import { splitToPages } from '../../utils/splitToPages'
2022-09-22 09:37:49 +00:00
type TopicsPageSearchParams = {
by: 'comments' | '' | 'recent' | 'viewed' | 'rating' | 'commented'
}
2022-09-09 11:53:35 +00:00
interface TopicProps {
topic: Topic
topicArticles: Shout[]
2022-10-05 15:11:14 +00:00
topicSlug: string
2022-09-09 11:53:35 +00:00
}
export const PRERENDERED_ARTICLES_COUNT = 21
const LOAD_MORE_PAGE_SIZE = 9 // Row3 + Row3 + Row3
2022-09-22 09:37:49 +00:00
export const TopicView = (props: TopicProps) => {
2022-10-25 16:25:42 +00:00
const { searchParams, changeSearchParam } = useRouter<TopicsPageSearchParams>()
2022-09-22 09:37:49 +00:00
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2022-09-28 20:16:44 +00:00
const { sortedArticles } = useArticlesStore({ sortedArticles: props.topicArticles })
const { topicEntities } = useTopicsStore({ topics: [props.topic] })
2022-09-13 08:05:11 +00:00
2022-09-28 20:16:44 +00:00
const { authorsByTopic } = useAuthorsStore()
2022-09-13 09:59:04 +00:00
2022-10-05 15:11:14 +00:00
const topic = createMemo(() => topicEntities()[props.topicSlug])
2022-09-13 09:59:04 +00:00
const loadMore = async () => {
saveScrollPosition()
const { hasMore } = await loadPublishedArticles({
limit: LOAD_MORE_PAGE_SIZE,
offset: sortedArticles().length
})
setIsLoadMoreButtonVisible(hasMore)
restoreScrollPosition()
}
onMount(async () => {
if (sortedArticles().length === PRERENDERED_ARTICLES_COUNT) {
loadMore()
}
})
2022-09-09 11:53:35 +00:00
const title = createMemo(() => {
2022-10-25 16:25:42 +00:00
const m = searchParams().by
2022-10-05 15:11:14 +00:00
if (m === 'viewed') return t('Top viewed')
if (m === 'rating') return t('Top rated')
if (m === 'commented') return t('Top discussed')
2022-09-09 11:53:35 +00:00
return t('Top recent')
})
const pages = createMemo<Shout[][]>(() =>
splitToPages(sortedArticles(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
)
2022-09-09 11:53:35 +00:00
return (
<div class="topic-page container">
<Show when={topic()}>
<FullTopic topic={topic()} />
<div class="row group__controls">
<div class="col-md-8">
<ul class="view-switcher">
2022-10-25 16:25:42 +00:00
<li classList={{ selected: searchParams().by === 'recent' || !searchParams().by }}>
2022-09-22 09:37:49 +00:00
<button type="button" onClick={() => changeSearchParam('by', 'recent')}>
2022-09-09 11:53:35 +00:00
{t('Recent')}
</button>
</li>
2022-10-09 10:56:39 +00:00
{/*TODO: server sort*/}
{/*<li classList={{ selected: getSearchParams().by === 'rating' }}>*/}
{/* <button type="button" onClick={() => changeSearchParam('by', 'rating')}>*/}
{/* {t('Popular')}*/}
{/* </button>*/}
{/*</li>*/}
{/*<li classList={{ selected: getSearchParams().by === 'viewed' }}>*/}
{/* <button type="button" onClick={() => changeSearchParam('by', 'viewed')}>*/}
{/* {t('Views')}*/}
{/* </button>*/}
{/*</li>*/}
{/*<li classList={{ selected: getSearchParams().by === 'commented' }}>*/}
{/* <button type="button" onClick={() => changeSearchParam('by', 'commented')}>*/}
{/* {t('Discussing')}*/}
{/* </button>*/}
{/*</li>*/}
2022-09-09 11:53:35 +00:00
</ul>
</div>
<div class="col-md-4">
<div class="mode-switcher">
{`${t('Show')} `}
<span class="mode-switcher__control">{t('All posts')}</span>
</div>
</div>
</div>
<div class="row floor floor--important">
<div class="container">
<div class="row">
<h3 class="col-12">{title()}</h3>
<For each={sortedArticles().slice(0, 6)}>
2022-09-13 09:59:04 +00:00
{(article) => (
2022-09-09 11:53:35 +00:00
<div class="col-md-6">
<ArticleCard
article={article}
settings={{ isFloorImportant: true, isBigTitle: true }}
/>
2022-09-09 11:53:35 +00:00
</div>
)}
</For>
</div>
</div>
</div>
<div class="row">
<Show when={sortedArticles().length > 5}>
<Beside
title={t('Topic is supported by')}
2022-09-28 20:16:44 +00:00
values={authorsByTopic()[topic().slug].slice(0, 7)}
2022-09-09 11:53:35 +00:00
beside={sortedArticles()[6]}
wrapper={'author'}
/>
2022-10-09 10:56:39 +00:00
<Row3 articles={sortedArticles().slice(7, 10)} />
<Row2 articles={sortedArticles().slice(10, 12)} />
<Row3 articles={sortedArticles().slice(12, 15)} />
<Row3 articles={sortedArticles().slice(15, 18)} />
<Row3 articles={sortedArticles().slice(18, 21)} />
2022-09-09 11:53:35 +00:00
</Show>
<For each={pages()}>
{(page) => (
<>
<Row3 articles={page.slice(0, 3)} />
<Row3 articles={page.slice(3, 6)} />
<Row3 articles={page.slice(6, 9)} />
</>
)}
</For>
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={loadMore}>
{t('Load more')}
</button>
</p>
</Show>
2022-09-09 11:53:35 +00:00
</div>
</Show>
</div>
)
}