webapp/src/routes/author/[slug]/[...tab].tsx

134 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-08-28 09:50:04 +00:00
import { RouteSectionProps, createAsync } from '@solidjs/router'
import { ErrorBoundary, Suspense, createEffect, createSignal, on } from 'solid-js'
2024-07-12 12:10:22 +00:00
import { AuthorView } from '~/components/Views/Author'
import { FourOuFourView } from '~/components/Views/FourOuFour'
2024-07-30 19:06:17 +00:00
import { Loading } from '~/components/_shared/Loading'
2024-07-12 12:10:22 +00:00
import { PageLayout } from '~/components/_shared/PageLayout'
import { useAuthors } from '~/context/authors'
2024-09-03 08:07:32 +00:00
import { SHOUTS_PER_PAGE } from '~/context/feed'
2024-07-12 12:10:22 +00:00
import { useLocalize } from '~/context/localize'
2024-07-18 10:22:58 +00:00
import { ReactionsProvider } from '~/context/reactions'
import { loadAuthors, loadShouts, loadTopics } from '~/graphql/api/public'
2024-07-12 12:10:22 +00:00
import {
Author,
LoadShoutsOptions,
QueryLoad_Authors_ByArgs,
Shout,
Topic
} from '~/graphql/schema/core.gen'
2024-07-13 12:25:25 +00:00
import { getImageUrl } from '~/lib/getThumbUrl'
2024-07-12 12:10:22 +00:00
const fetchAuthorShouts = async (slug: string, offset?: number) => {
const opts: LoadShoutsOptions = { filters: { author: slug }, limit: SHOUTS_PER_PAGE, offset }
const shoutsLoader = loadShouts(opts)
return await shoutsLoader()
}
const fetchAllTopics = async () => {
const topicsFetcher = loadTopics()
return await topicsFetcher()
}
const fetchAuthor = async (slug: string) => {
2024-07-13 07:01:41 +00:00
const authorFetcher = loadAuthors({ by: { slug }, limit: 1, offset: 0 } as QueryLoad_Authors_ByArgs)
2024-07-12 12:10:22 +00:00
const aaa = await authorFetcher()
return aaa?.[0]
}
export const route = {
load: async ({ params, location: { query } }: RouteSectionProps<{ articles: Shout[] }>) => {
const offset: number = Number.parseInt(query.offset, 10)
2024-08-28 09:50:04 +00:00
console.debug('route loading with offset', offset)
2024-07-12 12:10:22 +00:00
return {
author: await fetchAuthor(params.slug),
2024-08-28 09:50:04 +00:00
articles: await fetchAuthorShouts(params.slug, offset),
2024-07-12 12:10:22 +00:00
topics: await fetchAllTopics()
}
}
}
2024-07-13 09:06:49 +00:00
export type AuthorPageProps = { articles?: Shout[]; author?: Author; topics?: Topic[] }
2024-07-13 09:45:10 +00:00
export default function AuthorPage(props: RouteSectionProps<AuthorPageProps>) {
2024-07-12 12:10:22 +00:00
const { t } = useLocalize()
2024-07-26 15:49:15 +00:00
2024-08-28 09:50:04 +00:00
// load author's profile
const { addAuthor, authorsEntities } = useAuthors()
const [author, setAuthor] = createSignal<Author | undefined>(undefined)
2024-07-26 15:49:15 +00:00
createEffect(
2024-08-29 14:40:31 +00:00
on(
author,
2024-08-28 09:50:04 +00:00
async (profile) => {
// update only if no profile loaded
2024-07-26 15:49:15 +00:00
if (!profile) {
2024-08-29 14:40:31 +00:00
const loadedAuthor =
authorsEntities()[props.params.slug] || (await fetchAuthor(props.params.slug))
2024-07-26 15:49:15 +00:00
if (loadedAuthor) {
addAuthor(loadedAuthor)
setAuthor(loadedAuthor)
}
}
},
{ defer: true }
)
)
2024-08-28 09:50:04 +00:00
// author's data, view counter
const [title, setTitle] = createSignal<string>('')
const [desc, setDesc] = createSignal<string>('')
const [cover, setCover] = createSignal<string>('')
const [viewed, setViewed] = createSignal(false)
createEffect(
on(
[author, () => window],
([a, win]) => {
if (a && win) {
console.debug('[routes] author/[slug] author loaded fx')
if (!a) return
setTitle(() => `${t('Discours')}${a.name ? ` :: ${a.name}` : ''}`)
setDesc(() => a.about || a.bio || '')
setCover(() => (a.pic ? getImageUrl(a.pic || '', { width: 1200 }) : 'log.png'))
// views google counter increment
if (!viewed()) {
window?.gtag?.('event', 'page_view', {
page_title: author()?.name || '',
page_location: window?.location.href || '',
page_path: window?.location.pathname || ''
})
setViewed(true)
}
}
},
{}
)
)
// author's shouts
2024-09-03 10:29:01 +00:00
const authorShouts = createAsync(
async () => (props.data.articles as Shout[]) || (await fetchAuthorShouts(props.params.slug, 0))
)
2024-07-16 00:14:08 +00:00
2024-07-12 12:10:22 +00:00
return (
<ErrorBoundary fallback={(_err) => <FourOuFourView />}>
2024-07-30 19:06:17 +00:00
<Suspense fallback={<Loading />}>
<PageLayout
2024-08-28 09:50:04 +00:00
title={title()}
2024-07-30 19:06:17 +00:00
headerTitle={author()?.name || ''}
slug={author()?.slug}
2024-08-28 09:50:04 +00:00
desc={desc()}
2024-07-30 19:06:17 +00:00
cover={cover()}
>
<ReactionsProvider>
2024-09-03 08:07:32 +00:00
<AuthorView
author={author() as Author}
authorSlug={props.params.slug}
shouts={authorShouts() || []}
/>
2024-07-30 19:06:17 +00:00
</ReactionsProvider>
</PageLayout>
</Suspense>
2024-07-12 12:10:22 +00:00
</ErrorBoundary>
)
}