peppa/packages/core/src/palette.ts
2026-11-15 13:56:25 +05:00

68 lines
3.6 KiB
TypeScript

// Command palette ⌘K (PEP-52): what the signed-in user can jump to — only their own and shared
// projects (userProjectIds) — and the client-side ranking of a query against it.
import { projects, questions, sessions, tasks, type Db } from "@peppa/db";
import { formatTaskKey } from "@peppa/shared";
import { and, desc, inArray, isNull } from "drizzle-orm";
import { userProjectIds } from "./users.ts";
import type { PaletteItem } from "@peppa/shared/palette";
export { matchPalette, type PaletteItem } from "@peppa/shared/palette";
const PAGES: PaletteItem[] = [
{ kind: "page", key: "g h", title: "Главная", href: "/" },
{ kind: "page", key: "g p", title: "Проекты", href: "/projects" },
{ kind: "page", key: "g r", title: "Раннеры", href: "/runners" },
{ kind: "page", key: "g s", title: "Настройки", href: "/settings" },
{ kind: "page", key: "", title: "Новый проект", href: "/projects/new" },
{ kind: "page", key: "?", title: "Горячие клавиши", href: "/help/shortcuts" },
{ kind: "page", key: "", title: "Помощь", href: "/help" },
];
const STATUS_RU: Record<string, string> = {
backlog: "бэклог",
proposing: "план",
awaiting_approval: "ждёт PM",
in_progress: "в работе",
review: "ревью",
done: "готово",
blocked: "блок",
};
export async function paletteItems(db: Db, userId: number): Promise<PaletteItem[]> {
const ids = await userProjectIds(db, userId);
if (!ids.length) return PAGES;
const ps = await db.select().from(projects).where(inArray(projects.id, ids));
const byId = new Map(ps.map((p) => [p.id, p]));
const ts = await db
.select({ id: tasks.id, projectId: tasks.projectId, number: tasks.number, title: tasks.title, status: tasks.status })
.from(tasks)
.where(and(inArray(tasks.projectId, ids), isNull(tasks.archivedAt)))
.orderBy(desc(tasks.updatedAt))
.limit(500);
const ss = await db
.select({ id: sessions.id, projectId: sessions.projectId, taskId: sessions.taskId, status: sessions.status })
.from(sessions)
.where(inArray(sessions.projectId, ids))
.orderBy(desc(sessions.id))
.limit(50);
const qs = await db
.select({ id: questions.id, projectId: questions.projectId, taskId: questions.taskId, text: questions.text, kind: questions.kind })
.from(questions)
.where(and(inArray(questions.projectId, ids), isNull(questions.answeredAt)))
.orderBy(desc(questions.id))
.limit(50);
const taskKey = new Map(ts.map((t) => [t.id, formatTaskKey(byId.get(t.projectId)!.key, t.number)]));
const taskTitle = new Map(ts.map((t) => [t.id, t.title]));
return [
...qs.map((q): PaletteItem => {
const k = q.taskId ? taskKey.get(q.taskId) : undefined;
return { kind: "question", key: `#${q.id}`, title: q.text.split("\n")[0]!.slice(0, 140), hint: `ответить · ${k ?? byId.get(q.projectId)!.key}`, href: k ? `/t/${k}#q-${q.id}` : `/#q-${q.id}` };
}),
...ps.map((p): PaletteItem => ({ kind: "project", key: p.key, title: p.name, hint: "проект", href: `/p/${p.key}` })),
...ts.map((t): PaletteItem => ({ kind: "story", key: taskKey.get(t.id)!, title: t.title, hint: `${byId.get(t.projectId)!.name} · ${STATUS_RU[t.status] ?? t.status}`, href: `/t/${taskKey.get(t.id)}`, canStart: t.status === "backlog" })),
...ss.map((s): PaletteItem => ({ kind: "runner", key: `#${s.id}`, title: s.taskId ? `${taskKey.get(s.taskId) ?? ""} ${taskTitle.get(s.taskId) ?? ""}`.trim() : byId.get(s.projectId)!.name, hint: `раннер · ${s.status}`, href: `/runners?id=${s.id}` })),
...PAGES,
];
}