peppa/apps/web/lib/title.ts

27 lines
1.6 KiB
TypeScript

// PEP-274 (F-13): every browser tab says which page it is — «<страница> — FORGE».
import type { Metadata } from "next";
import { formatTaskKey } from "@peppa/shared";
import { productName, type Translator } from "@peppa/shared/i18n";
import { currentUser } from "@/lib/auth";
import { projectForUserByKey, taskForUser } from "@/lib/queries";
/** «часть · часть — FORGE». Parts are already translated (or are data: keys, names). */
export function pageTitle(t: Translator, ...parts: Array<string | null | undefined | false>): Metadata {
const head = parts.filter((x): x is string => !!x && !!x.trim()).join(" · ");
return { title: head ? `${head} — ${productName(t.locale)}` : productName(t.locale) };
}
/** A project page: «KEY · вкладка — FORGE»; a stranger gets no project name, only the key. */
export async function projectTitle(t: Translator, key: string, ...rest: Array<string | null | undefined | false>): Promise<Metadata> {
const user = await currentUser();
const p = user ? await projectForUserByKey(user.id, decodeURIComponent(key)).catch(() => null) : null;
return pageTitle(t, p?.key ?? null, ...rest);
}
/** A story page: «KEY-N · название — FORGE», only for whoever may see the story. */
export async function taskTitle(t: Translator, key: string): Promise<Metadata> {
const user = await currentUser();
const ref = user ? await taskForUser(user.id, decodeURIComponent(key)).catch(() => null) : null;
return ref ? pageTitle(t, formatTaskKey(ref.project.key, ref.task.number), ref.task.title) : pageTitle(t);
}