2024-07-09 09:13:13 +00:00
|
|
|
import { useSearchParams } from '@solidjs/router'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { clsx } from 'clsx'
|
2024-03-29 17:25:17 +00:00
|
|
|
import { For, Show, createEffect, createMemo, createSignal, on, onMount } from 'solid-js'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { useAuthors } from '~/context/authors'
|
|
|
|
import { useFeed } from '~/context/feed'
|
2024-06-24 17:50:27 +00:00
|
|
|
import { useGraphQL } from '~/context/graphql'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { useLocalize } from '~/context/localize'
|
|
|
|
import { useTopics } from '~/context/topics'
|
2024-06-24 17:50:27 +00:00
|
|
|
import getRandomTopShoutsQuery from '~/graphql/query/core/articles-load-random-top'
|
|
|
|
import loadShoutsRandomQuery from '~/graphql/query/core/articles-load-random-topic'
|
|
|
|
import loadAuthorsByQuery from '~/graphql/query/core/authors-load-by'
|
|
|
|
import getTopicFollowersQuery from '~/graphql/query/core/topic-followers'
|
2024-07-09 09:13:13 +00:00
|
|
|
import { Author, AuthorsBy, LoadShoutsOptions, Shout, Topic } from '~/graphql/schema/core.gen'
|
2024-07-04 07:51:15 +00:00
|
|
|
import { getUnixtime } from '~/utils/getServerDate'
|
|
|
|
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
|
|
|
|
import { splitToPages } from '~/utils/splitToPages'
|
2024-06-24 17:50:27 +00:00
|
|
|
import styles from '../../styles/Topic.module.scss'
|
2023-11-14 15:10:00 +00:00
|
|
|
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-02-04 11:25:21 +00:00
|
|
|
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
type TopicsPageSearchParams = {
|
|
|
|
by: 'comments' | '' | 'recent' | 'viewed' | 'rating' | 'commented'
|
|
|
|
}
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2023-12-13 10:39:31 +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[]
|
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
|
|
|
|
|
2023-12-13 10:39:31 +00:00
|
|
|
export const TopicView = (props: Props) => {
|
2024-07-09 09:13:13 +00:00
|
|
|
const { t } = useLocalize()
|
2024-06-24 17:50:27 +00:00
|
|
|
const { query } = useGraphQL()
|
|
|
|
const [searchParams, changeSearchParams] = useSearchParams<TopicsPageSearchParams>()
|
2022-10-28 21:21:47 +00:00
|
|
|
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
|
2024-06-24 17:50:27 +00:00
|
|
|
const { feedByTopic, loadShouts } = useFeed()
|
|
|
|
const sortedFeed = createMemo(() => feedByTopic()[topic()?.slug || ''] || [])
|
2024-05-06 23:44:25 +00:00
|
|
|
const { topicEntities } = useTopics()
|
2024-06-24 17:50:27 +00:00
|
|
|
const { authorsByTopic } = useAuthors()
|
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-06-06 08:36:07 +00:00
|
|
|
createEffect(
|
2024-06-06 09:27:49 +00:00
|
|
|
on([() => props.topicSlug, topic, topicEntities], async ([slug, t, ttt]) => {
|
2024-06-06 09:04:01 +00:00
|
|
|
if (slug && !t && ttt) {
|
2024-06-24 17:50:27 +00:00
|
|
|
const current = slug in ttt ? ttt[slug] : null
|
2024-06-06 09:04:01 +00:00
|
|
|
console.debug(current)
|
2024-06-24 17:50:27 +00:00
|
|
|
setTopic(current as Topic)
|
2024-06-06 09:27:49 +00:00
|
|
|
await loadTopicFollowers()
|
|
|
|
await loadTopicAuthors()
|
2024-06-24 17:50:27 +00:00
|
|
|
loadRandom()
|
2024-06-06 09:04:01 +00:00
|
|
|
}
|
2024-06-26 08:22:05 +00:00
|
|
|
})
|
2024-06-06 08:36:07 +00:00
|
|
|
)
|
2024-03-29 17:25:07 +00:00
|
|
|
|
2024-05-18 16:45:36 +00:00
|
|
|
const [followers, setFollowers] = createSignal<Author[]>(props.followers || [])
|
|
|
|
const loadTopicFollowers = async () => {
|
2024-06-24 17:50:27 +00:00
|
|
|
const resp = await query(getTopicFollowersQuery, { slug: props.topicSlug }).toPromise()
|
|
|
|
setFollowers(resp?.data?.get_topic_followers || [])
|
2024-06-06 09:27:49 +00:00
|
|
|
}
|
|
|
|
const [topicAuthors, setTopicAuthors] = createSignal<Author[]>([])
|
|
|
|
const loadTopicAuthors = async () => {
|
|
|
|
const by: AuthorsBy = { topic: props.topicSlug }
|
2024-06-24 17:50:27 +00:00
|
|
|
const resp = await query(loadAuthorsByQuery, { by, limit: 10, offset: 0 }).toPromise()
|
|
|
|
setTopicAuthors(resp?.data?.load_authors_by || [])
|
2024-05-18 16:45:36 +00:00
|
|
|
}
|
2024-03-29 17:25:07 +00:00
|
|
|
|
2024-04-02 11:27:56 +00:00
|
|
|
const loadFavoriteTopArticles = async (topic: string) => {
|
2024-03-29 17:25:07 +00:00
|
|
|
const options: LoadShoutsOptions = {
|
2024-03-29 17:25:17 +00:00
|
|
|
filters: { featured: true, topic: topic },
|
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-06-24 17:50:27 +00:00
|
|
|
const resp = await query(getRandomTopShoutsQuery, { options }).toPromise()
|
|
|
|
setFavoriteTopArticles(resp?.data?.l)
|
2024-03-29 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 11:27:56 +00:00
|
|
|
const loadReactedTopMonthArticles = async (topic: string) => {
|
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 = {
|
|
|
|
filters: { after: after, featured: true, topic: topic },
|
|
|
|
limit: 10,
|
2024-06-26 08:22:05 +00:00
|
|
|
random_limit: 10
|
2024-03-29 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
const resp = await query(loadShoutsRandomQuery, { options }).toPromise()
|
|
|
|
setReactedTopMonthArticles(resp?.data?.load_shouts_random)
|
2024-03-29 17:25:07 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 11:27:56 +00:00
|
|
|
const loadRandom = () => {
|
2024-06-24 17:50:27 +00:00
|
|
|
if (topic()) {
|
|
|
|
loadFavoriteTopArticles((topic() as Topic).slug)
|
|
|
|
loadReactedTopMonthArticles((topic() as Topic).slug)
|
|
|
|
}
|
2024-04-02 11:27:56 +00:00
|
|
|
}
|
|
|
|
|
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-12-20 07:45:29 +00:00
|
|
|
filters: { topic: topic()?.slug },
|
2022-10-28 21:21:47 +00:00
|
|
|
limit: LOAD_MORE_PAGE_SIZE,
|
2024-06-26 08:22:05 +00:00
|
|
|
offset: sortedFeed().length // FIXME: use feedByTopic
|
2022-10-28 21:21:47 +00:00
|
|
|
})
|
|
|
|
setIsLoadMoreButtonVisible(hasMore)
|
|
|
|
|
|
|
|
restoreScrollPosition()
|
|
|
|
}
|
|
|
|
|
2024-02-05 15:04:23 +00:00
|
|
|
onMount(() => {
|
2024-04-02 11:27:56 +00:00
|
|
|
loadRandom()
|
2024-06-24 17:50:27 +00:00
|
|
|
if (sortedFeed() || [].length === PRERENDERED_ARTICLES_COUNT) {
|
2022-10-28 21:21:47 +00:00
|
|
|
loadMore()
|
|
|
|
}
|
|
|
|
})
|
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
|
|
|
*/
|
2022-10-28 21:21:47 +00:00
|
|
|
const pages = createMemo<Shout[][]>(() =>
|
2024-06-26 08:22:05 +00:00
|
|
|
splitToPages(sortedFeed(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
|
2022-10-28 21:21:47 +00:00
|
|
|
)
|
2022-09-09 11:53:35 +00:00
|
|
|
return (
|
2022-11-07 21:07:42 +00:00
|
|
|
<div class={styles.topicPage}>
|
2024-06-24 17:50:27 +00:00
|
|
|
<FullTopic topic={topic() as Topic} followers={followers()} authors={topicAuthors()} />
|
2023-12-21 12:48:13 +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={{
|
2024-06-26 08:22:05 +00:00
|
|
|
'view-switcher__item--selected': searchParams?.by === 'recent' || !searchParams?.by
|
2023-12-21 12:48:13 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() =>
|
|
|
|
changeSearchParams({
|
2024-06-26 08:22:05 +00:00
|
|
|
by: 'recent'
|
2023-12-21 12:48:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{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>
|
2022-09-09 11:53:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-12-21 12:48:13 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
<Row1 article={sortedFeed()[0]} />
|
|
|
|
<Row2 articles={sortedFeed().slice(1, 3)} isEqual={true} />
|
2023-12-21 12:48:13 +00:00
|
|
|
|
|
|
|
<Beside
|
|
|
|
title={t('Topic is supported by')}
|
2024-06-24 17:50:27 +00:00
|
|
|
values={authorsByTopic()[topic()?.slug || '']?.slice(0, 6)}
|
|
|
|
beside={sortedFeed()[4]}
|
2023-12-21 12:48:13 +00:00
|
|
|
wrapper={'author'}
|
|
|
|
/>
|
2024-03-29 17:25:07 +00:00
|
|
|
<Show when={reactedTopMonthArticles()?.length > 0} keyed={true}>
|
2024-05-06 23:44:25 +00:00
|
|
|
<ArticleCardSwiper title={t('Top month')} slides={reactedTopMonthArticles()} />
|
2024-03-29 17:25:07 +00:00
|
|
|
</Show>
|
2023-12-21 12:48:13 +00:00
|
|
|
<Beside
|
2024-06-24 17:50:27 +00:00
|
|
|
beside={sortedFeed()[12]}
|
2023-12-21 12:48:13 +00:00
|
|
|
title={t('Top viewed')}
|
2024-06-24 17:50:27 +00:00
|
|
|
values={sortedFeed().slice(0, 5)}
|
2023-12-21 12:48:13 +00:00
|
|
|
wrapper={'top-article'}
|
|
|
|
/>
|
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
<Row2 articles={sortedFeed().slice(13, 15)} isEqual={true} />
|
|
|
|
<Row1 article={sortedFeed()[15]} />
|
2023-12-21 12:48:13 +00:00
|
|
|
|
2024-03-29 17:25:07 +00:00
|
|
|
<Show when={favoriteTopArticles()?.length > 0} keyed={true}>
|
|
|
|
<ArticleCardSwiper title={t('Favorite')} slides={favoriteTopArticles()} />
|
|
|
|
</Show>
|
2024-06-24 17:50:27 +00:00
|
|
|
<Show when={sortedFeed().length > 15}>
|
|
|
|
<Row3 articles={sortedFeed().slice(23, 26)} />
|
|
|
|
<Row2 articles={sortedFeed().slice(26, 28)} />
|
2023-12-21 12:48:13 +00:00
|
|
|
</Show>
|
2022-11-07 21:07:42 +00:00
|
|
|
|
2023-12-21 12:48:13 +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>
|
2022-09-09 11:53:35 +00:00
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|