webapp/src/routes/(main).tsx

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-06-24 17:50:27 +00:00
import { type RouteDefinition, type RouteSectionProps, createAsync } from '@solidjs/router'
2024-07-15 20:35:33 +00:00
import { LoadMoreItems, LoadMoreWrapper } from '~/components/_shared/LoadMoreWrapper'
2024-07-15 14:28:08 +00:00
import { useFeed } from '~/context/feed'
2024-07-05 22:45:42 +00:00
import { loadShouts, loadTopics } from '~/graphql/api/public'
2024-07-13 07:53:35 +00:00
import { LoadShoutsOptions, Shout } from '~/graphql/schema/core.gen'
2024-06-24 17:50:27 +00:00
import { HomeView, HomeViewProps } from '../components/Views/Home'
import { PageLayout } from '../components/_shared/PageLayout'
import { useLocalize } from '../context/localize'
export const SHOUTS_PER_PAGE = 20
2024-07-15 14:28:08 +00:00
const featuredLoader = (offset?: number) => {
const SHOUTS_PER_PAGE = 20
return loadShouts({
filters: { featured: true },
limit: SHOUTS_PER_PAGE,
offset
})
}
2024-07-05 22:45:42 +00:00
const fetchAllTopics = async () => {
const allTopicsLoader = loadTopics()
return await allTopicsLoader()
}
2024-06-24 17:50:27 +00:00
const fetchHomeTopData = async () => {
const topCommentedLoader = loadShouts({
filters: { featured: true },
2024-06-28 07:47:38 +00:00
order_by: 'comments_stat',
limit: 10
2024-06-24 17:50:27 +00:00
})
2024-06-28 07:47:38 +00:00
const daysago = Date.now() - 30 * 24 * 60 * 60 * 1000
const after = Math.floor(daysago / 1000)
const options: LoadShoutsOptions = {
filters: {
featured: true,
after
},
order_by: 'likes_stat',
limit: 10
}
const topMonthLoader = loadShouts({ ...options })
2024-06-24 17:50:27 +00:00
const topRatedLoader = loadShouts({
filters: { featured: true },
2024-06-28 07:47:38 +00:00
order_by: 'likes_stat',
limit: 10
2024-06-24 17:50:27 +00:00
})
2024-07-15 16:51:52 +00:00
const topRatedShouts = await topRatedLoader()
const topMonthShouts = await topMonthLoader()
const topCommentedShouts = await topCommentedLoader()
return { topCommentedShouts, topMonthShouts, topRatedShouts } as Partial<HomeViewProps>
2024-06-24 17:50:27 +00:00
}
export const route = {
load: async () => {
const limit = 20
const featuredLoader = loadShouts({
filters: { featured: true },
2024-06-26 08:22:05 +00:00
limit
2024-06-24 17:50:27 +00:00
})
2024-07-05 22:46:07 +00:00
return {
...(await fetchHomeTopData()),
featuredShouts: await featuredLoader(),
topics: await fetchAllTopics()
}
2024-06-26 08:22:05 +00:00
}
2024-06-24 17:50:27 +00:00
} satisfies RouteDefinition
export default function HomePage(props: RouteSectionProps<HomeViewProps>) {
const { t } = useLocalize()
2024-07-15 14:28:08 +00:00
const {
setFeaturedFeed,
featuredFeed,
topMonthFeed,
topViewedFeed,
topCommentedFeed,
topFeed: topRatedFeed
} = useFeed()
2024-06-24 17:50:27 +00:00
2024-07-15 20:35:33 +00:00
// load more faetured shouts
2024-07-15 14:28:08 +00:00
const loadMoreFeatured = async (offset?: number) => {
const shoutsLoader = featuredLoader(offset)
const loaded = await shoutsLoader()
loaded && setFeaturedFeed((prev: Shout[]) => [...prev, ...loaded])
2024-07-15 20:35:33 +00:00
return loaded as LoadMoreItems
2024-07-15 14:28:08 +00:00
}
2024-07-15 20:35:33 +00:00
// preload featured shouts
const shouts = createAsync(async () => {
if (props.data.featuredShouts) {
setFeaturedFeed(props.data.featuredShouts)
console.debug('[routes.main] featured feed preloaded')
return props.data.featuredShouts
}
return await loadMoreFeatured()
})
2024-07-15 14:28:08 +00:00
const SHOUTS_PER_PAGE = 20
2024-07-15 20:35:33 +00:00
2024-06-24 17:50:27 +00:00
return (
2024-07-15 14:28:08 +00:00
<PageLayout withPadding={true} title={t('Discours')} key="home">
2024-07-18 09:22:28 +00:00
<LoadMoreWrapper loadFunction={loadMoreFeatured} pageSize={SHOUTS_PER_PAGE} hidden={!featuredFeed()}>
2024-07-18 04:33:23 +00:00
<HomeView
featuredShouts={featuredFeed() || (shouts() as Shout[])}
topMonthShouts={topMonthFeed() as Shout[]}
topViewedShouts={topViewedFeed() as Shout[]}
topRatedShouts={topRatedFeed() as Shout[]}
topCommentedShouts={topCommentedFeed() as Shout[]}
/>
</LoadMoreWrapper>
2024-06-24 17:50:27 +00:00
</PageLayout>
)
}