trails/apps/journal/app/routes/settings.profile.tsx
Ullrich Schäfer df562742e1
fix(journal): extract loaders/actions for the remaining 21 mixed routes
Completes the .server.ts split started in #418. Every route that mixes
a default-export component with a server-only loader/action now has a
sibling <route>.server.ts holding the data-fetching helpers; the route
.tsx is a thin delegator.

Routes converted (21):
  activities._index, activities.$id, activities.new, auth.accept-terms,
  auth.verify, explore, feed, notifications, routes._index, routes.$id,
  routes.$id.edit, routes.new, settings, settings.account,
  settings.connections.komoot, settings.profile, settings.security,
  sync.import.$provider, sync.import.komoot, users.$username.followers,
  users.$username.following

Pattern (same as home.tsx / users.$username.tsx / settings.connections.tsx):
- loader → `return data(await loadX(request, params?))`
- action → `return await xAction(request, params?)`
- All `getDb` / Drizzle schema / `~/lib/*.server` imports move to the
  .server.ts sibling.
- `throw redirect(...)` and `throw data(...)` propagate through the
  delegator unchanged.

No behavior changes — pure module-graph cleanup. Component modules no
longer transitively import the DB client; Vite's tree-shake of
server-only code is now backed by an explicit, file-local contract.

Verified:
- pnpm typecheck — green
- pnpm lint — green
- pnpm test — 181 passed, 31 integration-gated skipped
- pnpm --filter @trails-cool/journal build — succeeds

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 11:05:40 +02:00

129 lines
4.9 KiB
TypeScript

import { useState, useEffect } from "react";
import { data, useFetcher } from "react-router";
import { useTranslation } from "react-i18next";
import type { Route } from "./+types/settings.profile";
import { loadProfileSettings } from "./settings.profile.server";
export function meta() {
return [{ title: "Profile — Settings — trails.cool" }];
}
export async function loader({ request }: Route.LoaderArgs) {
return data(await loadProfileSettings(request));
}
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>
);
}