v4: settings are stored on the server, starting with the style, the color scheme and the digest switches — every setting lived in the browser's localStorage, so it didn't follow an account to another browser and the server couldn't read it, which the digests need (whether to send each, and in which style). user_settings (migration 0019) holds one row per user who changed anything, the defaults otherwise, and GET/PATCH /api/users/me/settings read and change it (settings-service, model/settings, validators/settings, shared/settings). AuthProvider reads the settings into the session alongside the user, on mount and on focus, so a change made elsewhere arrives; useSettings changes them at once and puts them back if the server refuses. ThemeProvider now sits inside AuthProvider: a signed-in user's choice comes from their settings and is mirrored to localStorage for index.html's first-paint script, a visitor's stays in the browser, and an account with no choice yet takes the one this browser had, once. Until anyone picks a style, the instance's GERBEUR_DEFAULT_STYLE applies, written into index.html's default-style tag along with email-enabled (src/config/instance.ts), and a browser no longer stores the default as if it had been chosen, so changing the instance's style reaches everyone who never picked one. The settings tab gets an Email section with the weekly digest and monthly recap switches, disabled with a note when the server has no SMTP. The style and scheme names moved to shared/theme.ts in the previous commit; the default feed tab and sounds stay in the browser for now. 223 backend tests (new: settings defaults, patches, isolation, refusals and cascade; index.html's new tags) and 658 frontend tests (new: useSettings, the account's theme winning and a browser's carried over, settings read into the session, the digest switches) pass, and tsc, eslint, doc:lint, deno check, deno lint and deno fmt are clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-10-04 21:20:57 +00:00
parent f20ca83f0c
commit 89014639cb
38 changed files with 923 additions and 128 deletions

View File

@@ -12,6 +12,8 @@ import type { FeedTab } from "../../config/feedTabs.ts";
import { useTheme } from "../../hooks/theme/useTheme.ts";
import { useDefaultFeedTab } from "../../hooks/feed/useDefaultFeedTab.ts";
import { useSoundsEnabled } from "../../hooks/shared/useSoundsEnabled.ts";
import { useSettings } from "../../hooks/auth/useSettings.ts";
import { EMAIL_ENABLED } from "../../config/instance.ts";
import { previewChime } from "../../utils/shared/sound.ts";
import { Segmented, type SegmentedOption } from "../shared/Segmented.tsx";
import { ChangePasswordModal } from "./ChangePasswordModal.tsx";
@@ -48,14 +50,18 @@ const ON_OFF_OPTIONS: SegmentedOption<boolean>[] = [
/**
* Renders the password change, which opens {@link ChangePasswordModal}, and
* the style, color scheme, default feed tab and sounds, each stored for this
* browser as soon as it is clicked. `UserPublicProfile` renders it on one's
* own profile only.
* the settings, each saved as soon as it is clicked. The style, color scheme
* and digests are saved to the account; the default feed tab and sounds, for
* now, to this browser. `UserPublicProfile` renders it on one's own profile
* only.
*/
export function ProfileSettingsTab(): React.JSX.Element {
const { style, colorScheme, setStyle, setColorScheme } = useTheme();
const [defaultFeedTab, setDefaultFeedTab] = useDefaultFeedTab();
const [soundsEnabled, setSoundsEnabled] = useSoundsEnabled();
const { settings, updateSettings } = useSettings();
// Until the session has the settings, a click would save over unknown ones.
const digestsDisabled = !EMAIL_ENABLED || !settings;
const [changePasswordOpen, setChangePasswordOpen] = useState(false);
return (
@@ -117,6 +123,35 @@ export function ProfileSettingsTab(): React.JSX.Element {
</SettingRow>
</div>
</ProfileSection>
<ProfileSection title={<Trans>Email</Trans>}>
<div className="profile-appearance-grid">
<SettingRow label={<Trans>Weekly digest</Trans>}>
<Segmented
options={ON_OFF_OPTIONS}
value={settings?.digestWeekly ?? true}
disabled={digestsDisabled}
onChange={(on) =>
updateSettings({ digestWeekly: on }).catch(() => {})}
/>
</SettingRow>
<SettingRow label={<Trans>Monthly recap</Trans>}>
<Segmented
options={ON_OFF_OPTIONS}
value={settings?.digestMonthly ?? true}
disabled={digestsDisabled}
onChange={(on) =>
updateSettings({ digestMonthly: on }).catch(() => {})}
/>
</SettingRow>
</div>
{!EMAIL_ENABLED && (
<p className="form-hint profile-appearance-note">
<Trans>
This site doesn't send email, so no digest goes out.
</Trans>
</p>
)}
</ProfileSection>
{changePasswordOpen && (
<ChangePasswordModal onClose={() => setChangePasswordOpen(false)} />
)}