import { useState } from "react"; import { Link, useParams } from "react-router"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { api, errMsg } from "../api"; import { useAuthChanged, useMe } from "../auth"; import { avatarUrl, linkify } from "../format"; import { useUserKey } from "../useKey"; 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: 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(`/u/${encodeURIComponent(username)}/info`), }); const me = useMe().data; const [editing, setEditing] = useState(false); const info = profile.data; let name = ""; if (profile.isError) name = errMsg(profile.error, "Profil konnte nicht geladen werden."); else if (info) name = info.username; const isOwner = !!me && !!info && me.username === info.username; return (
{info?.header && }
{isOwner && ( )} {me && info && !isOwner && }

{name}

{info?.bio &&

{linkify(info.bio)}

}

{info && `Mitglied seit ${new Date(info.created_at * 1000).toLocaleDateString("de-DE")} · ` + `${info.entries} ${info.entries === 1 ? "Beitrag" : "Beiträge"}`}

{isOwner && editing && info && }
{info && ( `/u/${encodeURIComponent(username)}/feed/${page}`} /> )}
); } // Contact führt zum Chat mit dem Profil -- nur, wenn beide einen Schlüssel // haben; wer selbst keinen hat, sieht den Knopf gar nicht. function Contact({ me, other }: { me: string; other: string }) { const own = useUserKey(me); const theirs = useUserKey(other); if (!own.data?.key || !theirs.data?.key) return null; return ( Ansprechen ); } // ProfileEdit: Header-Bild, Profilbild und Beschreibung, nur für den // Eigentümer. function ProfileEdit({ info }: { info: Profile }) { const qc = useQueryClient(); const authChanged = useAuthChanged(); const [bio, setBio] = useState(info.bio); 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"] }); refreshProfile(); return authChanged(); }; const header = useFormAction("/user/header", "Hochladen fehlgeschlagen.", (form) => { form.reset(); refreshProfile(); }); 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 (

Header-Bild

{" "}
{info.header && (
)}

{header.msg || removeHeader.msg}

Profilbild

{" "}

{avatar.msg}

Beschreibung