Files
pinkudex/lib/sortServer.ts
T
2026-05-26 22:46:00 +02:00

23 lines
827 B
TypeScript

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<SortKey> {
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");
}