website refactor
This commit is contained in:
@@ -1,29 +1,30 @@
|
||||
'use client';
|
||||
|
||||
import React from 'react';
|
||||
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 { Heading } from '@/ui/Heading';
|
||||
import { Box } from '@/ui/Box';
|
||||
import { Stack } from '@/ui/Stack';
|
||||
import { Container } from '@/ui/Container';
|
||||
import { Icon } from '@/ui/Icon';
|
||||
import { StatusBadge } from '@/ui/StatusBadge';
|
||||
import { InfoBox } from '@/ui/InfoBox';
|
||||
import {
|
||||
Search,
|
||||
Filter,
|
||||
RefreshCw,
|
||||
Users,
|
||||
Shield,
|
||||
Trash2,
|
||||
AlertTriangle
|
||||
Users
|
||||
} from 'lucide-react';
|
||||
import { AdminUsersViewData } from '@/lib/view-data/AdminUsersViewData';
|
||||
import { UserFilters } from '@/components/admin/UserFilters';
|
||||
import { UserStatsSummary } from '@/components/admin/UserStatsSummary';
|
||||
import { Surface } from '@/ui/Surface';
|
||||
|
||||
/**
|
||||
* AdminUsersTemplate
|
||||
*
|
||||
* Pure template for admin users page.
|
||||
* Accepts ViewData only, no business logic.
|
||||
*/
|
||||
export function AdminUsersTemplate(props: {
|
||||
adminUsersViewData: AdminUsersViewData;
|
||||
interface AdminUsersTemplateProps {
|
||||
viewData: AdminUsersViewData;
|
||||
onRefresh: () => void;
|
||||
onSearch: (search: string) => void;
|
||||
onFilterRole: (role: string) => void;
|
||||
@@ -37,309 +38,216 @@ export function AdminUsersTemplate(props: {
|
||||
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 } => {
|
||||
export function AdminUsersTemplate({
|
||||
viewData,
|
||||
onRefresh,
|
||||
onSearch,
|
||||
onFilterRole,
|
||||
onFilterStatus,
|
||||
onClearFilters,
|
||||
onUpdateStatus,
|
||||
onDeleteUser,
|
||||
search,
|
||||
roleFilter,
|
||||
statusFilter,
|
||||
loading,
|
||||
error,
|
||||
deletingUser
|
||||
}: AdminUsersTemplateProps) {
|
||||
const getStatusBadgeVariant = (status: string): 'success' | 'warning' | 'error' | 'info' => {
|
||||
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 };
|
||||
case 'active': return 'success';
|
||||
case 'suspended': return 'warning';
|
||||
case 'deleted': return 'error';
|
||||
default: return 'info';
|
||||
}
|
||||
};
|
||||
|
||||
const getRoleBadgeClass = (role: string) => {
|
||||
const getRoleBadgeStyle = (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;
|
||||
case 'owner': return { backgroundColor: 'rgba(168, 85, 247, 0.2)', color: '#d8b4fe', border: '1px solid rgba(168, 85, 247, 0.3)' };
|
||||
case 'admin': return { backgroundColor: 'rgba(59, 130, 246, 0.2)', color: '#93c5fd', border: '1px solid rgba(59, 130, 246, 0.3)' };
|
||||
default: return { backgroundColor: 'rgba(115, 115, 115, 0.2)', color: '#d1d5db', border: '1px solid rgba(115, 115, 115, 0.3)' };
|
||||
}
|
||||
};
|
||||
|
||||
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>
|
||||
<Container size="lg" py={6}>
|
||||
<Stack gap={6}>
|
||||
{/* Header */}
|
||||
<Stack direction="row" align="center" justify="between">
|
||||
<Box>
|
||||
<Heading level={1}>User Management</Heading>
|
||||
<Text size="sm" color="text-gray-400" block mt={1}>Manage and monitor all system users</Text>
|
||||
</Box>
|
||||
<Button
|
||||
onClick={() => {}}
|
||||
onClick={onRefresh}
|
||||
disabled={loading}
|
||||
variant="secondary"
|
||||
className="text-racing-red hover:opacity-70 p-0"
|
||||
icon={<Icon icon={RefreshCw} size={4} className={loading ? 'animate-spin' : ''} />}
|
||||
>
|
||||
×
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</Stack>
|
||||
|
||||
{/* 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) && (
|
||||
{/* Error Banner */}
|
||||
{error && (
|
||||
<InfoBox
|
||||
icon={Users}
|
||||
title="Error"
|
||||
description={error}
|
||||
variant="warning"
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Filters Card */}
|
||||
<UserFilters
|
||||
search={search}
|
||||
roleFilter={roleFilter}
|
||||
statusFilter={statusFilter}
|
||||
onSearch={onSearch}
|
||||
onFilterRole={onFilterRole}
|
||||
onFilterStatus={onFilterStatus}
|
||||
onClearFilters={onClearFilters}
|
||||
/>
|
||||
|
||||
{/* Users Table */}
|
||||
<Card p={0}>
|
||||
{loading ? (
|
||||
<Stack center py={12} gap={3}>
|
||||
<Box className="animate-spin" style={{ borderRadius: '9999px', height: '2rem', width: '2rem', borderBottom: '2px solid #3b82f6' }} />
|
||||
<Text color="text-gray-400">Loading users...</Text>
|
||||
</Stack>
|
||||
) : !viewData.users || viewData.users.length === 0 ? (
|
||||
<Stack center py={12} gap={3}>
|
||||
<Icon icon={Users} size={12} color="#525252" />
|
||||
<Text color="text-gray-400">No users found</Text>
|
||||
<Button
|
||||
onClick={onClearFilters}
|
||||
variant="secondary"
|
||||
className="text-xs p-0"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
>
|
||||
Clear all
|
||||
Clear filters
|
||||
</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>
|
||||
</Stack>
|
||||
) : (
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<TableHeader>User</TableHeader>
|
||||
<TableHeader>Email</TableHeader>
|
||||
<TableHeader>Roles</TableHeader>
|
||||
<TableHeader>Status</TableHeader>
|
||||
<TableHeader>Last Login</TableHeader>
|
||||
<TableHeader>Actions</TableHeader>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
)}
|
||||
</Card>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{viewData.users.map((user) => (
|
||||
<TableRow key={user.id}>
|
||||
<TableCell>
|
||||
<Stack direction="row" align="center" gap={3}>
|
||||
<Surface variant="muted" rounded="full" padding={2} style={{ backgroundColor: 'rgba(59, 130, 246, 0.2)' }}>
|
||||
<Icon icon={Shield} size={4} color="#3b82f6" />
|
||||
</Surface>
|
||||
<Box>
|
||||
<Text weight="medium" color="text-white" block>{user.displayName}</Text>
|
||||
<Text size="xs" color="text-gray-500" block>ID: {user.id}</Text>
|
||||
{user.primaryDriverId && (
|
||||
<Text size="xs" color="text-gray-500" block>Driver: {user.primaryDriverId}</Text>
|
||||
)}
|
||||
</Box>
|
||||
</Stack>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Text size="sm" color="text-gray-300">{user.email}</Text>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Stack direction="row" gap={1} wrap>
|
||||
{user.roles.map((role, idx) => {
|
||||
const style = getRoleBadgeStyle(role);
|
||||
return (
|
||||
<Surface
|
||||
key={idx}
|
||||
variant="muted"
|
||||
rounded="full"
|
||||
padding={1}
|
||||
style={{
|
||||
paddingLeft: '0.5rem',
|
||||
paddingRight: '0.5rem',
|
||||
backgroundColor: style.backgroundColor,
|
||||
color: style.color,
|
||||
borderColor: style.border,
|
||||
border: '1px solid'
|
||||
}}
|
||||
>
|
||||
<Text size="xs" weight="medium">{role.charAt(0).toUpperCase() + role.slice(1)}</Text>
|
||||
</Surface>
|
||||
);
|
||||
})}
|
||||
</Stack>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<StatusBadge variant={getStatusBadgeVariant(user.status)}>
|
||||
{user.status.charAt(0).toUpperCase() + user.status.slice(1)}
|
||||
</StatusBadge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Text size="sm" color="text-gray-400">
|
||||
{user.lastLoginAt ? new Date(user.lastLoginAt).toLocaleDateString() : 'Never'}
|
||||
</Text>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Stack direction="row" align="center" gap={2}>
|
||||
{user.status === 'active' && (
|
||||
<Button
|
||||
onClick={() => onUpdateStatus(user.id, 'suspended')}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
>
|
||||
Suspend
|
||||
</Button>
|
||||
)}
|
||||
{user.status === 'suspended' && (
|
||||
<Button
|
||||
onClick={() => onUpdateStatus(user.id, 'active')}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
>
|
||||
Activate
|
||||
</Button>
|
||||
)}
|
||||
{user.status !== 'deleted' && (
|
||||
<Button
|
||||
onClick={() => onDeleteUser(user.id)}
|
||||
disabled={deletingUser === user.id}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
icon={<Icon icon={Trash2} size={3} />}
|
||||
>
|
||||
{deletingUser === user.id ? 'Deleting...' : 'Delete'}
|
||||
</Button>
|
||||
)}
|
||||
</Stack>
|
||||
</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>
|
||||
{/* Stats Summary */}
|
||||
{viewData.users.length > 0 && (
|
||||
<UserStatsSummary
|
||||
total={viewData.total}
|
||||
activeCount={viewData.activeUserCount}
|
||||
adminCount={viewData.adminCount}
|
||||
/>
|
||||
)}
|
||||
</Stack>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user