Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

3.9 KB
Raw Blame
/**
 * The parts of importing a theme that are not the dialog, kept out of it so they can be tested: how
 * a failed import reads, and the two checks the browser can make before anything is sent.
 *
 * A failure's text is mostly the server's own codes turned into the reader's language
 * (`theme-import-dialog.tsx` explains why the dialog branches the same way). One code is special:
 * `theme_invalid` carries the reader's collected REASONS — what the file got wrong, in as many
 * lines as it takes — and those are the whole message for whoever wrote the file, so they are shown
 * as they came rather than replaced by a sentence of the app's own.
 */
import { ApiError } from "../../api/client";
import { apiErrorText } from "../../lib/api-error";
import { S } from "../../lib/strings";

/**
 * The cap the server and core apply to a theme file. The browser refuses to read a picked file past
 * it, the way the plugin and skill imports do: reading 40MB into a base64 body to be told it is too
 * large is work nobody asked for. The number is core's `MAX_THEME_FILE_BYTES`, which the web does
 * not import (it is a data contract, and this is a screen) — a test holds the two equal.
 */
export const THEME_FILE_LIMIT_KB = 256;

/** Whether a picked file is past the cap, judged on the file itself rather than after reading it. */
export function themeFileTooLarge(sizeBytes: number): boolean {
  return sizeBytes > THEME_FILE_LIMIT_KB * 1024;
}

/**
 * The text of a failed theme import: the codes the import routes answer with get their own copy (the
 * server's message is English-only, and the UI language may not be), everything else goes through
 * the shared error mapping. A 409 `theme_exists` never arrives here — the caller turns it into the
 * overwrite confirmation, which is a question rather than a failure.
 *
 * The registry import answers the same codes — it fetches and writes through the same two steps —
 * plus the four that only it can answer, which are here for the same reason the others are: the
 * server's own sentence is not translated.
 */
export function themeImportErrorText(err: unknown): string {
  if (err instanceof ApiError) {
    switch (err.code) {
      case "theme_invalid":
        return S.settings.themeImportInvalid(err.message);
      case "theme_too_large":
        return S.settings.themeImportTooLarge;
      case "unsupported_url":
        return S.settings.themeImportBadUrl;
      case "blocked_url":
        return S.settings.themeImportBlockedUrl;
      case "download_failed":
        return S.settings.themeImportDownloadFailed;
      case "registry_theme_not_found":
        return S.settings.themeRegistryGone;
      case "registry_mismatch":
        return S.settings.themeRegistryMismatch;
      case "registry_unusable":
        return S.settings.themeRegistryUnusable;
      case "registry_unavailable":
        return S.settings.themeRegistryFailed;
      case "registry_invalid":
        return S.settings.themeRegistryInvalid(err.message);
      default:
        break;
    }
  }
  return apiErrorText(err);
}

/**
 * The same failure, told apart from the ONE answer that is a question rather than a refusal.
 *
 * A 409 `theme_exists` is the overwrite confirmation's cue: both flows answer it by asking, and its
 * card resends the very request. Everything else is a refusal — the reason the surface that asked
 * keeps on screen (the dialog's own body, the card under the button that offered it) instead of a
 * notification that dismisses itself while the reader is still looking at what did not happen. So
 * the rule "409 means ask, anything else means say why" lives here once rather than in each
 * caller's catch, and `null` means "open the question" — never "nothing to say".
 */
export function themeImportRefusal(err: unknown): string | null {
  if (err instanceof ApiError && err.status === 409 && err.code === "theme_exists") return null;
  return themeImportErrorText(err);
}