Fix custom theme override validation
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
b5e9e47b11
commit
82756816ef
4 files changed
+35
-11
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { ThemePaletteConfig, type ThemePaletteDefinition } from "@t3tools/contracts";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, FileSystem, Layer, Logger, Path, Schema } from "effect";
|
||||
import { Effect, FileSystem, Layer, Path, Schema } from "effect";
|
||||
import { ServerConfig, type ServerConfigShape } from "./config";
|
||||
import { Themes, ThemesLive } from "./themes";
|
||||
|
||||
@@ -120,7 +120,7 @@ it.layer(NodeServices.layer)("themes", (it) => {
|
||||
message: configState.issues[0]?.message ?? "",
|
||||
},
|
||||
]);
|
||||
}).pipe(Effect.provide(makeThemesLayer()), Logger.withMinimumLogLevel("Fatal")),
|
||||
}).pipe(Effect.provide(makeThemesLayer())),
|
||||
);
|
||||
|
||||
it.effect("persists valid custom themes without mutation", () =>
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import type { ThemePaletteDefinition, ThemeTokenName } from "@t3tools/contracts";
|
||||
import {
|
||||
THEME_TOKEN_NAMES,
|
||||
type ThemePaletteDefinition,
|
||||
type ThemeTokenName,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
export type ResolvedThemeMode = "light" | "dark";
|
||||
export type ThemePreference = ResolvedThemeMode | "system";
|
||||
@@ -202,11 +206,27 @@ function resolveThemePalette(
|
||||
label: input.label,
|
||||
description,
|
||||
source,
|
||||
light: { ...DEFAULT_LIGHT_TOKENS, ...(input.light ?? {}) },
|
||||
dark: { ...DEFAULT_DARK_TOKENS, ...(input.dark ?? {}) },
|
||||
light: { ...DEFAULT_LIGHT_TOKENS, ...sanitizeThemeTokenOverrides(input.light) },
|
||||
dark: { ...DEFAULT_DARK_TOKENS, ...sanitizeThemeTokenOverrides(input.dark) },
|
||||
} satisfies ThemePalette;
|
||||
}
|
||||
|
||||
function sanitizeThemeTokenOverrides(overrides: Record<string, string> | undefined) {
|
||||
const sanitized: ThemeTokenOverrides = {};
|
||||
if (!overrides) {
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
for (const tokenName of THEME_TOKEN_NAMES) {
|
||||
const value = overrides[tokenName];
|
||||
if (typeof value === "string") {
|
||||
sanitized[tokenName] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
const BUILT_IN_THEME_PALETTE_MAP = new Map(
|
||||
BUILT_IN_THEME_PALETTES.map((palette) => [palette.id, resolveThemePalette(palette, "built-in")]),
|
||||
);
|
||||
|
||||
@@ -433,8 +433,15 @@ function SettingsRouteView() {
|
||||
<p className="text-xs font-medium text-destructive">
|
||||
Invalid custom theme entries
|
||||
</p>
|
||||
{themeIssues.map((issue, index) => (
|
||||
<p key={`${issue.kind}:${index}`} className="text-xs text-destructive">
|
||||
{themeIssues.map((issue) => (
|
||||
<p
|
||||
key={
|
||||
issue.kind === "themes.invalid-entry"
|
||||
? `${issue.kind}:${issue.index}`
|
||||
: `${issue.kind}:${issue.message}`
|
||||
}
|
||||
className="text-xs text-destructive"
|
||||
>
|
||||
{issue.kind === "themes.invalid-entry"
|
||||
? `Entry ${issue.index + 1}: ${issue.message}`
|
||||
: issue.message}
|
||||
|
||||
@@ -44,10 +44,7 @@ const ThemeTokenValue = TrimmedNonEmptyString.check(
|
||||
Schema.isPattern(/^[^\n\r{};]+$/),
|
||||
);
|
||||
|
||||
export const ThemeTokenNameSchema = Schema.Literals(THEME_TOKEN_NAMES);
|
||||
export const ThemePaletteTokens = Schema.Record(ThemeTokenNameSchema, ThemeTokenValue).check(
|
||||
Schema.isMaxLength(THEME_TOKEN_NAMES.length),
|
||||
);
|
||||
export const ThemePaletteTokens = Schema.Record(Schema.String, ThemeTokenValue);
|
||||
export type ThemePaletteTokens = typeof ThemePaletteTokens.Type;
|
||||
|
||||
export const ThemePaletteDefinition = Schema.Struct({
|
||||
|
||||
Reference in new issue
Block a user