webapp/src/pages/layoutShouts.page.tsx

186 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-02-17 09:21:02 +00:00
import { PageLayout } from '../components/_shared/PageLayout'
import type { LayoutType, PageProps } from './types'
import { createEffect, createMemo, createSignal, For, onCleanup, onMount, Show } from 'solid-js'
import { loadShouts, resetSortedArticles, useArticlesStore } from '../stores/zine/articles'
import { router, useRouter } from '../stores/router'
2023-02-17 09:21:02 +00:00
import { Loading } from '../components/_shared/Loading'
import { restoreScrollPosition, saveScrollPosition } from '../utils/scroll'
import type { Shout } from '../graphql/types.gen'
import { splitToPages } from '../utils/splitToPages'
2022-11-18 02:23:04 +00:00
import { clsx } from 'clsx'
2023-02-17 09:21:02 +00:00
import { Row3 } from '../components/Feed/Row3'
import { Row2 } from '../components/Feed/Row2'
import { Beside } from '../components/Feed/Beside'
import { Slider } from '../components/_shared/Slider'
2023-02-17 09:21:02 +00:00
import { Row1 } from '../components/Feed/Row1'
import styles from '../styles/Topic.module.scss'
2023-05-01 18:32:32 +00:00
import { ArticleCard } from '../components/Feed/ArticleCard'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../context/localize'
import { getPagePath } from '@nanostores/router'
import { Title } from '@solidjs/meta'
2022-11-12 18:59:29 +00:00
export const PRERENDERED_ARTICLES_COUNT = 27
2022-11-13 12:14:04 +00:00
const LOAD_MORE_PAGE_SIZE = 9 // Row3 + Row3 + Row3
2022-11-12 18:59:29 +00:00
export const LayoutShoutsPage = (props: PageProps) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
const { page: getPage } = useRouter()
2023-02-17 09:21:02 +00:00
const getLayout = createMemo<LayoutType>(() => {
const layout = getPage().params['layout']
2023-02-17 09:21:02 +00:00
return layout as LayoutType
2022-11-12 18:59:29 +00:00
})
const { sortedArticles } = useArticlesStore({
shouts: props.layoutShouts
})
2022-11-13 12:14:04 +00:00
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2022-11-18 02:23:04 +00:00
const loadMore = async (count) => {
saveScrollPosition()
const { hasMore } = await loadShouts({
filters: { layout: getLayout() },
limit: count,
2022-11-13 12:14:04 +00:00
offset: sortedArticles().length
})
setIsLoadMoreButtonVisible(hasMore)
restoreScrollPosition()
}
2022-11-12 18:59:29 +00:00
2022-11-13 12:14:04 +00:00
const title = createMemo(() => {
2023-02-17 09:21:02 +00:00
const l = getLayout()
2022-11-13 12:14:04 +00:00
if (l === 'audio') return t('Audio')
if (l === 'video') return t('Video')
if (l === 'image') return t('Artworks')
return t('Literature')
})
2022-11-12 18:59:29 +00:00
2022-11-13 12:14:04 +00:00
const pages = createMemo<Shout[][]>(() =>
splitToPages(sortedArticles(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
)
2022-11-12 18:59:29 +00:00
const isLoaded = createMemo(() => Boolean(props.layoutShouts))
onMount(() => {
if (isLoaded()) {
return
2022-11-13 12:14:04 +00:00
}
loadMore(PRERENDERED_ARTICLES_COUNT + LOAD_MORE_PAGE_SIZE)
2022-11-12 18:59:29 +00:00
})
onMount(() => {
if (sortedArticles().length === PRERENDERED_ARTICLES_COUNT) {
loadMore(LOAD_MORE_PAGE_SIZE)
}
})
createEffect((prevLayout) => {
if (prevLayout !== getLayout()) {
resetSortedArticles()
loadMore(PRERENDERED_ARTICLES_COUNT + LOAD_MORE_PAGE_SIZE)
}
return getLayout()
}, getLayout())
onCleanup(() => {
resetSortedArticles()
})
const handleLoadMoreClick = () => {
loadMore(LOAD_MORE_PAGE_SIZE)
}
2022-11-12 18:59:29 +00:00
return (
2023-02-17 09:21:02 +00:00
<PageLayout>
<Title>{title()}</Title>
2022-11-12 18:59:29 +00:00
<Show when={isLoaded()} fallback={<Loading />}>
2022-11-13 12:14:04 +00:00
<div class={styles.topicPage}>
<Show when={getLayout()}>
2023-01-23 21:31:47 +00:00
<div class="wide-container">
<h1>{title()}</h1>
</div>
<div class="wide-container">
<div class={clsx(styles.groupControls, 'row group__controls')}>
<div class="col-md-16">
<ul class="view-switcher">
<li classList={{ 'view-switcher__item--selected': getLayout() === 'audio' }}>
<a href={getPagePath(router, 'expo', { layout: 'audio' })}>{t('Audio')}</a>
</li>
<li classList={{ 'view-switcher__item--selected': getLayout() === 'video' }}>
<a href={getPagePath(router, 'expo', { layout: 'video' })}>{t('Video')}</a>
</li>
<li classList={{ 'view-switcher__item--selected': getLayout() === 'image' }}>
<a href={getPagePath(router, 'expo', { layout: 'image' })}>{t('Artworks')}</a>
</li>
<li classList={{ 'view-switcher__item--selected': getLayout() === 'literature' }}>
<a href={getPagePath(router, 'expo', { layout: 'literature' })}>{t('Literature')}</a>
</li>
</ul>
</div>
</div>
</div>
<Show when={sortedArticles().length > 0} fallback={<Loading />}>
<Row1 article={sortedArticles()[0]} />
<Row2 articles={sortedArticles().slice(1, 3)} />
<Slider title={title()}>
<For each={sortedArticles().slice(5, 11)}>
{(article) => (
<ArticleCard
article={article}
settings={{
additionalClass: 'swiper-slide',
isFloorImportant: true,
isWithCover: true,
nodate: true
}}
/>
)}
</For>
</Slider>
<Beside
beside={sortedArticles()[12]}
title={t('Top viewed')}
values={sortedArticles().slice(0, 5)}
wrapper={'top-article'}
/>
<Show when={sortedArticles().length > 5}>
<Row3 articles={sortedArticles().slice(13, 16)} />
<Row2 articles={sortedArticles().slice(16, 18)} />
<Row3 articles={sortedArticles().slice(18, 21)} />
<Row3 articles={sortedArticles().slice(21, 24)} />
<Row3 articles={sortedArticles().slice(24, 27)} />
</Show>
<For each={pages()}>
{(page) => (
<>
<Row3 articles={page.slice(0, 3)} />
<Row3 articles={page.slice(3, 6)} />
<Row3 articles={page.slice(6, 9)} />
</>
2022-11-27 17:02:04 +00:00
)}
</For>
2022-11-13 12:14:04 +00:00
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={handleLoadMoreClick}>
{t('Load more')}
</button>
</p>
</Show>
2022-11-13 12:14:04 +00:00
</Show>
</Show>
</div>
2022-11-12 18:59:29 +00:00
</Show>
2023-02-17 09:21:02 +00:00
</PageLayout>
2022-11-12 18:59:29 +00:00
)
}
2023-02-17 09:21:02 +00:00
export const Page = LayoutShoutsPage