import "server-only"; import { cookies } from "next/headers"; import { isValidSort, SORT_COOKIE, type SortKey } from "./sort"; import { getAppSetting } from "./db/appSettings"; /** * Resolution order: explicit URL ?sort= > server-persisted setting > cookie > default. * The cookie is fallback only (e.g. before any setting is saved); the DB value * survives anything short of deleting the database. */ export async function resolveSort(urlSort?: string): Promise { if (isValidSort(urlSort)) return urlSort; const stored = getAppSetting("defaultSort"); if (isValidSort(stored)) return stored; const cookie = (await cookies()).get(SORT_COOKIE)?.value; if (isValidSort(cookie)) return cookie; return "newest"; } export function getStoredDefaultSort(): SortKey { return getAppSetting("defaultSort"); }