The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | CheckIcon, |
| 3 | CopyIcon, |
| 4 | MoonIcon, |
| 5 | PaintbrushIcon, |
| 6 | PenLineIcon, |
| 7 | PlusIcon, |
| 8 | SunIcon, |
| 9 | Trash2Icon, |
| 10 | UploadIcon, |
| 11 | } from "lucide-react"; |
| 12 | import { useState, type ReactElement } from "react"; |
| 13 | import { useEnvironmentThemeDefinitions } from "../../hooks/useEnvironmentTheme"; |
| 14 | import { readThemeHalvesRaw } from "../../hooks/useTheme"; |
| 15 | import { cn } from "../../lib/utils"; |
| 16 | import { |
| 17 | getThemeDefinition, |
| 18 | singleAppearanceOf, |
| 19 | getThemeModes, |
| 20 | removeCustomThemes, |
| 21 | serializeThemeFile, |
| 22 | type ThemeAppearance, |
| 23 | type ThemeDefinition, |
| 24 | type ThemeHalves, |
| 25 | T3_CHAT_THEME, |
| 26 | EMBER_THEME, |
| 27 | GROVE_THEME, |
| 28 | IRIS_THEME, |
| 29 | OCEAN_THEME, |
| 30 | } from "../../themePalette"; |
| 31 | import { |
| 32 | AlertDialog, |
| 33 | AlertDialogClose, |
| 34 | AlertDialogDescription, |
| 35 | AlertDialogFooter, |
| 36 | AlertDialogHeader, |
| 37 | AlertDialogPopup, |
| 38 | AlertDialogTitle, |
| 39 | } from "../ui/alert-dialog"; |
| 40 | import { Button } from "../ui/button"; |
| 41 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 42 | import { Tooltip, TooltipPopup, TooltipProvider, TooltipTrigger } from "../ui/tooltip"; |
| 43 | import { ThemeImportDialog } from "./ThemeImportDialog"; |
| 44 | import { useSearchableSetting } from "./settingsSearch"; |
| 45 | import { useThemeEditorStore } from "./themeEditorStore"; |
| 46 | import { |
| 47 | STANDARD_THEME_CARDS, |
| 48 | getThemeCardDefinition, |
| 49 | previewColorsOf, |
| 50 | ThemePreviewCircles, |
| 51 | ThemePreviewCircle, |
| 52 | type ThemeCardDefinition, |
| 53 | type ThemeMode, |
| 54 | } from "./ThemePreviewCircles"; |
| 55 | import { ThemeWireframe } from "./ThemeWireframe"; |
| 56 | import { T, useGT } from "gt-react"; |
| 57 | |
| 58 | const MAINTAINER_THEMES: ReadonlyArray<ThemeDefinition> = [ |
| 59 | T3_CHAT_THEME, |
| 60 | GROVE_THEME, |
| 61 | OCEAN_THEME, |
| 62 | EMBER_THEME, |
| 63 | IRIS_THEME, |
| 64 | ]; |
| 65 | |
| 66 | function collectionVariantLabels(themes: ReadonlyArray<ThemeDefinition>): ReadonlyArray<string> { |
| 67 | if (themes.length === 0) return []; |
| 68 | const words = themes.map((theme) => theme.label.trim().split(/\s+/)); |
| 69 | const firstWords = words[0]!; |
| 70 | const sharedWordCount = firstWords.findIndex((word, index) => |
| 71 | words.some((labelWords) => labelWords[index]?.toLocaleLowerCase() !== word.toLocaleLowerCase()), |
| 72 | ); |
| 73 | const prefixLength = sharedWordCount === -1 ? firstWords.length - 1 : sharedWordCount; |
| 74 | |
| 75 | return themes.map((theme, index) => { |
| 76 | const shortLabel = words[index]?.slice(Math.max(0, prefixLength)).join(" ").trim(); |
| 77 | return shortLabel || theme.label; |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | function downloadThemeFile(filename: string, contents: string): void { |
| 82 | const url = URL.createObjectURL(new Blob([contents], { type: "application/json" })); |
| 83 | const anchor = document.createElement("a"); |
| 84 | anchor.href = url; |
| 85 | anchor.download = filename; |
| 86 | anchor.click(); |
| 87 | // Delay revocation so browsers can open the download stream. |
| 88 | setTimeout(() => URL.revokeObjectURL(url), 30_000); |
| 89 | } |
| 90 | |
| 91 | function ThemeVariantTooltip({ label, children }: { label: string; children: ReactElement }) { |
| 92 | return ( |
| 93 | <Tooltip> |
| 94 | <TooltipTrigger render={children} /> |
| 95 | <TooltipPopup>{label}</TooltipPopup> |
| 96 | </Tooltip> |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | function ThemeLibraryCard({ |
| 101 | theme, |
| 102 | isActive, |
| 103 | onUse, |
| 104 | onUseMode, |
| 105 | activeModes, |
| 106 | onEdit, |
| 107 | onDuplicate, |
| 108 | onDownload, |
| 109 | onRemove, |
| 110 | variantNavigation, |
| 111 | }: { |
| 112 | theme: ThemeCardDefinition; |
| 113 | isActive: boolean; |
| 114 | onUse: () => void; |
| 115 | onUseMode: (mode: ThemeMode) => void; |
| 116 | activeModes: ReadonlyArray<ThemeMode>; |
| 117 | onEdit?: () => void; |
| 118 | onDuplicate?: () => void; |
| 119 | onDownload?: () => void; |
| 120 | onRemove?: () => void; |
| 121 | variantNavigation?: { |
| 122 | collectionLabel: string; |
| 123 | options: ReadonlyArray<{ |
| 124 | themeIndex: number; |
| 125 | label: string; |
| 126 | activeModes: ReadonlyArray<ThemeMode>; |
| 127 | preview: ThemeCardDefinition["previews"][number]; |
| 128 | }>; |
| 129 | onSelectAndUse: (themeIndex: number, mode: ThemeAppearance) => void; |
| 130 | }; |
| 131 | }) { |
| 132 | const gt = useGT(); |
| 133 | const cardModes = theme.previews.map((preview) => preview.mode); |
| 134 | const [radialModeOpen, setRadialModeOpen] = useState<ThemeAppearance | null>(null); |
| 135 | const radialModeGroups = (["light", "dark"] as const).map((mode) => { |
| 136 | const options = |
| 137 | variantNavigation?.options.flatMap((option) => { |
| 138 | const preview = option.preview; |
| 139 | return preview.mode === mode ? [{ option, preview }] : []; |
| 140 | }) ?? []; |
| 141 | return { |
| 142 | mode, |
| 143 | options, |
| 144 | selected: options.find(({ option }) => option.activeModes.includes(mode)) ?? options[0], |
| 145 | }; |
| 146 | }); |
| 147 | return ( |
| 148 | <Tooltip> |
| 149 | <TooltipTrigger |
| 150 | render={ |
| 151 | <div |
| 152 | className={cn( |
| 153 | "cursor-pointer overflow-hidden rounded-xl border border-border/70 bg-card/60 transition-colors hover:bg-accent/10", |
| 154 | isActive && "bg-accent/30", |
| 155 | )} |
| 156 | data-theme-library-card={theme.id} |
| 157 | onClick={onUse} |
| 158 | style={isActive ? { boxShadow: "inset 0 0 0 1px var(--ring)" } : undefined} |
| 159 | > |
| 160 | <div className="relative"> |
| 161 | {variantNavigation ? ( |
| 162 | <div |
| 163 | aria-label={gt("Light and dark theme variants")} |
| 164 | className="relative h-20" |
| 165 | role="group" |
| 166 | onBlurCapture={(event) => { |
| 167 | const nextTarget = event.relatedTarget; |
| 168 | if ( |
| 169 | !(nextTarget instanceof Node) || |
| 170 | !event.currentTarget.contains(nextTarget) |
| 171 | ) { |
| 172 | setRadialModeOpen(null); |
| 173 | } |
| 174 | }} |
| 175 | onKeyDown={(event) => { |
| 176 | if (event.key === "Escape") setRadialModeOpen(null); |
| 177 | }} |
| 178 | onMouseLeave={() => setRadialModeOpen(null)} |
| 179 | > |
| 180 | {radialModeGroups.map(({ mode, options, selected }) => { |
| 181 | if (!selected) return null; |
| 182 | const rootOffsetX = mode === "light" ? -52 : 52; |
| 183 | const isOpen = radialModeOpen === mode; |
| 184 | const isActive = selected.option.activeModes.includes(mode); |
| 185 | const modeLabel = mode === "light" ? gt("Light") : gt("Dark"); |
| 186 | return ( |
| 187 | <div className="contents" key={mode}> |
| 188 | <ThemeVariantTooltip label={`${modeLabel}: ${selected.option.label}`}> |
| 189 | <button |
| 190 | aria-label={ |
| 191 | options.length > 1 |
| 192 | ? mode === "light" |
| 193 | ? gt( |
| 194 | "Choose light variant, {length} options, currently {label}", |
| 195 | { |
| 196 | length: options.length, |
| 197 | label: selected.option.label, |
| 198 | }, |
| 199 | ) |
| 200 | : gt("Choose dark variant, {length} options, currently {label}", { |
| 201 | length: options.length, |
| 202 | label: selected.option.label, |
| 203 | }) |
| 204 | : mode === "light" |
| 205 | ? gt("Use light variant, currently {label}", { |
| 206 | label: selected.option.label, |
| 207 | }) |
| 208 | : gt("Use dark variant, currently {label}", { |
| 209 | label: selected.option.label, |
| 210 | }) |
| 211 | } |
| 212 | aria-pressed={isActive} |
| 213 | className="absolute left-1/2 top-2 z-20 flex size-14 items-center justify-center rounded-full outline-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-5 focus-visible:outline-foreground/60" |
| 214 | style={{ |
| 215 | transform: `translateX(calc(-50% + ${rootOffsetX}px))`, |
| 216 | }} |
| 217 | type="button" |
| 218 | onClick={(event) => { |
| 219 | event.stopPropagation(); |
| 220 | variantNavigation.onSelectAndUse(selected.option.themeIndex, mode); |
| 221 | }} |
| 222 | onFocus={() => setRadialModeOpen(mode)} |
| 223 | onMouseEnter={() => setRadialModeOpen(mode)} |
| 224 | > |
| 225 | <ThemePreviewCircle |
| 226 | colors={selected.preview.colors} |
| 227 | mode={selected.preview.mode} |
| 228 | /> |
| 229 | {isActive ? ( |
| 230 | <span |
| 231 | aria-hidden |
| 232 | className="pointer-events-none absolute inset-0 rounded-full ring-2 ring-inset ring-ring" |
| 233 | /> |
| 234 | ) : null} |
| 235 | {isActive ? ( |
| 236 | <span className="pointer-events-none absolute -bottom-0.5 -right-0.5 flex size-4 items-center justify-center rounded-full border border-border/70 bg-background text-foreground shadow-sm"> |
| 237 | {mode === "light" ? ( |
| 238 | <SunIcon className="size-2.5" /> |
| 239 | ) : ( |
| 240 | <MoonIcon className="size-2.5" /> |
| 241 | )} |
| 242 | </span> |
| 243 | ) : null} |
| 244 | </button> |
| 245 | </ThemeVariantTooltip> |
| 246 | <span |
| 247 | className="pointer-events-none absolute bottom-0 left-1/2 inline-flex max-w-24 -translate-x-1/2 items-center gap-1 text-2xs font-medium text-foreground" |
| 248 | style={{ marginLeft: rootOffsetX }} |
| 249 | > |
| 250 | <span className="truncate">{selected.option.label}</span> |
| 251 | {options.length > 1 ? ( |
| 252 | <span className="shrink-0 rounded-full bg-muted px-1 text-3xs text-muted-foreground"> |
| 253 | +{options.length - 1} |
| 254 | </span> |
| 255 | ) : null} |
| 256 | </span> |
| 257 | {options.length > 1 |
| 258 | ? options.map(({ option, preview }, optionIndex) => { |
| 259 | const progress = optionIndex / (options.length - 1) - 0.5; |
| 260 | const childOffsetX = rootOffsetX + progress * 68; |
| 261 | const childOffsetY = Math.abs(progress) * 10; |
| 262 | const optionIsActive = option.activeModes.includes(mode); |
| 263 | return ( |
| 264 | <ThemeVariantTooltip |
| 265 | key={option.label} |
| 266 | label={ |
| 267 | mode === "light" |
| 268 | ? gt("Use {label} for light mode", { label: option.label }) |
| 269 | : gt("Use {label} for dark mode", { label: option.label }) |
| 270 | } |
| 271 | > |
| 272 | <button |
| 273 | aria-label={ |
| 274 | mode === "light" |
| 275 | ? optionIsActive |
| 276 | ? gt("Use {label} for light mode, currently active", { |
| 277 | label: option.label, |
| 278 | }) |
| 279 | : gt("Use {label} for light mode", { |
| 280 | label: option.label, |
| 281 | }) |
| 282 | : optionIsActive |
| 283 | ? gt("Use {label} for dark mode, currently active", { |
| 284 | label: option.label, |
| 285 | }) |
| 286 | : gt("Use {label} for dark mode", { label: option.label }) |
| 287 | } |
| 288 | aria-pressed={optionIsActive} |
| 289 | className={cn( |
| 290 | "absolute left-1/2 top-1 z-30 flex size-7 items-center justify-center rounded-full bg-background shadow-sm outline-none transition-[opacity,transform] duration-200 ease-out motion-reduce:transition-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring", |
| 291 | optionIsActive |
| 292 | ? "ring-2 ring-inset ring-ring" |
| 293 | : "ring-1 ring-border/70", |
| 294 | )} |
| 295 | style={{ |
| 296 | opacity: isOpen ? 1 : 0, |
| 297 | pointerEvents: isOpen ? "auto" : "none", |
| 298 | transform: `translate(calc(-50% + ${isOpen ? childOffsetX : rootOffsetX}px), ${isOpen ? childOffsetY : 28}px) scale(${isOpen ? 1 : 0.55})`, |
| 299 | transitionDelay: isOpen ? `${optionIndex * 35}ms` : "0ms", |
| 300 | }} |
| 301 | type="button" |
| 302 | onClick={(event) => { |
| 303 | event.stopPropagation(); |
| 304 | variantNavigation.onSelectAndUse(option.themeIndex, mode); |
| 305 | }} |
| 306 | onFocus={() => setRadialModeOpen(mode)} |
| 307 | onMouseEnter={() => setRadialModeOpen(mode)} |
| 308 | > |
| 309 | <span className="pointer-events-none scale-[0.43]"> |
| 310 | <ThemePreviewCircle |
| 311 | colors={preview.colors} |
| 312 | mode={preview.mode} |
| 313 | /> |
| 314 | </span> |
| 315 | </button> |
| 316 | </ThemeVariantTooltip> |
| 317 | ); |
| 318 | }) |
| 319 | : null} |
| 320 | </div> |
| 321 | ); |
| 322 | })} |
| 323 | </div> |
| 324 | ) : ( |
| 325 | <ThemePreviewCircles |
| 326 | label={theme.label} |
| 327 | activeModes={activeModes} |
| 328 | onSelectMode={onUseMode} |
| 329 | previews={theme.previews} |
| 330 | /> |
| 331 | )} |
| 332 | </div> |
| 333 | <div className="flex items-center gap-2 px-3 pb-3 pt-2"> |
| 334 | <div className="min-w-0 flex-1"> |
| 335 | <div className="flex items-center gap-1.5"> |
| 336 | <button |
| 337 | aria-label={ |
| 338 | variantNavigation |
| 339 | ? isActive |
| 340 | ? gt("Use {collection}, {label} variant, currently active", { |
| 341 | collection: variantNavigation.collectionLabel, |
| 342 | label: theme.label, |
| 343 | }) |
| 344 | : gt("Use {collection}, {label} variant", { |
| 345 | collection: variantNavigation.collectionLabel, |
| 346 | label: theme.label, |
| 347 | }) |
| 348 | : isActive |
| 349 | ? gt("Use {label} theme, currently active", { label: theme.label }) |
| 350 | : gt("Use {label} theme", { label: theme.label }) |
| 351 | } |
| 352 | aria-pressed={isActive} |
| 353 | className="min-w-0 cursor-pointer truncate rounded-sm text-left text-sm font-medium text-foreground outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card" |
| 354 | type="button" |
| 355 | onClick={(event) => { |
| 356 | event.stopPropagation(); |
| 357 | onUse(); |
| 358 | }} |
| 359 | > |
| 360 | {variantNavigation?.collectionLabel ?? theme.label} |
| 361 | </button> |
| 362 | </div> |
| 363 | </div> |
| 364 | {onEdit || onDuplicate || onDownload || onRemove ? ( |
| 365 | <div className="flex shrink-0 items-center gap-1"> |
| 366 | {onDuplicate ? ( |
| 367 | <Tooltip> |
| 368 | <TooltipTrigger |
| 369 | render={ |
| 370 | <Button |
| 371 | aria-label={gt("Duplicate {label}", { label: theme.label })} |
| 372 | size="icon-xs" |
| 373 | variant="ghost" |
| 374 | onClick={(event) => { |
| 375 | event.stopPropagation(); |
| 376 | onDuplicate(); |
| 377 | }} |
| 378 | > |
| 379 | <CopyIcon /> |
| 380 | </Button> |
| 381 | } |
| 382 | /> |
| 383 | <TooltipPopup> |
| 384 | <T>Duplicate theme</T> |
| 385 | </TooltipPopup> |
| 386 | </Tooltip> |
| 387 | ) : null} |
| 388 | {onEdit ? ( |
| 389 | <Tooltip> |
| 390 | <TooltipTrigger |
| 391 | render={ |
| 392 | <Button |
| 393 | aria-label={gt("Edit {label}", { label: theme.label })} |
| 394 | size="icon-xs" |
| 395 | variant="ghost" |
| 396 | onClick={(event) => { |
| 397 | event.stopPropagation(); |
| 398 | onEdit(); |
| 399 | }} |
| 400 | > |
| 401 | <PenLineIcon /> |
| 402 | </Button> |
| 403 | } |
| 404 | /> |
| 405 | <TooltipPopup> |
| 406 | <T>Edit theme</T> |
| 407 | </TooltipPopup> |
| 408 | </Tooltip> |
| 409 | ) : null} |
| 410 | {onDownload ? ( |
| 411 | <Tooltip> |
| 412 | <TooltipTrigger |
| 413 | render={ |
| 414 | <Button |
| 415 | aria-label={gt("Export {label}", { label: theme.label })} |
| 416 | size="icon-xs" |
| 417 | variant="ghost" |
| 418 | onClick={(event) => { |
| 419 | event.stopPropagation(); |
| 420 | onDownload(); |
| 421 | }} |
| 422 | > |
| 423 | <UploadIcon /> |
| 424 | </Button> |
| 425 | } |
| 426 | /> |
| 427 | <TooltipPopup> |
| 428 | <T>Export theme file</T> |
| 429 | </TooltipPopup> |
| 430 | </Tooltip> |
| 431 | ) : null} |
| 432 | {onRemove ? ( |
| 433 | <Tooltip> |
| 434 | <TooltipTrigger |
| 435 | render={ |
| 436 | <Button |
| 437 | aria-label={ |
| 438 | variantNavigation |
| 439 | ? gt("Remove themes from {collectionLabel}", { |
| 440 | collectionLabel: variantNavigation.collectionLabel, |
| 441 | }) |
| 442 | : gt("Remove {label}", { label: theme.label }) |
| 443 | } |
| 444 | size="icon-xs" |
| 445 | variant="ghost-destructive" |
| 446 | onClick={(event) => { |
| 447 | event.stopPropagation(); |
| 448 | onRemove(); |
| 449 | }} |
| 450 | > |
| 451 | <Trash2Icon /> |
| 452 | </Button> |
| 453 | } |
| 454 | /> |
| 455 | <TooltipPopup> |
| 456 | {variantNavigation ? gt("Remove themes") : gt("Remove theme")} |
| 457 | </TooltipPopup> |
| 458 | </Tooltip> |
| 459 | ) : null} |
| 460 | </div> |
| 461 | ) : null} |
| 462 | </div> |
| 463 | </div> |
| 464 | } |
| 465 | /> |
| 466 | <TooltipPopup> |
| 467 | {variantNavigation |
| 468 | ? gt("Use the first variants for light and dark") |
| 469 | : cardModes.length > 1 |
| 470 | ? gt("Use for both light and dark") |
| 471 | : cardModes[0] === "light" |
| 472 | ? gt("Use for light mode only") |
| 473 | : gt("Use for dark mode only")} |
| 474 | </TooltipPopup> |
| 475 | </Tooltip> |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | function CustomThemeCollectionCard({ |
| 480 | themes, |
| 481 | activeModesFor, |
| 482 | onUse, |
| 483 | onUseMode, |
| 484 | onDuplicate, |
| 485 | onEdit, |
| 486 | onDownload, |
| 487 | onRemove, |
| 488 | }: { |
| 489 | themes: ReadonlyArray<ThemeDefinition>; |
| 490 | activeModesFor: (themeId: string) => ReadonlyArray<ThemeMode>; |
| 491 | onUse: (theme: ThemeDefinition) => void; |
| 492 | onUseMode: (theme: ThemeDefinition, mode: ThemeMode) => void; |
| 493 | onDuplicate: (theme: ThemeDefinition) => void; |
| 494 | onEdit: (theme: ThemeDefinition) => void; |
| 495 | onDownload: (theme: ThemeDefinition) => void; |
| 496 | onRemove: (theme: ThemeDefinition) => void; |
| 497 | }) { |
| 498 | const [variantIndex, setVariantIndex] = useState(() => { |
| 499 | const activeIndex = themes.findIndex((theme) => activeModesFor(theme.id).length > 0); |
| 500 | return activeIndex < 0 ? 0 : activeIndex; |
| 501 | }); |
| 502 | const safeIndex = Math.min(variantIndex, themes.length - 1); |
| 503 | const theme = themes[safeIndex]; |
| 504 | |
| 505 | if (variantIndex !== safeIndex) setVariantIndex(safeIndex); |
| 506 | |
| 507 | if (!theme) return null; |
| 508 | const collectionLabel = theme.collection?.label ?? theme.label; |
| 509 | const variantLabels = collectionVariantLabels(themes); |
| 510 | const defaultLightTheme = themes.find((candidate) => getThemeModes(candidate).includes("light")); |
| 511 | const defaultDarkTheme = themes.find((candidate) => getThemeModes(candidate).includes("dark")); |
| 512 | const selectCollectionDefaults = () => { |
| 513 | if (themes.length === 1) { |
| 514 | onUse(theme); |
| 515 | return; |
| 516 | } |
| 517 | if (defaultLightTheme) onUseMode(defaultLightTheme, "light"); |
| 518 | if (defaultDarkTheme) onUseMode(defaultDarkTheme, "dark"); |
| 519 | setVariantIndex(0); |
| 520 | }; |
| 521 | |
| 522 | return ( |
| 523 | <ThemeLibraryCard |
| 524 | activeModes={activeModesFor(theme.id)} |
| 525 | isActive={false} |
| 526 | onDownload={() => onDownload(theme)} |
| 527 | onDuplicate={() => onDuplicate(theme)} |
| 528 | onEdit={() => onEdit(theme)} |
| 529 | onRemove={() => onRemove(theme)} |
| 530 | onUse={selectCollectionDefaults} |
| 531 | onUseMode={(mode) => onUseMode(theme, mode)} |
| 532 | theme={getThemeCardDefinition(theme)} |
| 533 | {...(themes.length > 1 |
| 534 | ? { |
| 535 | variantNavigation: { |
| 536 | collectionLabel, |
| 537 | options: themes.flatMap((variant, themeIndex) => |
| 538 | getThemeCardDefinition(variant).previews.map((preview) => ({ |
| 539 | themeIndex, |
| 540 | label: variantLabels[themeIndex] ?? variant.label, |
| 541 | activeModes: activeModesFor(variant.id), |
| 542 | preview, |
| 543 | })), |
| 544 | ), |
| 545 | onSelectAndUse: (themeIndex, mode) => { |
| 546 | const selectedTheme = themes[themeIndex]; |
| 547 | if (!selectedTheme) return; |
| 548 | setVariantIndex(themeIndex); |
| 549 | onUseMode(selectedTheme, mode); |
| 550 | }, |
| 551 | }, |
| 552 | } |
| 553 | : {})} |
| 554 | /> |
| 555 | ); |
| 556 | } |
| 557 | |
| 558 | export function ThemeLibrary({ |
| 559 | theme, |
| 560 | setTheme, |
| 561 | appearanceMode, |
| 562 | setAppearanceMode, |
| 563 | customThemes, |
| 564 | initialAppearance, |
| 565 | refreshTheme, |
| 566 | isImportOpen, |
| 567 | onImportOpenChange, |
| 568 | themeHalves, |
| 569 | setThemeHalf, |
| 570 | }: { |
| 571 | theme: string; |
| 572 | setTheme: (theme: string) => boolean; |
| 573 | appearanceMode: ThemeMode; |
| 574 | setAppearanceMode: (mode: ThemeMode) => boolean; |
| 575 | customThemes: ReadonlyArray<ThemeDefinition>; |
| 576 | initialAppearance: ThemeAppearance; |
| 577 | refreshTheme: () => void; |
| 578 | isImportOpen: boolean; |
| 579 | onImportOpenChange: (open: boolean) => void; |
| 580 | themeHalves: ThemeHalves | null; |
| 581 | setThemeHalf: (appearance: ThemeAppearance, themeId: string | null) => boolean; |
| 582 | }) { |
| 583 | const setting = useSearchableSetting(); |
| 584 | const gt = useGT(); |
| 585 | const openThemeEditor = useThemeEditorStore((store) => store.openThemeEditor); |
| 586 | const environmentThemes = useEnvironmentThemeDefinitions(); |
| 587 | const [themeRemovalTarget, setThemeRemovalTarget] = useState<{ |
| 588 | theme: ThemeDefinition; |
| 589 | collectionThemes: ReadonlyArray<ThemeDefinition>; |
| 590 | } | null>(null); |
| 591 | const [isThemeRemovalOpen, setIsThemeRemovalOpen] = useState(false); |
| 592 | const [themeIdsToRemove, setThemeIdsToRemove] = useState<ReadonlyArray<string>>([]); |
| 593 | const themeIdsToRemoveSet = new Set(themeIdsToRemove); |
| 594 | const removeDialogTheme = themeRemovalTarget?.theme; |
| 595 | const removeDialogCollectionThemes = themeRemovalTarget?.collectionThemes ?? []; |
| 596 | const canRemoveCollection = removeDialogCollectionThemes.length > 1; |
| 597 | const removeDialogCollectionLabel = |
| 598 | removeDialogTheme?.collection?.label ?? removeDialogTheme?.label; |
| 599 | |
| 600 | const notifyThemeSaveFailure = () => { |
| 601 | toastManager.add( |
| 602 | stackedThreadToast({ |
| 603 | type: "error", |
| 604 | title: gt("Couldn’t save theme selection"), |
| 605 | description: gt("Try again."), |
| 606 | }), |
| 607 | ); |
| 608 | }; |
| 609 | |
| 610 | const notifyThemeRemovalFailure = () => { |
| 611 | toastManager.add( |
| 612 | stackedThreadToast({ |
| 613 | type: "error", |
| 614 | title: gt("Couldn’t remove theme"), |
| 615 | description: gt("Try again."), |
| 616 | }), |
| 617 | ); |
| 618 | }; |
| 619 | |
| 620 | const persistTheme = (nextTheme: string) => { |
| 621 | const didSave = setTheme(nextTheme); |
| 622 | if (!didSave) notifyThemeSaveFailure(); |
| 623 | return didSave; |
| 624 | }; |
| 625 | |
| 626 | const handleRemoveTheme = ( |
| 627 | customTheme: ThemeDefinition, |
| 628 | collectionThemes: ReadonlyArray<ThemeDefinition>, |
| 629 | ) => { |
| 630 | setThemeRemovalTarget({ theme: customTheme, collectionThemes }); |
| 631 | setThemeIdsToRemove(collectionThemes.length > 1 ? [] : [customTheme.id]); |
| 632 | setIsThemeRemovalOpen(true); |
| 633 | }; |
| 634 | |
| 635 | const handleConfirmRemoveTheme = () => { |
| 636 | if (!themeRemovalTarget) return; |
| 637 | const removedIds = new Set(themeIdsToRemove); |
| 638 | if (removedIds.size === 0) return; |
| 639 | const removesBase = removedIds.has(getThemeDefinition(theme)?.id ?? ""); |
| 640 | // Read raw halves before persistTheme clears them; themeHalves omits published themes not yet streamed |
| 641 | // in. |
| 642 | const storedHalves = readThemeHalvesRaw(); |
| 643 | if (removesBase && !persistTheme(appearanceMode === "system" ? "system" : appearanceMode)) { |
| 644 | return; |
| 645 | } |
| 646 | for (const appearance of ["light", "dark"] as const) { |
| 647 | const half = storedHalves[appearance]; |
| 648 | if (half === undefined) continue; |
| 649 | const next = half && removedIds.has(half) ? null : removesBase ? half : undefined; |
| 650 | if (next !== undefined && !setThemeHalf(appearance, next)) { |
| 651 | notifyThemeRemovalFailure(); |
| 652 | return; |
| 653 | } |
| 654 | } |
| 655 | try { |
| 656 | removeCustomThemes([...removedIds]); |
| 657 | } catch { |
| 658 | notifyThemeRemovalFailure(); |
| 659 | return; |
| 660 | } |
| 661 | setIsThemeRemovalOpen(false); |
| 662 | }; |
| 663 | |
| 664 | const baseCardId = getThemeDefinition(theme)?.id ?? null; |
| 665 | const lightOwner = themeHalves?.light ?? baseCardId; |
| 666 | const darkOwner = themeHalves?.dark ?? baseCardId; |
| 667 | |
| 668 | const assignHalf = (appearance: ThemeAppearance, cardId: string | null) => { |
| 669 | const otherAppearance = appearance === "light" ? "dark" : "light"; |
| 670 | // To restore one default appearance, move the themed base to the opposite explicit half. |
| 671 | if (cardId === null && baseCardId !== null) { |
| 672 | // Read raw before persistTheme clears the mix; unstreamed published halves must not fall back to the |
| 673 | // base. |
| 674 | const otherOwner = readThemeHalvesRaw()[otherAppearance] ?? baseCardId; |
| 675 | if (!persistTheme(appearanceMode === "system" ? "system" : appearanceMode)) return; |
| 676 | if (!setThemeHalf(otherAppearance, otherOwner)) { |
| 677 | setTheme(theme); |
| 678 | notifyThemeSaveFailure(); |
| 679 | } |
| 680 | return; |
| 681 | } |
| 682 | if (!setThemeHalf(appearance, cardId)) { |
| 683 | notifyThemeSaveFailure(); |
| 684 | } |
| 685 | }; |
| 686 | |
| 687 | const activeThemeForAppearance = |
| 688 | getThemeDefinition((initialAppearance === "light" ? lightOwner : darkOwner) ?? "") ?? null; |
| 689 | |
| 690 | const cardDefById = (id: string | null): ThemeCardDefinition => { |
| 691 | if (id === null) return STANDARD_THEME_CARDS[0]!; |
| 692 | const definition = getThemeDefinition(id); |
| 693 | return definition ? getThemeCardDefinition(definition) : STANDARD_THEME_CARDS[0]!; |
| 694 | }; |
| 695 | |
| 696 | const pickColors = (id: string | null, appearance: ThemeAppearance) => { |
| 697 | const card = cardDefById(id); |
| 698 | return previewColorsOf(card, appearance) ?? card.previews[0]!.colors; |
| 699 | }; |
| 700 | |
| 701 | const setMode = (mode: ThemeMode) => { |
| 702 | if (!setAppearanceMode(mode)) notifyThemeSaveFailure(); |
| 703 | }; |
| 704 | |
| 705 | const handlePairPick = (cardId: string | null) => (mode: ThemeMode) => { |
| 706 | if (mode === "system") return; |
| 707 | assignHalf(mode, cardId); |
| 708 | }; |
| 709 | |
| 710 | const pickedModesFor = (cardId: string | null): ThemeMode[] => { |
| 711 | const rings: ThemeMode[] = []; |
| 712 | if (lightOwner === cardId) rings.push("light"); |
| 713 | if (darkOwner === cardId) rings.push("dark"); |
| 714 | return rings; |
| 715 | }; |
| 716 | |
| 717 | const wireframeColors = (appearance: ThemeAppearance) => |
| 718 | pickColors(appearance === "light" ? lightOwner : darkOwner, appearance); |
| 719 | |
| 720 | const renderWireframe = (mode: ThemeMode) => ( |
| 721 | <ThemeWireframe |
| 722 | className="h-[8.75rem]" |
| 723 | panes={ |
| 724 | mode === "system" |
| 725 | ? [ |
| 726 | { clip: "left", colors: wireframeColors("light") }, |
| 727 | { clip: "right", colors: wireframeColors("dark") }, |
| 728 | ] |
| 729 | : [{ colors: wireframeColors(mode === "dark" ? "dark" : "light") }] |
| 730 | } |
| 731 | /> |
| 732 | ); |
| 733 | |
| 734 | const renderModeTiles = () => ( |
| 735 | <div aria-label={gt("Appearance mode")} className="grid w-full grid-cols-3 gap-3" role="group"> |
| 736 | {(["system", "light", "dark"] as const).map((mode) => { |
| 737 | const isActive = appearanceMode === mode; |
| 738 | return ( |
| 739 | <button |
| 740 | aria-label={ |
| 741 | mode === "system" |
| 742 | ? gt("Follow the system appearance") |
| 743 | : mode === "light" |
| 744 | ? gt("Use light mode") |
| 745 | : gt("Use dark mode") |
| 746 | } |
| 747 | aria-pressed={isActive} |
| 748 | className={cn( |
| 749 | // The accent ring marks the picked tile; focus is a neutral ring set inside it with a gap. |
| 750 | "flex cursor-pointer flex-col items-stretch gap-1.5 rounded-xl border p-2 outline-none transition-colors focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-7 focus-visible:outline-foreground/60", |
| 751 | isActive |
| 752 | ? "border-transparent bg-accent/30 ring-2 ring-inset ring-ring" |
| 753 | : "border-border/70 bg-card/60 hover:bg-accent/10", |
| 754 | )} |
| 755 | key={mode} |
| 756 | onClick={() => setMode(mode)} |
| 757 | type="button" |
| 758 | > |
| 759 | {renderWireframe(mode)} |
| 760 | <span |
| 761 | className={cn( |
| 762 | "flex items-center justify-center text-xs font-medium", |
| 763 | isActive ? "text-foreground" : "text-muted-foreground", |
| 764 | )} |
| 765 | > |
| 766 | {mode === "system" ? gt("System") : mode === "light" ? gt("Light") : gt("Dark")} |
| 767 | </span> |
| 768 | </button> |
| 769 | ); |
| 770 | })} |
| 771 | </div> |
| 772 | ); |
| 773 | |
| 774 | const customThemeCollections = [ |
| 775 | ...customThemes |
| 776 | .reduce((groups, customTheme) => { |
| 777 | const groupId = customTheme.collection |
| 778 | ? `collection:${customTheme.collection.id}` |
| 779 | : `theme:${customTheme.id}`; |
| 780 | const group = groups.get(groupId); |
| 781 | if (group) group.push(customTheme); |
| 782 | else groups.set(groupId, [customTheme]); |
| 783 | return groups; |
| 784 | }, new Map<string, ThemeDefinition[]>()) |
| 785 | .entries(), |
| 786 | ]; |
| 787 | |
| 788 | const renderPairGrid = () => ( |
| 789 | <TooltipProvider> |
| 790 | <div |
| 791 | className="grid w-full gap-2" |
| 792 | style={{ gridTemplateColumns: "repeat(auto-fit, minmax(min(100%, 16rem), 1fr))" }} |
| 793 | > |
| 794 | {STANDARD_THEME_CARDS.map((standardTheme) => ( |
| 795 | <ThemeLibraryCard |
| 796 | activeModes={pickedModesFor(null)} |
| 797 | isActive={false} |
| 798 | key={standardTheme.id} |
| 799 | onDuplicate={() => |
| 800 | openThemeEditor({ |
| 801 | editingThemeId: null, |
| 802 | seedThemeId: null, |
| 803 | seedName: `${standardTheme.label} copy`, |
| 804 | initialAppearance, |
| 805 | }) |
| 806 | } |
| 807 | onUse={() => persistTheme(appearanceMode === "system" ? "system" : appearanceMode)} |
| 808 | onUseMode={handlePairPick(null)} |
| 809 | theme={standardTheme} |
| 810 | /> |
| 811 | ))} |
| 812 | {MAINTAINER_THEMES.map((maintainerTheme) => { |
| 813 | const card = getThemeCardDefinition(maintainerTheme); |
| 814 | return ( |
| 815 | <ThemeLibraryCard |
| 816 | activeModes={pickedModesFor(maintainerTheme.id)} |
| 817 | isActive={false} |
| 818 | key={maintainerTheme.id} |
| 819 | onDuplicate={() => |
| 820 | openThemeEditor({ |
| 821 | editingThemeId: null, |
| 822 | seedThemeId: maintainerTheme.id, |
| 823 | seedName: `${maintainerTheme.label} copy`, |
| 824 | initialAppearance, |
| 825 | }) |
| 826 | } |
| 827 | onUse={() => persistTheme(maintainerTheme.id)} |
| 828 | onUseMode={handlePairPick(maintainerTheme.id)} |
| 829 | theme={card} |
| 830 | /> |
| 831 | ); |
| 832 | })} |
| 833 | {environmentThemes |
| 834 | .filter( |
| 835 | // Saved themes win id resolution; show only their card to avoid duplicate keys. |
| 836 | (environmentTheme) => !customThemes.some((theme) => theme.id === environmentTheme.id), |
| 837 | ) |
| 838 | .map((environmentTheme) => ( |
| 839 | <ThemeLibraryCard |
| 840 | activeModes={pickedModesFor(environmentTheme.id)} |
| 841 | isActive={false} |
| 842 | key={environmentTheme.id} |
| 843 | onDuplicate={() => |
| 844 | openThemeEditor({ |
| 845 | editingThemeId: null, |
| 846 | seedThemeId: environmentTheme.id, |
| 847 | seedName: `${environmentTheme.label} copy`, |
| 848 | initialAppearance, |
| 849 | }) |
| 850 | } |
| 851 | onUse={() => { |
| 852 | const half = singleAppearanceOf(environmentTheme); |
| 853 | if (half === null) persistTheme(environmentTheme.id); |
| 854 | else assignHalf(half, environmentTheme.id); |
| 855 | }} |
| 856 | onUseMode={handlePairPick(environmentTheme.id)} |
| 857 | theme={getThemeCardDefinition(environmentTheme)} |
| 858 | /> |
| 859 | ))} |
| 860 | {customThemeCollections.map(([collectionId, themes]) => ( |
| 861 | <CustomThemeCollectionCard |
| 862 | activeModesFor={pickedModesFor} |
| 863 | key={collectionId} |
| 864 | onDownload={(customTheme) => |
| 865 | downloadThemeFile(`${customTheme.id}.json`, serializeThemeFile(customTheme)) |
| 866 | } |
| 867 | onDuplicate={(customTheme) => |
| 868 | openThemeEditor({ |
| 869 | editingThemeId: null, |
| 870 | seedThemeId: customTheme.id, |
| 871 | seedName: `${customTheme.label} copy`, |
| 872 | initialAppearance, |
| 873 | }) |
| 874 | } |
| 875 | onEdit={(customTheme) => |
| 876 | openThemeEditor({ |
| 877 | editingThemeId: customTheme.id, |
| 878 | seedThemeId: null, |
| 879 | seedName: null, |
| 880 | initialAppearance, |
| 881 | }) |
| 882 | } |
| 883 | onRemove={(customTheme) => handleRemoveTheme(customTheme, themes)} |
| 884 | onUse={(customTheme) => { |
| 885 | const modes = getThemeModes(customTheme); |
| 886 | if (modes.length === 1) assignHalf(modes[0]!, customTheme.id); |
| 887 | else persistTheme(customTheme.id); |
| 888 | }} |
| 889 | onUseMode={(customTheme, mode) => handlePairPick(customTheme.id)(mode)} |
| 890 | themes={themes} |
| 891 | /> |
| 892 | ))} |
| 893 | </div> |
| 894 | </TooltipProvider> |
| 895 | ); |
| 896 | |
| 897 | return ( |
| 898 | <div className="space-y-3"> |
| 899 | <h3 className="flex min-h-7 items-center px-3 text-base font-semibold text-foreground sm:px-4"> |
| 900 | {setting("color-scheme").title} |
| 901 | </h3> |
| 902 | {renderModeTiles()} |
| 903 | <div className="flex min-h-8 flex-wrap items-center justify-between gap-3 px-3 pt-5 sm:px-4"> |
| 904 | <h3 className="text-base font-semibold text-foreground">{setting("theme").title}</h3> |
| 905 | <div className="flex flex-wrap items-center justify-end gap-2"> |
| 906 | <Button |
| 907 | size="xs" |
| 908 | variant="outline" |
| 909 | onClick={() => |
| 910 | openThemeEditor({ |
| 911 | editingThemeId: null, |
| 912 | seedThemeId: activeThemeForAppearance?.id ?? null, |
| 913 | seedName: null, |
| 914 | initialAppearance, |
| 915 | }) |
| 916 | } |
| 917 | > |
| 918 | <PaintbrushIcon /> |
| 919 | <T>Create theme</T> |
| 920 | </Button> |
| 921 | <Button size="xs" variant="outline" onClick={() => onImportOpenChange(true)}> |
| 922 | <PlusIcon /> |
| 923 | <T>Add theme</T> |
| 924 | </Button> |
| 925 | </div> |
| 926 | </div> |
| 927 | {renderPairGrid()} |
| 928 | <ThemeImportDialog |
| 929 | onImportedMany={(importedThemes, { updated }) => { |
| 930 | if (updated) refreshTheme(); |
| 931 | const title = updated |
| 932 | ? gt("{count, plural, =1 {{label} updated} other {{count} themes updated}}", { |
| 933 | count: importedThemes.length, |
| 934 | label: importedThemes[0]!.label, |
| 935 | }) |
| 936 | : gt("{count, plural, =1 {{label} added} other {{count} themes added}}", { |
| 937 | count: importedThemes.length, |
| 938 | label: importedThemes[0]!.label, |
| 939 | }); |
| 940 | toastManager.add( |
| 941 | stackedThreadToast({ |
| 942 | type: "success", |
| 943 | title, |
| 944 | description: importedThemes.map((imported) => imported.label).join(", "), |
| 945 | }), |
| 946 | ); |
| 947 | }} |
| 948 | onImported={(importedTheme) => { |
| 949 | const modes = getThemeModes(importedTheme); |
| 950 | if (modes.length === 1) { |
| 951 | assignHalf(modes[0]!, importedTheme.id); |
| 952 | toastManager.add( |
| 953 | stackedThreadToast({ |
| 954 | type: "success", |
| 955 | title: gt("{label} added", { label: importedTheme.label }), |
| 956 | description: |
| 957 | modes[0] === "light" |
| 958 | ? gt("It’s now your light theme.") |
| 959 | : gt("It’s now your dark theme."), |
| 960 | }), |
| 961 | ); |
| 962 | return true; |
| 963 | } |
| 964 | if (!persistTheme(importedTheme.id)) return false; |
| 965 | toastManager.add( |
| 966 | stackedThreadToast({ |
| 967 | type: "success", |
| 968 | title: `${importedTheme.label} added`, |
| 969 | description: gt("It’s now active."), |
| 970 | }), |
| 971 | ); |
| 972 | return true; |
| 973 | }} |
| 974 | onOpenChange={onImportOpenChange} |
| 975 | open={isImportOpen} |
| 976 | /> |
| 977 | <AlertDialog open={isThemeRemovalOpen} onOpenChange={setIsThemeRemovalOpen}> |
| 978 | <AlertDialogPopup> |
| 979 | <AlertDialogHeader> |
| 980 | <AlertDialogTitle> |
| 981 | {canRemoveCollection |
| 982 | ? gt("Remove themes from “{removeDialogCollectionLabel}”?", { |
| 983 | removeDialogCollectionLabel, |
| 984 | }) |
| 985 | : gt("Remove “{label}”?", { label: removeDialogTheme?.label })} |
| 986 | </AlertDialogTitle> |
| 987 | <AlertDialogDescription> |
| 988 | {canRemoveCollection |
| 989 | ? gt( |
| 990 | "Select the variants you want to remove. You can restore them by importing the extension again.", |
| 991 | ) |
| 992 | : gt("You can bring it back anytime by importing its JSON file.")} |
| 993 | </AlertDialogDescription> |
| 994 | </AlertDialogHeader> |
| 995 | {canRemoveCollection ? ( |
| 996 | <div className="grid max-h-72 grid-cols-1 gap-2 overflow-y-auto px-6 pb-6 sm:grid-cols-2"> |
| 997 | {removeDialogCollectionThemes.map((customTheme) => { |
| 998 | const checked = themeIdsToRemoveSet.has(customTheme.id); |
| 999 | const card = getThemeCardDefinition(customTheme); |
| 1000 | const checkboxId = `remove-theme-${customTheme.id}`; |
| 1001 | return ( |
| 1002 | <label |
| 1003 | className="group relative flex min-h-28 cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border border-border/70 bg-muted/25 p-3 has-checked:border-ring has-checked:bg-accent/20 hover:bg-muted/40" |
| 1004 | htmlFor={checkboxId} |
| 1005 | key={customTheme.id} |
| 1006 | > |
| 1007 | <span className="absolute right-2 top-2 inline-grid size-5 grid-cols-1 sm:size-4"> |
| 1008 | <input |
| 1009 | checked={checked} |
| 1010 | className="col-start-1 row-start-1 size-full appearance-none rounded-sm border border-input bg-background outline-none checked:border-primary checked:bg-primary focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring checked:focus-visible:inset-ring-3 checked:focus-visible:inset-ring-background dark:not-checked:bg-input/32 forced-colors:appearance-auto" |
| 1011 | id={checkboxId} |
| 1012 | name="themes-to-remove" |
| 1013 | type="checkbox" |
| 1014 | onChange={(event) => { |
| 1015 | const shouldRemove = event.currentTarget.checked; |
| 1016 | setThemeIdsToRemove((current) => |
| 1017 | shouldRemove |
| 1018 | ? [...current, customTheme.id] |
| 1019 | : current.filter((themeId) => themeId !== customTheme.id), |
| 1020 | ); |
| 1021 | }} |
| 1022 | /> |
| 1023 | <CheckIcon className="pointer-events-none col-start-1 row-start-1 size-3.5 shrink-0 self-center justify-self-center stroke-primary-foreground opacity-0 group-has-checked:opacity-100 sm:size-3" /> |
| 1024 | </span> |
| 1025 | <span className="flex min-h-12 items-center justify-center gap-1"> |
| 1026 | {card.previews.map((preview) => ( |
| 1027 | <span |
| 1028 | className="flex size-11 shrink-0 items-center justify-center" |
| 1029 | key={preview.mode} |
| 1030 | > |
| 1031 | <span className="flex scale-75"> |
| 1032 | <ThemePreviewCircle colors={preview.colors} mode={preview.mode} /> |
| 1033 | </span> |
| 1034 | </span> |
| 1035 | ))} |
| 1036 | </span> |
| 1037 | <p className="max-w-full truncate text-center text-base font-medium text-foreground sm:text-sm"> |
| 1038 | {customTheme.label} |
| 1039 | </p> |
| 1040 | </label> |
| 1041 | ); |
| 1042 | })} |
| 1043 | </div> |
| 1044 | ) : null} |
| 1045 | <AlertDialogFooter> |
| 1046 | <AlertDialogClose render={<Button variant="outline" />}> |
| 1047 | <T>Cancel</T> |
| 1048 | </AlertDialogClose> |
| 1049 | <Button |
| 1050 | disabled={themeIdsToRemove.length === 0} |
| 1051 | variant="destructive" |
| 1052 | onClick={handleConfirmRemoveTheme} |
| 1053 | > |
| 1054 | {canRemoveCollection |
| 1055 | ? themeIdsToRemove.length > 0 |
| 1056 | ? gt("Remove selected ({count})", { count: themeIdsToRemove.length }) |
| 1057 | : gt("Remove selected") |
| 1058 | : gt("Remove theme")} |
| 1059 | </Button> |
| 1060 | </AlertDialogFooter> |
| 1061 | </AlertDialogPopup> |
| 1062 | </AlertDialog> |
| 1063 | </div> |
| 1064 | ); |
| 1065 | } |