topic-!-routing
This commit is contained in:
parent
17d2600142
commit
24e594138f
|
@ -20,15 +20,15 @@ import { FullTopic } from '../Topic/Full'
|
||||||
import { Loading } from '../_shared/Loading'
|
import { Loading } from '../_shared/Loading'
|
||||||
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
|
import { ArticleCardSwiper } from '../_shared/SolidSwiper/ArticleCardSwiper'
|
||||||
|
|
||||||
type TopicsPageSearchParams = {
|
// FIXME: should be 'last_comment' and 'comments_stat' or just one?
|
||||||
by: 'comments' | '' | 'recent' | 'viewed' | 'rating' | 'commented'
|
export type TopicFeedSortBy = 'comments' | '' | 'recent' | 'viewed' | 'rating' | 'commented'
|
||||||
}
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
topic: Topic
|
topic: Topic
|
||||||
shouts: Shout[]
|
shouts: Shout[]
|
||||||
topicSlug: string
|
topicSlug: string
|
||||||
followers?: Author[]
|
followers?: Author[]
|
||||||
|
selectedTab?: TopicFeedSortBy
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PRERENDERED_ARTICLES_COUNT = 28
|
export const PRERENDERED_ARTICLES_COUNT = 28
|
||||||
|
@ -39,7 +39,7 @@ export const TopicView = (props: Props) => {
|
||||||
const { feedByTopic, addFeed } = useFeed()
|
const { feedByTopic, addFeed } = useFeed()
|
||||||
const { topicEntities } = useTopics()
|
const { topicEntities } = useTopics()
|
||||||
const { authorsByTopic } = useAuthors()
|
const { authorsByTopic } = useAuthors()
|
||||||
const [searchParams, changeSearchParams] = useSearchParams<TopicsPageSearchParams>()
|
const [searchParams, changeSearchParams] = useSearchParams<{by: TopicFeedSortBy}>()
|
||||||
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
|
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[]>([])
|
||||||
|
|
|
@ -5,7 +5,6 @@ import {
|
||||||
Show,
|
Show,
|
||||||
Suspense,
|
Suspense,
|
||||||
createEffect,
|
createEffect,
|
||||||
createMemo,
|
|
||||||
createSignal,
|
createSignal,
|
||||||
on,
|
on,
|
||||||
onMount
|
onMount
|
||||||
|
@ -15,13 +14,14 @@ import { Loading } from '~/components/_shared/Loading'
|
||||||
import { gaIdentity } from '~/config'
|
import { gaIdentity } from '~/config'
|
||||||
import { useLocalize } from '~/context/localize'
|
import { useLocalize } from '~/context/localize'
|
||||||
import { getShout } from '~/graphql/api/public'
|
import { getShout } from '~/graphql/api/public'
|
||||||
import type { Author, Reaction, Shout } from '~/graphql/schema/core.gen'
|
import type { Author, Reaction, Shout, Topic } from '~/graphql/schema/core.gen'
|
||||||
import { initGA, loadGAScript } from '~/utils/ga'
|
import { initGA, loadGAScript } from '~/utils/ga'
|
||||||
import { descFromBody, keywordsFromTopics } from '~/utils/meta'
|
import { descFromBody, keywordsFromTopics } from '~/utils/meta'
|
||||||
import { FullArticle } from '../../components/Article/FullArticle'
|
import { FullArticle } from '../../components/Article/FullArticle'
|
||||||
import { PageLayout } from '../../components/_shared/PageLayout'
|
import { PageLayout } from '../../components/_shared/PageLayout'
|
||||||
import { ReactionsProvider } from '../../context/reactions'
|
import { ReactionsProvider } from '../../context/reactions'
|
||||||
import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]'
|
import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]'
|
||||||
|
import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]'
|
||||||
|
|
||||||
const fetchShout = async (slug: string): Promise<Shout | undefined> => {
|
const fetchShout = async (slug: string): Promise<Shout | undefined> => {
|
||||||
const shoutLoader = getShout({ slug })
|
const shoutLoader = getShout({ slug })
|
||||||
|
@ -35,29 +35,16 @@ export const route: RouteDefinition = {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type SlugPageProps = { article?: Shout; comments?: Reaction[]; votes?: Reaction[]; author?: Author }
|
type ArticlePageProps = { article?: Shout; comments?: Reaction[]; votes?: Reaction[]; author?: Author }
|
||||||
|
|
||||||
export default (props: RouteSectionProps<SlugPageProps>) => {
|
|
||||||
if (props.params.slug.startsWith('@')) {
|
|
||||||
console.debug('[slug] @ found, render as author page')
|
|
||||||
const patchedProps = {
|
|
||||||
...props,
|
|
||||||
params: {
|
|
||||||
...props.params,
|
|
||||||
slug: props.params.slug.slice(1, props.params.slug.length)
|
|
||||||
}
|
|
||||||
} as RouteSectionProps<AuthorPageProps>
|
|
||||||
return AuthorPage(patchedProps)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export const ArticlePage = (props: RouteSectionProps<ArticlePageProps>) => {
|
||||||
const loc = useLocation()
|
const loc = useLocation()
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
const [scrollToComments, setScrollToComments] = createSignal<boolean>(false)
|
const [scrollToComments, setScrollToComments] = createSignal<boolean>(false)
|
||||||
const article = createAsync(async () => props.data.article || (await fetchShout(props.params.slug)))
|
const data = createAsync(async () => props.data?.article || await fetchShout(props.params.slug))
|
||||||
const titleSuffix = createMemo(() => (article()?.title ? ` :: ${article()?.title || ''}` : ''))
|
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
if (gaIdentity && article()?.id) {
|
if (gaIdentity && data()?.id) {
|
||||||
try {
|
try {
|
||||||
await loadGAScript(gaIdentity)
|
await loadGAScript(gaIdentity)
|
||||||
initGA(gaIdentity)
|
initGA(gaIdentity)
|
||||||
|
@ -69,7 +56,7 @@ export default (props: RouteSectionProps<SlugPageProps>) => {
|
||||||
|
|
||||||
createEffect(
|
createEffect(
|
||||||
on(
|
on(
|
||||||
article,
|
data,
|
||||||
(a?: Shout) => {
|
(a?: Shout) => {
|
||||||
if (!a?.id) return
|
if (!a?.id) return
|
||||||
window?.gtag?.('event', 'page_view', {
|
window?.gtag?.('event', 'page_view', {
|
||||||
|
@ -86,7 +73,7 @@ export default (props: RouteSectionProps<SlugPageProps>) => {
|
||||||
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
|
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
|
||||||
<Suspense fallback={<Loading />}>
|
<Suspense fallback={<Loading />}>
|
||||||
<Show
|
<Show
|
||||||
when={article()?.id}
|
when={data()?.id}
|
||||||
fallback={
|
fallback={
|
||||||
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
|
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
|
||||||
<FourOuFourView />
|
<FourOuFourView />
|
||||||
|
@ -95,16 +82,16 @@ export default (props: RouteSectionProps<SlugPageProps>) => {
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<PageLayout
|
<PageLayout
|
||||||
title={`${t('Discours')}${titleSuffix()}`}
|
title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title||''}`}
|
||||||
desc={descFromBody(article()?.body || '')}
|
desc={descFromBody(data()?.body || '')}
|
||||||
keywords={keywordsFromTopics(article()?.topics as { title: string }[])}
|
keywords={keywordsFromTopics(data()?.topics as { title: string }[])}
|
||||||
headerTitle={article()?.title || ''}
|
headerTitle={data()?.title || ''}
|
||||||
slug={article()?.slug}
|
slug={data()?.slug}
|
||||||
cover={article()?.cover || ''}
|
cover={data()?.cover || ''}
|
||||||
scrollToComments={(value) => setScrollToComments(value)}
|
scrollToComments={(value) => setScrollToComments(value)}
|
||||||
>
|
>
|
||||||
<ReactionsProvider>
|
<ReactionsProvider>
|
||||||
<FullArticle article={article() as Shout} scrollToComments={scrollToComments()} />
|
<FullArticle article={data() as Shout} scrollToComments={scrollToComments()} />
|
||||||
</ReactionsProvider>
|
</ReactionsProvider>
|
||||||
</PageLayout>
|
</PageLayout>
|
||||||
</Show>
|
</Show>
|
||||||
|
@ -112,3 +99,33 @@ export default (props: RouteSectionProps<SlugPageProps>) => {
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SlugPageProps = { article?: Shout; comments?: Reaction[]; votes?: Reaction[]; author?: Author, topics: Topic[] }
|
||||||
|
|
||||||
|
export default (props: RouteSectionProps<SlugPageProps>) => {
|
||||||
|
if (props.params.slug.startsWith('@')) {
|
||||||
|
console.debug('[slug] starts with @, render as author page')
|
||||||
|
const patchedProps = {
|
||||||
|
...props,
|
||||||
|
params: {
|
||||||
|
...props.params,
|
||||||
|
slug: props.params.slug.slice(1, props.params.slug.length)
|
||||||
|
}
|
||||||
|
} as RouteSectionProps<AuthorPageProps>
|
||||||
|
return <AuthorPage {...patchedProps} />
|
||||||
|
}
|
||||||
|
|
||||||
|
if (props.params.slug.startsWith('!')) {
|
||||||
|
console.debug('[slug] starts with !, render as topic page')
|
||||||
|
const patchedProps = {
|
||||||
|
...props,
|
||||||
|
params: {
|
||||||
|
...props.params,
|
||||||
|
slug: props.params.slug.slice(1, props.params.slug.length)
|
||||||
|
}
|
||||||
|
} as RouteSectionProps<TopicPageProps>
|
||||||
|
return <TopicPage {...patchedProps} />
|
||||||
|
}
|
||||||
|
|
||||||
|
return <ArticlePage {...props} />
|
||||||
|
}
|
|
@ -2,16 +2,16 @@ 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, createMemo, createSignal } from 'solid-js'
|
||||||
import { FourOuFourView } from '~/components/Views/FourOuFour'
|
import { FourOuFourView } from '~/components/Views/FourOuFour'
|
||||||
import { TopicView } from '~/components/Views/Topic'
|
import { TopicFeedSortBy, TopicView } from '~/components/Views/Topic'
|
||||||
import { Loading } from '~/components/_shared/Loading'
|
import { Loading } from '~/components/_shared/Loading'
|
||||||
import { PageLayout } from '~/components/_shared/PageLayout'
|
import { PageLayout } from '~/components/_shared/PageLayout'
|
||||||
import { useLocalize } from '~/context/localize'
|
import { useLocalize } from '~/context/localize'
|
||||||
import { useTopics } from '~/context/topics'
|
import { useTopics } from '~/context/topics'
|
||||||
import { loadShouts, loadTopics } from '~/graphql/api/public'
|
import { loadShouts, loadTopics } from '~/graphql/api/public'
|
||||||
import { LoadShoutsOptions, Shout, Topic } from '~/graphql/schema/core.gen'
|
import { Author, LoadShoutsOptions, Shout, Topic } from '~/graphql/schema/core.gen'
|
||||||
import { getImageUrl } from '~/lib/getImageUrl'
|
import { getImageUrl } from '~/lib/getImageUrl'
|
||||||
import { descFromBody } from '~/utils/meta'
|
import { descFromBody } from '~/utils/meta'
|
||||||
import { SHOUTS_PER_PAGE } from '../(main)'
|
import { SHOUTS_PER_PAGE } from '../../(main)'
|
||||||
|
|
||||||
const fetchTopicShouts = async (slug: string, offset?: number) => {
|
const fetchTopicShouts = async (slug: string, offset?: number) => {
|
||||||
const opts: LoadShoutsOptions = { filters: { topic: slug }, limit: SHOUTS_PER_PAGE, offset }
|
const opts: LoadShoutsOptions = { filters: { topic: slug }, limit: SHOUTS_PER_PAGE, offset }
|
||||||
|
@ -34,8 +34,9 @@ export const route = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export type TopicPageProps = { articles?: Shout[]; topics: Topic[], authors?: Author[] }
|
||||||
|
|
||||||
export default (props: RouteSectionProps<{ articles: Shout[]; topics: Topic[] }>) => {
|
export default function TopicPage(props: RouteSectionProps<TopicPageProps>) {
|
||||||
const { t } = useLocalize()
|
const { t } = useLocalize()
|
||||||
const { addTopics } = useTopics()
|
const { addTopics } = useTopics()
|
||||||
const [loadingError, setLoadingError] = createSignal(false)
|
const [loadingError, setLoadingError] = createSignal(false)
|
||||||
|
@ -104,6 +105,7 @@ export default (props: RouteSectionProps<{ articles: Shout[]; topics: Topic[] }>
|
||||||
topic={topic() as Topic}
|
topic={topic() as Topic}
|
||||||
topicSlug={props.params.slug}
|
topicSlug={props.params.slug}
|
||||||
shouts={articles() as Shout[]}
|
shouts={articles() as Shout[]}
|
||||||
|
selectedTab={props.params.tab as TopicFeedSortBy}
|
||||||
/>
|
/>
|
||||||
</PageLayout>
|
</PageLayout>
|
||||||
</Show>
|
</Show>
|
Loading…
Reference in New Issue
Block a user