Profil: Header-Bild, Beschreibung, eigene Einstellungsseite
Das Profil zeigt ein Header-Bild (ohne Bild ein schwarzer Balken), den
überlappenden Avatar, Name, Beschreibung mit klickbaren Links und die
Beitragszahl. Der Eigentümer bearbeitet Bilder und Beschreibung direkt
auf dem Profil; Name ändern und Konto löschen liegen jetzt unter
/settings, das in der Navigation eingeblendet ist.
Backend: Spalten bio und header in account, neue Endpunkte
/api/user/bio, /api/user/header und /api/user/header/remove;
/u/{name}/info liefert header, bio und entries. Kontolöschung räumt das
Header-Bild mit ab.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f8cd808c2d
commit
726f5491ff
@@ -1,27 +1,33 @@
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { useNavigate, useParams } from "react-router";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
import { useParams } from "react-router";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { api, errMsg } from "../api";
|
||||
import { useAuthChanged, useMe } from "../auth";
|
||||
import { avatarUrl } from "../format";
|
||||
import { avatarUrl, linkify } from "../format";
|
||||
import { useFormAction } from "../useFormAction";
|
||||
import { Feed } from "../components/Feed";
|
||||
import type { Profile } from "../types";
|
||||
|
||||
// Muss zu maxBioRunes im Server passen (internal/httpapi/users.go).
|
||||
const maxBio = 300;
|
||||
|
||||
// Beim Wechsel zu einem anderen Profil frisch anfangen (Meldungen, Formulare).
|
||||
export function UserPage() {
|
||||
const { username = "" } = useParams();
|
||||
return <ProfileView key={username} username={username} />;
|
||||
}
|
||||
|
||||
// ProfileView: Header-Bild (ohne Bild ein schwarzer Balken), darauf
|
||||
// überlappend der Avatar, darunter Name, Beschreibung, Meta-Zeile und die
|
||||
// Beiträge. Der Eigentümer bearbeitet Bilder und Beschreibung direkt hier;
|
||||
// Name und Konto stehen unter /settings.
|
||||
function ProfileView({ username }: { username: string }) {
|
||||
const profile = useQuery({
|
||||
queryKey: ["profile", username],
|
||||
queryFn: () => api.get<Profile>(`/u/${encodeURIComponent(username)}/info`),
|
||||
});
|
||||
const me = useMe().data;
|
||||
// Nach einem Upload das neue Bild sofort zeigen: Zeitstempel am URL
|
||||
// umgeht den Browser-Cache.
|
||||
const [avatarBust, setAvatarBust] = useState("");
|
||||
const [editing, setEditing] = useState(false);
|
||||
|
||||
const info = profile.data;
|
||||
let name = "";
|
||||
@@ -32,23 +38,26 @@ function ProfileView({ username }: { username: string }) {
|
||||
|
||||
return (
|
||||
<section id="profile">
|
||||
<div className="profile-head">
|
||||
<img
|
||||
id="profile-avatar"
|
||||
className="avatar avatar-lg"
|
||||
src={avatarUrl(info?.avatar ?? "") + avatarBust}
|
||||
alt=""
|
||||
/>
|
||||
<div>
|
||||
<h2 id="profile-name">{name}</h2>
|
||||
<p id="profile-info" className="muted">
|
||||
{info && `Mitglied seit ${new Date(info.created_at * 1000).toLocaleDateString("de-DE")}`}
|
||||
</p>
|
||||
</div>
|
||||
<div className="profile-header">
|
||||
{info?.header && <img src={`/${info.header}`} alt="" />}
|
||||
</div>
|
||||
|
||||
<div id="settings" hidden={!isOwner}>
|
||||
{isOwner && <Settings onAvatar={() => setAvatarBust(`?t=${Date.now()}`)} />}
|
||||
<div className="profile-body">
|
||||
<div className="profile-top">
|
||||
<img id="profile-avatar" className="avatar avatar-xl" src={avatarUrl(info?.avatar ?? "")} alt="" />
|
||||
{isOwner && (
|
||||
<button onClick={() => setEditing(!editing)} aria-expanded={editing}>
|
||||
{editing ? "Fertig" : "Profil bearbeiten"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<h2 id="profile-name">{name}</h2>
|
||||
{info?.bio && <p className="bio">{linkify(info.bio)}</p>}
|
||||
<p id="profile-info" className="muted">
|
||||
{info &&
|
||||
`Mitglied seit ${new Date(info.created_at * 1000).toLocaleDateString("de-DE")} · ` +
|
||||
`${info.entries} ${info.entries === 1 ? "Beitrag" : "Beiträge"}`}
|
||||
</p>
|
||||
{isOwner && editing && info && <ProfileEdit info={info} />}
|
||||
</div>
|
||||
|
||||
{info && (
|
||||
@@ -62,81 +71,64 @@ function ProfileView({ username }: { username: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
// useFormAction verdrahtet ein Einstellungsformular: POST auf path, bei Erfolg
|
||||
// onSuccess mit Formular und Antwort, bei Fehler die Meldung unter dem Formular.
|
||||
function useFormAction<T>(path: string, fallback: string, onSuccess: (form: HTMLFormElement, data: T) => void) {
|
||||
const m = useMutation({ mutationFn: (fd: FormData) => api.post<T>(path, fd) });
|
||||
const onSubmit = (e: FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
const form = e.currentTarget;
|
||||
m.mutate(new FormData(form), { onSuccess: (data) => onSuccess(form, data) });
|
||||
};
|
||||
return { onSubmit, busy: m.isPending, msg: m.isError ? errMsg(m.error, fallback) : "" };
|
||||
}
|
||||
|
||||
// Settings: nur für den Eigentümer.
|
||||
function Settings({ onAvatar }: { onAvatar: () => void }) {
|
||||
// ProfileEdit: Header-Bild, Profilbild und Beschreibung, nur für den
|
||||
// Eigentümer.
|
||||
function ProfileEdit({ info }: { info: Profile }) {
|
||||
const qc = useQueryClient();
|
||||
const navigate = useNavigate();
|
||||
const authChanged = useAuthChanged();
|
||||
const [bio, setBio] = useState(info.bio);
|
||||
|
||||
// Neues Bild und neuer Name tauchen auch in den Beitragskarten auf -> die
|
||||
// Feeds und das eigene Konto neu laden.
|
||||
const refresh = () => {
|
||||
const refreshProfile = () => qc.invalidateQueries({ queryKey: ["profile"] });
|
||||
// Das Profilbild taucht auch in den Beitragskarten und im eigenen Konto auf.
|
||||
const refreshAvatar = () => {
|
||||
qc.invalidateQueries({ queryKey: ["feed"] });
|
||||
qc.invalidateQueries({ queryKey: ["thread"] });
|
||||
qc.invalidateQueries({ queryKey: ["profile"] });
|
||||
refreshProfile();
|
||||
return authChanged();
|
||||
};
|
||||
|
||||
const avatar = useFormAction<{ avatar: string }>("/user/avatar", "Hochladen fehlgeschlagen.", (form) => {
|
||||
const header = useFormAction("/user/header", "Hochladen fehlgeschlagen.", (form) => {
|
||||
form.reset();
|
||||
onAvatar();
|
||||
refresh();
|
||||
refreshProfile();
|
||||
});
|
||||
const rename = useFormAction<{ username: string }>("/user/rename", "Umbenennen fehlgeschlagen.", async (_form, data) => {
|
||||
await refresh();
|
||||
navigate(`/u/${encodeURIComponent(data.username)}`);
|
||||
});
|
||||
const del = useFormAction("/user/delete", "Löschen fehlgeschlagen.", async () => {
|
||||
await refresh();
|
||||
navigate("/");
|
||||
const removeHeader = useFormAction("/user/header/remove", "Entfernen fehlgeschlagen.", refreshProfile);
|
||||
const avatar = useFormAction("/user/avatar", "Hochladen fehlgeschlagen.", (form) => {
|
||||
form.reset();
|
||||
refreshAvatar();
|
||||
});
|
||||
const saveBio = useFormAction("/user/bio", "Speichern fehlgeschlagen.", refreshProfile);
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2>Einstellungen</h2>
|
||||
<details>
|
||||
<summary>Profilbild ändern</summary>
|
||||
<form id="avatar-form" onSubmit={avatar.onSubmit}>
|
||||
<input name="avatar" type="file" accept="image/*" />{" "}
|
||||
<button type="submit" disabled={avatar.busy}>Bild hochladen</button>
|
||||
<div className="profile-edit">
|
||||
<h3>Header-Bild</h3>
|
||||
<form id="header-form" onSubmit={header.onSubmit}>
|
||||
<input name="header" type="file" accept="image/*" />{" "}
|
||||
<button type="submit" disabled={header.busy}>Bild hochladen</button>
|
||||
</form>
|
||||
{info.header && (
|
||||
<form onSubmit={removeHeader.onSubmit}>
|
||||
<button type="submit" disabled={removeHeader.busy}>Header-Bild entfernen</button>
|
||||
</form>
|
||||
<p className="msg" id="avatar-msg">{avatar.msg}</p>
|
||||
</details>
|
||||
<details>
|
||||
<summary>Name ändern</summary>
|
||||
<form id="rename-form" onSubmit={rename.onSubmit}>
|
||||
<input name="user" type="text" placeholder="Neuer Nutzername" autoComplete="username" />{" "}
|
||||
<button type="submit" disabled={rename.busy}>Namen ändern</button>
|
||||
</form>
|
||||
<p className="msg" id="rename-msg">{rename.msg}</p>
|
||||
</details>
|
||||
<details>
|
||||
<summary>Konto löschen</summary>
|
||||
<form
|
||||
id="delete-form"
|
||||
onSubmit={(e) => {
|
||||
if (confirm("Konto wirklich unwiderruflich löschen?")) del.onSubmit(e);
|
||||
else e.preventDefault();
|
||||
}}
|
||||
>
|
||||
<p className="muted">Löscht dein Konto samt Beiträgen und Votes unwiderruflich.</p>{" "}
|
||||
<input name="pass1" type="password" placeholder="Passwort bestätigen" autoComplete="current-password" />{" "}
|
||||
<button type="submit" disabled={del.busy}>Konto endgültig löschen</button>
|
||||
</form>
|
||||
<p className="msg" id="delete-msg">{del.msg}</p>
|
||||
</details>
|
||||
</>
|
||||
)}
|
||||
<p className="msg">{header.msg || removeHeader.msg}</p>
|
||||
|
||||
<h3>Profilbild</h3>
|
||||
<form id="avatar-form" onSubmit={avatar.onSubmit}>
|
||||
<input name="avatar" type="file" accept="image/*" />{" "}
|
||||
<button type="submit" disabled={avatar.busy}>Bild hochladen</button>
|
||||
</form>
|
||||
<p className="msg" id="avatar-msg">{avatar.msg}</p>
|
||||
|
||||
<h3>Beschreibung</h3>
|
||||
<form id="bio-form" onSubmit={saveBio.onSubmit}>
|
||||
<textarea name="bio" rows={4} maxLength={maxBio} value={bio} onChange={(e) => setBio(e.target.value)} />
|
||||
<div className="row">
|
||||
<button type="submit" disabled={saveBio.busy}>Speichern</button>
|
||||
{saveBio.ok && bio === info.bio && <span className="muted">Gespeichert.</span>}
|
||||
<span className="muted counter">{[...bio].length}/{maxBio}</span>
|
||||
</div>
|
||||
</form>
|
||||
<p className="msg">{saveBio.msg}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user