Files
mtusi2026lab3/frontend/src/components/ProfileDrawer.tsx
T

124 lines
3.5 KiB
TypeScript

import {
CalendarDays,
Check,
Copy,
LogOut,
ShieldCheck,
UserRound,
X,
} from "lucide-react";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import type { User } from "../api/types";
import { useAuth } from "../auth/AuthProvider";
import { Avatar } from "./Avatar";
type ProfileDrawerProps = {
user: User;
};
const createdAtFormatter = new Intl.DateTimeFormat("ru-RU", {
day: "numeric",
month: "long",
year: "numeric",
});
export function ProfileDrawer({ user }: ProfileDrawerProps) {
const { logout } = useAuth();
const navigate = useNavigate();
const [copied, setCopied] = useState(false);
const [error, setError] = useState("");
const [isLoggingOut, setIsLoggingOut] = useState(false);
async function copyID() {
await navigator.clipboard.writeText(user.id);
setCopied(true);
window.setTimeout(() => setCopied(false), 1800);
}
async function submitLogout() {
setError("");
setIsLoggingOut(true);
try {
await logout();
navigate("/login", { replace: true });
} catch {
setError("Не удалось завершить сессию. Попробуйте ещё раз.");
setIsLoggingOut(false);
}
}
return (
<>
<button
aria-label="Закрыть профиль"
className="drawer-backdrop"
onClick={() => navigate("/app")}
type="button"
/>
<aside className="profile-drawer" aria-label="Профиль пользователя">
<header className="profile-drawer__header">
<p className="eyebrow">Ваш аккаунт</p>
<button
aria-label="Закрыть"
className="soft-icon-button"
onClick={() => navigate("/app")}
type="button"
>
<X aria-hidden="true" />
</button>
</header>
<div className="profile-identity">
<Avatar label={user.display_name} seed={user.id} size="large" />
<h2>{user.display_name}</h2>
<p>@{user.username}</p>
<span className="profile-badge">
<ShieldCheck aria-hidden="true" />
серверная сессия
</span>
</div>
<dl className="profile-details">
<div>
<dt><UserRound aria-hidden="true" /> ID аккаунта</dt>
<dd>
<code>{user.id}</code>
<button
aria-label="Скопировать ID"
className="copy-button"
onClick={copyID}
type="button"
>
{copied ? <Check /> : <Copy />}
</button>
</dd>
</div>
<div>
<dt><CalendarDays aria-hidden="true" /> Дата регистрации</dt>
<dd>{createdAtFormatter.format(new Date(user.created_at))}</dd>
</div>
</dl>
<div className="profile-roadmap">
<strong>Следующие версии</strong>
<p>Аватар, редактирование имени и смена пароля появятся после API-контрактов.</p>
</div>
{error && <div className="form-error" role="alert">{error}</div>}
<button
className="logout-button"
disabled={isLoggingOut}
onClick={submitLogout}
type="button"
>
<LogOut aria-hidden="true" />
{isLoggingOut ? "Завершаем…" : "Выйти из аккаунта"}
</button>
</aside>
</>
);
}