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
14 changes: 14 additions & 0 deletions apps/server/src/environmentTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ it.layer(NodeServices.layer)("environment theme", (it) => {
);
});

it.effect("switches back to the stock theme after selecting another", () => {
const selected: Array<{ readonly theme?: string; readonly appearance?: string }> = [];
return withEnvironmentThemes(
{},
Effect.gen(function* () {
const themes = yield* EnvironmentTheme.EnvironmentThemeService;
yield* themes.apply({ themeId: "agent-look", theme: NIGHTFALL_THEME });
yield* themes.apply({ themeId: "t3-code" });
assert.deepEqual(selected, [{ theme: "agent-look" }, { theme: "" }]);
}),
selected,
);
});

it.effect(
"reports the image beside a theme as its background, with a new revision per file",
() =>
Expand Down
12 changes: 9 additions & 3 deletions apps/server/src/environmentTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
} from "@t3tools/contracts";
import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
import { themeCssLoadsRemoteFiles } from "@t3tools/shared/themeCss";
import { BUILT_IN_THEME_IDS, UNPUBLISHABLE_THEME_IDS } from "@t3tools/shared/themePalettes";
import {
MOBILE_DEFAULT_THEME_ID,
MOBILE_THEME_IDS,
UNPUBLISHABLE_THEME_IDS,
} from "@t3tools/shared/themePalettes";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
Expand Down Expand Up @@ -406,11 +410,13 @@ const make = Effect.gen(function* () {
}
// Timestamps make clients apply a set again when its value did not change. Theme and
// appearance have one each, so setting one does not undo a later local choice of the other.
// The stock theme is an empty default with a set time, which clients adopt as stock.
const defaultTheme = themeId === MOBILE_DEFAULT_THEME_ID ? "" : themeId;
const select = Effect.gen(function* () {
const setAt = DateTime.formatIso(yield* DateTime.now);
yield* settings
.updateSettings({
...(themeId === undefined ? {} : { defaultTheme: themeId, defaultThemeSetAt: setAt }),
...(defaultTheme === undefined ? {} : { defaultTheme, defaultThemeSetAt: setAt }),
...(appearance === undefined
? {}
: { defaultAppearance: appearance, defaultAppearanceSetAt: setAt }),
Expand All @@ -423,7 +429,7 @@ const make = Effect.gen(function* () {
Effect.gen(function* () {
if (themeId === undefined) return yield* select;
if (theme === undefined) {
const known = [...BUILT_IN_THEME_IDS, ...(yield* refresh).themes.map(({ id }) => id)];
const known = [...MOBILE_THEME_IDS, ...(yield* refresh).themes.map(({ id }) => id)];
if (!known.includes(themeId)) {
return yield* new EnvironmentThemeRefusedError({
themeId,
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/mcp/toolkits/theme/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OrchestratorMcpFailure } from "@t3tools/contracts";
import { THEME_GUIDE } from "@t3tools/shared/themeCss";
import { BUILT_IN_THEME_IDS } from "@t3tools/shared/themePalettes";
import { MOBILE_THEME_IDS } from "@t3tools/shared/themePalettes";
import * as Effect from "effect/Effect";
import * as Struct from "effect/Struct";
import * as EnvironmentTheme from "../../../environmentTheme.ts";
Expand Down Expand Up @@ -30,7 +30,7 @@ export const layer = McpToolAccess.toLayer(ThemeToolkit, {
);
return {
current,
builtIn: BUILT_IN_THEME_IDS,
builtIn: MOBILE_THEME_IDS,
published: published.map(({ id, name, css, background }) => ({
id,
name,
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/hooks/useDefaultTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ describe("default theme adoption", () => {
expect(next).not.toBe(null);
expect(next).not.toBe(applied);
});

it("applies the stock theme only when the environment set it", () => {
expect(defaultThemeToApply({ ...BASE, defaultTheme: "", defaultThemeSetAt: "" })).toBe(null);
expect(defaultThemeToApply({ ...BASE, defaultTheme: "" })).not.toBe(null);
});
});

it("lets a theme set its own mode only when no mode was set with it or after it", () => {
Expand Down
15 changes: 11 additions & 4 deletions apps/web/src/hooks/useDefaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { appAtomRegistry } from "../rpc/atomRegistry";
import { themeEnvironmentIdAtom } from "../state/primaryEnvironment";
import { themeServerSettingsAtom } from "../state/server";
import { getThemeDefinition, singleAppearanceOf, subscribeToCustomThemes } from "../themePalette";
import { setAppearanceMode, setTheme } from "./useTheme";
import { getThemeSnapshot, setAppearanceMode, setTheme } from "./useTheme";

const APPLIED_DEFAULT_THEME_STORAGE_PREFIX = "t3code:default-theme-applied:v2:";
const APPLIED_DEFAULT_APPEARANCE_STORAGE_PREFIX = "t3code:default-appearance-applied:v1:";
Expand All @@ -19,7 +19,9 @@ export function defaultThemeToApply(input: {
readonly appliedGeneration: string | null;
readonly resolves: boolean;
}): string | null {
if (input.environmentId === null || input.defaultTheme.length === 0) return null;
// Empty with a set time means the stock theme was set; with none, no theme was.
if (input.environmentId === null) return null;
if (input.defaultTheme.length === 0 && input.defaultThemeSetAt.length === 0) return null;
const generation = defaultThemeGeneration(input.defaultTheme, input.defaultThemeSetAt);
if (input.appliedGeneration === generation) return null;
// The setting and the palette it names arrive independently, so an id that
Expand Down Expand Up @@ -67,9 +69,14 @@ function adoptDefaultTheme(): void {
defaultTheme,
defaultThemeSetAt,
appliedGeneration: readAppliedGeneration(storageKey),
resolves: definition !== null,
resolves: definition !== null || defaultTheme === "",
});
if (generation === null || definition === null) return;
if (generation === null) return;
if (definition === null) {
// The stock theme keeps the client's own light, dark, or system mode.
if (setTheme(getThemeSnapshot().appearanceMode)) writeAppliedGeneration(storageKey, generation);
return;
}

// A single-appearance theme needs both preference and appearance writes to take effect.
if (!setTheme(defaultTheme)) return;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/themeCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ export const THEME_GUIDE = [
...THEME_CSS_HOOKS.map((hook) => `- ${hook.selector}: ${hook.styles}`),
"",
"You cannot see the result. Make small changes, then ask the user how it looks.",
"To undo, set a built-in theme: t3-chat, grove, ocean, ember, or iris.",
"To undo, set `t3-code`, the stock T3 Code theme, or another built-in theme. An empty `current` means the stock theme or no environment theme.",
].join("\n");
Loading