Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions apps/web/src/themeBackground.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,25 @@ vi.mock(
);

import { applyThemeBackground, startThemeBackground } from "./themeBackground";
import {
CUSTOM_THEMES_STORAGE_KEY,
getThemeDefinition,
invalidateCustomThemes,
} from "./themePalette";

const BACKGROUND = "--theme-background-image";
const image = new Blob(["image"], { type: "image/png" });
const offline = () => Promise.reject(new TypeError("Failed to fetch"));
const loaded = async () => ({ ok: true, blob: async () => image });

function setup() {
function setup(storage: Record<string, string> = {}) {
vi.useFakeTimers();
const properties = new Map<string, string>();
const browser = new EventTarget();
const browser = Object.assign(new EventTarget(), {
localStorage: { getItem: (key: string) => storage[key] ?? null },
});
vi.stubGlobal("window", browser);
invalidateCustomThemes();
vi.stubGlobal("document", {
documentElement: {
dataset: { themeId: "nightfall" },
Expand All @@ -71,6 +79,7 @@ function setup() {
stop();
applyThemeBackground(undefined);
vi.unstubAllGlobals();
invalidateCustomThemes();
vi.useRealTimers();
},
};
Expand Down Expand Up @@ -129,4 +138,23 @@ describe("theme backgrounds", () => {
await vi.advanceTimersByTimeAsync(30_000);
expect(t.fetchBackground).toHaveBeenCalledTimes(1);
});

it("shows no published background when a saved theme shares its id", async () => {
using t = setup({
[CUSTOM_THEMES_STORAGE_KEY]: JSON.stringify([
{
id: "nightfall",
label: "Nightfall",
appearance: "dark",
colors: { canvas: "#162327", accent: "#cc3344" },
},
]),
});
t.fetchBackground.mockImplementation(loaded);
expect(getThemeDefinition("nightfall")?.colors.accent).toBeDefined();
applyThemeBackground("nightfall");
await vi.advanceTimersByTimeAsync(0);
expect(t.fetchBackground).not.toHaveBeenCalled();
expect(t.properties.has(BACKGROUND)).toBe(false);
});
});
4 changes: 3 additions & 1 deletion apps/web/src/themeBackground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assetEnvironment } from "./state/assets";
import { themeEnvironmentIdAtom } from "./state/primaryEnvironment";
import { themeServerEnvironmentThemesAtom } from "./state/server";
import { environmentSession } from "./state/session";
import { getCustomThemes } from "./themePalette";

const THEME_BACKGROUND_VARIABLE = "--theme-background-image";
const RETRY_DELAYS_MS = [1_000, 2_000, 4_000, 8_000];
Expand Down Expand Up @@ -55,8 +56,9 @@ async function loadBackground(
export function applyThemeBackground(themeId: string | undefined): void {
if (typeof document === "undefined") return;
const environmentId = appAtomRegistry.get(themeEnvironmentIdAtom);
// A saved theme wins its id over a published one, so the published image is not its.
const revision =
themeId === undefined
themeId === undefined || getCustomThemes().some((theme) => theme.id === themeId)
? undefined
: appAtomRegistry.get(themeServerEnvironmentThemesAtom)?.find((theme) => theme.id === themeId)
?.background;
Expand Down
Loading