diff --git a/apps/web/src/components/settings/ThemeSearchSection.tsx b/apps/web/src/components/settings/ThemeSearchSection.tsx index 6bc505b212..cf241b1939 100644 --- a/apps/web/src/components/settings/ThemeSearchSection.tsx +++ b/apps/web/src/components/settings/ThemeSearchSection.tsx @@ -10,6 +10,7 @@ import { getCustomThemes, installCustomTheme, removeCustomTheme, + updateCustomTheme, type ThemeDefinition, } from "../../themePalette"; import { Button } from "../ui/button"; @@ -95,26 +96,40 @@ export function ThemeSearchSection({ try { const themes = await importOpenVsxThemeExtension(extension, controller.signal); if (controller.signal.aborted) return; - const existingIds = new Set(getCustomThemes().map((theme) => theme.id)); - const missingThemes = themes.filter((theme) => !existingIds.has(theme.id)); - if (missingThemes.length === 0) { - throw new Error(`${extension.name} is already added.`); - } - - const installed: ThemeDefinition[] = []; + const existingById = new Map(getCustomThemes().map((theme) => [theme.id, theme])); + const installedIds: string[] = []; + const replaced: ThemeDefinition[] = []; + const imported: ThemeDefinition[] = []; try { - for (const theme of missingThemes) installed.push(installCustomTheme(theme)); + for (const theme of themes) { + const existing = existingById.get(theme.id); + if (existing) { + replaced.push(existing); + imported.push(updateCustomTheme(theme)); + } else { + const installed = installCustomTheme(theme); + installedIds.push(installed.id); + imported.push(installed); + } + } } catch (cause) { - for (const theme of installed) { + for (const themeId of installedIds) { try { - removeCustomTheme(theme.id); + removeCustomTheme(themeId); + } catch { + // Best effort rollback. The original storage failure is clearer. + } + } + for (const theme of replaced) { + try { + updateCustomTheme(theme); } catch { // Best effort rollback. The original storage failure is clearer. } } throw cause; } - onInstalled(installed); + onInstalled(imported); } catch (cause) { if (!controller.signal.aborted) { setError(cause instanceof Error ? cause.message : "That theme could not be added."); @@ -161,6 +176,14 @@ export function ThemeSearchSection({ +
+ {isSearching + ? "Finding themes..." + : results + ? `${results.length} supported ${results.length === 1 ? "theme" : "themes"} found.` + : ""} +
+ {error ? (
{ await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow( "does not match its advertised license", ); + + Reflect.deleteProperty(packagedManifest, "license"); + await rebuildPackage(); + await expect(importOpenVsxThemeExtension(extension)).rejects.toThrow( + "does not match its advertised license", + ); }); it("stops import work when the request is cancelled", async () => { diff --git a/apps/web/src/openVsxThemes.ts b/apps/web/src/openVsxThemes.ts index 458ff42fed..7a406fe469 100644 --- a/apps/web/src/openVsxThemes.ts +++ b/apps/web/src/openVsxThemes.ts @@ -651,8 +651,8 @@ export async function importOpenVsxThemeExtension( throw new Error("That extension package does not match the selected Open VSX theme."); } if ( - typeof packagedManifest.license === "string" && - packagedManifest.license.trim() !== extension.license + typeof packagedManifest.license !== "string" || + packagedManifest.license.trim().toLowerCase() !== extension.license.toLowerCase() ) { throw new Error("That extension package does not match its advertised license."); } diff --git a/apps/web/src/themePalette.test.ts b/apps/web/src/themePalette.test.ts index 1cb876a2aa..a12f8d125e 100644 --- a/apps/web/src/themePalette.test.ts +++ b/apps/web/src/themePalette.test.ts @@ -195,13 +195,25 @@ describe("theme files", () => { }); it("serializes a theme back into the importable file shape", () => { - const serialized = serializeThemeFile(T3_CHAT_THEME); + const theme = { + ...parseThemeFile({ + version: THEME_FILE_VERSION, + id: "community-demo", + name: "Community Demo", + appearance: "dark", + colors: { canvas: "#111111" }, + }), + collection: { id: "open-vsx:demo.theme", label: "Demo Theme" }, + }; + const serialized = serializeThemeFile(theme); expect(JSON.parse(serialized)).toMatchObject({ version: THEME_FILE_VERSION, - id: T3_CHAT_THEME.id, - name: T3_CHAT_THEME.label, - appearance: "light", + id: theme.id, + name: theme.label, + appearance: "dark", + collection: theme.collection, }); + expect(parseThemeFile(JSON.parse(serialized)).collection).toEqual(theme.collection); }); it("keeps sidebar artwork opt-in through theme files", () => { diff --git a/apps/web/src/themePalette.ts b/apps/web/src/themePalette.ts index 35b8643e46..42c3353ea3 100644 --- a/apps/web/src/themePalette.ts +++ b/apps/web/src/themePalette.ts @@ -112,6 +112,7 @@ export type ThemeFile = Readonly<{ appearance: ThemeAppearance; colors: ThemeColorOverrides; variants?: ThemeVariantOverrides; + collection?: ThemeCollection; sidebarArtwork?: boolean; managed?: boolean; }>; @@ -175,6 +176,15 @@ function isThemeLabel(value: unknown): value is string { return typeof value === "string" && value.trim().length > 0 && value.trim().length <= 48; } +function parseThemeCollection(value: unknown): ThemeCollection | undefined { + return isRecord(value) && + typeof value.id === "string" && + /^[a-z0-9][a-z0-9.:-]{0,127}$/i.test(value.id) && + isThemeLabel(value.label) + ? { id: value.id, label: value.label.trim() } + : undefined; +} + function parseStoredThemeColors(value: unknown, appearance: ThemeAppearance): ThemeColors | null { if (!isRecord(value)) return null; @@ -219,13 +229,7 @@ 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; + const collection = parseThemeCollection(value.collection); return { id: value.id, @@ -1547,6 +1551,10 @@ export function parseThemeFile(value: unknown): ThemeDefinition { } const overrides = parseThemeColorOverrides(rawColors); + const collection = parseThemeCollection(value.collection); + if (value.collection !== undefined && !collection) { + throw new Error("Theme collections need a valid id and label."); + } const fallback = getDefaultThemeColors(appearance); const variants: Partial> = {}; @@ -1573,6 +1581,7 @@ export function parseThemeFile(value: unknown): ThemeDefinition { appearance, colors: { ...fallback, ...overrides }, ...(Object.keys(variants).length > 0 ? { variants } : {}), + ...(collection ? { collection } : {}), ...(value.sidebarArtwork === true ? { sidebarArtwork: true } : {}), ...(value.managed === true ? { managed: true } : {}), }; @@ -1586,6 +1595,7 @@ export function serializeThemeFile(theme: ThemeDefinition): string { appearance: theme.appearance, colors: theme.colors, ...(theme.variants ? { variants: theme.variants } : {}), + ...(theme.collection ? { collection: theme.collection } : {}), ...(theme.sidebarArtwork ? { sidebarArtwork: true } : {}), ...(theme.managed ? { managed: true } : {}), };