The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"; |
| 2 | import { imageMimeType } from "@t3tools/shared/image"; |
| 3 | import { videoMimeType } from "@t3tools/shared/video"; |
| 4 | |
| 5 | import type { CONTEXT_CHIP_ICON_SOURCES } from "./contextChipIcons.generated"; |
| 6 | import { resolveMarkdownFileIcon, type MarkdownFileIcon } from "./markdownLinks"; |
| 7 | import { |
| 8 | flattenThemeColor, |
| 9 | isDarkThemeColor, |
| 10 | themeAccentWithContrast, |
| 11 | type MobileThemeVariables, |
| 12 | } from "./mobileTheme"; |
| 13 | |
| 14 | export type ContextChipIcon = keyof typeof CONTEXT_CHIP_ICON_SOURCES; |
| 15 | |
| 16 | interface 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 | */ |
| 25 | const 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 | citation: { accent: "#8a70dd", icon: "message-circle" }, |
| 38 | "pull-request": { |
| 39 | accent: "#7079e4", |
| 40 | icon: "git-pull-request-arrow", |
| 41 | }, |
| 42 | skill: { accent: "#b261be", icon: "skill" }, |
| 43 | thread: { |
| 44 | accent: "#009c96", |
| 45 | icon: "messages-square", |
| 46 | }, |
| 47 | // A machine: the record carries only its identity, so every machine wears one glyph. |
| 48 | environment: { accent: "#3f9a4f", icon: "server" }, |
| 49 | // An earlier response the message replies to. Mobile shows it as a plain labelled chip. |
| 50 | response: { accent: "#7f8793", icon: null }, |
| 51 | } as const satisfies Record<string, ContextChipPresentation>; |
| 52 | |
| 53 | const CHIP_CONTRAST = 5; |
| 54 | |
| 55 | const readableChipAccents = new Map<string, string>(); |
| 56 | |
| 57 | /** |
| 58 | * A chip keeps one color per theme so it looks the same in a message, a reply and the composer, |
| 59 | * which means it must read on all of them; the user bubble is usually the hardest. A surface |
| 60 | * whose text is the opposite shade, like Material You's primary-colored light bubble, can't share |
| 61 | * a color with the canvas, so chips are matched only against surfaces like the one they sit on. |
| 62 | */ |
| 63 | export function contextChipAccent( |
| 64 | accent: string | undefined, |
| 65 | theme: MobileThemeVariables, |
| 66 | textColor: string, |
| 67 | ): string | undefined { |
| 68 | if (!accent) return undefined; |
| 69 | const screen = theme["--color-screen"]; |
| 70 | const darkText = isDarkThemeColor(textColor); |
| 71 | const surfaces = ( |
| 72 | [ |
| 73 | ["--color-user-bubble", "--color-user-bubble-foreground"], |
| 74 | ["--color-thread-canvas", "--color-md-body"], |
| 75 | ["--color-composer-surface", "--color-foreground"], |
| 76 | ] as const |
| 77 | ) |
| 78 | .filter(([, text]) => isDarkThemeColor(theme[text]) === darkText) |
| 79 | .map(([surface]) => flattenThemeColor(theme[surface], screen)); |
| 80 | const key = [accent, ...surfaces].join(); |
| 81 | let readable = readableChipAccents.get(key); |
| 82 | if (readable === undefined) { |
| 83 | readable = themeAccentWithContrast(accent, surfaces, CHIP_CONTRAST); |
| 84 | readableChipAccents.set(key, readable); |
| 85 | } |
| 86 | return readable; |
| 87 | } |
| 88 | |
| 89 | export const UNAVAILABLE_CONTEXT_CHIP_PRESENTATION: ContextChipPresentation = { |
| 90 | icon: "circle-dashed", |
| 91 | }; |
| 92 | |
| 93 | export function composerChipSizeSuffix(record?: { |
| 94 | readonly kind?: string; |
| 95 | readonly sizeBytes?: number; |
| 96 | }): string { |
| 97 | if (record?.kind !== "file" && record?.kind !== "image") return ""; |
| 98 | return typeof record.sizeBytes === "number" ? formatAttachmentSize(record.sizeBytes) : ""; |
| 99 | } |
| 100 | |
| 101 | const PULL_REQUEST_CHIP_PRESENTATIONS = { |
| 102 | open: { accent: "#009f6e", icon: "git-pull-request-arrow" }, |
| 103 | draft: { accent: "#7f8793", icon: "git-pull-request-draft" }, |
| 104 | merged: { accent: "#8a70dd", icon: "git-merge" }, |
| 105 | closed: { accent: "#d55665", icon: "git-pull-request-closed" }, |
| 106 | } as const satisfies Record<string, ContextChipPresentation>; |
| 107 | |
| 108 | export function contextChipPresentation( |
| 109 | kind: string, |
| 110 | record?: { |
| 111 | readonly kind?: string; |
| 112 | readonly name?: string; |
| 113 | readonly mimeType?: string; |
| 114 | readonly sectionId?: string; |
| 115 | readonly pullRequest?: { |
| 116 | readonly state?: string; |
| 117 | readonly isDraft?: boolean; |
| 118 | }; |
| 119 | }, |
| 120 | ): ContextChipPresentation { |
| 121 | const presentationKind = |
| 122 | kind === "file" && |
| 123 | videoMimeType({ |
| 124 | name: record?.name ?? "", |
| 125 | mimeType: record?.mimeType ?? "", |
| 126 | }) |
| 127 | ? "video" |
| 128 | : kind === "file" && |
| 129 | imageMimeType({ name: record?.name ?? "", mimeType: record?.mimeType ?? "" }) !== null |
| 130 | ? "image" |
| 131 | : kind === "review-comment" && record?.sectionId?.startsWith("pull-request:") |
| 132 | ? "pull-request" |
| 133 | : kind; |
| 134 | if (presentationKind === "pull-request") { |
| 135 | const pullRequest = record?.pullRequest; |
| 136 | const state = |
| 137 | pullRequest?.state === "open" && pullRequest.isDraft === true |
| 138 | ? "draft" |
| 139 | : (pullRequest?.state ?? ""); |
| 140 | if (Object.hasOwn(PULL_REQUEST_CHIP_PRESENTATIONS, state)) { |
| 141 | return PULL_REQUEST_CHIP_PRESENTATIONS[state as keyof typeof PULL_REQUEST_CHIP_PRESENTATIONS]; |
| 142 | } |
| 143 | } |
| 144 | return Object.hasOwn(CONTEXT_CHIP_PRESENTATIONS, presentationKind) |
| 145 | ? CONTEXT_CHIP_PRESENTATIONS[presentationKind as keyof typeof CONTEXT_CHIP_PRESENTATIONS] |
| 146 | : UNAVAILABLE_CONTEXT_CHIP_PRESENTATION; |
| 147 | } |
| 148 | |
| 149 | export type ContextChipIconRef = |
| 150 | | { readonly kind: "chip"; readonly name: ContextChipIcon } |
| 151 | | { readonly kind: "file"; readonly name: MarkdownFileIcon }; |
| 152 | |
| 153 | export function contextChipIcon( |
| 154 | presentation: Pick<ContextChipPresentation, "icon">, |
| 155 | fileName: string, |
| 156 | ): ContextChipIconRef { |
| 157 | return presentation.icon |
| 158 | ? { kind: "chip", name: presentation.icon } |
| 159 | : { kind: "file", name: resolveMarkdownFileIcon(fileName) }; |
| 160 | } |
| 161 | |
| 162 | export function contextRecordFileName(record?: object): string { |
| 163 | if (record && "path" in record && typeof record.path === "string") return record.path; |
| 164 | if (record && "name" in record && typeof record.name === "string") return record.name; |
| 165 | return ""; |
| 166 | } |