Files
accounted/app/global-error.tsx
T
Jakob Wennberg 9686b54b41 refactor(design): lock the border-radius ladder, one radius per role (#1607)
Seven radii were in circulation (4/5/6/8/12/16px + pill) with no rule for
which went where; one toolbar row on /transactions mixed four shape
languages. This locks a 4-tier ladder (design.md convention 16):

- pill: interactive toolbar controls (buttons, chips, pickers, segmented
  controls, toolbar search, count nubs)
- rounded-xl (12px): overlay tier: page panel, dialogs, slide-overs
- rounded-lg (8px): cards, form fields, popover/menu content, boxes
- rounded-sm (4px): nested leaves (menu items, checkboxes, kbd/code nubs)

Changes:
- New SegmentedControl primitive (pill-in-pill tablist, h-8) replaces the
  hand-rolled bg-muted/70 tablist copied across 11 files
- New ToolbarSearch primitive (pill, h-8) adopted on 9 page toolbars;
  dialog/picker searches keep the rounded-lg Input
- dialog.tsx 8px -> 12px, matching SettingsModal/slide-over/CommandPalette
- ContextPicker chips at the shared h-8 toolbar height
- ~300 rounded-md / bare rounded call sites remapped by role; auth icon
  tiles and the mobile nav sheet come down from 16px to 12px
- rounded-md, bare rounded, rounded-2xl and rounded-[Npx] are dead
  vocabulary, enforced by a new off-ladder-radius check in check:guards

Verified: lint 0 errors, 14422 unit tests pass, check:guards green, tsc
clean on all changed files, sandbox screenshots of transactions/
bookkeeping/granskning toolbars and the Ny verifikation dialog.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-14 08:55:37 +02:00

92 lines
3.4 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
// Last-resort boundary: only fires when the root layout itself throws, so it
// renders its own <html> and must stay free of app providers/components
// (NextIntlClientProvider, CompanyContext, etc. are not mounted here). It is
// also server-rendered when the root layout throws during SSR, so the lazy
// initializer must never touch window without a guard.
//
// Like app/error.tsx, transient failures here (e.g. a token race in the first
// request after login) heal on a fresh request, so auto-reload ONCE before
// showing the manual fallback. A per-path, per-tab-session flag (a monotonic
// one-shot, not a time window that could still loop on a slow failing render)
// keeps the single reload from becoming a loop on a genuinely broken root layout.
const RELOAD_FLAG_PREFIX = "accounted:global-error-reloaded:";
function reloadKey(): string {
return (
RELOAD_FLAG_PREFIX +
(typeof window !== "undefined" ? window.location.pathname : "")
);
}
// support@gnubok.se is hardcoded on purpose: this boundary renders when the root
// layout failed, so the branding service and any provider are unavailable here.
const SUPPORT_EMAIL = "support@gnubok.se";
function decideInitialPhase(): "reloading" | "fallback" {
if (typeof window === "undefined") return "reloading";
try {
if (window.sessionStorage.getItem(reloadKey())) return "fallback";
// Claim the one-shot and reload only if it persisted: writing here (not in
// the effect) keeps "mark reloaded" and "decide to reload" atomic, so a
// failed write (quota full / blocked) falls through to 'fallback' instead
// of reloading forever without ever recording it.
window.sessionStorage.setItem(reloadKey(), "1");
return "reloading";
} catch {
return "fallback";
}
}
export default function GlobalError({
error,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
const [phase] = useState<"reloading" | "fallback">(decideInitialPhase);
useEffect(() => {
console.error("[global] Unhandled error:", error);
// The one-shot flag is already claimed in decideInitialPhase, so reaching
// 'reloading' guarantees it persisted: reload exactly once.
if (phase === "reloading") window.location.reload();
}, [phase, error]);
return (
<html lang="sv" translate="no">
<head>
<meta name="google" content="notranslate" />
</head>
<body>
{phase === "fallback" ? (
<div className="flex min-h-dvh items-center justify-center p-8">
<div className="text-center space-y-4">
<h2 className="text-xl">Något gick fel</h2>
<p className="text-muted-foreground">
Ett oväntat fel inträffade. Försök igen eller{" "}
<a
href={`mailto:${SUPPORT_EMAIL}?subject=Oväntat fel`}
className="underline underline-offset-4 hover:text-foreground"
>
kontakta support
</a>{" "}
om problemet kvarstår.
</p>
<button
onClick={() => window.location.reload()}
className="rounded-full bg-primary px-4 py-2 text-sm text-primary-foreground hover:bg-primary/90"
>
Försök igen
</button>
</div>
</div>
) : null}
</body>
</html>
);
}