feat: animate sidebar and refine account menu with Google avatars
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
"use client";
|
||||
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { api, errorText, type Profile } from "@/lib/api";
|
||||
import type { Workspace } from "@/lib/workspace";
|
||||
import { UserAvatar } from "./user-avatar";
|
||||
import styles from "./workspace.module.css";
|
||||
|
||||
export function AccountPanel({ mode, profile, demo, onClose, onProfile, onWorkspace }: {
|
||||
mode: "settings" | "workspace";
|
||||
profile: Profile;
|
||||
demo: boolean;
|
||||
onClose: () => void;
|
||||
onProfile: (profile: Profile) => void;
|
||||
onWorkspace: (workspace: Workspace) => void;
|
||||
}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const settings = mode === "settings";
|
||||
const name = profile.display_name || profile.username;
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (busy) return;
|
||||
const data = new FormData(event.currentTarget);
|
||||
setBusy(true);
|
||||
setError("");
|
||||
try {
|
||||
if (settings) {
|
||||
const input = { display_name: String(data.get("display_name")).trim(), username: String(data.get("username")).trim() };
|
||||
const updated = demo ? { ...profile, ...input } : await api<Profile>("profile", { method: "PATCH", body: JSON.stringify(input) });
|
||||
onProfile(updated);
|
||||
} else {
|
||||
const name = String(data.get("name")).trim();
|
||||
const created = demo ? { id: crypto.randomUUID(), name, role: "admin" as const } : await api<Workspace>("workspaces", { method: "POST", body: JSON.stringify({ name }) });
|
||||
onWorkspace(created);
|
||||
}
|
||||
onClose();
|
||||
} catch (e) { setError(errorText(e)); }
|
||||
finally { setBusy(false); }
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={(open) => { if (!open && !busy) onClose(); }}>
|
||||
<DialogContent className={styles.settingsPanel}>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{settings ? "账户设置" : "新建工作空间"}</DialogTitle>
|
||||
<DialogDescription>{settings ? "管理你的个人资料。" : "为相关的图谱和资料创建一个空间。"}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<form className={styles.settingsForm} onSubmit={submit}>
|
||||
{settings ? (
|
||||
<>
|
||||
<div className={styles.settingsIdentity}><UserAvatar name={name} url={profile.avatar_url} large /><span>{profile.email || profile.username}</span></div>
|
||||
<label>显示名称<input name="display_name" defaultValue={profile.display_name || ""} maxLength={80} /></label>
|
||||
<label>用户名<input name="username" defaultValue={profile.username} pattern="[a-z0-9][a-z0-9_-]{2,39}" minLength={3} maxLength={40} required /></label>
|
||||
</>
|
||||
) : <label>空间名称<input name="name" placeholder="例如:我的创作空间" maxLength={50} required autoFocus /></label>}
|
||||
{error && <p className={styles.error} role="alert">{error}</p>}
|
||||
<div className={styles.settingsActions}>
|
||||
<button type="button" onClick={onClose} disabled={busy}>取消</button>
|
||||
<button type="submit" disabled={busy}>{busy ? "正在保存…" : settings ? "保存" : "创建空间"}</button>
|
||||
</div>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user