0}>
}>
}>
diff --git a/src/routes/(home).tsx b/src/routes/(home).tsx
index d840b53a..2639e817 100644
--- a/src/routes/(home).tsx
+++ b/src/routes/(home).tsx
@@ -1,7 +1,9 @@
import { type RouteDefinition, type RouteSectionProps, createAsync } from '@solidjs/router'
import { Show, Suspense, createEffect, createSignal, on, onMount } from 'solid-js'
+import { LoadShoutsOptions } from '~/graphql/schema/core.gen'
import { loadShouts } from '~/lib/api'
import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll'
+import { byStat } from '~/utils/sortby'
import { HomeView, HomeViewProps } from '../components/Views/Home'
import { Loading } from '../components/_shared/Loading'
import { PageLayout } from '../components/_shared/PageLayout'
@@ -11,32 +13,33 @@ import { ReactionsProvider } from '../context/reactions'
export const SHOUTS_PER_PAGE = 20
const fetchHomeTopData = async () => {
- const limit = 20
-
const topCommentedLoader = loadShouts({
filters: { featured: true },
- limit
+ order_by: 'comments_stat',
+ limit: 10
})
- const topMonthLoader = loadShouts({
- filters: { featured: true },
- limit
- })
-
- const topViewedLoader = loadShouts({
- filters: { featured: true },
- limit
- })
+ 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 })
const topRatedLoader = loadShouts({
filters: { featured: true },
- limit
+ order_by: 'likes_stat',
+ limit: 10
})
return {
- topCommentedShouts: await topCommentedLoader(),
- topMonthShouts: await topMonthLoader(),
topRatedShouts: await topRatedLoader(),
- topViewedShouts: await topViewedLoader()
+ topMonthShouts: await topMonthLoader(),
+ topCommentedShouts: await topCommentedLoader()
} as Partial
}
@@ -69,12 +72,19 @@ export default function HomePage(props: RouteSectionProps) {
// async ssr-friendly router-level cached data source
const data = createAsync(async (prev?: HomeViewProps) => {
const featuredShoutsLoader = featuredLoader(featuredOffset())
- const featuredShouts = await featuredShoutsLoader()
- return {
+ const featuredShouts = [
+ ...(prev?.featuredShouts || []),
+ ...((await featuredShoutsLoader()) || props.data?.featuredShouts || [])
+ ]
+ const sortFn = byStat('viewed')
+ const topViewedShouts = featuredShouts?.sort(sortFn) || []
+ const result = {
...prev,
...props.data,
- featuredShouts: featuredShouts || prev?.featuredShouts || props.data?.featuredShouts
+ topViewedShouts,
+ featuredShouts
}
+ return result
})
const [canLoadMoreFeatured, setCanLoadMoreFeatured] = createSignal(false)
diff --git a/src/routes/[...slug].tsx b/src/routes/[...slug].tsx
index 331f8312..bee9c52e 100644
--- a/src/routes/[...slug].tsx
+++ b/src/routes/[...slug].tsx
@@ -28,7 +28,7 @@ export const ArticlePage = (props: RouteSectionProps<{ article: Shout }>) => {
() => `${article()?.authors?.[0]?.name || t('Discours')}: ${article()?.title || ''}`
)
onMount(async () => {
- if(gaIdentity) {
+ if (gaIdentity) {
try {
console.info('[routes.slug] mounted, connecting ga...')
await loadGAScript(gaIdentity)
@@ -44,10 +44,11 @@ export const ArticlePage = (props: RouteSectionProps<{ article: Shout }>) => {
// wrapped by the returned tracking function is notified of a change`
createReaction(() => {
if (article()) {
+ console.debug('[routes.slug] article signal changed once')
window.gtag?.('event', 'page_view', {
page_title: article()?.title,
page_location: window.location.href,
- page_path: window.location.pathname,
+ page_path: window.location.pathname
})
}
})
diff --git a/src/utils/ga.ts b/src/utils/ga.ts
index fc8c0968..c5ca7c4b 100644
--- a/src/utils/ga.ts
+++ b/src/utils/ga.ts
@@ -19,10 +19,12 @@ export const loadGAScript = (id: string) => {
export const initGA = (id: string) => {
const w = window as Window
if (w) {
+ // @ts-ignore
// biome-ignore lint/suspicious/noExplicitAny: ga-script
w.dataLayer = (w.dataLayer as any) || []
// biome-ignore lint/suspicious/noExplicitAny: ga-script
function gtag(...args: any[]) {
+ // @ts-ignore
w.dataLayer.push(args)
}
gtag('js', new Date())
diff --git a/src/utils/sortby.ts b/src/utils/sortby.ts
index bd048278..61ef9676 100644
--- a/src/utils/sortby.ts
+++ b/src/utils/sortby.ts
@@ -1,4 +1,4 @@
-import type { Author, Reaction, Shout, Topic, TopicStat } from '../graphql/schema/core.gen'
+import type { Author, Maybe, Reaction, Shout, Topic, TopicStat } from '../graphql/schema/core.gen'
// biome-ignore lint/suspicious/noExplicitAny: sort by first char
export const byFirstChar = (a: { name?: any; title?: any }, b: { name?: any; title?: any }) =>
@@ -26,8 +26,10 @@ export const byLength = (
return 0
}
+export type SomeStat = { [x: string]: Maybe } | undefined | null
+
export const byStat = (metric: string) => {
- return (a: { stat: { [x: string]: number } }, b: { stat: { [x: string]: number } }) => {
+ return (a: { stat?: SomeStat }, b: { stat?: SomeStat }) => {
const aStat = a.stat?.[metric] ?? 0
const bStat = b.stat?.[metric] ?? 0
return aStat - bStat