71 lines
3.9 KiB
TypeScript
71 lines
3.9 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 { m as tr } from "@peppa/shared/i18n";
|
|
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: tr("Главная"), href: "/" },
|
|
{ kind: "page", key: "g p", title: tr("Проекты"), href: "/projects" },
|
|
// glossary-ignore: страница агентов и поиск по ним есть только в виде «Инженер»
|
|
{ kind: "page", key: "g r", title: tr("Раннеры"), href: "/runners" },
|
|
{ kind: "page", key: "g s", title: tr("Настройки"), href: "/settings" },
|
|
{ kind: "page", key: "", title: tr("Новый проект"), href: "/projects/new" },
|
|
{ kind: "page", key: "?", title: tr("Горячие клавиши"), href: "/help/shortcuts" },
|
|
{ kind: "page", key: "", title: tr("Помощь"), href: "/help" },
|
|
];
|
|
|
|
const STATUS_RU: Record<string, string> = {
|
|
backlog: tr("бэклог"),
|
|
proposing: tr("план"),
|
|
awaiting_approval: tr("ждёт PM"),
|
|
in_progress: tr("в работе"),
|
|
review: tr("ревью"),
|
|
done: tr("готово"),
|
|
blocked: tr("блок"),
|
|
};
|
|
|
|
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: `${tr("ответить")} · ${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: tr("проект"), 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" })),
|
|
// glossary-ignore: страница агентов и поиск по ним есть только в виде «Инженер»
|
|
...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: `${tr("раннер")} · ${s.status}`, href: `/runners?id=${s.id}` })),
|
|
...PAGES,
|
|
];
|
|
}
|
|
|