trails/apps/journal/app/routes/settings.profile.tsx
Ullrich Schäfer c0c1a53322 Split /settings into 4 sub-pages with a sidebar layout
Stream E from docs/information-architecture.md. The settings page was
a single scrollable list of five concerns; split it into four
deep-linkable sections behind a shared sidebar layout, so each
concern has a stable URL, a focused loader (only fetches what that
section needs), and a meta title that reflects the section.

Sections:
- /settings/profile — display name, bio, profile visibility
- /settings/account — email change + danger-zone account deletion
- /settings/security — passkeys
- /settings/connections — sync providers (Wahoo today)

URL pattern: nested routes under a layout. /settings itself
redirects to /settings/profile so the bare URL still lands somewhere
useful without rendering an empty container.

- apps/journal/app/routes/settings.tsx — converted from a single
  scrollable page to a layout that renders the sidebar nav + Outlet.
- apps/journal/app/routes/settings.{profile,account,security,
  connections,_index}.tsx — new files, each with its own loader and
  meta. _index redirects to /settings/profile.
- apps/journal/app/routes.ts — registers the four children + index
  under the settings layout route.
- packages/i18n/src/locales/{en,de}.ts — new settings.nav.{profile,
  account,security,connections} keys for the sidebar labels.

Specs (profile-settings, account-management, connected-services)
are unchanged — they describe behavior, not URL structure. The
journal-landing spec is also unchanged; the navbar still has a
single "Settings" link.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 09:48:00 +02:00

138 lines
5.1 KiB
TypeScript

import { useState, useEffect } from "react";
import { data, redirect, useFetcher } from "react-router";
import { useTranslation } from "react-i18next";
import type { Route } from "./+types/settings.profile";
import { getSessionUser } from "~/lib/auth.server";
export function meta() {
return [{ title: "Profile — Settings — trails.cool" }];
}
export async function loader({ request }: Route.LoaderArgs) {
const user = await getSessionUser(request);
if (!user) throw redirect("/auth/login");
return data({
user: {
username: user.username,
displayName: user.displayName,
bio: user.bio,
profileVisibility: user.profileVisibility,
},
});
}
export default function ProfileSettings({ loaderData }: Route.ComponentProps) {
const { user } = loaderData;
const { t } = useTranslation(["journal", "common"]);
const profileFetcher = useFetcher();
const [displayName, setDisplayName] = useState(user.displayName ?? "");
const [bio, setBio] = useState(user.bio ?? "");
const [profileVisibility, setProfileVisibility] = useState<"public" | "private">(
user.profileVisibility,
);
const [profileSaved, setProfileSaved] = useState(false);
useEffect(() => {
if (profileFetcher.data && !profileFetcher.data.error) {
setProfileSaved(true);
const timer = setTimeout(() => setProfileSaved(false), 3000);
return () => clearTimeout(timer);
}
}, [profileFetcher.data]);
return (
<section>
<h2 className="text-lg font-semibold text-gray-900">{t("settings.profile.title")}</h2>
<profileFetcher.Form method="post" action="/api/settings/profile" className="mt-4 space-y-4">
<div>
<label htmlFor="displayName" className="block text-sm font-medium text-gray-700">
{t("settings.profile.displayName")}
</label>
<input
id="displayName"
name="displayName"
type="text"
value={displayName}
onChange={(e) => setDisplayName(e.target.value)}
placeholder={user.username}
className="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
/>
</div>
<div>
<label htmlFor="bio" className="block text-sm font-medium text-gray-700">
{t("settings.profile.bio")}
</label>
<textarea
id="bio"
name="bio"
value={bio}
onChange={(e) => setBio(e.target.value)}
maxLength={160}
rows={3}
className="mt-1 block w-full rounded-md border border-gray-300 px-3 py-2 shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
/>
<p className="mt-1 text-xs text-gray-500">{bio.length}/160</p>
</div>
<fieldset>
<legend className="block text-sm font-medium text-gray-700">
{t("settings.profile.visibility.label")}
</legend>
<div className="mt-2 space-y-2">
<label className="flex items-start gap-2 text-sm">
<input
type="radio"
name="profileVisibility"
value="public"
checked={profileVisibility === "public"}
onChange={() => setProfileVisibility("public")}
className="mt-0.5"
/>
<span>
<span className="font-medium text-gray-900">
{t("settings.profile.visibility.public")}
</span>
<span className="ml-2 text-gray-500">
{t("settings.profile.visibility.publicHelp")}
</span>
</span>
</label>
<label className="flex items-start gap-2 text-sm">
<input
type="radio"
name="profileVisibility"
value="private"
checked={profileVisibility === "private"}
onChange={() => setProfileVisibility("private")}
className="mt-0.5"
/>
<span>
<span className="font-medium text-gray-900">
{t("settings.profile.visibility.private")}
</span>
<span className="ml-2 text-gray-500">
{t("settings.profile.visibility.privateHelp")}
</span>
</span>
</label>
</div>
</fieldset>
<div className="flex items-center gap-3">
<button
type="submit"
disabled={profileFetcher.state !== "idle"}
className="rounded-md bg-blue-600 px-4 py-2 text-sm text-white hover:bg-blue-700 disabled:opacity-50"
>
{profileFetcher.state !== "idle" ? t("common:loading") : t("common:save")}
</button>
{profileSaved && (
<p className="text-sm text-green-600">{t("settings.profile.saved")}</p>
)}
{profileFetcher.data?.error && (
<p className="text-sm text-red-600">{profileFetcher.data.error}</p>
)}
</div>
</profileFetcher.Form>
</section>
);
}