Passt zu den übrigen Aktionen der Seite; der erklärende Zusatz steht ohnehin auf der Schlüsselseite selbst. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TiXsPUqw7oeomZ8wZrQW5q
116 lines
4.0 KiB
TypeScript
116 lines
4.0 KiB
TypeScript
import { useState } from "react";
|
|
import { Link, useNavigate } from "react-router";
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
import { useAuthChanged, useMe } from "../auth";
|
|
import { useFormAction } from "../useFormAction";
|
|
import { AuthBox } from "../components/AuthBox";
|
|
import { loadTheme, saveTheme, type Theme } from "../theme";
|
|
|
|
// SettingsPage: Darstellung (auch ohne Konto) und Kontoeinstellungen (Name,
|
|
// Passwort, Löschen). Profilbild, Header und Beschreibung bearbeitet man auf
|
|
// dem eigenen Profil.
|
|
export function SettingsPage() {
|
|
const me = useMe();
|
|
return (
|
|
<>
|
|
<section id="settings">
|
|
<h2>Einstellungen</h2>
|
|
<Appearance />
|
|
{me.data && <AccountSettings />}
|
|
</section>
|
|
{me.isSuccess && !me.data && <AuthBox />}
|
|
</>
|
|
);
|
|
}
|
|
|
|
const themes: { value: Theme; label: string }[] = [
|
|
{ value: "light", label: "Hell" },
|
|
{ value: "dark", label: "Dunkel (experimentell)" },
|
|
];
|
|
|
|
function Appearance() {
|
|
const [theme, setTheme] = useState(loadTheme);
|
|
const pick = (t: Theme) => {
|
|
saveTheme(t);
|
|
setTheme(t);
|
|
};
|
|
return (
|
|
<>
|
|
<h3>Darstellung</h3>
|
|
<div className="row" role="group" aria-label="Darstellung">
|
|
{themes.map((t) => (
|
|
<button key={t.value} aria-pressed={theme === t.value} onClick={() => pick(t.value)}>
|
|
{t.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
function AccountSettings() {
|
|
const qc = useQueryClient();
|
|
const navigate = useNavigate();
|
|
const authChanged = useAuthChanged();
|
|
|
|
// Der neue Name taucht auch in den Beitragskarten auf -> Feeds, Profile und
|
|
// das eigene Konto neu laden.
|
|
const refresh = () => {
|
|
qc.invalidateQueries({ queryKey: ["feed"] });
|
|
qc.invalidateQueries({ queryKey: ["thread"] });
|
|
qc.invalidateQueries({ queryKey: ["profile"] });
|
|
return authChanged();
|
|
};
|
|
|
|
const rename = useFormAction<{ username: string }>("/user/rename", "Umbenennen fehlgeschlagen.", async (form) => {
|
|
form.reset();
|
|
await refresh();
|
|
});
|
|
const password = useFormAction("/user/password", "Ändern fehlgeschlagen.", (form) => form.reset());
|
|
const del = useFormAction("/user/delete", "Löschen fehlgeschlagen.", async () => {
|
|
await refresh();
|
|
navigate("/");
|
|
});
|
|
|
|
return (
|
|
<>
|
|
<h3>Name ändern</h3>
|
|
<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>
|
|
{rename.ok && <p className="muted">Name geändert.</p>}
|
|
|
|
<h3>Passwort ändern</h3>
|
|
<form id="password-form" onSubmit={password.onSubmit}>
|
|
<input name="pass" type="password" placeholder="Bisheriges Passwort" autoComplete="current-password" />{" "}
|
|
<input name="pass1" type="password" placeholder="Neues Passwort" autoComplete="new-password" />{" "}
|
|
<input name="pass2" type="password" placeholder="Neues Passwort wiederholen" autoComplete="new-password" />{" "}
|
|
<button type="submit" disabled={password.busy}>Passwort ändern</button>
|
|
</form>
|
|
<p className="msg" id="password-msg">{password.msg}</p>
|
|
{password.ok && <p className="muted">Passwort geändert. Andere Geräte sind abgemeldet.</p>}
|
|
|
|
<h3>Schlüssel</h3>
|
|
<p>
|
|
<Link className="button" to="/settings/keys">Schlüssel verwalten</Link>
|
|
</p>
|
|
|
|
<h3>Konto löschen</h3>
|
|
<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>
|
|
</>
|
|
);
|
|
}
|