Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ function isolateThemes() {
};
}

it("sizes an oversized theme file just under 1 MB as 1.00 MB", async () => {
using _themes = isolateThemes();
await using _screen = await render(
<ThemeImportDialog
open
onOpenChange={() => {}}
onImported={() => true}
onImportedMany={() => {}}
/>,
);
const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
await userEvent.upload(
input,
new File(["x".repeat(1_048_100)], "huge.json", { type: "application/json" }),
);

await expect
.element(page.getByText(/^That file is/))
.toHaveTextContent(
"That file is 1.00 MB. Theme files are only a few KB, so this one was not read (limit 256 KB).",
);
});

function themeJsonc(name: string, background: string) {
return `{
// VS Code themes use JSONC even with a .json extension.
Expand Down
9 changes: 2 additions & 7 deletions apps/web/src/components/settings/ThemeImportDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DownloadIcon, PlusIcon } from "lucide-react";
import type { ChangeEvent, DragEvent, UIEvent } from "react";
import { settlePromise } from "@t3tools/client-runtime/state/runtime";
import { useRef, useState } from "react";
import { formatBytes } from "../../lib/formatBytes";
import { cn } from "../../lib/utils";
import {
getCustomThemes,
Expand Down Expand Up @@ -33,15 +34,9 @@ const MAX_THEME_FILE_BYTES = 256 * 1024;
/** Highlighting rebuilds markup per keystroke; fall back to plain text for oversized pastes. */
const MAX_HIGHLIGHTED_JSON_LENGTH = 20_000;

function formatByteSize(bytes: number): string {
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;
return `${bytes} bytes`;
}

function describeOversizedThemeFile(bytes: number): string | null {
if (bytes <= MAX_THEME_FILE_BYTES) return null;
return `That file is ${formatByteSize(bytes)}. Theme files are only a few KB, so this one was not read (limit ${formatByteSize(MAX_THEME_FILE_BYTES)}).`;
return `That file is ${formatBytes(bytes)}. Theme files are only a few KB, so this one was not read (limit ${formatBytes(MAX_THEME_FILE_BYTES)}).`;
}

function escapeJsonHtml(value: string): string {
Expand Down
Loading