webapp/src/components/Views/TopicView.tsx

273 lines
9.7 KiB
TypeScript
Raw Normal View History

2024-07-09 09:13:13 +00:00
import { useSearchParams } from '@solidjs/router'
import { clsx } from 'clsx'
2024-09-03 11:36:33 +00:00
import { For, Match, Show, Suspense, Switch, createEffect, createMemo, createSignal, on } from 'solid-js'
2024-09-16 12:57:00 +00:00
import { SHOUTS_PER_PAGE, useFeed } from '~/context/feed'
import { useLocalize } from '~/context/localize'
import { useTopics } from '~/context/topics'
2024-07-09 17:41:14 +00:00
import { loadAuthors, loadFollowersByTopic, loadShouts } from '~/graphql/api/public'
2024-07-09 09:13:13 +00:00
import { Author, AuthorsBy, LoadShoutsOptions, Shout, Topic } from '~/graphql/schema/core.gen'
2024-07-13 11:42:53 +00:00
import { getUnixtime } from '~/utils/date'
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
2024-09-03 11:36:33 +00:00
import { byPublished, byStat } from '~/utils/sort'
import { Beside } from '../Feed/Beside'
import { Row1 } from '../Feed/Row1'
import { Row2 } from '../Feed/Row2'
import { Row3 } from '../Feed/Row3'
import { FullTopic } from '../Topic/Full'
2024-08-27 21:51:17 +00:00
import { LoadMoreItems, LoadMoreWrapper } from '../_shared/LoadMoreWrapper'
2024-07-09 17:41:14 +00:00
import { Loading } from '../_shared/Loading'
2024-02-04 11:25:21 +00:00
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
2024-10-02 20:12:14 +00:00
import styles from '~/styles/views/Topic.module.scss'
2024-08-06 15:19:38 +00:00
export type TopicFeedSortBy = 'comments' | '' | 'recent' | 'viewed' | 'rating' | 'last_comment'
2022-09-09 11:53:35 +00:00
interface Props {
2022-09-09 11:53:35 +00:00
topic: Topic
2022-11-15 14:24:50 +00:00
shouts: Shout[]
2022-10-05 15:11:14 +00:00
topicSlug: string
2024-05-18 16:45:36 +00:00
followers?: Author[]
2024-07-13 10:32:27 +00:00
selectedTab?: TopicFeedSortBy
2022-09-09 11:53:35 +00:00
}
export const PRERENDERED_ARTICLES_COUNT = 28
2024-08-28 13:10:00 +00:00
// const LOAD_MORE_PAGE_SIZE = 9 // Row3 + Row3 + Row3
export const TopicView = (props: Props) => {
2024-07-09 09:13:13 +00:00
const { t } = useLocalize()
2024-07-09 17:41:14 +00:00
const { feedByTopic, addFeed } = useFeed()
2024-05-06 23:44:25 +00:00
const { topicEntities } = useTopics()
2024-07-13 10:33:49 +00:00
const [searchParams, changeSearchParams] = useSearchParams<{ by: TopicFeedSortBy }>()
2024-03-29 17:25:07 +00:00
const [favoriteTopArticles, setFavoriteTopArticles] = createSignal<Shout[]>([])
const [reactedTopMonthArticles, setReactedTopMonthArticles] = createSignal<Shout[]>([])
2024-01-31 12:34:15 +00:00
const [topic, setTopic] = createSignal<Topic>()
2024-05-18 16:45:36 +00:00
const [followers, setFollowers] = createSignal<Author[]>(props.followers || [])
2024-08-27 21:51:17 +00:00
// TODO: filter + sort
const [sortedFeed, setSortedFeed] = createSignal([] as Shout[])
2024-08-29 14:40:31 +00:00
createEffect(
on(
[feedByTopic, () => props.topicSlug, topicEntities],
([feed, slug, ttt]) => {
2024-08-28 13:10:00 +00:00
if (Object.values(ttt).length === 0) return
const sss = (feed[slug] || []) as Shout[]
sss && setSortedFeed(sss)
console.debug('topic slug loaded', slug)
const tpc = ttt[slug]
console.debug('topics loaded', ttt)
tpc && setTopic(tpc)
2024-08-29 14:40:31 +00:00
},
{}
)
)
2024-07-09 17:41:14 +00:00
2024-05-18 16:45:36 +00:00
const loadTopicFollowers = async () => {
2024-07-09 17:41:14 +00:00
const topicFollowersFetcher = loadFollowersByTopic(props.topicSlug)
const topicFollowers = await topicFollowersFetcher()
topicFollowers && setFollowers(topicFollowers)
2024-08-27 21:51:17 +00:00
console.debug('loadTopicFollowers', topicFollowers)
2024-06-06 09:27:49 +00:00
}
2024-07-09 17:41:14 +00:00
2024-06-06 09:27:49 +00:00
const [topicAuthors, setTopicAuthors] = createSignal<Author[]>([])
const loadTopicAuthors = async () => {
const by: AuthorsBy = { topic: props.topicSlug }
2024-07-09 17:41:14 +00:00
const topicAuthorsFetcher = await loadAuthors({ by, limit: 10, offset: 0 })
const result = await topicAuthorsFetcher()
result && setTopicAuthors(result)
2024-08-27 21:51:17 +00:00
console.debug('loadTopicAuthors', result)
2024-05-18 16:45:36 +00:00
}
2024-03-29 17:25:07 +00:00
2024-07-09 17:41:14 +00:00
const loadFavoriteTopArticles = async () => {
2024-03-29 17:25:07 +00:00
const options: LoadShoutsOptions = {
2024-07-09 17:41:14 +00:00
filters: { featured: true, topic: props.topicSlug },
2024-03-29 17:25:07 +00:00
limit: 10,
2024-06-26 08:22:05 +00:00
random_limit: 100
2024-03-29 17:25:07 +00:00
}
2024-07-09 17:41:14 +00:00
const topicRandomShoutsFetcher = loadShouts(options)
const result = await topicRandomShoutsFetcher()
result && setFavoriteTopArticles(result)
2024-08-27 21:51:17 +00:00
console.debug('loadFavoriteTopArticles', result)
2024-03-29 17:25:07 +00:00
}
2024-07-09 17:41:14 +00:00
const loadReactedTopMonthArticles = async () => {
2024-03-29 17:25:07 +00:00
const now = new Date()
const after = getUnixtime(new Date(now.setMonth(now.getMonth() - 1)))
const options: LoadShoutsOptions = {
2024-07-09 17:41:14 +00:00
filters: { after: after, featured: true, topic: props.topicSlug },
2024-03-29 17:25:07 +00:00
limit: 10,
2024-06-26 08:22:05 +00:00
random_limit: 10
2024-03-29 17:25:07 +00:00
}
2024-07-09 17:41:14 +00:00
const reactedTopMonthShoutsFetcher = loadShouts(options)
const result = await reactedTopMonthShoutsFetcher()
result && setReactedTopMonthArticles(result)
2024-08-27 21:51:17 +00:00
console.debug('loadReactedTopMonthArticles', result)
2024-03-29 17:25:07 +00:00
}
2024-07-09 17:41:14 +00:00
// второй этап начальной загрузки данных
2024-08-29 14:40:31 +00:00
createEffect(
on(
topic,
(tpc) => {
2024-08-28 13:10:00 +00:00
console.debug('topic loaded', tpc)
if (!tpc) return
loadFavoriteTopArticles()
loadReactedTopMonthArticles()
loadTopicAuthors()
loadTopicFollowers()
2024-08-29 14:40:31 +00:00
},
{ defer: true }
)
)
2024-04-02 11:27:56 +00:00
2024-07-09 17:41:14 +00:00
// дозагрузка
const loadMore = async () => {
saveScrollPosition()
2024-07-09 17:41:14 +00:00
const amountBefore = feedByTopic()?.[props.topicSlug]?.length || 0
const topicShoutsFetcher = loadShouts({
filters: { topic: props.topicSlug },
limit: SHOUTS_PER_PAGE,
offset: amountBefore
})
2024-07-09 17:41:14 +00:00
const result = await topicShoutsFetcher()
2024-08-27 21:51:17 +00:00
result && addFeed(result)
restoreScrollPosition()
2024-08-27 21:51:17 +00:00
return result as LoadMoreItems
}
2023-12-24 12:56:30 +00:00
/*
2023-12-20 07:45:29 +00:00
const selectionTitle = createMemo(() => {
2024-06-24 17:50:27 +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')
})
2023-12-24 12:56:30 +00:00
*/
2024-08-27 21:51:17 +00:00
2024-09-03 11:36:33 +00:00
const topViewedShouts = createMemo(() => {
const loaded = feedByTopic()?.[props.topicSlug] || []
const sss = [...loaded] as Shout[]
const sortfn = byStat('views') || byPublished
sortfn && sss.sort(sortfn as ((a: Shout, b: Shout) => number) | undefined)
return sss
})
2022-09-09 11:53:35 +00:00
return (
2022-11-07 21:07:42 +00:00
<div class={styles.topicPage}>
2024-07-09 17:41:14 +00:00
<Suspense fallback={<Loading />}>
2024-08-29 14:40:31 +00:00
<Show when={topic()}>
<FullTopic topic={topic() as Topic} followers={followers()} authors={topicAuthors()} />
</Show>
2024-07-09 17:41:14 +00:00
<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': searchParams?.by === 'recent' || !searchParams?.by
}}
>
2024-07-09 17:41:14 +00:00
<button
type="button"
onClick={() =>
changeSearchParams({
by: 'recent'
})
}
>
{t('Recent')}
</button>
</li>
{/*TODO: server sort*/}
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'rating' }}>*/}
{/* <button type="button" onClick={() => changeSearchParams('by', 'rating')}>*/}
{/* {t('Popular')}*/}
{/* </button>*/}
{/*</li>*/}
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'viewed' }}>*/}
{/* <button type="button" onClick={() => changeSearchParams('by', 'viewed')}>*/}
{/* {t('Views')}*/}
{/* </button>*/}
{/*</li>*/}
{/*<li classList={{ 'view-switcher__item--selected': getSearchParams().by === 'commented' }}>*/}
{/* <button type="button" onClick={() => changeSearchParams('by', 'commented')}>*/}
{/* {t('Discussing')}*/}
{/* </button>*/}
{/*</li>*/}
</ul>
</div>
<div class="col-md-8">
<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>
2024-07-09 17:41:14 +00:00
<Row1 article={sortedFeed()[0]} />
<Row2 articles={sortedFeed().slice(1, 3)} isEqual={true} />
<Beside
2024-08-26 18:22:27 +00:00
beside={sortedFeed()[3]}
2024-09-03 11:36:33 +00:00
title={t('Topic is supported by')}
values={topicAuthors().slice(0, 6)}
2024-07-09 17:41:14 +00:00
wrapper={'author'}
/>
<Show when={reactedTopMonthArticles()?.length > 0} keyed={true}>
<ArticleCardSwiper title={t('Top month')} slides={reactedTopMonthArticles()} />
</Show>
<Beside
2024-09-03 11:36:33 +00:00
beside={sortedFeed()[4]}
2024-07-09 17:41:14 +00:00
title={t('Top viewed')}
2024-09-03 11:36:33 +00:00
values={topViewedShouts().slice(0, 5)}
2024-07-09 17:41:14 +00:00
wrapper={'top-article'}
/>
2024-09-03 11:36:33 +00:00
<Row2 articles={sortedFeed().slice(5, 7)} isEqual={true} />
<Row1 article={sortedFeed()[7]} />
2024-07-09 17:41:14 +00:00
<Show when={favoriteTopArticles()?.length > 0} keyed={true}>
<ArticleCardSwiper title={t('Favorite')} slides={favoriteTopArticles()} />
</Show>
2024-08-26 18:22:27 +00:00
2024-09-03 11:36:33 +00:00
<Show when={sortedFeed().length > 7}>
<Row3 articles={sortedFeed().slice(8, 11)} />
<Row2 articles={sortedFeed().slice(11, 13)} />
2024-07-09 17:41:14 +00:00
</Show>
2024-08-27 21:51:17 +00:00
<LoadMoreWrapper loadFunction={loadMore} pageSize={SHOUTS_PER_PAGE}>
2024-08-29 14:40:31 +00:00
<For
each={sortedFeed()
2024-09-03 11:36:33 +00:00
.slice(13)
2024-08-29 14:40:31 +00:00
.filter((_, i) => i % 3 === 0)}
>
{(_shout, index) => {
const articles = sortedFeed()
2024-09-03 11:36:33 +00:00
.slice(13)
2024-08-29 14:40:31 +00:00
.slice(index() * 3, index() * 3 + 3)
return (
<>
<Switch>
<Match when={articles.length === 1}>
<Row1 article={articles[0]} noauthor={true} nodate={true} />
</Match>
<Match when={articles.length === 2}>
<Row2 articles={articles} noauthor={true} nodate={true} isEqual={true} />
</Match>
<Match when={articles.length === 3}>
<Row3 articles={articles} noauthor={true} nodate={true} />
</Match>
</Switch>
</>
)
}}
</For>
2024-08-27 21:51:17 +00:00
</LoadMoreWrapper>
2024-07-09 17:41:14 +00:00
</Suspense>
2022-09-09 11:53:35 +00:00
</div>
)
}