topic-page-fix

This commit is contained in:
Untone 2024-08-28 00:51:17 +03:00
parent 71772ae0d6
commit 95198e9791
5 changed files with 97 additions and 114 deletions

View File

@ -1,7 +1,7 @@
import type { Author, Topic } from '~/graphql/schema/core.gen' import type { Author, Topic } from '~/graphql/schema/core.gen'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import { Show, createEffect, createMemo, createSignal } from 'solid-js' import { Show, createEffect, createSignal, on } from 'solid-js'
import { useFollowing } from '~/context/following' import { useFollowing } from '~/context/following'
import { useLocalize } from '~/context/localize' import { useLocalize } from '~/context/localize'
@ -25,16 +25,18 @@ export const FullTopic = (props: Props) => {
const { follows, changeFollowing } = useFollowing() const { follows, changeFollowing } = useFollowing()
const { requireAuthentication } = useSession() const { requireAuthentication } = useSession()
const [followed, setFollowed] = createSignal() const [followed, setFollowed] = createSignal()
const [title, setTitle] = createSignal('')
const title = createMemo(() => { createEffect(on(() => props.topic, (tpc) => {
if (!tpc) return
/* FIXME: use title translation*/ /* FIXME: use title translation*/
setTitle((_) => tpc?.title || '')
return `#${capitalize( return `#${capitalize(
lang() === 'en' lang() === 'en' ? tpc.slug.replace(/-/, ' ') : tpc.title || tpc.slug.replace(/-/, ' '),
? props.topic.slug.replace(/-/, ' ')
: props.topic.title || props.topic.slug.replace(/-/, ' '),
true true
)}` )}`
}) }, {} ))
createEffect(() => { createEffect(() => {
if (follows?.topics?.length !== 0) { if (follows?.topics?.length !== 0) {
const items = follows.topics || [] const items = follows.topics || []

View File

@ -1,6 +1,6 @@
import { useSearchParams } from '@solidjs/router' import { useSearchParams } from '@solidjs/router'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import { For, Show, Suspense, createEffect, createMemo, createSignal, on, onMount } from 'solid-js' import { For, Show, Suspense, createEffect, createMemo, createSignal, on } from 'solid-js'
import { useAuthors } from '~/context/authors' import { useAuthors } from '~/context/authors'
import { useFeed } from '~/context/feed' import { useFeed } from '~/context/feed'
import { useLocalize } from '~/context/localize' import { useLocalize } from '~/context/localize'
@ -17,6 +17,7 @@ import { Row1 } from '../Feed/Row1'
import { Row2 } from '../Feed/Row2' import { Row2 } from '../Feed/Row2'
import { Row3 } from '../Feed/Row3' import { Row3 } from '../Feed/Row3'
import { FullTopic } from '../Topic/Full' import { FullTopic } from '../Topic/Full'
import { LoadMoreItems, LoadMoreWrapper } from '../_shared/LoadMoreWrapper'
import { Loading } from '../_shared/Loading' import { Loading } from '../_shared/Loading'
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper' import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
@ -40,17 +41,28 @@ export const TopicView = (props: Props) => {
const { topicEntities } = useTopics() const { topicEntities } = useTopics()
const { authorsByTopic } = useAuthors() const { authorsByTopic } = useAuthors()
const [searchParams, changeSearchParams] = useSearchParams<{ by: TopicFeedSortBy }>() const [searchParams, changeSearchParams] = useSearchParams<{ by: TopicFeedSortBy }>()
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
const [favoriteTopArticles, setFavoriteTopArticles] = createSignal<Shout[]>([]) const [favoriteTopArticles, setFavoriteTopArticles] = createSignal<Shout[]>([])
const [reactedTopMonthArticles, setReactedTopMonthArticles] = createSignal<Shout[]>([]) const [reactedTopMonthArticles, setReactedTopMonthArticles] = createSignal<Shout[]>([])
const [topic, setTopic] = createSignal<Topic>() const [topic, setTopic] = createSignal<Topic>()
const [followers, setFollowers] = createSignal<Author[]>(props.followers || []) const [followers, setFollowers] = createSignal<Author[]>(props.followers || [])
const sortedFeed = createMemo(() => feedByTopic()[topic()?.slug || ''] || []) // TODO: filter + sort
// TODO: filter + sort
const [sortedFeed, setSortedFeed] = createSignal([] as Shout[])
createEffect(on(([feedByTopic, () => props.topicSlug, topicEntities]), ([feed, slug, ttt]) => {
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)
}, {}))
const loadTopicFollowers = async () => { const loadTopicFollowers = async () => {
const topicFollowersFetcher = loadFollowersByTopic(props.topicSlug) const topicFollowersFetcher = loadFollowersByTopic(props.topicSlug)
const topicFollowers = await topicFollowersFetcher() const topicFollowers = await topicFollowersFetcher()
topicFollowers && setFollowers(topicFollowers) topicFollowers && setFollowers(topicFollowers)
console.debug('loadTopicFollowers', topicFollowers)
} }
const [topicAuthors, setTopicAuthors] = createSignal<Author[]>([]) const [topicAuthors, setTopicAuthors] = createSignal<Author[]>([])
@ -59,6 +71,7 @@ export const TopicView = (props: Props) => {
const topicAuthorsFetcher = await loadAuthors({ by, limit: 10, offset: 0 }) const topicAuthorsFetcher = await loadAuthors({ by, limit: 10, offset: 0 })
const result = await topicAuthorsFetcher() const result = await topicAuthorsFetcher()
result && setTopicAuthors(result) result && setTopicAuthors(result)
console.debug('loadTopicAuthors', result)
} }
const loadFavoriteTopArticles = async () => { const loadFavoriteTopArticles = async () => {
@ -70,6 +83,7 @@ export const TopicView = (props: Props) => {
const topicRandomShoutsFetcher = loadShouts(options) const topicRandomShoutsFetcher = loadShouts(options)
const result = await topicRandomShoutsFetcher() const result = await topicRandomShoutsFetcher()
result && setFavoriteTopArticles(result) result && setFavoriteTopArticles(result)
console.debug('loadFavoriteTopArticles', result)
} }
const loadReactedTopMonthArticles = async () => { const loadReactedTopMonthArticles = async () => {
@ -85,27 +99,18 @@ export const TopicView = (props: Props) => {
const reactedTopMonthShoutsFetcher = loadShouts(options) const reactedTopMonthShoutsFetcher = loadShouts(options)
const result = await reactedTopMonthShoutsFetcher() const result = await reactedTopMonthShoutsFetcher()
result && setReactedTopMonthArticles(result) result && setReactedTopMonthArticles(result)
console.debug('loadReactedTopMonthArticles', result)
} }
// второй этап начальной загрузки данных // второй этап начальной загрузки данных
createEffect( createEffect(on(topic, (tpc) => {
on( console.debug('topic loaded', tpc)
topicEntities, if (!tpc) return
(ttt: Record<string, Topic>) => { loadFavoriteTopArticles()
if (props.topicSlug in ttt) { loadReactedTopMonthArticles()
Promise.all([ loadTopicAuthors()
loadFavoriteTopArticles(),
loadReactedTopMonthArticles(),
loadTopicAuthors(),
loadTopicFollowers() loadTopicFollowers()
]).finally(() => { }, { defer: true }))
setTopic(ttt[props.topicSlug])
})
}
},
{ defer: true }
)
)
// дозагрузка // дозагрузка
const loadMore = async () => { const loadMore = async () => {
@ -117,19 +122,11 @@ export const TopicView = (props: Props) => {
offset: amountBefore offset: amountBefore
}) })
const result = await topicShoutsFetcher() const result = await topicShoutsFetcher()
if (result) { result && addFeed(result)
addFeed(result)
const amountAfter = feedByTopic()[props.topicSlug].length
setIsLoadMoreButtonVisible(amountBefore !== amountAfter)
}
restoreScrollPosition() restoreScrollPosition()
return result as LoadMoreItems
} }
onMount(() => {
if (sortedFeed() || [].length === PRERENDERED_ARTICLES_COUNT) {
loadMore()
}
})
/* /*
const selectionTitle = createMemo(() => { const selectionTitle = createMemo(() => {
const m = searchParams?.by const m = searchParams?.by
@ -139,15 +136,14 @@ export const TopicView = (props: Props) => {
return t('Top recent') return t('Top recent')
}) })
*/ */
const pages = createMemo<Shout[][]>(() => const pages = createMemo<Shout[][]>(() =>
paginate(sortedFeed(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE) paginate(sortedFeed(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
) )
return ( return (
<div class={styles.topicPage}> <div class={styles.topicPage}>
<Suspense fallback={<Loading />}> <Suspense fallback={<Loading />}>
<Show when={topic()}> <Show when={topic()}><FullTopic topic={topic() as Topic} followers={followers()} authors={topicAuthors()} /></Show>
<FullTopic topic={topic() as Topic} followers={followers()} authors={topicAuthors()} />
</Show>
<div class="wide-container"> <div class="wide-container">
<div class={clsx(styles.groupControls, 'row group__controls')}> <div class={clsx(styles.groupControls, 'row group__controls')}>
<div class="col-md-16"> <div class="col-md-16">
@ -226,6 +222,7 @@ export const TopicView = (props: Props) => {
<Row2 articles={sortedFeed().slice(17, 19)} /> <Row2 articles={sortedFeed().slice(17, 19)} />
</Show> </Show>
<LoadMoreWrapper loadFunction={loadMore} pageSize={SHOUTS_PER_PAGE}>
<For each={pages()}> <For each={pages()}>
{(page) => ( {(page) => (
<> <>
@ -235,14 +232,7 @@ export const TopicView = (props: Props) => {
</> </>
)} )}
</For> </For>
</LoadMoreWrapper>
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={loadMore}>
{t('Load more')}
</button>
</p>
</Show>
</Suspense> </Suspense>
</div> </div>
) )

View File

@ -36,7 +36,6 @@ export const LoadMoreWrapper = (props: LoadMoreProps) => {
saveScrollPosition() saveScrollPosition()
const newItems = await props.loadFunction(offset()) const newItems = await props.loadFunction(offset())
if (!Array.isArray(newItems)) return if (!Array.isArray(newItems)) return
console.debug('[_share] load more items', newItems)
setItems( setItems(
(prev) => (prev) =>
Array.from(new Set([...prev, ...newItems])).sort( Array.from(new Set([...prev, ...newItems])).sort(

View File

@ -79,7 +79,7 @@ export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
topFeed: topRatedFeed topFeed: topRatedFeed
} = useFeed() } = useFeed()
// load more faetured shouts // load more featured shouts
const loadMoreFeatured = async (offset?: number) => { const loadMoreFeatured = async (offset?: number) => {
const shoutsLoader = featuredLoader(offset) const shoutsLoader = featuredLoader(offset)
const loaded = await shoutsLoader() const loaded = await shoutsLoader()

View File

@ -1,6 +1,6 @@
import { RouteSectionProps, createAsync } from '@solidjs/router' import { RouteSectionProps, createAsync } from '@solidjs/router'
import { HttpStatusCode } from '@solidjs/start' import { HttpStatusCode } from '@solidjs/start'
import { Show, Suspense, createEffect, createMemo, createSignal } from 'solid-js' import { Show, Suspense, createEffect, createSignal, on } from 'solid-js'
import { FourOuFourView } from '~/components/Views/FourOuFour' import { FourOuFourView } from '~/components/Views/FourOuFour'
import { TopicFeedSortBy, TopicView } from '~/components/Views/Topic' import { TopicFeedSortBy, TopicView } from '~/components/Views/Topic'
import { Loading } from '~/components/_shared/Loading' import { Loading } from '~/components/_shared/Loading'
@ -21,6 +21,7 @@ const fetchTopicShouts = async (slug: string, offset?: number) => {
const fetchAllTopics = async () => { const fetchAllTopics = async () => {
const topicsFetcher = loadTopics() const topicsFetcher = loadTopics()
console.debug('all topics fetched')
return await topicsFetcher() return await topicsFetcher()
} }
@ -38,55 +39,45 @@ export type TopicPageProps = { articles?: Shout[]; topics: Topic[]; authors?: Au
export default function TopicPage(props: RouteSectionProps<TopicPageProps>) { export default function TopicPage(props: RouteSectionProps<TopicPageProps>) {
const { t } = useLocalize() const { t } = useLocalize()
const { addTopics, sortedTopics } = useTopics() const { addTopics } = useTopics()
const [loadingError, setLoadingError] = createSignal(false) const topics = createAsync(async () => props.data.topics || (await fetchAllTopics()) || [])
const articles = createAsync(async () => {
const topic = createAsync(async () => { const result = (await props.data).articles || (await fetchTopicShouts(props.params.slug))
try { setShoutsLoaded(true)
let ttt: Topic[] = sortedTopics() return result || []
if (!ttt) {
ttt = props.data.topics || (await fetchAllTopics()) || []
addTopics(ttt)
console.debug('[route.topic] all topics loaded')
}
const t = ttt.find((x) => x.slug === props.params.slug)
return t
} catch (_error) {
setLoadingError(true)
return null
}
}) })
const [topic, setTopic] = createSignal<Topic>()
const articles = createAsync( const [title, setTitle] = createSignal<string>('')
async () => props.data.articles || (await fetchTopicShouts(props.params.slug)) || [] const [desc, setDesc] = createSignal<string>('')
) const [cover, setCover] = createSignal<string>('')
const [shoutsLoaded, setShoutsLoaded] = createSignal(false)
const title = createMemo(() => `${t('Discours')}${topic()?.title ? ` :: ${topic()?.title}` : ''}`) createEffect(on([topics, () => window], ([ttt, win]) => {
if (ttt) {
createEffect(() => { // console.debug('all topics:', ttt)
if (topic() && window) { ttt && addTopics(ttt)
window?.gtag?.('event', 'page_view', { const tpc = ttt.find((x) => x.slug === props.params.slug)
page_title: topic()?.title, if (!tpc) return
page_location: window?.location.href, setTopic(tpc)
page_path: window?.location.pathname setTitle(() => `${t('Discours')}${topic()?.title ? ` :: ${topic()?.title}` : ''}`)
}) setDesc(() =>
}
})
const desc = createMemo(() =>
topic()?.body topic()?.body
? descFromBody(topic()?.body || '') ? descFromBody(topic()?.body || '')
: t('The most interesting publications on the topic', { topicName: title() }) : t('The most interesting publications on the topic', { topicName: title() })
) )
setCover(() =>
const cover = createMemo(() =>
topic()?.pic ? getImageUrl(topic()?.pic || '', { width: 1200 }) : '/logo.png' topic()?.pic ? getImageUrl(topic()?.pic || '', { width: 1200 }) : '/logo.png'
) )
if (win) window?.gtag?.('event', 'page_view', {
page_title: tpc.title,
page_location: window?.location.href,
page_path: window?.location.pathname
})
}
}, { defer: true }))
return ( return (
<Suspense fallback={<Loading />}>
<Show <Show
when={!loadingError()} when={shoutsLoaded()}
fallback={ fallback={
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}> <PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
<FourOuFourView /> <FourOuFourView />
@ -94,6 +85,7 @@ export default function TopicPage(props: RouteSectionProps<TopicPageProps>) {
</PageLayout> </PageLayout>
} }
> >
<Suspense fallback={<Loading />}>
<PageLayout <PageLayout
key="topic" key="topic"
title={title()} title={title()}
@ -109,7 +101,7 @@ export default function TopicPage(props: RouteSectionProps<TopicPageProps>) {
selectedTab={props.params.tab as TopicFeedSortBy} selectedTab={props.params.tab as TopicFeedSortBy}
/> />
</PageLayout> </PageLayout>
</Show>
</Suspense> </Suspense>
</Show>
) )
} }