freezmo/lib/image.ts
Martin Dvorak 618ad5fb9c Add an English/Czech interface language switch
English stays the default; Czech is a full translation. Only the app chrome
is translated - notes and generated decks keep their own language, which is a
core feature, not a setting.

i18n
- lib/i18n-dict.ts: flat en/cs dictionaries (441 keys each), translate() and
  translatePlural() with real Czech plural rules (1 sada / 2-4 sady / 5+ sad).
  English is the default and the fallback for a missing key.
- lib/i18n.ts: browser store (freezmo.locale.v1), useI18n() hook, and the
  x-freezmo-locale header. The hook's t/plural are bound to the locale React
  is rendering with, so the first client render matches the server HTML and
  then re-renders in the stored language; module-level t is only for event
  handlers and non-React helpers.
- <html lang> follows the choice (inline script + setLocale).

UI
- Settings -> Language segmented control (English / Cestina), plus an EN/CS
  button on the key gate for first run.
- Every screen translated: sidebar, header, tab bar, library, deck cards,
  folder bar, create flow, all seven activities, study chrome, results,
  settings + health panel, API-key gate and help, install sheet, offline and
  the deck-not-found page.
- Plural-aware counts everywhere (decks, folders, activities, runs, photos,
  documents, pages, words, wrong attempts).

Server
- API routes and guards (generate, grade, health, http-guard, rate-limit,
  ollama error mapping) answer in the request's language, so error cards and
  the health panel never fall back to English mid-sentence.

Fixes
- A hydration mismatch when a non-default locale was stored silently dropped
  the .dark class after hydration; render-time t() is now bound to the
  hook's locale (UploadScreen.describeExtraction, ResultsScreen.answerFor,
  DeckCard's folder fallback).
- Bumped the service worker cache to freezmo-v4 for this deploy.

Tests: i18n 27/27 (gate toggle, persistence, html lang, Czech plurals,
settings switch) and a rebuilt regression suite 72/72 (English default,
library, create, all 7 activity types, results, settings, install sheet,
offline, PWA, security, Czech smoke).
2026-10-08 13:56:32 +00:00

133 lines
4.5 KiB
TypeScript

"use client";
import { t } from "./i18n";
/** Client-side image helpers: validation, resizing and compression. */
export const MAX_IMAGE_DIMENSION = 1600;
export const JPEG_QUALITY = 0.82;
export const MAX_FILE_BYTES = 20 * 1024 * 1024; // 20 MB per source file
export const MAX_IMAGES = 8;
export const ACCEPTED_TYPES = ["image/jpeg", "image/png", "image/webp", "image/gif", "image/heic", "image/heif"];
export type PreparedImage = {
id: string;
dataUrl: string;
name: string;
width: number;
height: number;
bytes: number;
};
export function isImageFile(file: File): boolean {
if (file.type.startsWith("image/")) return true;
return /\.(jpe?g|png|webp|gif|heic|heif|bmp)$/i.test(file.name);
}
export function describeFileError(file: File): string | null {
if (!isImageFile(file)) return t("file.notImage", { name: file.name });
if (file.size > MAX_FILE_BYTES) return t("file.imageTooLarge", { name: file.name });
return null;
}
/** Rough byte length of a base64 data URL. */
export function dataUrlBytes(dataUrl: string): number {
const index = dataUrl.indexOf(",");
if (index === -1) return dataUrl.length;
return Math.floor(((dataUrl.length - index - 1) * 3) / 4);
}
async function loadBitmap(blob: Blob, fallbackName: string): Promise<{ width: number; height: number; draw: (ctx: CanvasRenderingContext2D, w: number, h: number) => void }> {
// Prefer createImageBitmap: it respects EXIF orientation and is fast.
if (typeof createImageBitmap === "function") {
try {
const bitmap = await createImageBitmap(blob, { imageOrientation: "from-image" });
return {
width: bitmap.width,
height: bitmap.height,
draw: (ctx, w, h) => ctx.drawImage(bitmap, 0, 0, w, h),
};
} catch {
/* fall through to the <img> path (Safari/HEIC edge cases) */
}
}
const url = URL.createObjectURL(blob);
try {
const image = await new Promise<HTMLImageElement>((resolve, reject) => {
const element = new Image();
element.onload = () => resolve(element);
element.onerror = () => reject(new Error(`Could not read "${fallbackName}".`));
element.src = url;
});
return {
width: image.naturalWidth || image.width,
height: image.naturalHeight || image.height,
draw: (ctx, w, h) => ctx.drawImage(image, 0, 0, w, h),
};
} finally {
URL.revokeObjectURL(url);
}
}
/**
* Resize to max 1600px and re-encode as JPEG to keep uploads small.
* Accepts any image blob (photos, canvas output from PDF pages, images
* embedded in documents).
*/
export async function prepareImageFromBlob(blob: Blob, name = "image.jpg"): Promise<PreparedImage> {
const bitmap = await loadBitmap(blob, name);
const scale = Math.min(1, MAX_IMAGE_DIMENSION / Math.max(bitmap.width, bitmap.height));
const width = Math.max(1, Math.round(bitmap.width * scale));
const height = Math.max(1, Math.round(bitmap.height * scale));
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Your browser could not process this image.");
ctx.fillStyle = "#ffffff"; // flatten transparency before JPEG encoding
ctx.fillRect(0, 0, width, height);
bitmap.draw(ctx, width, height);
const dataUrl = canvas.toDataURL("image/jpeg", JPEG_QUALITY);
return {
id: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`,
dataUrl,
name,
width,
height,
bytes: dataUrlBytes(dataUrl),
};
}
/** Resize to max 1600px and re-encode as JPEG to keep uploads small. */
export async function prepareImage(file: File): Promise<PreparedImage> {
return prepareImageFromBlob(file, file.name || "photo.jpg");
}
/** Extract image files from a paste or drop event. */
export function imageFilesFromDataTransfer(data: DataTransfer | null): File[] {
if (!data) return [];
const files: File[] = [];
if (data.files?.length) {
for (const file of Array.from(data.files)) if (isImageFile(file)) files.push(file);
}
if (data.items?.length) {
for (const item of Array.from(data.items)) {
if (item.kind !== "file") continue;
const file = item.getAsFile();
if (file && isImageFile(file) && !files.some((f) => f.name === file.name && f.size === file.size)) {
files.push(file);
}
}
}
return files;
}
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}