Files
multica/packages/views/settings/components/preferences-tab.tsx
Jiayuan Zhang 7803a5b9ea feat(ui): establish a role-named type scale and migrate ad-hoc font sizes (MUL-5451) (#6136)
tokens.css defined colours, radii and font families but not a single --text-*
step, so font sizes had no baseline to align to and grew wherever they were
needed: 51 distinct sizes across web + desktop, 370 written as arbitrary
values, six at half a pixel (10.5 / 11.5 / 12.5 / 13.5 / 14.5 / 15.5px).
text-xs and text-sm carried nearly all UI text while the range between them —
11, 13, 15px — could only be reached with arbitrary values. Hierarchy does not
come from having more sizes; past a handful, each extra size makes the
hierarchy blurrier.

Add ten role-named steps, each with its own line-height so leading cannot
fragment the way size did, and move every product-UI call site onto them.
Steps are named for what the text is for, not for a t-shirt size, because
that is what keeps the scale from drifting again.

Six steps deliberately keep the exact size/line-height pairs of the Tailwind
defaults they replace, so the ~1,900-call-site rename moves nothing on screen.
The visible changes are confined to former arbitrary values snapping to a step:
8/9/10px -> micro (11px) on badges and overlines; 17 -> 18; 22 -> 24; 30
(text-3xl) -> 36 on headings and stat numbers; 12.8px -> label (13px) on small
buttons and toggles. Half-pixel sizes are gone.

This supersedes #6108, which was reverted by #6116 because the sidebar group
labels rendered at the inherited 16px. The cause was not the scale but cn():
`text-<x>` is ambiguous in Tailwind, and tailwind-merge resolves it against a
table listing only the default sizes, so it filed every role step under
text-colour and dropped whichever of `text-caption` /
`text-sidebar-foreground/70` came first. Registering the steps as a font-size
class group restores the real conflict groups — size beats size, colour beats
colour, the two coexist — and a test pins the list against the scale, since
the failure is silent in source.

Hand-written CSS is covered too. The transcript kept a 12.5px body long after
every Tailwind call site was on the scale, so the "no half-pixel sizes" claim
was true of the classes and false of the product; the editor's prose, code and
mermaid ramps had the same blind spot, and seven of their eight values already
equalled a step exactly. All now reference var(--text-*). The guard test reads
raw `font-size:` declarations as well as class names, exempting only the 16px
iOS input-zoom workaround in base.css and the landing pages' marketing ramp.

apps/mobile (own NativeWind config) and apps/docs (fumadocs' own type system)
keep Tailwind's default scale and are untouched. Landing display type
(rem/clamp, 2.2-6.4rem) stays on its separate ramp, as do four decorative
emoji / serif-hero sizes.

Verified on a running local stack: pinned sidebar rows and group labels
measure 12px/16px, nav items 14px/20px — identical to pre-migration. An audit
of every rendered font size across the product surfaces finds nothing off the
scale; the only exceptions are avatar initials and emoji, which
actor-avatar.tsx sizes proportionally to the avatar diameter by design.

Co-authored-by: Lambda <lambda@multica.ai>
Co-authored-by: multica-agent <github@multica.ai>
2026-07-30 13:42:33 +08:00

310 lines
10 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { toast } from "sonner";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@multica/ui/components/ui/select";
import { Switch } from "@multica/ui/components/ui/switch";
import { useTheme } from "@multica/ui/components/common/theme-provider";
import {
DEFAULT_LOCALE,
SUPPORTED_LOCALES,
type SupportedLocale,
} from "@multica/core/i18n";
import { useLocaleAdapter } from "@multica/core/i18n/react";
import { useAuthStore } from "@multica/core/auth";
import {
useCommentComposerStore,
useIssueLinkStore,
} from "@multica/core/issues/stores";
import { api } from "@multica/core/api";
import { browserTimezone, timezoneOptions } from "../../common/timezone-select";
import { useT } from "../../i18n";
import {
SettingsCard,
SettingsRow,
SettingsSection,
SettingsTab,
} from "./settings-layout";
export function PreferencesTab() {
const { theme, setTheme } = useTheme();
const { t, i18n } = useT("settings");
const localeAdapter = useLocaleAdapter();
const user = useAuthStore((s) => s.user);
// i18next.language can be a region-tagged BCP-47 string (e.g. "en-US",
// "zh-Hans-CN") returned by intl-localematcher. Normalize to a supported
// locale before comparing — otherwise the radio shows neither option active.
const currentLocale: SupportedLocale = SUPPORTED_LOCALES.includes(
i18n.language as SupportedLocale,
)
? (i18n.language as SupportedLocale)
: DEFAULT_LOCALE;
const themeOptions = [
{ value: "light" as const, label: t(($) => $.preferences.theme.light) },
{ value: "dark" as const, label: t(($) => $.preferences.theme.dark) },
{ value: "system" as const, label: t(($) => $.preferences.theme.system) },
];
const languageOptions: { value: SupportedLocale; label: string }[] = [
{ value: "en", label: t(($) => $.preferences.language.english) },
{ value: "zh-Hans", label: t(($) => $.preferences.language.chinese) },
{ value: "ko", label: t(($) => $.preferences.language.korean) },
{ value: "ja", label: t(($) => $.preferences.language.japanese) },
];
// Persist locally → sync to user.language → reload. Reload (vs in-place
// changeLanguage) avoids hydration mismatch and is the i18next-recommended
// pattern for App Router.
//
// If the cross-device sync (PATCH /api/me) fails, the local cookie is
// already written so the new locale will take effect after reload — but
// the user's other devices won't see the change. Surface that explicitly
// via a toast and delay the reload long enough for the toast to be read,
// otherwise the failure would be invisible.
const handleLanguageChange = async (next: SupportedLocale) => {
if (next === currentLocale) return;
localeAdapter.persist(next);
let syncFailed = false;
if (user) {
try {
await api.updateMe({ language: next });
} catch {
syncFailed = true;
}
}
if (syncFailed) {
toast.warning(t(($) => $.preferences.language.sync_failed));
// Give the toast 2.5s of visible time before navigating away.
setTimeout(() => window.location.reload(), 2500);
return;
}
toast.success(t(($) => $.auto_save.toast_saved), {
id: "settings-auto-save",
});
// Keep the confirmation visible before the locale reload replaces the UI.
setTimeout(() => window.location.reload(), 900);
};
return (
<SettingsTab title={t(($) => $.page.tabs.preferences)}>
<SettingsSection title={t(($) => $.preferences.general_title)}>
<SettingsCard>
<SettingsRow
label={t(($) => $.preferences.theme.title)}
size="select"
>
<Select
items={themeOptions}
value={theme}
onValueChange={(next) => {
if (!next || next === theme) return;
setTheme(next as (typeof themeOptions)[number]["value"]);
toast.success(t(($) => $.auto_save.toast_saved), {
id: "settings-auto-save",
});
}}
>
<SelectTrigger
size="sm"
className="w-full"
aria-label={t(($) => $.preferences.theme.title)}
>
<SelectValue>
{themeOptions.find((option) => option.value === theme)?.label}
</SelectValue>
</SelectTrigger>
<SelectContent align="end">
{themeOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</SettingsRow>
<SettingsRow
label={t(($) => $.preferences.language.title)}
size="select"
>
<Select
items={languageOptions}
value={currentLocale}
onValueChange={(next) => {
if (next) void handleLanguageChange(next as SupportedLocale);
}}
>
<SelectTrigger
size="sm"
className="w-full"
aria-label={t(($) => $.preferences.language.title)}
>
<SelectValue>
{languageOptions.find((option) => option.value === currentLocale)?.label}
</SelectValue>
</SelectTrigger>
<SelectContent align="end">
{languageOptions.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</SettingsRow>
<TimezoneRow />
<StickyCommentBarRow />
<IssueLinkNewTabRow />
</SettingsCard>
</SettingsSection>
</SettingsTab>
);
}
function StickyCommentBarRow() {
const { t } = useT("settings");
const sticky = useCommentComposerStore((s) => s.sticky);
const toggleSticky = useCommentComposerStore((s) => s.toggleSticky);
return (
<SettingsRow
label={t(($) => $.preferences.sticky_comment_bar.title)}
description={t(($) => $.preferences.sticky_comment_bar.hint)}
>
<Switch
checked={sticky}
onCheckedChange={() => {
toggleSticky();
toast.success(t(($) => $.auto_save.toast_saved), {
id: "settings-auto-save",
});
}}
aria-label={t(($) => $.preferences.sticky_comment_bar.title)}
/>
</SettingsRow>
);
}
function IssueLinkNewTabRow() {
const { t } = useT("settings");
const openInNewTab = useIssueLinkStore((s) => s.openInNewTab);
const setOpenInNewTab = useIssueLinkStore((s) => s.setOpenInNewTab);
return (
<SettingsRow
label={t(($) => $.preferences.issue_link_new_tab.title)}
description={t(($) => $.preferences.issue_link_new_tab.hint)}
>
<Switch
checked={openInNewTab}
onCheckedChange={(checked) => {
setOpenInNewTab(checked === true);
toast.success(t(($) => $.auto_save.toast_saved), {
id: "settings-auto-save",
});
}}
aria-label={t(($) => $.preferences.issue_link_new_tab.title)}
/>
</SettingsRow>
);
}
// Base UI rejects "" as a SelectItem value, so route the "no preference"
// state through this sentinel and translate at the wire boundary.
const BROWSER_TZ_VALUE = "__browser__";
function TimezoneRow() {
const { t } = useT("settings");
const user = useAuthStore((s) => s.user);
const setUser = useAuthStore((s) => s.setUser);
const stored = user?.timezone ?? null;
const browser = browserTimezone();
const value = stored ?? BROWSER_TZ_VALUE;
// Full IANA list (from timezoneOptions in common/timezone-select) so a
// user needing a non-curated zone isn't stuck with ~18 common ones.
// Memoized — timezoneOptions enumerates ~600 IANA zones per call.
const options = useMemo(
() => timezoneOptions(stored ?? browser),
[stored, browser],
);
const handleChange = async (next: string) => {
if (next === value) return;
const payload = next === BROWSER_TZ_VALUE ? "" : next;
try {
const updated = await api.updateMe({ timezone: payload });
setUser(updated);
toast.success(t(($) => $.auto_save.toast_saved), {
id: "settings-auto-save",
});
} catch (err) {
toast.error(
err instanceof Error && err.message
? err.message
: t(($) => $.preferences.timezone.sync_failed),
);
}
};
const formatTZLabel = (tz: string) => {
if (tz === BROWSER_TZ_VALUE) {
return `${browser}${t(($) => $.preferences.timezone.browser_suffix)}`;
}
return tz;
};
return (
<SettingsRow
label={t(($) => $.preferences.timezone.title)}
description={t(($) => $.preferences.timezone.hint)}
size="select-wide"
>
<Select
items={[
{ value: BROWSER_TZ_VALUE, label: formatTZLabel(BROWSER_TZ_VALUE) },
...options.map((timezone) => ({
value: timezone,
label: formatTZLabel(timezone),
})),
]}
value={value}
onValueChange={(next) => {
if (next) void handleChange(next);
}}
>
<SelectTrigger
size="sm"
className="w-full font-mono text-caption"
aria-label={t(($) => $.preferences.timezone.title)}
>
<SelectValue>{formatTZLabel(value)}</SelectValue>
</SelectTrigger>
<SelectContent align="end" className="max-h-72">
<SelectItem value={BROWSER_TZ_VALUE} className="font-mono text-caption">
{formatTZLabel(BROWSER_TZ_VALUE)}
</SelectItem>
{options.map((timezone) => (
<SelectItem key={timezone} value={timezone} className="font-mono text-caption">
{formatTZLabel(timezone)}
</SelectItem>
))}
</SelectContent>
</Select>
</SettingsRow>
);
}