feat(prefs): per-user UI font scale (A−/A+), saved to the profile

A header A−/value/A+ control scales the whole UI, persisted per user and
restored on login from any booth — cloning the theme-pref pattern end to end.

- DB: users.font_scale (migration 0014; percent, 100 = base, NOT NULL default).
- Server: PUT /api/auth/font-scale (auth-guarded; clamps to 80–160, snaps to a
  10-step); fontScale flows through sessionView → login + /me.
- Client: setFontScalePref + applyFontScale; applied in App alongside theme;
  FontScaleToggle in the header; i18n sq+en.

Scaling uses CSS `zoom` on the root, NOT root font-size: the app's type is pinned
in px (text-[12px] etc., ~230 spots), which a font-size change would not scale —
so the dense Active-sessions / Live-feed logs stayed tiny. `zoom` scales
everything uniformly (text, spacing, icons) like the browser's Ctrl+/−, which is
the readability win for operators who need larger text.

Tests: 4 font-scale auth-route cases (persist + /me, clamp/snap, 400, default-100).
Full workspace build/lint/test green.

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-06-28 12:25:04 +02:00
parent 6734e9815e
commit f706726eeb
12 changed files with 203 additions and 12 deletions
+6 -4
View File
@@ -5,7 +5,7 @@ import { fetchMe, type SessionUser } from "./api.js";
import { Login } from "./Login.js";
import { queryClient } from "./lib/query.js";
import { setLanguage } from "./lib/i18n/index.js";
import { applyTheme } from "./lib/theme.js";
import { applyTheme, applyFontScale } from "./lib/theme.js";
import { router } from "./router.js";
// App root: bootstraps the session (cookie-based, from /api/auth/me), then hands
@@ -24,15 +24,17 @@ export function App() {
.finally(() => setLoading(false));
}, []);
// Apply the signed-in user's preferred language + theme whenever they resolve/
// change (login, bootstrap, or a toggle). Albanian + dark are the defaults before
// auth resolves; on logout, fall back to dark so the Login screen is consistent.
// Apply the signed-in user's preferred language + theme + font scale whenever they
// resolve/change (login, bootstrap, or a toggle). Albanian + dark + 100% are the defaults
// before auth resolves; on logout, fall back so the Login screen is consistent.
useEffect(() => {
if (user) {
setLanguage(user.language);
applyTheme(user.theme);
applyFontScale(user.fontScale);
} else {
applyTheme("dark");
applyFontScale(100);
}
}, [user]);
+12
View File
@@ -73,6 +73,8 @@ export interface SessionUser {
language: Lang;
/** Preferred UI theme (loaded from the server on login). */
theme: Theme;
/** Preferred UI font scale, percent of base (100 = base; clamped 80–160). */
fontScale: number;
/** Optional display name (profile metadata); null if unset. */
fullName: string | null;
/** Optional contact email (profile metadata); null if unset. */
@@ -105,6 +107,16 @@ export function setThemePref(theme: Theme): Promise<{ theme: Theme }> {
return apiFetch("/api/auth/theme", { method: "PUT", body: JSON.stringify({ theme }) });
}
/** Allowed font-scale band (percent of base) + step. The header control clamps to these. */
export const FONT_SCALE_MIN = 80;
export const FONT_SCALE_MAX = 160;
export const FONT_SCALE_STEP = 10;
/** Persist the current user's UI font scale (percent; restored on next login). */
export function setFontScalePref(fontScale: number): Promise<{ fontScale: number }> {
return apiFetch("/api/auth/font-scale", { method: "PUT", body: JSON.stringify({ fontScale }) });
}
/** Edit MY own profile (display name / email). Returns the refreshed session.
* Self-service — touches only the signed-in user; no `user:*` permission needed. */
export function updateMyProfile(patch: {
+3
View File
@@ -14,6 +14,9 @@ export const en: Catalog = {
themeDark: "dark",
themeLight: "light",
theme: "Theme",
fontSmaller: "Smaller text",
fontLarger: "Larger text",
fontSize: "Text size",
today: "Today",
yesterday: "Yesterday",
months: [
+3
View File
@@ -14,6 +14,9 @@ export const sq = {
themeDark: "errët",
themeLight: "çelët",
theme: "Tema",
fontSmaller: "Zvogëlo tekstin",
fontLarger: "Rrit tekstin",
fontSize: "Madhësia e tekstit",
today: "Sot",
yesterday: "Dje",
// Month names (index 0 = January) — kept in the catalog because the appliance's
+17 -6
View File
@@ -1,14 +1,25 @@
import type { Theme } from "../api.js";
import { FONT_SCALE_MAX, FONT_SCALE_MIN } from "../api.js";
// Theme application. The whole UI reads colour through the --color-term-* tokens;
// the light palette lives in index.css under `html.theme-light`. Applying a theme is
// just toggling that class on <html>. The active theme is the LOGGED-IN USER's stored
// preference (users.theme), applied via applyTheme() after auth resolves — mirroring
// how language works. Dark is the default before auth resolves. Printed tickets are
// unaffected (always Albanian, dark-agnostic).
// Theme + font-scale application. The whole UI reads colour through the --color-term-*
// tokens; the light palette lives in index.css under `html.theme-light`. Applying a theme
// is just toggling that class on <html>. Both are the LOGGED-IN USER's stored preferences
// (users.theme / users.font_scale), applied after auth resolves — mirroring how language
// works. Defaults (dark, 100%) apply before auth resolves. Printed tickets are unaffected.
/** Apply a theme by toggling `theme-light` on <html>. Dark is the absence of the
* class (the base tokens). No-op-safe to call repeatedly. */
export function applyTheme(theme: Theme): void {
document.documentElement.classList.toggle("theme-light", theme === "light");
}
/** Apply a font scale as a whole-UI ZOOM (`pct`% on the root). The app's type is pinned in
* px (`text-[12px]` etc.), which a root font-size would NOT scale — `zoom` scales everything
* uniformly (text, spacing, icons), exactly like the browser's Ctrl+/−, so the feed/session
* logs grow too. Clamped to the allowed band; no-op-safe to call repeatedly. */
export function applyFontScale(pct: number): void {
const clamped = Math.min(FONT_SCALE_MAX, Math.max(FONT_SCALE_MIN, Math.round(pct)));
// `zoom` is supported in all the booth's target browsers (Chromium/WebKit/modern FF).
// 1 = 100%. Reset to "" at base so we don't leave an inline override lying around.
document.documentElement.style.zoom = clamped === 100 ? "" : String(clamped / 100);
}
+46 -2
View File
@@ -10,11 +10,23 @@ import { lazy, Suspense, useState } from "react";
import { useTranslation } from "react-i18next";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import type { Lang, Permission, SessionUser, Theme } from "./api.js";
import { can, closeShift, fetchShiftReport, logout, openShift, setLanguagePref, setThemePref } from "./api.js";
import {
can,
closeShift,
fetchShiftReport,
logout,
openShift,
setLanguagePref,
setThemePref,
setFontScalePref,
FONT_SCALE_MIN,
FONT_SCALE_MAX,
FONT_SCALE_STEP,
} from "./api.js";
import { qk, queryClient } from "./lib/query.js";
import { Modal } from "./ui/Modal.js";
import { setLanguage } from "./lib/i18n/index.js";
import { applyTheme } from "./lib/theme.js";
import { applyTheme, applyFontScale } from "./lib/theme.js";
import { useLiveFeed } from "./lib/use-live-feed.js";
import { useShift } from "./lib/use-shift.js";
import { DeviceFooter } from "./ui/DeviceFooter.js";
@@ -210,6 +222,37 @@ function ThemeToggle({
);
}
/** Header font-size control: A−/value/A+ scaling the whole UI (root font-size). Persisted
* to the user profile like the theme, restored on next login. Local `active` state seeded
* from the prop (the router context doesn't re-render on setUser); App's effect keeps the
* DOM in sync with the persisted user on (re)login. */
function FontScaleToggle({ user, setUser }: { user: SessionUser; setUser: (u: SessionUser | null) => void }) {
const { t } = useTranslation();
const [active, setActive] = useState<number>(user.fontScale);
function step(delta: number) {
const next = Math.min(FONT_SCALE_MAX, Math.max(FONT_SCALE_MIN, active + delta));
if (next === active) return;
setActive(next);
applyFontScale(next); // instant UI
setUser({ ...user, fontScale: next });
void setFontScalePref(next).catch(() => {
/* non-fatal — the choice still applies this session */
});
}
const btn = "rounded-term px-1.5 py-0.5 text-term-muted hover:text-term-text disabled:opacity-40";
return (
<div className="flex items-center gap-0.5 text-[10px] uppercase tracking-wider">
<button type="button" className={btn} onClick={() => step(-FONT_SCALE_STEP)} disabled={active <= FONT_SCALE_MIN} title={t("common.fontSmaller")} aria-label={t("common.fontSmaller")}>
A−
</button>
<span className="min-w-[2.5rem] text-center text-term-muted" title={t("common.fontSize")}>{active}%</span>
<button type="button" className={btn} onClick={() => step(FONT_SCALE_STEP)} disabled={active >= FONT_SCALE_MAX} title={t("common.fontLarger")} aria-label={t("common.fontLarger")}>
A+
</button>
</div>
);
}
/**
* Header shift control — the site-wide single-open shift expressed as one button:
* - no shift open → "Open shift" (enabled; opens this operator's shift)
@@ -399,6 +442,7 @@ function RootLayout() {
{user && <ShiftButton />}
{user && <LanguageToggle user={user} setUser={setUser} />}
{user && <ThemeToggle user={user} setUser={setUser} />}
{user && <FontScaleToggle user={user} setUser={setUser} />}
<StatusDot />
{user && (
<Link