diff --git a/src/app/discover/actions.ts b/src/app/discover/actions.ts index e934227..cd389b3 100644 --- a/src/app/discover/actions.ts +++ b/src/app/discover/actions.ts @@ -52,7 +52,7 @@ export async function getDiscoverData(sort: DiscoverSortOption): Promise(); + const publishedAtByPatchId = new Map(); if (patchIds.length > 0) { const { data: patchRows, error: patchesError } = await supabase .from("patches") - .select("id,version") + .select("id,version,published_at") .in("id", patchIds); if (patchesError) throw patchesError; (patchRows || []).forEach((p: any) => { if (typeof p.id === "number") { versionsByPatchId.set(p.id, p.version || "Pre-release"); + publishedAtByPatchId.set(p.id, p.published_at ?? null); } }); } @@ -186,14 +188,19 @@ export async function getDiscoverData(sort: DiscoverSortOption): Promise(); + const publishedAtBySlug = new Map(); (rows || []).forEach((r: any) => { if (typeof r.current_patch === "number") { const version = versionsByPatchId.get(r.current_patch) || "Pre-release"; mappedVersions.set(r.slug, version); + + const publishedAt = publishedAtByPatchId.get(r.current_patch) ?? null; + publishedAtBySlug.set(r.slug, publishedAt); } else { mappedVersions.set(r.slug, r.original_author ? "Archive" : "Pre-release"); + publishedAtBySlug.set(r.slug, null); } }); @@ -227,6 +234,30 @@ export async function getDiscoverData(sort: DiscoverSortOption): Promise { + const aPub = publishedAtBySlug.get(a.slug); + const bPub = publishedAtBySlug.get(b.slug); + + // Nulls (no published_at) go last + if (!aPub && !bPub) return 0; + if (!aPub) return 1; + if (!bPub) return -1; + + const aTime = new Date(aPub).getTime(); + const bTime = new Date(bPub).getTime(); + + // Secondary sort: when times are equal, push archives to end + if (aTime === bTime) { + if (a.isArchive && !b.isArchive) return 1; + if (!a.isArchive && b.isArchive) return -1; + } + + return bTime - aTime; // Descending order (newest first) + }); + } + // Sort by trending score if needed if (sort === "trending" && trendingScores) { mapped = [...mapped].sort((a, b) => {