Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerChipPresentation.ts

75 lines2.8 KB

1import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
2import { imageMimeType } from "@t3tools/shared/image";
3import { videoMimeType } from "@t3tools/shared/video";
4/**
5 * These are the sRGB form of the same OKLCH set web uses, so a chip looks the same on every
6 * surface. See `composerInlineChip.ts`.
7 */
8const CONTEXT_CHIP_PRESENTATIONS = {
9 image: { accent: "#d55665", symbol: "photo" },
10 video: { accent: "#d06217", symbol: "play.rectangle" },
11 file: { accent: "#0090cd", symbol: "doc" },
12 mention: { accent: "#0096af", symbol: "doc" },
13 terminal: { accent: "#009f6e", symbol: "terminal" },
14 element: { accent: "#b87501", symbol: "cursorarrow.click" },
15 "preview-annotation": { accent: "#b87501", symbol: "cursorarrow.click" },
16 "review-comment": { accent: "#8a70dd", symbol: "text.bubble" },
17 "pull-request": { accent: "#7079e4", symbol: "git-pull-request" },
18 skill: { accent: "#b261be", symbol: "cube" },
19} as const;
20​
21export function composerChipSizeSuffix(record?: {
22 readonly kind?: string;
23 readonly sizeBytes?: number;
24}): string {
25 if (record?.kind !== "file" && record?.kind !== "image") return "";
26 return typeof record.sizeBytes === "number" ? formatAttachmentSize(record.sizeBytes) : "";
28​
29const PULL_REQUEST_CHIP_PRESENTATIONS = {
30 open: { accent: "#009f6e", symbol: "git-pull-request" },
31 draft: { accent: "#7f8793", symbol: "git-pull-request" },
32 merged: { accent: "#8a70dd", symbol: "git-pull-request" },
33 closed: { accent: "#d55665", symbol: "git-pull-request" },
34} as const;
35​
36export function contextChipPresentation(
37 kind: string,
38 record?: {
39 readonly kind?: string;
40 readonly name?: string;
41 readonly mimeType?: string;
42 readonly sectionId?: string;
43 readonly pullRequest?: {
44 readonly state?: string;
45 readonly isDraft?: boolean;
46 };
47 },
48) {
49 const presentationKind =
50 kind === "file" &&
51 videoMimeType({
52 name: record?.name ?? "",
53 mimeType: record?.mimeType ?? "",
54 })
55 ? "video"
56 : kind === "file" &&
57 imageMimeType({ name: record?.name ?? "", mimeType: record?.mimeType ?? "" }) !== null
58 ? "image"
59 : kind === "review-comment" && record?.sectionId?.startsWith("pull-request:")
60 ? "pull-request"
61 : kind;
62 if (presentationKind === "pull-request") {
63 const pullRequest = record?.pullRequest;
64 const state =
65 pullRequest?.state === "open" && pullRequest.isDraft === true
66 ? "draft"
67 : (pullRequest?.state ?? "");
68 if (Object.hasOwn(PULL_REQUEST_CHIP_PRESENTATIONS, state)) {
69 return PULL_REQUEST_CHIP_PRESENTATIONS[state as keyof typeof PULL_REQUEST_CHIP_PRESENTATIONS];
70 }
71 }
72 return Object.hasOwn(CONTEXT_CHIP_PRESENTATIONS, presentationKind)
73 ? CONTEXT_CHIP_PRESENTATIONS[presentationKind as keyof typeof CONTEXT_CHIP_PRESENTATIONS]
74 : CONTEXT_CHIP_PRESENTATIONS.file;