Open History to see the last commit
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";
7import {
8 flattenThemeColor,
9 isDarkThemeColor,
10 themeAccentWithContrast,
11 type MobileThemeVariables,
12} from "./mobileTheme";
13
14export type ContextChipIcon = keyof typeof CONTEXT_CHIP_ICON_SOURCES;
15
16interface ContextChipPresentation {
17 readonly accent?: string;
18 readonly icon: ContextChipIcon | null;
19}
20
21/**
22 * These are the sRGB form of the same OKLCH set web uses, so a chip looks the same on every
23 * surface. See `ContextChip.tsx`.
24 */
25const CONTEXT_CHIP_PRESENTATIONS = {
26 image: { accent: "#0090cd", icon: "image" },
27 video: { accent: "#d06217", icon: "film" },
28 file: { accent: "#0090cd", icon: null },
29 mention: { accent: "#0096af", icon: null },
30 terminal: { accent: "#009f6e", icon: "terminal" },
31 element: { accent: "#b87501", icon: "mouse-pointer-click" },
32 "preview-annotation": {
33 accent: "#b87501",
34 icon: "mouse-pointer-click",
35 },
36 "review-comment": { accent: "#8a70dd", icon: "message-circle" },
37 "pull-request": {
38 accent: "#7079e4",
39 icon: "git-pull-request-arrow",
40 },
41 skill: { accent: "#b261be", icon: "skill" },
42 thread: {
43 accent: "#009c96",
44 icon: "messages-square",
45 },
46 // A machine: the record carries only its identity, so every machine wears one glyph.
47 environment: { accent: "#3f9a4f", icon: "server" },
48} as const satisfies Record<string, ContextChipPresentation>;
49
50const CHIP_CONTRAST = 5;
51
52const readableChipAccents = new Map<string, string>();
53
54/**
55 * A chip keeps one color per theme so it looks the same in a message, a reply and the composer,
56 * which means it must read on all of them; the user bubble is usually the hardest. A surface
57 * whose text is the opposite shade, like Material You's primary-colored light bubble, can't share
58 * a color with the canvas, so chips are matched only against surfaces like the one they sit on.
59 */
60export function contextChipAccent(
61 accent: string | undefined,
62 theme: MobileThemeVariables,
63 textColor: string,
64): string | undefined {
65 if (!accent) return undefined;
66 const screen = theme["--color-screen"];
67 const darkText = isDarkThemeColor(textColor);
68 const surfaces = (
69 [
70 ["--color-user-bubble", "--color-user-bubble-foreground"],
71 ["--color-thread-canvas", "--color-md-body"],
72 ["--color-composer-surface", "--color-foreground"],
73 ] as const
74 )
75 .filter(([, text]) => isDarkThemeColor(theme[text]) === darkText)
76 .map(([surface]) => flattenThemeColor(theme[surface], screen));
77 const key = [accent, ...surfaces].join();
78 let readable = readableChipAccents.get(key);
79 if (readable === undefined) {
80 readable = themeAccentWithContrast(accent, surfaces, CHIP_CONTRAST);
81 readableChipAccents.set(key, readable);
82 }
83 return readable;
84}
85
86export const UNAVAILABLE_CONTEXT_CHIP_PRESENTATION: ContextChipPresentation = {
87 icon: "circle-dashed",
88};
89
90export function composerChipSizeSuffix(record?: {
91 readonly kind?: string;
92 readonly sizeBytes?: number;
93}): string {
94 if (record?.kind !== "file" && record?.kind !== "image") return "";
95 return typeof record.sizeBytes === "number" ? formatAttachmentSize(record.sizeBytes) : "";
96}
97
98const PULL_REQUEST_CHIP_PRESENTATIONS = {
99 open: { accent: "#009f6e", icon: "git-pull-request-arrow" },
100 draft: { accent: "#7f8793", icon: "git-pull-request-draft" },
101 merged: { accent: "#8a70dd", icon: "git-merge" },
102 closed: { accent: "#d55665", icon: "git-pull-request-closed" },
103} as const satisfies Record<string, ContextChipPresentation>;
104
105export function contextChipPresentation(
106 kind: string,
107 record?: {
108 readonly kind?: string;
109 readonly name?: string;
110 readonly mimeType?: string;
111 readonly sectionId?: string;
112 readonly pullRequest?: {
113 readonly state?: string;
114 readonly isDraft?: boolean;
115 };
116 },
117): ContextChipPresentation {
118 const presentationKind =
119 kind === "file" &&
120 videoMimeType({
121 name: record?.name ?? "",
122 mimeType: record?.mimeType ?? "",
123 })
124 ? "video"
125 : kind === "file" &&
126 imageMimeType({ name: record?.name ?? "", mimeType: record?.mimeType ?? "" }) !== null
127 ? "image"
128 : kind === "review-comment" && record?.sectionId?.startsWith("pull-request:")
129 ? "pull-request"
130 : kind;
131 if (presentationKind === "pull-request") {
132 const pullRequest = record?.pullRequest;
133 const state =
134 pullRequest?.state === "open" && pullRequest.isDraft === true
135 ? "draft"
136 : (pullRequest?.state ?? "");
137 if (Object.hasOwn(PULL_REQUEST_CHIP_PRESENTATIONS, state)) {
138 return PULL_REQUEST_CHIP_PRESENTATIONS[state as keyof typeof PULL_REQUEST_CHIP_PRESENTATIONS];
139 }
140 }
141 return Object.hasOwn(CONTEXT_CHIP_PRESENTATIONS, presentationKind)
142 ? CONTEXT_CHIP_PRESENTATIONS[presentationKind as keyof typeof CONTEXT_CHIP_PRESENTATIONS]
143 : UNAVAILABLE_CONTEXT_CHIP_PRESENTATION;
144}
145
146export type ContextChipIconRef =
147 | { readonly kind: "chip"; readonly name: ContextChipIcon }
148 | { readonly kind: "file"; readonly name: MarkdownFileIcon };
149
150export function contextChipIcon(
151 presentation: Pick<ContextChipPresentation, "icon">,
152 fileName: string,
153): ContextChipIconRef {
154 return presentation.icon
155 ? { kind: "chip", name: presentation.icon }
156 : { kind: "file", name: resolveMarkdownFileIcon(fileName) };
157}
158
159export function contextRecordFileName(record?: object): string {
160 if (record && "path" in record && typeof record.path === "string") return record.path;
161 if (record && "name" in record && typeof record.name === "string") return record.name;
162 return "";
163}