webapp/src/components/Views/Topic.tsx

122 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-09-13 09:59:04 +00:00
import { For, Show, createMemo } 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'
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'
2022-09-09 11:53:35 +00:00
import { useArticlesStore } from '../../stores/zine/articles'
2022-09-13 09:59:04 +00:00
import { useAuthorsStore } from '../../stores/zine/authors'
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-09-22 09:37:49 +00:00
export const TopicView = (props: TopicProps) => {
const { getSearchParams, changeSearchParam } = useRouter<TopicsPageSearchParams>()
2022-09-13 09:59:04 +00:00
const { getSortedArticles: sortedArticles } = useArticlesStore({ sortedArticles: props.topicArticles })
const { getTopicEntities } = useTopicsStore({ topics: [props.topic] })
2022-09-13 08:05:11 +00:00
2022-09-13 09:59:04 +00:00
const { getAuthorsByTopic } = useAuthorsStore()
const topic = createMemo(() => getTopicEntities()[props.topic.slug])
/*
const slug = createMemo<string>(() => {
let slug = props?.slug
if (props?.slug.startsWith('@')) slug = slug.replace('@', '')
return slug
2022-09-09 11:53:35 +00:00
})
2022-09-13 09:59:04 +00:00
*/
2022-09-09 11:53:35 +00:00
const title = createMemo(() => {
2022-09-22 09:37:49 +00:00
// FIXME
// const m = getSearchParams().by
// 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')
})
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-09-22 09:37:49 +00:00
<li classList={{ selected: getSearchParams().by === 'recent' || !getSearchParams().by }}>
<button type="button" onClick={() => changeSearchParam('by', 'recent')}>
2022-09-09 11:53:35 +00:00
{t('Recent')}
</button>
</li>
2022-09-22 09:37:49 +00:00
<li classList={{ selected: getSearchParams().by === 'rating' }}>
<button type="button" onClick={() => changeSearchParam('by', 'rating')}>
2022-09-09 11:53:35 +00:00
{t('Popular')}
</button>
</li>
2022-09-22 09:37:49 +00:00
<li classList={{ selected: getSearchParams().by === 'viewed' }}>
<button type="button" onClick={() => changeSearchParam('by', 'viewed')}>
2022-09-09 11:53:35 +00:00
{t('Views')}
</button>
</li>
2022-09-22 09:37:49 +00:00
<li classList={{ selected: getSearchParams().by === 'commented' }}>
<button type="button" onClick={() => changeSearchParam('by', 'commented')}>
2022-09-09 11:53:35 +00:00
{t('Discussing')}
</button>
</li>
</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">
2022-09-13 09:59:04 +00:00
<ArticleCard article={article} />
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-14 11:28:43 +00:00
values={getAuthorsByTopic()[topic().slug].slice(0, 7)}
2022-09-09 11:53:35 +00:00
beside={sortedArticles()[6]}
wrapper={'author'}
/>
<Row3 articles={sortedArticles().slice(6, 9)} />
<Row2 articles={sortedArticles().slice(9, 11)} />
<Row3 articles={sortedArticles().slice(11, 14)} />
<Row3 articles={sortedArticles().slice(14, 17)} />
<Row3 articles={sortedArticles().slice(17, 20)} />
</Show>
</div>
</Show>
</div>
)
}