198 lines
6.1 KiB
TypeScript
198 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
import { useRef, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import Image from "next/image";
|
|
import { useAuth } from "@/components/auth/auth-provider";
|
|
import { auth, AuthError, type User } from "@/lib/auth";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
/**
|
|
* Profile edit form. Two responsibilities:
|
|
*
|
|
* 1. Inline name edit (debounced save on form submit).
|
|
* 2. Avatar upload via R2 presigned PUT:
|
|
* click [Change] → presign → PUT to R2 → PATCH /users/me with new URL.
|
|
*
|
|
* The new avatar URL appended with a `?v=<random>` cache-buster forces the
|
|
* browser to fetch the new image even if the key happened to collide
|
|
* (extremely unlikely given our random suffix, but cheap insurance).
|
|
*/
|
|
export function ProfileForm({ user }: { user: User }) {
|
|
const router = useRouter();
|
|
const { logOut, setUser } = useAuth();
|
|
const fileInput = useRef<HTMLInputElement>(null);
|
|
|
|
const [name, setName] = useState(user.name);
|
|
const [avatarUrl, setAvatarUrl] = useState<string | null>(user.avatar_url);
|
|
const [saving, setSaving] = useState(false);
|
|
const [uploading, setUploading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [success, setSuccess] = useState<string | null>(null);
|
|
|
|
async function onSave(e: React.SyntheticEvent) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setSuccess(null);
|
|
setSaving(true);
|
|
try {
|
|
const updated = await auth.updateProfile(name);
|
|
setUser(updated);
|
|
setSuccess("Profile saved.");
|
|
} catch (err) {
|
|
setError(err instanceof AuthError ? err.message : "Could not save.");
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
async function onAvatarChange(e: React.ChangeEvent<HTMLInputElement>) {
|
|
const file = e.target.files?.[0];
|
|
if (!file) return;
|
|
setError(null);
|
|
setSuccess(null);
|
|
setUploading(true);
|
|
try {
|
|
const publicURL = await auth.uploadAvatar(file);
|
|
const updated = await auth.updateProfile(undefined, publicURL);
|
|
setUser(updated);
|
|
setAvatarUrl(updated.avatar_url);
|
|
setSuccess("Avatar updated.");
|
|
} catch (err) {
|
|
setError(err instanceof AuthError ? err.message : "Upload failed.");
|
|
} finally {
|
|
setUploading(false);
|
|
if (fileInput.current) fileInput.current.value = "";
|
|
}
|
|
}
|
|
|
|
async function onRemoveAvatar() {
|
|
setError(null);
|
|
setUploading(true);
|
|
try {
|
|
const updated = await auth.updateProfile(undefined, "");
|
|
setUser(updated);
|
|
setAvatarUrl(null);
|
|
} catch (err) {
|
|
setError(err instanceof AuthError ? err.message : "Could not remove avatar.");
|
|
} finally {
|
|
setUploading(false);
|
|
}
|
|
}
|
|
|
|
async function onLogout() {
|
|
await logOut();
|
|
router.push("/");
|
|
router.refresh();
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-8">
|
|
<section className="space-y-4">
|
|
<h2 className="font-heading text-lg font-medium">Avatar</h2>
|
|
<div className="flex items-center gap-4">
|
|
<AvatarPreview src={avatarUrl} name={user.name} />
|
|
<div className="flex flex-col gap-2">
|
|
<input
|
|
ref={fileInput}
|
|
type="file"
|
|
accept="image/png,image/jpeg,image/webp,image/gif"
|
|
hidden
|
|
onChange={onAvatarChange}
|
|
/>
|
|
<Button
|
|
variant="outline"
|
|
type="button"
|
|
onClick={() => fileInput.current?.click()}
|
|
disabled={uploading}
|
|
className="h-11 px-5 text-base"
|
|
>
|
|
{uploading ? "Uploading…" : avatarUrl ? "Change avatar" : "Upload avatar"}
|
|
</Button>
|
|
{avatarUrl ? (
|
|
<Button
|
|
variant="ghost"
|
|
type="button"
|
|
onClick={onRemoveAvatar}
|
|
disabled={uploading}
|
|
className="h-11 px-5 text-base"
|
|
>
|
|
Remove
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<form onSubmit={onSave} className="space-y-4">
|
|
<h2 className="font-heading text-lg font-medium">Details</h2>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="email">Email</Label>
|
|
<Input id="email" value={user.email} disabled />
|
|
<p className="text-xs text-muted-foreground">
|
|
Email changes aren't supported yet — drop us a note if you need this.
|
|
</p>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="name">Display name</Label>
|
|
<Input
|
|
id="name"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
maxLength={120}
|
|
required
|
|
/>
|
|
</div>
|
|
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
|
{success ? <p className="text-sm text-emerald-600">{success}</p> : null}
|
|
<div className="flex justify-between items-center pt-2">
|
|
<Button
|
|
type="submit"
|
|
disabled={saving || name === user.name}
|
|
className="h-11 px-6 text-base"
|
|
>
|
|
{saving ? "Saving…" : "Save changes"}
|
|
</Button>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
onClick={onLogout}
|
|
className="h-11 px-5 text-base"
|
|
>
|
|
Log out
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function AvatarPreview({ src, name }: { src: string | null; name: string }) {
|
|
if (src) {
|
|
return (
|
|
<Image
|
|
src={src}
|
|
alt={name}
|
|
width={80}
|
|
height={80}
|
|
className="size-20 rounded-full object-cover ring-1 ring-foreground/10"
|
|
unoptimized
|
|
/>
|
|
);
|
|
}
|
|
const initials =
|
|
name
|
|
.split(/\s+/)
|
|
.map((p) => p[0])
|
|
.slice(0, 2)
|
|
.join("")
|
|
.toUpperCase() || "?";
|
|
return (
|
|
<div className="size-20 rounded-full bg-muted flex items-center justify-center text-2xl font-medium text-muted-foreground ring-1 ring-foreground/10">
|
|
{initials}
|
|
</div>
|
|
);
|
|
}
|