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