From bd9e3f953d6628f49ef915bd157c530fa018ad0a Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Fri, 7 Aug 2026 23:20:56 +0000 Subject: [PATCH] feat(web): group imported theme variants --- .../src/components/settings/ThemeSettings.tsx | 191 ++++++++++++++---- apps/web/src/openVsxThemes.test.ts | 1 + apps/web/src/openVsxThemes.ts | 8 +- apps/web/src/themePalette.test.ts | 36 ++++ apps/web/src/themePalette.ts | 11 + 5 files changed, 208 insertions(+), 39 deletions(-) diff --git a/apps/web/src/components/settings/ThemeSettings.tsx b/apps/web/src/components/settings/ThemeSettings.tsx index 2a9a4989b2..1072a3d4ad 100644 --- a/apps/web/src/components/settings/ThemeSettings.tsx +++ b/apps/web/src/components/settings/ThemeSettings.tsx @@ -1,4 +1,6 @@ import { + ChevronLeftIcon, + ChevronRightIcon, CopyIcon, DownloadIcon, PenLineIcon, @@ -75,6 +77,7 @@ function ThemeLibraryCard({ onDuplicate, onDownload, onRemove, + variantNavigation, }: { theme: ThemeCardDefinition; isActive: boolean; @@ -85,6 +88,13 @@ function ThemeLibraryCard({ onDuplicate?: () => void; onDownload?: () => void; onRemove?: () => void; + variantNavigation?: { + collectionLabel: string; + index: number; + count: number; + onPrevious: () => void; + onNext: () => void; + }; }) { // A one-appearance theme can only take its own side of the mix, so the card // tooltip promises exactly what clicking it does. @@ -111,6 +121,41 @@ function ThemeLibraryCard({ onSelectMode={onUseMode} previews={theme.previews} /> + {variantNavigation ? ( +
+ + + {theme.label} ยท {variantNavigation.index + 1} of {variantNavigation.count} + + +
+ ) : null}
@@ -124,7 +169,7 @@ function ThemeLibraryCard({ onUse(); }} > - {theme.label} + {variantNavigation?.collectionLabel ?? theme.label}
@@ -224,6 +269,68 @@ function ThemeLibraryCard({ ); } +function CustomThemeCollectionCard({ + themes, + activeModesFor, + onUse, + onUseMode, + onDuplicate, + onEdit, + onDownload, + onRemove, +}: { + themes: ReadonlyArray; + activeModesFor: (themeId: string) => ReadonlyArray; + onUse: (theme: ThemeDefinition) => void; + onUseMode: (theme: ThemeDefinition, mode: ThemeMode) => void; + onDuplicate: (theme: ThemeDefinition) => void; + onEdit: (theme: ThemeDefinition) => void; + onDownload: (theme: ThemeDefinition) => void; + onRemove: (theme: ThemeDefinition) => void; +}) { + const [variantIndex, setVariantIndex] = useState(() => { + const activeIndex = themes.findIndex((theme) => activeModesFor(theme.id).length > 0); + return activeIndex < 0 ? 0 : activeIndex; + }); + const safeIndex = Math.min(variantIndex, themes.length - 1); + const theme = themes[safeIndex]; + + useEffect(() => { + if (variantIndex !== safeIndex) setVariantIndex(safeIndex); + }, [safeIndex, variantIndex]); + + if (!theme) return null; + const collectionLabel = theme.collection?.label ?? theme.label; + const move = (offset: number) => { + setVariantIndex((current) => (current + offset + themes.length) % themes.length); + }; + + return ( + onDownload(theme)} + onDuplicate={() => onDuplicate(theme)} + onEdit={() => onEdit(theme)} + onRemove={() => onRemove(theme)} + onUse={() => onUse(theme)} + onUseMode={(mode) => onUseMode(theme, mode)} + theme={getThemeCardDefinition(theme)} + {...(themes.length > 1 + ? { + variantNavigation: { + collectionLabel, + index: safeIndex, + count: themes.length, + onPrevious: () => move(-1), + onNext: () => move(1), + }, + } + : {})} + /> + ); +} + export function ThemeLibrary({ theme, setTheme, @@ -460,6 +567,18 @@ export function ThemeLibrary({
); + const customThemeCollections = [ + ...customThemes + .reduce((groups, customTheme) => { + const groupId = customTheme.collection?.id ?? `theme:${customTheme.id}`; + const group = groups.get(groupId); + if (group) group.push(customTheme); + else groups.set(groupId, [customTheme]); + return groups; + }, new Map()) + .entries(), + ]; + const renderPairGrid = () => ( // One shared provider so every tooltip in the grid hands off instantly to // the next hovered trigger instead of stacking on top of it. The card @@ -501,43 +620,39 @@ export function ThemeLibrary({ /> ); })} - {customThemes.map((customTheme) => { - const card = getThemeCardDefinition(customTheme); - return ( - - openThemeEditor({ - editingThemeId: null, - seedThemeId: customTheme.id, - seedName: `${customTheme.label} copy`, - initialAppearance, - }) - } - onEdit={() => - openThemeEditor({ - editingThemeId: customTheme.id, - seedThemeId: null, - seedName: null, - initialAppearance, - }) - } - onDownload={() => - downloadThemeFile(`${customTheme.id}.json`, serializeThemeFile(customTheme)) - } - onRemove={() => handleRemoveTheme(customTheme)} - onUse={() => { - const modes = getThemeModes(customTheme); - if (modes.length === 1) assignHalf(modes[0]!, customTheme.id); - else persistTheme(customTheme.id); - }} - onUseMode={handlePairPick(customTheme.id)} - theme={card} - /> - ); - })} + {customThemeCollections.map(([collectionId, themes]) => ( + + downloadThemeFile(`${customTheme.id}.json`, serializeThemeFile(customTheme)) + } + onDuplicate={(customTheme) => + openThemeEditor({ + editingThemeId: null, + seedThemeId: customTheme.id, + seedName: `${customTheme.label} copy`, + initialAppearance, + }) + } + onEdit={(customTheme) => + openThemeEditor({ + editingThemeId: customTheme.id, + seedThemeId: null, + seedName: null, + initialAppearance, + }) + } + onRemove={handleRemoveTheme} + onUse={(customTheme) => { + const modes = getThemeModes(customTheme); + if (modes.length === 1) assignHalf(modes[0]!, customTheme.id); + else persistTheme(customTheme.id); + }} + onUseMode={(customTheme, mode) => handlePairPick(customTheme.id)(mode)} + themes={themes} + /> + ))} ); diff --git a/apps/web/src/openVsxThemes.test.ts b/apps/web/src/openVsxThemes.test.ts index a0c64c9cb0..0c721d39c8 100644 --- a/apps/web/src/openVsxThemes.test.ts +++ b/apps/web/src/openVsxThemes.test.ts @@ -260,6 +260,7 @@ describe("Open VSX themes", () => { expect(themes).toHaveLength(2); expect(new Set(themes.map((theme) => theme.id)).size).toBe(2); + expect(themes.every((theme) => theme.collection?.id === "open-vsx:demo.theme")).toBe(true); const paired = themes.find( (theme) => getThemeColorsForMode(theme, "light") !== null && diff --git a/apps/web/src/openVsxThemes.ts b/apps/web/src/openVsxThemes.ts index cfde87dfbd..4bfe96955d 100644 --- a/apps/web/src/openVsxThemes.ts +++ b/apps/web/src/openVsxThemes.ts @@ -587,6 +587,7 @@ export async function importOpenVsxThemeExtension( signal?.throwIfAborted(); inspectZip(zip); } catch (cause) { + if (signal?.aborted) signal.throwIfAborted(); if (cause instanceof Error && cause.message.startsWith("That extension package")) throw cause; throw new Error("That Open VSX extension package could not be opened.", { cause }); } @@ -649,5 +650,10 @@ export async function importOpenVsxThemeExtension( throw new Error(failures[0] ?? "That extension has no compatible color themes."); } const paired = pairVsCodeThemes(resolveThemeLabelCollisions(parsed)); - return resolveThemeLabelCollisions(paired.map((theme) => ({ theme }))); + const themes = resolveThemeLabelCollisions(paired.map((theme) => ({ theme }))); + const collection = { + id: `open-vsx:${extension.id.toLowerCase()}`, + label: extension.name.slice(0, 48), + }; + return themes.map((theme) => ({ ...theme, collection })); } diff --git a/apps/web/src/themePalette.test.ts b/apps/web/src/themePalette.test.ts index 671b5dbb76..1cb876a2aa 100644 --- a/apps/web/src/themePalette.test.ts +++ b/apps/web/src/themePalette.test.ts @@ -417,6 +417,42 @@ describe("theme files", () => { vi.unstubAllGlobals(); }); + it("preserves valid imported-theme collections and drops malformed metadata", () => { + vi.stubGlobal("window", { + localStorage: { + getItem: (key: string) => + key === CUSTOM_THEMES_STORAGE_KEY + ? JSON.stringify([ + { + id: "github-dark", + label: "GitHub Dark", + appearance: "dark", + colors: { canvas: "#0d1117" }, + collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" }, + }, + { + id: "github-light", + label: "GitHub Light", + appearance: "light", + colors: { canvas: "#ffffff" }, + collection: { id: "bad collection id", label: "GitHub Theme" }, + }, + ]) + : null, + }, + }); + + invalidateCustomThemes(); + expect(getCustomThemes()).toMatchObject([ + { collection: { id: "open-vsx:github.github-vscode-theme", label: "GitHub Theme" } }, + { id: "github-light" }, + ]); + expect(getCustomThemes()[1]).not.toHaveProperty("collection"); + + vi.unstubAllGlobals(); + invalidateCustomThemes(); + }); + it("updates a personal theme without changing its id", () => { const stored = new Map(); vi.stubGlobal("window", { diff --git a/apps/web/src/themePalette.ts b/apps/web/src/themePalette.ts index 2f6fb04345..35b8643e46 100644 --- a/apps/web/src/themePalette.ts +++ b/apps/web/src/themePalette.ts @@ -90,12 +90,15 @@ export type ThemeColorOverrides = Readonly>>; export type ThemeVariantOverrides = Readonly>>; export type ThemePreferenceMode = ThemeAppearance | "system"; +export type ThemeCollection = Readonly<{ id: string; label: string }>; export type ThemeDefinition = Readonly<{ id: string; label: string; appearance: ThemeAppearance; colors: ThemeColors; variants?: ThemeVariants; + /** Groups related imported variants into one library card. */ + collection?: ThemeCollection; /** Allows fixed Dev/Nightly artwork to render over this theme's sidebar. */ sidebarArtwork?: boolean; /** True when the palette was generated by the guided editor from its @@ -216,6 +219,13 @@ function parseStoredTheme(value: unknown): ThemeDefinition | null { if (!colors) return null; const variants = parseStoredThemeVariants(value.variants, value.appearance); if (value.variants !== undefined && variants === null) return null; + const collection = + isRecord(value.collection) && + typeof value.collection.id === "string" && + /^[a-z0-9][a-z0-9.:-]{0,127}$/i.test(value.collection.id) && + isThemeLabel(value.collection.label) + ? { id: value.collection.id, label: value.collection.label.trim() } + : undefined; return { id: value.id, @@ -223,6 +233,7 @@ function parseStoredTheme(value: unknown): ThemeDefinition | null { appearance: value.appearance, colors, ...(variants ? { variants } : {}), + ...(collection ? { collection } : {}), ...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}), ...(value.managed === true ? { managed: true } : {}), };