webapp/src/components/Views/Home.tsx

160 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-09-14 14:21:26 +00:00
import { createMemo, createSignal, Show, Suspense } from 'solid-js'
2022-09-09 11:53:35 +00:00
import Banner from '../Discours/Banner'
import NavTopics from '../Nav/Topics'
import Row5 from '../Feed/Row5'
import Row3 from '../Feed/Row3'
import Row2 from '../Feed/Row2'
import Row1 from '../Feed/Row1'
import Hero from '../Discours/Hero'
import Beside from '../Feed/Beside'
import RowShort from '../Feed/RowShort'
import Slider from '../Feed/Slider'
import Group from '../Feed/Group'
import type { Shout, Topic } from '../../graphql/types.gen'
import Icon from '../Nav/Icon'
import { t } from '../../utils/intl'
import { useTopicsStore } from '../../stores/zine/topics'
2022-09-13 09:59:04 +00:00
import { loadPublishedArticles, useArticlesStore } from '../../stores/zine/articles'
import { useAuthorsStore } from '../../stores/zine/authors'
2022-09-09 11:53:35 +00:00
type HomeProps = {
randomTopics: Topic[]
recentPublishedArticles: Shout[]
topMonthArticles: Shout[]
topOverallArticles: Shout[]
2022-09-14 11:27:10 +00:00
limit?: number
offset?: number
2022-09-09 11:53:35 +00:00
}
const LAYOUTS = ['article', 'prose', 'music', 'video', 'image']
export const HomePage = (props: HomeProps) => {
const [someLayout, setSomeLayout] = createSignal([] as Shout[])
const [selectedLayout, setSelectedLayout] = createSignal('article')
2022-09-13 09:59:04 +00:00
const [byLayout, setByLayout] = createSignal({} as { [layout: string]: Shout[] })
const [byTopic, setByTopic] = createSignal({} as { [topic: string]: Shout[] })
const {
getSortedArticles,
getTopRatedArticles,
getTopRatedMonthArticles,
getTopViewedArticles,
getTopCommentedArticles
} = useArticlesStore({
sortedArticles: props.recentPublishedArticles,
topRatedArticles: props.topOverallArticles,
topRatedMonthArticles: props.topMonthArticles
2022-09-13 08:05:11 +00:00
})
2022-09-09 11:53:35 +00:00
2022-09-13 09:59:04 +00:00
const articles = createMemo(() => getSortedArticles())
const { getRandomTopics, getSortedTopics, getTopTopics } = useTopicsStore({
randomTopics: props.randomTopics
})
2022-09-09 11:53:35 +00:00
2022-09-13 09:59:04 +00:00
const { getTopAuthors } = useAuthorsStore()
2022-09-09 11:53:35 +00:00
2022-09-13 09:59:04 +00:00
// FIXME
// createEffect(() => {
// if (articles() && articles().length > 0 && Object.keys(byTopic()).length === 0) {
// console.debug('[home] ' + getRandomTopics().length.toString() + ' random topics loaded')
// console.debug('[home] ' + articles().length.toString() + ' overall shouts loaded')
// console.log('[home] preparing published articles...')
// // get shouts lists by
// const bl: { [key: string]: Shout[] } = {}
// const bt: { [key: string]: Shout[] } = {}
// articles().forEach((s: Shout) => {
// // by topic
// s.topics?.forEach(({ slug }: any) => {
// if (!bt[slug || '']) bt[slug || ''] = []
// bt[slug as string].push(s)
// })
// // by layout
// const l = s.layout || 'article'
// if (!bl[l]) bl[l] = []
// bl[l].push(s)
// })
// setByLayout(bl)
// setByTopic(bt)
// console.log('[home] some grouped articles are ready')
// }
// }, [articles()])
// FIXME
// createEffect(() => {
// if (Object.keys(byLayout()).length > 0 && getSortedTopics()) {
// // random special layout pick
// const special = LAYOUTS.filter((la) => la !== 'article')
// const layout = special[Math.floor(Math.random() * special.length)]
// setSomeLayout(byLayout()[layout])
// setSelectedLayout(layout)
// console.log(`[home] <${layout}> layout picked`)
// }
// }, [byLayout()])
const loadMore = () => {
2022-09-14 11:27:10 +00:00
const limit = props.limit || 50
const offset = props.offset || 0
loadPublishedArticles({ limit, offset })
2022-09-13 09:59:04 +00:00
}
2022-09-09 11:53:35 +00:00
return (
<Suspense fallback={t('Loading')}>
2022-09-13 09:59:04 +00:00
<Show when={articles().length > 0}>
2022-09-09 11:53:35 +00:00
<NavTopics topics={getRandomTopics()} />
2022-09-13 09:59:04 +00:00
<Row5 articles={articles().slice(0, 5)} />
2022-09-09 11:53:35 +00:00
<Hero />
<Beside
2022-09-13 09:59:04 +00:00
beside={articles().slice(5, 6)[0]}
2022-09-09 11:53:35 +00:00
title={t('Top viewed')}
2022-09-13 09:59:04 +00:00
values={getTopViewedArticles().slice(0, 5)}
2022-09-09 11:53:35 +00:00
wrapper={'top-article'}
/>
2022-09-13 09:59:04 +00:00
<Row3 articles={articles().slice(6, 9)} />
2022-09-09 11:53:35 +00:00
<Beside
2022-09-13 09:59:04 +00:00
beside={articles().slice(9, 10)[0]}
2022-09-09 11:53:35 +00:00
title={t('Top authors')}
2022-09-13 09:59:04 +00:00
values={getTopAuthors().slice(0, 5)}
2022-09-09 11:53:35 +00:00
wrapper={'author'}
/>
2022-09-13 09:59:04 +00:00
<Slider title={t('Top month articles')} articles={getTopRatedMonthArticles()} />
2022-09-09 11:53:35 +00:00
2022-09-13 09:59:04 +00:00
<Row2 articles={articles().slice(10, 12)} />
<RowShort articles={articles().slice(12, 16)} />
<Row1 article={articles().slice(16, 17)[0]} />
<Row3 articles={articles().slice(17, 20)} />
<Row3 articles={getTopCommentedArticles()} header={<h2>{t('Top commented')}</h2>} />
2022-09-09 11:53:35 +00:00
<Group
articles={someLayout()}
header={
<div class="layout-icon">
<Icon name={selectedLayout()} />
</div>
}
/>
2022-09-13 09:59:04 +00:00
<Slider title={t('Favorite')} articles={getTopRatedArticles()} />
2022-09-09 11:53:35 +00:00
<Beside
2022-09-13 09:59:04 +00:00
beside={articles().slice(20, 21)[0]}
2022-09-09 11:53:35 +00:00
title={t('Top topics')}
2022-09-13 09:59:04 +00:00
values={getTopTopics().slice(0, 5)}
2022-09-09 11:53:35 +00:00
wrapper={'topic'}
isTopicCompact={true}
/>
2022-09-13 09:59:04 +00:00
<Row3 articles={articles().slice(21, 24)} />
2022-09-09 11:53:35 +00:00
<Banner />
2022-09-13 09:59:04 +00:00
<Row2 articles={articles().slice(24, 26)} />
<Row3 articles={articles().slice(26, 29)} />
<Row2 articles={articles().slice(29, 31)} />
<Row3 articles={articles().slice(31, 34)} />
2022-09-09 11:53:35 +00:00
<p class="load-more-container">
2022-09-13 09:59:04 +00:00
<button class="button" onClick={loadMore}>
2022-09-09 11:53:35 +00:00
{t('Load more')}
</button>
</p>
</Show>
</Suspense>
)
}