From 54f745dcd01bec6f66b511baf91fff00a8e50626 Mon Sep 17 00:00:00 2001 From: Cato <41178744+catomean@users.noreply.github.com> Date: Tue, 15 Sep 2026 04:58:37 -0700 Subject: [PATCH] fix(settings): the profile form told three lies about what it saved (#728) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(settings): the profile form told three lies about what it saved Renaming works — but the form misreported it in ways that each hide a real mismatch between what you see and what is stored. 1. The Save button never went quiet again. `dirty` compared against the `user` PROP, which a successful PATCH never refreshes, so after one save the baseline was stale forever: the button stayed enabled and accented, permanently claiming unsaved work that had already been saved. The saved values are now state, moved on success, and router.refresh() re-renders anything server-rendered from the session. 2. The username was silently rewritten. The field accepted "Cato Nakamoto!!" with no complaint; on save normalizeUsername() turned it into "cato-nakamoto" and the field kept showing the typed text, so the form displayed a value the server did not hold. The normalized value is now written back on save, and shown BEFORE saving as "Will be saved as …". 3. The rules existed only in the API's zod schema. A user's first encounter with "lowercase letters, numbers and hyphens, 2-40" was a rejected save. They are stated under the field, with a specific message for too-short, and the button is disabled rather than letting a doomed save through. Also: `dirty` now compares the NORMALIZED username, so "Cato" and "cato" are not treated as a change when they save identically; the public-profile link and dirty state finally agree. The "Saved" status moved beside the button — as a block above it, it pushed the button down on every save, moving the target the user had just aimed at — and gained role="status" so it is announced rather than only seen. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01DzY8KuwgUM7u95GVTpRL2m * style: prettier Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01DzY8KuwgUM7u95GVTpRL2m --------- Co-authored-by: Claude Opus 5 (1M context) --- src/components/settings/ProfileSettings.tsx | 101 ++++++++++++++++---- 1 file changed, 84 insertions(+), 17 deletions(-) diff --git a/src/components/settings/ProfileSettings.tsx b/src/components/settings/ProfileSettings.tsx index f492a56b9..0bd688820 100644 --- a/src/components/settings/ProfileSettings.tsx +++ b/src/components/settings/ProfileSettings.tsx @@ -1,8 +1,9 @@ "use client"; import Link from "next/link"; +import { useRouter } from "next/navigation"; import { useState } from "react"; -import { Loader2, ExternalLink } from "lucide-react"; +import { Loader2, ExternalLink, Check } from "lucide-react"; import { patchJson, throwApiError } from "@/lib/api/fetch"; import { normalizeUsername } from "@/lib/username"; import { APP_DOMAIN } from "@/config/brand"; @@ -12,23 +13,46 @@ type Props = { user: { id: string; name: string; username: string; image: string }; }; +/** What the API accepts — stated here because the user has to satisfy it. */ +const USERNAME_RULE = "Lowercase letters, numbers and hyphens. 2–40 characters."; + export function ProfileSettings({ user }: Props) { + const router = useRouter(); + // The saved values, as a baseline `dirty` can be measured against. This was + // the `user` PROP, which a successful PATCH never refreshes — so after one + // save the form compared against a stale baseline forever and the button + // stayed enabled, claiming unsaved work that had already been saved. + const [saved, setSaved] = useState({ name: user.name, username: user.username }); const [name, setName] = useState(user.name); const [username, setUsername] = useState(user.username); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); - const [saved, setSaved] = useState(false); + const [justSaved, setJustSaved] = useState(false); - const dirty = name !== user.name || username !== user.username; + // The username is STORED normalized, so that is what `dirty` and the public + // link must both reason about — comparing the raw field would call "Cato" + // and "cato" different values when they save identically. + const normalized = normalizeUsername(username); + const dirty = name !== saved.name || normalized !== saved.username; + // Typing is never blocked; the user is told what will actually be stored. + const willRename = Boolean(username) && normalized !== username.trim(); + const tooShort = Boolean(username) && normalized.length < 2; const save = async () => { setSaving(true); setError(""); - setSaved(false); + setJustSaved(false); try { - const res = await patchJson("/api/me", { name, username: normalizeUsername(username) }); + const res = await patchJson("/api/me", { name, username: normalized }); if (!res.ok) await throwApiError(res, "Failed to save"); - setSaved(true); + // Show what was actually stored, not what was typed, and move the + // baseline so the button can go quiet again. + setUsername(normalized); + setSaved({ name, username: normalized }); + setJustSaved(true); + // Anything server-rendered from the session — the public profile, any + // surface that greets you by name — is now stale. + router.refresh(); } catch (e) { setError(e instanceof Error ? e.message : "Something went wrong"); } finally { @@ -44,16 +68,24 @@ export function ProfileSettings({ user }: Props) {
- + setName(e.target.value)} + onChange={(e) => { + setName(e.target.value); + setJustSaved(false); + }} className="ui-input" placeholder="Your name" />
- +
{/* The full host is 26 characters of prefix. On a 390px phone that left ~120px for the field the user is actually here to edit, @@ -63,15 +95,38 @@ export function ProfileSettings({ user }: Props) { {APP_DOMAIN}/u/ setUsername(e.target.value)} + onChange={(e) => { + setUsername(e.target.value); + setJustSaved(false); + }} + aria-describedby="profile-username-rule" className="ui-tap w-full min-w-0 flex-1 bg-transparent text-base text-text-primary outline-none placeholder:text-text-muted sm:text-sm" placeholder="yourname" />
- {username && ( + {/* The rule, always. It used to live only in the API's zod schema, so + the first a user heard of it was a rejected save — or worse, a + silent rewrite they never saw: the form kept showing what they + typed while the server stored something else. */} +

+ {tooShort ? ( + + Too short — a username needs at least 2 characters. + + ) : willRename ? ( + <> + Will be saved as {normalized} + . {USERNAME_RULE} + + ) : ( + USERNAME_RULE + )} +

+ {normalized && !tooShort && ( @@ -83,12 +138,24 @@ export function ProfileSettings({ user }: Props) {
{error &&

{error}

} - {saved &&

Saved.

} - + {/* Status sits BESIDE the button rather than above it: as a block it + pushed the button down on every save, moving the target the user had + just aimed at. */} +
+ +

+ {justSaved && !dirty && ( + + + Saved + + )} +

+
); }