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

composerChipPresentation.ts

119 lines4.1 KB

1import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
2import { imageMimeType } from "@t3tools/shared/image";
3import { videoMimeType } from "@t3tools/shared/video";
4​
5import type { CONTEXT_CHIP_ICON_SOURCES } from "./contextChipIcons.generated";
6import { resolveMarkdownFileIcon, type MarkdownFileIcon } from "./markdownLinks";
7​
8export type ContextChipIcon = keyof typeof CONTEXT_CHIP_ICON_SOURCES;
9​
10interface ContextChipPresentation {
11 readonly accent?: string;
12 readonly icon: ContextChipIcon | null;
14​
15/**
16 * These are the sRGB form of the same OKLCH set web uses, so a chip looks the same on every
17 * surface. See `ContextChip.tsx`.
18 */
19const CONTEXT_CHIP_PRESENTATIONS = {
20 image: { accent: "#d55665", icon: "image" },
21 video: { accent: "#d06217", icon: "film" },
22 file: { accent: "#0090cd", icon: null },
23 mention: { accent: "#0096af", icon: null },
24 terminal: { accent: "#009f6e", icon: "terminal" },
25 element: { accent: "#b87501", icon: "mouse-pointer-click" },
26 "preview-annotation": {
27 accent: "#b87501",
28 icon: "mouse-pointer-click",
29 },
30 "review-comment": { accent: "#8a70dd", icon: "message-circle" },
31 "pull-request": {
32 accent: "#7079e4",
33 icon: "git-pull-request-arrow",
34 },
35 skill: { accent: "#b261be", icon: "skill" },
36 thread: {
37 accent: "#009c96",
38 icon: "messages-square",
39 },
40} as const satisfies Record<string, ContextChipPresentation>;
41​
42export const UNAVAILABLE_CONTEXT_CHIP_PRESENTATION: ContextChipPresentation = {
43 icon: "circle-dashed",
44};
45​
46export function composerChipSizeSuffix(record?: {
47 readonly kind?: string;
48 readonly sizeBytes?: number;
49}): string {
50 if (record?.kind !== "file" && record?.kind !== "image") return "";
51 return typeof record.sizeBytes === "number" ? formatAttachmentSize(record.sizeBytes) : "";
53​
54const PULL_REQUEST_CHIP_PRESENTATIONS = {
55 open: { accent: "#009f6e", icon: "git-pull-request-arrow" },
56 draft: { accent: "#7f8793", icon: "git-pull-request-draft" },
57 merged: { accent: "#8a70dd", icon: "git-merge" },
58 closed: { accent: "#d55665", icon: "git-pull-request-closed" },
59} as const satisfies Record<string, ContextChipPresentation>;
60​
61export function contextChipPresentation(
62 kind: string,
63 record?: {
64 readonly kind?: string;
65 readonly name?: string;
66 readonly mimeType?: string;
67 readonly sectionId?: string;
68 readonly pullRequest?: {
69 readonly state?: string;
70 readonly isDraft?: boolean;
71 };
72 },
73): ContextChipPresentation {
74 const presentationKind =
75 kind === "file" &&
76 videoMimeType({
77 name: record?.name ?? "",
78 mimeType: record?.mimeType ?? "",
79 })
80 ? "video"
81 : kind === "file" &&
82 imageMimeType({ name: record?.name ?? "", mimeType: record?.mimeType ?? "" }) !== null
83 ? "image"
84 : kind === "review-comment" && record?.sectionId?.startsWith("pull-request:")
85 ? "pull-request"
86 : kind;
87 if (presentationKind === "pull-request") {
88 const pullRequest = record?.pullRequest;
89 const state =
90 pullRequest?.state === "open" && pullRequest.isDraft === true
91 ? "draft"
92 : (pullRequest?.state ?? "");
93 if (Object.hasOwn(PULL_REQUEST_CHIP_PRESENTATIONS, state)) {
94 return PULL_REQUEST_CHIP_PRESENTATIONS[state as keyof typeof PULL_REQUEST_CHIP_PRESENTATIONS];
95 }
96 }
97 return Object.hasOwn(CONTEXT_CHIP_PRESENTATIONS, presentationKind)
98 ? CONTEXT_CHIP_PRESENTATIONS[presentationKind as keyof typeof CONTEXT_CHIP_PRESENTATIONS]
99 : UNAVAILABLE_CONTEXT_CHIP_PRESENTATION;
101​
102export type ContextChipIconRef =
103 | { readonly kind: "chip"; readonly name: ContextChipIcon }
104 | { readonly kind: "file"; readonly name: MarkdownFileIcon };
105​
106export function contextChipIcon(
107 presentation: Pick<ContextChipPresentation, "icon">,
108 fileName: string,
109): ContextChipIconRef {
110 return presentation.icon
111 ? { kind: "chip", name: presentation.icon }
112 : { kind: "file", name: resolveMarkdownFileIcon(fileName) };
114​
115export function contextRecordFileName(record?: object): string {
116 if (record && "path" in record && typeof record.path === "string") return record.path;
117 if (record && "name" in record && typeof record.name === "string") return record.name;
118 return "";