Files
gatehouse-ui/src/pages/user/ProfilePage.tsx
T

257 lines
8.8 KiB
TypeScript
Raw Normal View History

2026-01-06 16:06:53 +00:00
import { useState, useEffect } from "react";
2026-03-01 16:50:19 +05:45
import { Mail, Upload, CheckCircle, AlertCircle, Loader2, Bell } from "lucide-react";
2026-01-06 14:46:23 +00:00
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
2026-01-07 14:38:47 +00:00
import { Skeleton } from "@/components/ui/skeleton";
2026-01-06 16:06:53 +00:00
import { useAuth } from "@/contexts/AuthContext";
2026-03-01 16:50:19 +05:45
import { api, ApiError, PendingInvite } from "@/lib/api";
2026-01-06 16:06:53 +00:00
import { toast } from "@/hooks/use-toast";
2026-01-06 14:46:23 +00:00
2026-01-07 14:38:47 +00:00
function ProfileSkeleton() {
return (
<div className="page-container">
<div className="page-header">
<Skeleton className="h-8 w-24" />
<Skeleton className="h-4 w-80 mt-2" />
</div>
<div className="space-y-6">
{/* Personal Information Skeleton */}
<Card>
<CardHeader>
<Skeleton className="h-5 w-40" />
<Skeleton className="h-4 w-56 mt-1" />
</CardHeader>
<CardContent className="space-y-6">
<div className="flex items-center gap-6">
<Skeleton className="w-20 h-20 rounded-full" />
<div>
<Skeleton className="h-9 w-32" />
<Skeleton className="h-3 w-40 mt-2" />
</div>
</div>
<div className="space-y-2">
<Skeleton className="h-4 w-20" />
<Skeleton className="h-12 w-full" />
</div>
</CardContent>
</Card>
{/* Email Skeleton */}
<Card>
<CardHeader>
<Skeleton className="h-5 w-32" />
<Skeleton className="h-4 w-64 mt-1" />
</CardHeader>
<CardContent>
<Skeleton className="h-12 w-full" />
</CardContent>
</Card>
</div>
</div>
);
}
2026-01-06 14:46:23 +00:00
export default function ProfilePage() {
2026-01-07 14:38:47 +00:00
const { user, isLoading: authLoading, refreshUser } = useAuth();
2026-01-06 16:06:53 +00:00
const [name, setName] = useState("");
2026-01-06 14:46:23 +00:00
const [isEditing, setIsEditing] = useState(false);
2026-01-06 16:06:53 +00:00
const [isSaving, setIsSaving] = useState(false);
2026-03-01 16:50:19 +05:45
const [pendingInvites, setPendingInvites] = useState<PendingInvite[]>([]);
2026-01-06 14:46:23 +00:00
2026-01-06 16:06:53 +00:00
// Sync local name state with user data
useEffect(() => {
if (user?.full_name) {
setName(user.full_name);
}
}, [user?.full_name]);
2026-03-01 16:50:19 +05:45
// Fetch pending invitations for this user
2026-01-06 16:06:53 +00:00
useEffect(() => {
2026-03-01 16:50:19 +05:45
if (!user) return;
api.users.getMyInvites()
.then((res) => setPendingInvites(res.invites ?? []))
.catch(() => { /* silently ignore */ });
}, [user]);
2026-01-06 16:06:53 +00:00
const getInitials = (fullName: string | null) => {
if (!fullName) return "?";
return fullName
.split(" ")
.map((n) => n[0])
.join("")
.toUpperCase()
.slice(0, 2);
2026-01-06 14:46:23 +00:00
};
2026-01-06 16:06:53 +00:00
const handleSave = async () => {
if (!name.trim()) {
toast({
title: "Name required",
description: "Please enter your full name",
variant: "destructive",
});
return;
}
setIsSaving(true);
try {
await api.users.updateMe({ full_name: name.trim() });
await refreshUser();
setIsEditing(false);
toast({
title: "Profile updated",
description: "Your name has been updated successfully",
});
} catch (error) {
if (error instanceof ApiError) {
toast({
title: "Update failed",
description: error.message,
variant: "destructive",
});
}
} finally {
setIsSaving(false);
}
};
const handleCancel = () => {
setName(user?.full_name || "");
2026-01-06 14:46:23 +00:00
setIsEditing(false);
};
2026-01-07 14:38:47 +00:00
if (authLoading || !user) {
return <ProfileSkeleton />;
2026-01-06 16:06:53 +00:00
}
2026-01-06 14:46:23 +00:00
return (
<div className="page-container">
<div className="page-header">
<h1 className="page-title">Profile</h1>
<p className="page-description">
2026-03-01 16:50:19 +05:45
Manage your personal information and account settings
2026-01-06 14:46:23 +00:00
</p>
</div>
<div className="space-y-6">
2026-03-01 16:50:19 +05:45
{/* Pending Invitations Banner */}
{pendingInvites.length > 0 && (
<div className="rounded-lg border border-primary/40 bg-primary/10 p-4 space-y-3">
<div className="flex items-center gap-2 text-primary font-semibold text-sm">
<Bell className="w-4 h-4" />
You have {pendingInvites.length} pending invitation{pendingInvites.length > 1 ? "s" : ""}
</div>
{pendingInvites.map((invite) => (
<div
key={invite.token}
className="flex items-center justify-between rounded-md border border-border bg-card px-4 py-3"
>
<div>
<p className="text-sm font-medium text-foreground">{invite.organization.name}</p>
<p className="text-xs text-muted-foreground capitalize">
Invited as <span className="font-medium">{invite.role}</span>
</p>
</div>
<a
href={`/invite?token=${invite.token}`}
className="inline-flex items-center gap-1 rounded-md bg-primary px-3 py-1.5 text-xs font-semibold text-primary-foreground hover:bg-primary/90 transition-colors"
>
Accept
</a>
</div>
))}
</div>
)}
2026-01-06 14:46:23 +00:00
{/* Profile Photo & Name */}
<Card>
<CardHeader>
<CardTitle className="text-base">Personal Information</CardTitle>
<CardDescription>Update your photo and personal details</CardDescription>
</CardHeader>
<CardContent className="space-y-6">
{/* Avatar */}
<div className="flex items-center gap-6">
<Avatar className="w-20 h-20">
2026-01-06 16:06:53 +00:00
<AvatarImage src={user.avatar_url || undefined} />
2026-01-06 14:46:23 +00:00
<AvatarFallback className="bg-primary text-primary-foreground text-xl">
2026-01-06 16:06:53 +00:00
{getInitials(user.full_name)}
2026-01-06 14:46:23 +00:00
</AvatarFallback>
</Avatar>
<div>
<Button variant="outline" size="sm">
<Upload className="w-4 h-4 mr-2" />
Change photo
</Button>
<p className="text-xs text-muted-foreground mt-2">
JPG, PNG or GIF. Max 2MB.
</p>
</div>
</div>
{/* Name */}
<div className="space-y-2">
<Label htmlFor="name">Full name</Label>
{isEditing ? (
<div className="flex gap-2">
<Input
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
2026-01-06 16:06:53 +00:00
disabled={isSaving}
2026-01-06 14:46:23 +00:00
/>
2026-01-06 16:06:53 +00:00
<Button onClick={handleSave} disabled={isSaving}>
{isSaving && <Loader2 className="w-4 h-4 mr-2 animate-spin" />}
Save
</Button>
<Button variant="outline" onClick={handleCancel} disabled={isSaving}>
2026-01-06 14:46:23 +00:00
Cancel
</Button>
</div>
) : (
<div className="flex items-center justify-between p-3 border rounded-lg bg-secondary/30">
2026-01-06 16:06:53 +00:00
<span className="text-foreground">{user.full_name || "Not set"}</span>
2026-01-06 14:46:23 +00:00
<Button variant="ghost" size="sm" onClick={() => setIsEditing(true)}>
Edit
</Button>
</div>
)}
</div>
</CardContent>
</Card>
{/* Email */}
<Card>
<CardHeader>
<CardTitle className="text-base">Email Address</CardTitle>
<CardDescription>Your email is used for login and notifications</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center justify-between p-3 border rounded-lg bg-secondary/30">
<div className="flex items-center gap-3">
<Mail className="w-4 h-4 text-muted-foreground" />
<span className="text-foreground">{user.email}</span>
2026-01-06 16:06:53 +00:00
{user.email_verified ? (
2026-01-06 14:46:23 +00:00
<Badge variant="secondary" className="bg-success/10 text-success border-0">
<CheckCircle className="w-3 h-3 mr-1" />
Verified
</Badge>
) : (
<Badge variant="secondary" className="bg-warning/10 text-warning border-0">
<AlertCircle className="w-3 h-3 mr-1" />
Unverified
</Badge>
)}
</div>
</div>
</CardContent>
</Card>
</div>
</div>
);
}