345 lines
12 KiB
TypeScript
345 lines
12 KiB
TypeScript
import { Card } from '@/ui/Card';
|
||
import StatusBadge from '@/components/ui/StatusBadge';
|
||
import { Input } from '@/ui/Input';
|
||
import { Select } from '@/ui/Select';
|
||
import { Table, TableHead, TableBody, TableRow, TableHeader, TableCell } from '@/ui/Table';
|
||
import { Button } from '@/ui/Button';
|
||
import { Text } from '@/ui/Text';
|
||
import {
|
||
Search,
|
||
Filter,
|
||
RefreshCw,
|
||
Users,
|
||
Shield,
|
||
Trash2,
|
||
AlertTriangle
|
||
} from 'lucide-react';
|
||
import { AdminUsersViewData } from '@/lib/view-data/AdminUsersViewData';
|
||
|
||
/**
|
||
* AdminUsersTemplate
|
||
*
|
||
* Pure template for admin users page.
|
||
* Accepts ViewData only, no business logic.
|
||
*/
|
||
export function AdminUsersTemplate(props: {
|
||
adminUsersViewData: AdminUsersViewData;
|
||
onRefresh: () => void;
|
||
onSearch: (search: string) => void;
|
||
onFilterRole: (role: string) => void;
|
||
onFilterStatus: (status: string) => void;
|
||
onClearFilters: () => void;
|
||
onUpdateStatus: (userId: string, status: string) => void;
|
||
onDeleteUser: (userId: string) => void;
|
||
search: string;
|
||
roleFilter: string;
|
||
statusFilter: string;
|
||
loading: boolean;
|
||
error: string | null;
|
||
deletingUser: string | null;
|
||
}) {
|
||
const {
|
||
adminUsersViewData: viewData,
|
||
onRefresh,
|
||
onSearch,
|
||
onFilterRole,
|
||
onFilterStatus,
|
||
onClearFilters,
|
||
onUpdateStatus,
|
||
onDeleteUser,
|
||
search,
|
||
roleFilter,
|
||
statusFilter,
|
||
loading,
|
||
error,
|
||
deletingUser
|
||
} = props;
|
||
|
||
const toStatusBadgeProps = (
|
||
status: string,
|
||
): { status: 'success' | 'warning' | 'error' | 'neutral'; label: string } => {
|
||
switch (status) {
|
||
case 'active':
|
||
return { status: 'success', label: 'Active' };
|
||
case 'suspended':
|
||
return { status: 'warning', label: 'Suspended' };
|
||
case 'deleted':
|
||
return { status: 'error', label: 'Deleted' };
|
||
default:
|
||
return { status: 'neutral', label: status };
|
||
}
|
||
};
|
||
|
||
const getRoleBadgeClass = (role: string) => {
|
||
switch (role) {
|
||
case 'owner':
|
||
return 'bg-purple-500/20 text-purple-300 border border-purple-500/30';
|
||
case 'admin':
|
||
return 'bg-blue-500/20 text-blue-300 border border-blue-500/30';
|
||
default:
|
||
return 'bg-gray-500/20 text-gray-300 border border-gray-500/30';
|
||
}
|
||
};
|
||
|
||
const getRoleBadgeLabel = (role: string) => {
|
||
switch (role) {
|
||
case 'owner':
|
||
return 'Owner';
|
||
case 'admin':
|
||
return 'Admin';
|
||
case 'user':
|
||
return 'User';
|
||
default:
|
||
return role;
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="container mx-auto p-6 space-y-6">
|
||
{/* Header */}
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<Text size="2xl" weight="bold" color="text-white">User Management</Text>
|
||
<Text size="sm" color="text-gray-400" className="mt-1">Manage and monitor all system users</Text>
|
||
</div>
|
||
<Button
|
||
onClick={onRefresh}
|
||
disabled={loading}
|
||
variant="secondary"
|
||
className="flex items-center gap-2"
|
||
>
|
||
<RefreshCw className={`w-4 h-4 ${loading ? 'animate-spin' : ''}`} />
|
||
Refresh
|
||
</Button>
|
||
</div>
|
||
|
||
{/* Error Banner */}
|
||
{error && (
|
||
<div className="bg-racing-red/10 border border-racing-red text-racing-red px-4 py-3 rounded-lg flex items-start gap-3">
|
||
<AlertTriangle className="w-5 h-5 mt-0.5 flex-shrink-0" />
|
||
<div className="flex-1">
|
||
<Text weight="medium">Error</Text>
|
||
<Text size="sm" className="opacity-90">{error}</Text>
|
||
</div>
|
||
<Button
|
||
onClick={() => {}}
|
||
variant="secondary"
|
||
className="text-racing-red hover:opacity-70 p-0"
|
||
>
|
||
×
|
||
</Button>
|
||
</div>
|
||
)}
|
||
|
||
{/* Filters Card */}
|
||
<Card>
|
||
<div className="space-y-4">
|
||
<div className="flex items-center justify-between">
|
||
<div className="flex items-center gap-2">
|
||
<Filter className="w-4 h-4 text-gray-400" />
|
||
<Text weight="medium" color="text-white">Filters</Text>
|
||
</div>
|
||
{(search || roleFilter || statusFilter) && (
|
||
<Button
|
||
onClick={onClearFilters}
|
||
variant="secondary"
|
||
className="text-xs p-0"
|
||
>
|
||
Clear all
|
||
</Button>
|
||
)}
|
||
</div>
|
||
|
||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||
<div className="relative">
|
||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
||
<Input
|
||
type="text"
|
||
placeholder="Search by email or name..."
|
||
value={search}
|
||
onChange={(e) => onSearch(e.target.value)}
|
||
className="pl-9"
|
||
/>
|
||
</div>
|
||
|
||
<Select
|
||
value={roleFilter}
|
||
onChange={(e) => onFilterRole(e.target.value)}
|
||
options={[
|
||
{ value: '', label: 'All Roles' },
|
||
{ value: 'owner', label: 'Owner' },
|
||
{ value: 'admin', label: 'Admin' },
|
||
{ value: 'user', label: 'User' },
|
||
]}
|
||
/>
|
||
|
||
<Select
|
||
value={statusFilter}
|
||
onChange={(e) => onFilterStatus(e.target.value)}
|
||
options={[
|
||
{ value: '', label: 'All Status' },
|
||
{ value: 'active', label: 'Active' },
|
||
{ value: 'suspended', label: 'Suspended' },
|
||
{ value: 'deleted', label: 'Deleted' },
|
||
]}
|
||
/>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
|
||
{/* Users Table */}
|
||
<Card>
|
||
{loading ? (
|
||
<div className="flex flex-col items-center justify-center py-12 space-y-3">
|
||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-blue"></div>
|
||
<Text color="text-gray-400">Loading users...</Text>
|
||
</div>
|
||
) : !viewData.users || viewData.users.length === 0 ? (
|
||
<div className="flex flex-col items-center justify-center py-12 space-y-3">
|
||
<Users className="w-12 h-12 text-gray-600" />
|
||
<Text color="text-gray-400">No users found</Text>
|
||
<Button
|
||
onClick={onClearFilters}
|
||
variant="secondary"
|
||
className="text-sm p-0"
|
||
>
|
||
Clear filters
|
||
</Button>
|
||
</div>
|
||
) : (
|
||
<Table>
|
||
<TableHead>
|
||
<TableRow>
|
||
<TableHeader>User</TableHeader>
|
||
<TableHeader>Email</TableHeader>
|
||
<TableHeader>Roles</TableHeader>
|
||
<TableHeader>Status</TableHeader>
|
||
<TableHeader>Last Login</TableHeader>
|
||
<TableHeader>Actions</TableHeader>
|
||
</TableRow>
|
||
</TableHead>
|
||
<TableBody>
|
||
{viewData.users.map((user, index: number) => (
|
||
<TableRow
|
||
key={user.id}
|
||
className={index % 2 === 0 ? 'bg-transparent' : 'bg-iron-gray/10'}
|
||
>
|
||
<TableCell>
|
||
<div className="flex items-center gap-3">
|
||
<div className="w-8 h-8 rounded-full bg-primary-blue/20 flex items-center justify-center">
|
||
<Shield className="w-4 h-4 text-primary-blue" />
|
||
</div>
|
||
<div>
|
||
<div className="font-medium text-white">{user.displayName}</div>
|
||
<div className="text-xs text-gray-500">ID: {user.id}</div>
|
||
{user.primaryDriverId && (
|
||
<div className="text-xs text-gray-500">Driver: {user.primaryDriverId}</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</TableCell>
|
||
<TableCell>
|
||
<div className="text-sm text-gray-300">{user.email}</div>
|
||
</TableCell>
|
||
<TableCell>
|
||
<div className="flex flex-wrap gap-1">
|
||
{user.roles.map((role: string, idx: number) => (
|
||
<span
|
||
key={idx}
|
||
className={`px-2 py-1 text-xs rounded-full font-medium ${getRoleBadgeClass(role)}`}
|
||
>
|
||
{getRoleBadgeLabel(role)}
|
||
</span>
|
||
))}
|
||
</div>
|
||
</TableCell>
|
||
<TableCell>
|
||
{(() => {
|
||
const badge = toStatusBadgeProps(user.status);
|
||
return <StatusBadge status={badge.status} label={badge.label} />;
|
||
})()}
|
||
</TableCell>
|
||
<TableCell>
|
||
<div className="text-sm text-gray-400">
|
||
{user.lastLoginAt ? new Date(user.lastLoginAt).toLocaleDateString() : 'Never'}
|
||
</div>
|
||
</TableCell>
|
||
<TableCell>
|
||
<div className="flex items-center gap-2">
|
||
{user.status === 'active' && (
|
||
<Button
|
||
onClick={() => onUpdateStatus(user.id, 'suspended')}
|
||
variant="secondary"
|
||
className="px-3 py-1 text-xs bg-yellow-500/20 text-yellow-300 hover:bg-yellow-500/30"
|
||
>
|
||
Suspend
|
||
</Button>
|
||
)}
|
||
{user.status === 'suspended' && (
|
||
<Button
|
||
onClick={() => onUpdateStatus(user.id, 'active')}
|
||
variant="secondary"
|
||
className="px-3 py-1 text-xs bg-performance-green/20 text-performance-green hover:bg-performance-green/30"
|
||
>
|
||
Activate
|
||
</Button>
|
||
)}
|
||
{user.status !== 'deleted' && (
|
||
<Button
|
||
onClick={() => onDeleteUser(user.id)}
|
||
disabled={deletingUser === user.id}
|
||
variant="secondary"
|
||
className="px-3 py-1 text-xs bg-racing-red/20 text-racing-red hover:bg-racing-red/30 flex items-center gap-1"
|
||
>
|
||
<Trash2 className="w-3 h-3" />
|
||
{deletingUser === user.id ? 'Deleting...' : 'Delete'}
|
||
</Button>
|
||
)}
|
||
</div>
|
||
</TableCell>
|
||
</TableRow>
|
||
))}
|
||
</TableBody>
|
||
</Table>
|
||
)}
|
||
</Card>
|
||
|
||
{/* Stats Summary */}
|
||
{viewData.users.length > 0 && (
|
||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||
<Card className="bg-gradient-to-br from-blue-900/20 to-blue-700/10">
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<Text size="sm" color="text-gray-400" className="mb-1">Total Users</Text>
|
||
<Text size="2xl" weight="bold" color="text-white">{viewData.total}</Text>
|
||
</div>
|
||
<Users className="w-6 h-6 text-blue-400" />
|
||
</div>
|
||
</Card>
|
||
<Card className="bg-gradient-to-br from-green-900/20 to-green-700/10">
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<Text size="sm" color="text-gray-400" className="mb-1">Active</Text>
|
||
<Text size="2xl" weight="bold" color="text-white">
|
||
{viewData.activeUserCount}
|
||
</Text>
|
||
</div>
|
||
<div className="w-6 h-6 text-green-400">✓</div>
|
||
</div>
|
||
</Card>
|
||
<Card className="bg-gradient-to-br from-purple-900/20 to-purple-700/10">
|
||
<div className="flex items-center justify-between">
|
||
<div>
|
||
<Text size="sm" color="text-gray-400" className="mb-1">Admins</Text>
|
||
<Text size="2xl" weight="bold" color="text-white">
|
||
{viewData.adminCount}
|
||
</Text>
|
||
</div>
|
||
<Shield className="w-6 h-6 text-purple-400" />
|
||
</div>
|
||
</Card>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
} |