2022-10-28 21:21:47 +00:00
|
|
|
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'
|
2022-10-28 21:21:47 +00:00
|
|
|
import { Row3 } from '../Feed/Row3'
|
|
|
|
import { Row2 } from '../Feed/Row2'
|
|
|
|
import { Beside } from '../Feed/Beside'
|
2022-11-07 21:07:42 +00:00
|
|
|
import styles from '../../styles/Topic.module.scss'
|
2022-09-09 11:53:35 +00:00
|
|
|
import { FullTopic } from '../Topic/Full'
|
2023-02-17 09:21:02 +00:00
|
|
|
|
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'
|
2022-11-18 02:23:04 +00:00
|
|
|
import { loadShouts, useArticlesStore } from '../../stores/zine/articles'
|
2022-09-13 09:59:04 +00:00
|
|
|
import { useAuthorsStore } from '../../stores/zine/authors'
|
2022-10-28 21:21:47 +00:00
|
|
|
import { restoreScrollPosition, saveScrollPosition } from '../../utils/scroll'
|
|
|
|
import { splitToPages } from '../../utils/splitToPages'
|
2022-11-07 21:07:42 +00:00
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import { Row1 } from '../Feed/Row1'
|
2023-02-17 09:21:02 +00:00
|
|
|
import { useLocalize } from '../../context/localize'
|
2023-11-02 10:34:38 +00:00
|
|
|
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
|
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
|
2022-11-15 14:24:50 +00:00
|
|
|
shouts: Shout[]
|
2022-10-05 15:11:14 +00:00
|
|
|
topicSlug: string
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 19:35:57 +00:00
|
|
|
export const PRERENDERED_ARTICLES_COUNT = 28
|
2022-10-28 21:21:47 +00:00
|
|
|
const LOAD_MORE_PAGE_SIZE = 9 // Row3 + Row3 + Row3
|
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
export const TopicView = (props: TopicProps) => {
|
2023-02-17 09:21:02 +00:00
|
|
|
const { t } = useLocalize()
|
2022-10-25 16:25:42 +00:00
|
|
|
const { searchParams, changeSearchParam } = useRouter<TopicsPageSearchParams>()
|
2022-09-22 09:37:49 +00:00
|
|
|
|
2022-10-28 21:21:47 +00:00
|
|
|
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
|
|
|
|
|
2022-11-15 14:24:50 +00:00
|
|
|
const { sortedArticles } = useArticlesStore({ shouts: props.shouts })
|
2022-09-28 20:16:44 +00:00
|
|
|
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
|
|
|
|
2023-11-14 10:45:44 +00:00
|
|
|
onMount(() => {
|
|
|
|
document.title = topic().title
|
|
|
|
})
|
|
|
|
|
2022-10-28 21:21:47 +00:00
|
|
|
const loadMore = async () => {
|
|
|
|
saveScrollPosition()
|
|
|
|
|
2022-11-18 02:23:04 +00:00
|
|
|
const { hasMore } = await loadShouts({
|
2023-07-19 19:48:24 +00:00
|
|
|
filters: { topic: props.topicSlug },
|
2022-10-28 21:21:47 +00:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2022-10-28 21:21:47 +00:00
|
|
|
const pages = createMemo<Shout[][]>(() =>
|
|
|
|
splitToPages(sortedArticles(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
|
|
|
|
)
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
return (
|
2022-11-07 21:07:42 +00:00
|
|
|
<div class={styles.topicPage}>
|
2022-09-09 11:53:35 +00:00
|
|
|
<Show when={topic()}>
|
|
|
|
<FullTopic topic={topic()} />
|
2022-11-23 20:47:06 +00:00
|
|
|
<div class="wide-container">
|
2022-11-07 21:07:42 +00:00
|
|
|
<div class={clsx(styles.groupControls, 'row group__controls')}>
|
2023-03-10 17:42:48 +00:00
|
|
|
<div class="col-md-16">
|
2022-11-07 21:07:42 +00:00
|
|
|
<ul class="view-switcher">
|
2023-05-22 22:01:04 +00:00
|
|
|
<li
|
|
|
|
classList={{
|
|
|
|
'view-switcher__item--selected': searchParams().by === 'recent' || !searchParams().by
|
|
|
|
}}
|
|
|
|
>
|
2023-09-29 12:48:58 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() =>
|
|
|
|
changeSearchParam({
|
|
|
|
by: 'recent'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
2022-11-07 21:07:42 +00:00
|
|
|
{t('Recent')}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
{/*TODO: server sort*/}
|
2023-05-22 22:01:04 +00:00
|
|
|
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'rating' }}>*/}
|
2022-11-07 21:07:42 +00:00
|
|
|
{/* <button type="button" onClick={() => changeSearchParam('by', 'rating')}>*/}
|
|
|
|
{/* {t('Popular')}*/}
|
|
|
|
{/* </button>*/}
|
|
|
|
{/*</li>*/}
|
2023-05-22 22:01:04 +00:00
|
|
|
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'viewed' }}>*/}
|
2022-11-07 21:07:42 +00:00
|
|
|
{/* <button type="button" onClick={() => changeSearchParam('by', 'viewed')}>*/}
|
|
|
|
{/* {t('Views')}*/}
|
|
|
|
{/* </button>*/}
|
|
|
|
{/*</li>*/}
|
2023-05-22 22:01:04 +00:00
|
|
|
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'commented' }}>*/}
|
2022-11-07 21:07:42 +00:00
|
|
|
{/* <button type="button" onClick={() => changeSearchParam('by', 'commented')}>*/}
|
|
|
|
{/* {t('Discussing')}*/}
|
|
|
|
{/* </button>*/}
|
|
|
|
{/*</li>*/}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2023-03-10 17:42:48 +00:00
|
|
|
<div class="col-md-8">
|
2022-11-07 21:07:42 +00:00
|
|
|
<div class="mode-switcher">
|
|
|
|
{`${t('Show')} `}
|
|
|
|
<span class="mode-switcher__control">{t('All posts')}</span>
|
|
|
|
</div>
|
2022-09-09 11:53:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-11-07 21:07:42 +00:00
|
|
|
<Row1 article={sortedArticles()[0]} />
|
2022-11-13 19:35:57 +00:00
|
|
|
<Row2 articles={sortedArticles().slice(1, 3)} isEqual={true} />
|
2022-11-07 21:07:42 +00:00
|
|
|
|
|
|
|
<Beside
|
|
|
|
title={t('Topic is supported by')}
|
|
|
|
values={authorsByTopic()[topic().slug].slice(0, 6)}
|
|
|
|
beside={sortedArticles()[4]}
|
|
|
|
wrapper={'author'}
|
|
|
|
/>
|
|
|
|
|
2023-11-02 10:34:38 +00:00
|
|
|
<ArticleCardSwiper title={title()} slides={sortedArticles().slice(5, 11)} />
|
2022-11-07 21:07:42 +00:00
|
|
|
|
|
|
|
<Beside
|
|
|
|
beside={sortedArticles()[12]}
|
|
|
|
title={t('Top viewed')}
|
|
|
|
values={sortedArticles().slice(0, 5)}
|
|
|
|
wrapper={'top-article'}
|
|
|
|
/>
|
|
|
|
|
2022-11-14 01:18:41 +00:00
|
|
|
<Row2 articles={sortedArticles().slice(13, 15)} isEqual={true} />
|
|
|
|
<Row1 article={sortedArticles()[15]} />
|
2022-11-13 19:35:57 +00:00
|
|
|
|
2022-12-01 18:45:35 +00:00
|
|
|
<Show when={sortedArticles().length > 15}>
|
2023-11-02 10:34:38 +00:00
|
|
|
<ArticleCardSwiper slides={sortedArticles().slice(16, 22)} />
|
2022-12-01 18:45:35 +00:00
|
|
|
<Row3 articles={sortedArticles().slice(23, 26)} />
|
|
|
|
<Row2 articles={sortedArticles().slice(26, 28)} />
|
|
|
|
</Show>
|
2022-11-07 21:07:42 +00:00
|
|
|
|
|
|
|
<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
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|