feat(phase-9): implement MVP polishing and closure
T-032: User management admin panel - backend users router with CRUD, frontend UsersPage with modals T-033: Audit log viewer - backend audit router with filters/pagination, frontend AuditLogPage T-034: Global error handling - ErrorBoundary, LoadingSpinner, ErrorMessage, Toast components T-035: Backend tests - pytest setup with SQLite, tests for health/auth/techniques/tests T-036: Documentation - Updated README with testing section, created docs/API.md
This commit is contained in:
@@ -7,6 +7,8 @@ import TestsPage from "./pages/TestsPage";
|
||||
import TestCreatePage from "./pages/TestCreatePage";
|
||||
import TestDetailPage from "./pages/TestDetailPage";
|
||||
import SystemPage from "./pages/SystemPage";
|
||||
import UsersPage from "./pages/UsersPage";
|
||||
import AuditLogPage from "./pages/AuditLogPage";
|
||||
import Layout from "./components/Layout";
|
||||
import ProtectedRoute from "./components/ProtectedRoute";
|
||||
|
||||
@@ -38,6 +40,22 @@ export default function App() {
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/users"
|
||||
element={
|
||||
<ProtectedRoute roles={["admin"]}>
|
||||
<UsersPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
<Route
|
||||
path="/audit"
|
||||
element={
|
||||
<ProtectedRoute roles={["admin"]}>
|
||||
<AuditLogPage />
|
||||
</ProtectedRoute>
|
||||
}
|
||||
/>
|
||||
</Route>
|
||||
|
||||
{/* Catch-all → dashboard */}
|
||||
|
||||
58
frontend/src/api/audit.ts
Normal file
58
frontend/src/api/audit.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import client from "./client";
|
||||
|
||||
export interface AuditLogOut {
|
||||
id: string;
|
||||
user_id: string | null;
|
||||
username: string | null;
|
||||
action: string;
|
||||
entity_type: string | null;
|
||||
entity_id: string | null;
|
||||
timestamp: string;
|
||||
details: Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
export interface AuditLogPage {
|
||||
items: AuditLogOut[];
|
||||
total: number;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface AuditLogFilters {
|
||||
user_id?: string;
|
||||
action?: string;
|
||||
entity_type?: string;
|
||||
start_date?: string;
|
||||
end_date?: string;
|
||||
offset?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
/** Fetch paginated audit logs with filters. */
|
||||
export async function getAuditLogs(filters?: AuditLogFilters): Promise<AuditLogPage> {
|
||||
const params = new URLSearchParams();
|
||||
if (filters?.user_id) params.append("user_id", filters.user_id);
|
||||
if (filters?.action) params.append("action", filters.action);
|
||||
if (filters?.entity_type) params.append("entity_type", filters.entity_type);
|
||||
if (filters?.start_date) params.append("start_date", filters.start_date);
|
||||
if (filters?.end_date) params.append("end_date", filters.end_date);
|
||||
if (filters?.offset !== undefined) params.append("offset", String(filters.offset));
|
||||
if (filters?.limit !== undefined) params.append("limit", String(filters.limit));
|
||||
|
||||
const { data } = await client.get<AuditLogPage>(
|
||||
`/audit-logs${params.toString() ? `?${params}` : ""}`
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Fetch list of distinct actions. */
|
||||
export async function getAuditActions(): Promise<string[]> {
|
||||
const { data } = await client.get<string[]>("/audit-logs/actions");
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Fetch list of distinct entity types. */
|
||||
export async function getAuditEntityTypes(): Promise<string[]> {
|
||||
const { data } = await client.get<string[]>("/audit-logs/entity-types");
|
||||
return data;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import axios from "axios";
|
||||
import axios, { type AxiosError } from "axios";
|
||||
|
||||
const client = axios.create({
|
||||
baseURL: "http://localhost:8000/api/v1",
|
||||
@@ -14,14 +14,33 @@ client.interceptors.request.use((config) => {
|
||||
return config;
|
||||
});
|
||||
|
||||
// On 401, clear token so the UI can redirect to login
|
||||
// Response interceptor for error handling
|
||||
client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
(error: AxiosError<{ detail?: string; code?: string }>) => {
|
||||
const status = error.response?.status;
|
||||
|
||||
// On 401, clear token and redirect to login
|
||||
if (status === 401) {
|
||||
localStorage.removeItem("token");
|
||||
// Only redirect if not already on login page
|
||||
if (window.location.pathname !== "/login") {
|
||||
window.location.href = "/login";
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
|
||||
// Extract error message from response
|
||||
const message =
|
||||
error.response?.data?.detail ||
|
||||
error.message ||
|
||||
"An unexpected error occurred";
|
||||
|
||||
// Create a more descriptive error
|
||||
const enhancedError = new Error(message);
|
||||
(enhancedError as Error & { status?: number; code?: string }).status = status;
|
||||
(enhancedError as Error & { code?: string }).code = error.response?.data?.code;
|
||||
|
||||
return Promise.reject(enhancedError);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
43
frontend/src/api/users.ts
Normal file
43
frontend/src/api/users.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import client from "./client";
|
||||
|
||||
export interface UserOut {
|
||||
id: string;
|
||||
username: string;
|
||||
email: string | null;
|
||||
role: string;
|
||||
is_active: boolean;
|
||||
created_at: string | null;
|
||||
last_login: string | null;
|
||||
}
|
||||
|
||||
export interface UserCreatePayload {
|
||||
username: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
export interface UserUpdatePayload {
|
||||
email?: string;
|
||||
role?: string;
|
||||
is_active?: boolean;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
/** Fetch all users (admin only). */
|
||||
export async function getUsers(): Promise<UserOut[]> {
|
||||
const { data } = await client.get<UserOut[]>("/users");
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Create a new user (admin only). */
|
||||
export async function createUser(payload: UserCreatePayload): Promise<UserOut> {
|
||||
const { data } = await client.post<UserOut>("/users", payload);
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Update a user (admin only). */
|
||||
export async function updateUser(userId: string, payload: UserUpdatePayload): Promise<UserOut> {
|
||||
const { data } = await client.patch<UserOut>(`/users/${userId}`, payload);
|
||||
return data;
|
||||
}
|
||||
101
frontend/src/components/ErrorBoundary.tsx
Normal file
101
frontend/src/components/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,101 @@
|
||||
import { Component, type ReactNode, type ErrorInfo } from "react";
|
||||
import { AlertTriangle, RefreshCw, Home } from "lucide-react";
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
fallback?: ReactNode;
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean;
|
||||
error: Error | null;
|
||||
errorInfo: ErrorInfo | null;
|
||||
}
|
||||
|
||||
export default class ErrorBoundary extends Component<Props, State> {
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
hasError: false,
|
||||
error: null,
|
||||
errorInfo: null,
|
||||
};
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): Partial<State> {
|
||||
return { hasError: true, error };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||||
console.error("ErrorBoundary caught an error:", error, errorInfo);
|
||||
this.setState({ errorInfo });
|
||||
}
|
||||
|
||||
handleReload = () => {
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
handleGoHome = () => {
|
||||
window.location.href = "/dashboard";
|
||||
};
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
if (this.props.fallback) {
|
||||
return this.props.fallback;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-gray-950 p-6">
|
||||
<div className="w-full max-w-md rounded-xl border border-red-500/30 bg-gray-900 p-8">
|
||||
<div className="flex flex-col items-center text-center">
|
||||
<div className="rounded-full bg-red-900/30 p-4">
|
||||
<AlertTriangle className="h-12 w-12 text-red-400" />
|
||||
</div>
|
||||
|
||||
<h1 className="mt-6 text-xl font-bold text-white">
|
||||
Something went wrong
|
||||
</h1>
|
||||
|
||||
<p className="mt-2 text-sm text-gray-400">
|
||||
An unexpected error occurred. Please try refreshing the page or
|
||||
return to the dashboard.
|
||||
</p>
|
||||
|
||||
{process.env.NODE_ENV === "development" && this.state.error && (
|
||||
<details className="mt-4 w-full text-left">
|
||||
<summary className="cursor-pointer text-sm text-gray-500 hover:text-gray-300">
|
||||
Error details
|
||||
</summary>
|
||||
<pre className="mt-2 max-h-40 overflow-auto rounded bg-gray-800 p-3 text-xs text-red-400">
|
||||
{this.state.error.toString()}
|
||||
{this.state.errorInfo?.componentStack}
|
||||
</pre>
|
||||
</details>
|
||||
)}
|
||||
|
||||
<div className="mt-6 flex gap-3">
|
||||
<button
|
||||
onClick={this.handleReload}
|
||||
className="flex items-center gap-2 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 transition-colors"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
Refresh Page
|
||||
</button>
|
||||
<button
|
||||
onClick={this.handleGoHome}
|
||||
className="flex items-center gap-2 rounded-lg border border-gray-700 bg-gray-800 px-4 py-2 text-sm text-gray-300 hover:border-gray-600 hover:text-white transition-colors"
|
||||
>
|
||||
<Home className="h-4 w-4" />
|
||||
Go to Dashboard
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
40
frontend/src/components/ErrorMessage.tsx
Normal file
40
frontend/src/components/ErrorMessage.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { AlertCircle, RefreshCw } from "lucide-react";
|
||||
|
||||
interface ErrorMessageProps {
|
||||
title?: string;
|
||||
message?: string;
|
||||
onRetry?: () => void;
|
||||
fullHeight?: boolean;
|
||||
}
|
||||
|
||||
export default function ErrorMessage({
|
||||
title = "Something went wrong",
|
||||
message = "An error occurred while loading this content.",
|
||||
onRetry,
|
||||
fullHeight = true,
|
||||
}: ErrorMessageProps) {
|
||||
return (
|
||||
<div
|
||||
className={`flex flex-col items-center justify-center gap-4 ${
|
||||
fullHeight ? "h-64" : "py-8"
|
||||
}`}
|
||||
>
|
||||
<div className="rounded-full bg-red-900/30 p-4">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
</div>
|
||||
<div className="text-center">
|
||||
<h3 className="text-lg font-semibold text-white">{title}</h3>
|
||||
<p className="mt-1 text-sm text-gray-400">{message}</p>
|
||||
</div>
|
||||
{onRetry && (
|
||||
<button
|
||||
onClick={onRetry}
|
||||
className="flex items-center gap-2 rounded-lg border border-gray-700 bg-gray-800 px-4 py-2 text-sm text-gray-300 hover:border-cyan-500/50 hover:text-cyan-400 transition-colors"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
Try Again
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
36
frontend/src/components/LoadingSpinner.tsx
Normal file
36
frontend/src/components/LoadingSpinner.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Loader2 } from "lucide-react";
|
||||
|
||||
interface LoadingSpinnerProps {
|
||||
size?: "sm" | "md" | "lg";
|
||||
text?: string;
|
||||
fullScreen?: boolean;
|
||||
}
|
||||
|
||||
const sizeClasses = {
|
||||
sm: "h-4 w-4",
|
||||
md: "h-8 w-8",
|
||||
lg: "h-12 w-12",
|
||||
};
|
||||
|
||||
export default function LoadingSpinner({
|
||||
size = "md",
|
||||
text,
|
||||
fullScreen = false,
|
||||
}: LoadingSpinnerProps) {
|
||||
const content = (
|
||||
<div className="flex flex-col items-center justify-center gap-3">
|
||||
<Loader2 className={`animate-spin text-cyan-400 ${sizeClasses[size]}`} />
|
||||
{text && <p className="text-sm text-gray-400">{text}</p>}
|
||||
</div>
|
||||
);
|
||||
|
||||
if (fullScreen) {
|
||||
return (
|
||||
<div className="fixed inset-0 flex items-center justify-center bg-gray-950/80 backdrop-blur-sm z-50">
|
||||
{content}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return <div className="flex h-64 items-center justify-center">{content}</div>;
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
Shield,
|
||||
FlaskConical,
|
||||
Settings,
|
||||
Users,
|
||||
FileText,
|
||||
} from "lucide-react";
|
||||
import { useAuth } from "../context/AuthContext";
|
||||
|
||||
@@ -14,6 +16,8 @@ const baseLinks = [
|
||||
];
|
||||
|
||||
const adminLinks = [
|
||||
{ to: "/users", label: "Users", icon: Users },
|
||||
{ to: "/audit", label: "Audit Log", icon: FileText },
|
||||
{ to: "/system", label: "System", icon: Settings },
|
||||
];
|
||||
|
||||
|
||||
88
frontend/src/components/Toast.tsx
Normal file
88
frontend/src/components/Toast.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
import { useState, useEffect, createContext, useContext, useCallback, type ReactNode } from "react";
|
||||
import { CheckCircle, AlertCircle, Info, X } from "lucide-react";
|
||||
|
||||
type ToastType = "success" | "error" | "info";
|
||||
|
||||
interface Toast {
|
||||
id: string;
|
||||
type: ToastType;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ToastContextValue {
|
||||
showToast: (type: ToastType, message: string) => void;
|
||||
}
|
||||
|
||||
const ToastContext = createContext<ToastContextValue | undefined>(undefined);
|
||||
|
||||
export function useToast() {
|
||||
const context = useContext(ToastContext);
|
||||
if (!context) {
|
||||
throw new Error("useToast must be used within a ToastProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
const icons = {
|
||||
success: CheckCircle,
|
||||
error: AlertCircle,
|
||||
info: Info,
|
||||
};
|
||||
|
||||
const colors = {
|
||||
success: "border-green-500/30 bg-green-900/30 text-green-400",
|
||||
error: "border-red-500/30 bg-red-900/30 text-red-400",
|
||||
info: "border-cyan-500/30 bg-cyan-900/30 text-cyan-400",
|
||||
};
|
||||
|
||||
export function ToastProvider({ children }: { children: ReactNode }) {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const showToast = useCallback((type: ToastType, message: string) => {
|
||||
const id = Math.random().toString(36).substr(2, 9);
|
||||
setToasts((prev) => [...prev, { id, type, message }]);
|
||||
}, []);
|
||||
|
||||
const removeToast = useCallback((id: string) => {
|
||||
setToasts((prev) => prev.filter((toast) => toast.id !== id));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ showToast }}>
|
||||
{children}
|
||||
<div className="fixed bottom-4 right-4 z-50 flex flex-col gap-2">
|
||||
{toasts.map((toast) => (
|
||||
<ToastItem
|
||||
key={toast.id}
|
||||
toast={toast}
|
||||
onClose={() => removeToast(toast.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function ToastItem({ toast, onClose }: { toast: Toast; onClose: () => void }) {
|
||||
const Icon = icons[toast.type];
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(onClose, 5000);
|
||||
return () => clearTimeout(timer);
|
||||
}, [onClose]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`flex items-center gap-3 rounded-lg border px-4 py-3 shadow-lg animate-in slide-in-from-right ${colors[toast.type]}`}
|
||||
>
|
||||
<Icon className="h-5 w-5 flex-shrink-0" />
|
||||
<p className="text-sm">{toast.message}</p>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="ml-2 rounded p-1 hover:bg-white/10"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import { createRoot } from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { AuthProvider } from "./context/AuthContext";
|
||||
import { ToastProvider } from "./components/Toast";
|
||||
import ErrorBoundary from "./components/ErrorBoundary";
|
||||
import App from "./App";
|
||||
import "./index.css";
|
||||
|
||||
@@ -14,12 +16,16 @@ const queryClient = new QueryClient({
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<AuthProvider>
|
||||
<App />
|
||||
</AuthProvider>
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
<ErrorBoundary>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<AuthProvider>
|
||||
<ToastProvider>
|
||||
<App />
|
||||
</ToastProvider>
|
||||
</AuthProvider>
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
</ErrorBoundary>
|
||||
</StrictMode>,
|
||||
);
|
||||
|
||||
293
frontend/src/pages/AuditLogPage.tsx
Normal file
293
frontend/src/pages/AuditLogPage.tsx
Normal file
@@ -0,0 +1,293 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import {
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
FileText,
|
||||
Filter,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
getAuditLogs,
|
||||
getAuditActions,
|
||||
getAuditEntityTypes,
|
||||
type AuditLogFilters,
|
||||
} from "../api/audit";
|
||||
|
||||
const PAGE_SIZE = 25;
|
||||
|
||||
export default function AuditLogPage() {
|
||||
const [filters, setFilters] = useState<AuditLogFilters>({
|
||||
offset: 0,
|
||||
limit: PAGE_SIZE,
|
||||
});
|
||||
|
||||
const {
|
||||
data: logsData,
|
||||
isLoading: logsLoading,
|
||||
error: logsError,
|
||||
} = useQuery({
|
||||
queryKey: ["audit-logs", filters],
|
||||
queryFn: () => getAuditLogs(filters),
|
||||
});
|
||||
|
||||
const { data: actions } = useQuery({
|
||||
queryKey: ["audit-actions"],
|
||||
queryFn: getAuditActions,
|
||||
});
|
||||
|
||||
const { data: entityTypes } = useQuery({
|
||||
queryKey: ["audit-entity-types"],
|
||||
queryFn: getAuditEntityTypes,
|
||||
});
|
||||
|
||||
const handleFilterChange = (key: keyof AuditLogFilters, value: string) => {
|
||||
setFilters((prev) => ({
|
||||
...prev,
|
||||
[key]: value || undefined,
|
||||
offset: 0, // Reset pagination on filter change
|
||||
}));
|
||||
};
|
||||
|
||||
const clearFilters = () => {
|
||||
setFilters({ offset: 0, limit: PAGE_SIZE });
|
||||
};
|
||||
|
||||
const goToPage = (newOffset: number) => {
|
||||
setFilters((prev) => ({ ...prev, offset: newOffset }));
|
||||
};
|
||||
|
||||
const hasActiveFilters = filters.action || filters.entity_type || filters.start_date || filters.end_date;
|
||||
|
||||
const formatDate = (dateStr: string) => {
|
||||
return new Date(dateStr).toLocaleString("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
};
|
||||
|
||||
const formatDetails = (details: Record<string, unknown> | null) => {
|
||||
if (!details) return null;
|
||||
return JSON.stringify(details, null, 2);
|
||||
};
|
||||
|
||||
const totalPages = logsData ? Math.ceil(logsData.total / PAGE_SIZE) : 0;
|
||||
const currentPage = Math.floor((filters.offset || 0) / PAGE_SIZE) + 1;
|
||||
|
||||
if (logsLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (logsError) {
|
||||
return (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load audit logs</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-white">Audit Log</h1>
|
||||
<p className="mt-1 text-sm text-gray-400">
|
||||
System activity and change history
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Filters */}
|
||||
<div className="flex flex-wrap items-center gap-4 rounded-xl border border-gray-800 bg-gray-900 p-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Filter className="h-4 w-4 text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-400">Filters:</span>
|
||||
</div>
|
||||
|
||||
{/* Action filter */}
|
||||
<select
|
||||
value={filters.action || ""}
|
||||
onChange={(e) => handleFilterChange("action", e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
<option value="">All Actions</option>
|
||||
{actions?.map((action) => (
|
||||
<option key={action} value={action}>
|
||||
{action.replace(/_/g, " ")}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Entity type filter */}
|
||||
<select
|
||||
value={filters.entity_type || ""}
|
||||
onChange={(e) => handleFilterChange("entity_type", e.target.value)}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
>
|
||||
<option value="">All Entity Types</option>
|
||||
{entityTypes?.map((type) => (
|
||||
<option key={type} value={type}>
|
||||
{type}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{/* Date range */}
|
||||
<input
|
||||
type="date"
|
||||
value={filters.start_date?.split("T")[0] || ""}
|
||||
onChange={(e) =>
|
||||
handleFilterChange(
|
||||
"start_date",
|
||||
e.target.value ? `${e.target.value}T00:00:00` : ""
|
||||
)
|
||||
}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
placeholder="Start date"
|
||||
/>
|
||||
<span className="text-gray-500">to</span>
|
||||
<input
|
||||
type="date"
|
||||
value={filters.end_date?.split("T")[0] || ""}
|
||||
onChange={(e) =>
|
||||
handleFilterChange(
|
||||
"end_date",
|
||||
e.target.value ? `${e.target.value}T23:59:59` : ""
|
||||
)
|
||||
}
|
||||
className="rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-200 focus:border-cyan-500 focus:outline-none"
|
||||
placeholder="End date"
|
||||
/>
|
||||
|
||||
{hasActiveFilters && (
|
||||
<button
|
||||
onClick={clearFilters}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 bg-gray-800 px-3 py-1.5 text-sm text-gray-400 hover:border-red-500/50 hover:text-red-400"
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
Clear
|
||||
</button>
|
||||
)}
|
||||
|
||||
<div className="ml-auto text-sm text-gray-500">
|
||||
{logsData?.total || 0} total records
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Logs Table */}
|
||||
<div className="rounded-xl border border-gray-800 bg-gray-900">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-gray-800">
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Timestamp</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">User</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Action</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Entity</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Details</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{logsData?.items.map((log) => (
|
||||
<tr
|
||||
key={log.id}
|
||||
className="border-b border-gray-800/50 hover:bg-gray-800/30 transition-colors"
|
||||
>
|
||||
<td className="px-6 py-4">
|
||||
<span className="font-mono text-xs text-gray-400">
|
||||
{formatDate(log.timestamp)}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className="text-gray-200">
|
||||
{log.username || "System"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className="inline-flex rounded-full border border-cyan-500/30 bg-cyan-900/30 px-2 py-0.5 text-xs font-medium text-cyan-400">
|
||||
{log.action.replace(/_/g, " ")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{log.entity_type && (
|
||||
<div>
|
||||
<span className="text-gray-300">{log.entity_type}</span>
|
||||
{log.entity_id && (
|
||||
<span className="ml-1 font-mono text-xs text-gray-500">
|
||||
({log.entity_id.slice(0, 8)}...)
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{!log.entity_type && <span className="text-gray-600">—</span>}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{log.details ? (
|
||||
<details className="cursor-pointer">
|
||||
<summary className="text-xs text-gray-400 hover:text-gray-200">
|
||||
View details
|
||||
</summary>
|
||||
<pre className="mt-2 max-w-xs overflow-auto rounded bg-gray-800 p-2 text-xs text-gray-300">
|
||||
{formatDetails(log.details)}
|
||||
</pre>
|
||||
</details>
|
||||
) : (
|
||||
<span className="text-gray-600">—</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{logsData?.items.length === 0 && (
|
||||
<div className="py-12 text-center text-gray-400">
|
||||
No audit logs found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{logsData && logsData.total > PAGE_SIZE && (
|
||||
<div className="flex items-center justify-between border-t border-gray-800 px-6 py-4">
|
||||
<div className="text-sm text-gray-400">
|
||||
Showing {(filters.offset || 0) + 1} to{" "}
|
||||
{Math.min((filters.offset || 0) + PAGE_SIZE, logsData.total)} of{" "}
|
||||
{logsData.total}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => goToPage((filters.offset || 0) - PAGE_SIZE)}
|
||||
disabled={(filters.offset || 0) === 0}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 px-3 py-1.5 text-sm text-gray-400 hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
<ChevronLeft className="h-4 w-4" />
|
||||
Previous
|
||||
</button>
|
||||
<span className="text-sm text-gray-400">
|
||||
Page {currentPage} of {totalPages}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => goToPage((filters.offset || 0) + PAGE_SIZE)}
|
||||
disabled={(filters.offset || 0) + PAGE_SIZE >= logsData.total}
|
||||
className="flex items-center gap-1 rounded-lg border border-gray-700 px-3 py-1.5 text-sm text-gray-400 hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
Next
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
472
frontend/src/pages/UsersPage.tsx
Normal file
472
frontend/src/pages/UsersPage.tsx
Normal file
@@ -0,0 +1,472 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
Loader2,
|
||||
AlertCircle,
|
||||
Users,
|
||||
Plus,
|
||||
X,
|
||||
Check,
|
||||
UserX,
|
||||
UserCheck,
|
||||
Edit,
|
||||
} from "lucide-react";
|
||||
import { getUsers, createUser, updateUser, type UserOut, type UserCreatePayload } from "../api/users";
|
||||
|
||||
const ROLES = [
|
||||
{ value: "viewer", label: "Viewer" },
|
||||
{ value: "red_tech", label: "Red Tech" },
|
||||
{ value: "blue_tech", label: "Blue Tech" },
|
||||
{ value: "red_lead", label: "Red Lead" },
|
||||
{ value: "blue_lead", label: "Blue Lead" },
|
||||
{ value: "admin", label: "Admin" },
|
||||
];
|
||||
|
||||
const roleBadgeColors: Record<string, string> = {
|
||||
admin: "bg-purple-900/50 text-purple-400 border-purple-500/30",
|
||||
red_tech: "bg-red-900/50 text-red-400 border-red-500/30",
|
||||
blue_tech: "bg-blue-900/50 text-blue-400 border-blue-500/30",
|
||||
red_lead: "bg-orange-900/50 text-orange-400 border-orange-500/30",
|
||||
blue_lead: "bg-cyan-900/50 text-cyan-400 border-cyan-500/30",
|
||||
viewer: "bg-gray-800/50 text-gray-400 border-gray-600/30",
|
||||
};
|
||||
|
||||
export default function UsersPage() {
|
||||
const queryClient = useQueryClient();
|
||||
const [showCreateModal, setShowCreateModal] = useState(false);
|
||||
const [editingUser, setEditingUser] = useState<UserOut | null>(null);
|
||||
|
||||
const {
|
||||
data: users,
|
||||
isLoading,
|
||||
error,
|
||||
} = useQuery({
|
||||
queryKey: ["users"],
|
||||
queryFn: getUsers,
|
||||
});
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: createUser,
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["users"] });
|
||||
setShowCreateModal(false);
|
||||
},
|
||||
});
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: ({ userId, payload }: { userId: string; payload: Parameters<typeof updateUser>[1] }) =>
|
||||
updateUser(userId, payload),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["users"] });
|
||||
setEditingUser(null);
|
||||
},
|
||||
});
|
||||
|
||||
const toggleUserActive = (user: UserOut) => {
|
||||
updateMutation.mutate({
|
||||
userId: user.id,
|
||||
payload: { is_active: !user.is_active },
|
||||
});
|
||||
};
|
||||
|
||||
const formatDate = (dateStr: string | null) => {
|
||||
if (!dateStr) return "Never";
|
||||
return new Date(dateStr).toLocaleDateString("en-US", {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
});
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex h-64 items-center justify-center">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-cyan-400" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex h-64 flex-col items-center justify-center gap-2">
|
||||
<AlertCircle className="h-10 w-10 text-red-400" />
|
||||
<p className="text-red-400">Failed to load users</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-white">User Management</h1>
|
||||
<p className="mt-1 text-sm text-gray-400">
|
||||
Manage user accounts and permissions
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowCreateModal(true)}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 transition-colors"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
New User
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Users Table */}
|
||||
<div className="rounded-xl border border-gray-800 bg-gray-900">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-gray-800">
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Username</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Email</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Role</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Status</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Created</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Last Login</th>
|
||||
<th className="px-6 py-4 font-medium text-gray-400">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{users?.map((user) => (
|
||||
<tr
|
||||
key={user.id}
|
||||
className="border-b border-gray-800/50 hover:bg-gray-800/30 transition-colors"
|
||||
>
|
||||
<td className="px-6 py-4">
|
||||
<span className="font-medium text-white">{user.username}</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{user.email || "—"}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span
|
||||
className={`inline-flex rounded-full border px-2 py-0.5 text-xs font-medium ${
|
||||
roleBadgeColors[user.role] || roleBadgeColors.viewer
|
||||
}`}
|
||||
>
|
||||
{user.role.replace(/_/g, " ")}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
{user.is_active ? (
|
||||
<span className="inline-flex items-center gap-1 text-green-400">
|
||||
<Check className="h-3.5 w-3.5" />
|
||||
Active
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center gap-1 text-red-400">
|
||||
<X className="h-3.5 w-3.5" />
|
||||
Inactive
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{formatDate(user.created_at)}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-gray-400">
|
||||
{formatDate(user.last_login)}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => setEditingUser(user)}
|
||||
className="rounded p-1.5 text-gray-400 hover:bg-gray-800 hover:text-cyan-400"
|
||||
title="Edit user"
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => toggleUserActive(user)}
|
||||
disabled={updateMutation.isPending}
|
||||
className={`rounded p-1.5 ${
|
||||
user.is_active
|
||||
? "text-gray-400 hover:bg-red-900/50 hover:text-red-400"
|
||||
: "text-gray-400 hover:bg-green-900/50 hover:text-green-400"
|
||||
}`}
|
||||
title={user.is_active ? "Deactivate" : "Activate"}
|
||||
>
|
||||
{user.is_active ? (
|
||||
<UserX className="h-4 w-4" />
|
||||
) : (
|
||||
<UserCheck className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{users?.length === 0 && (
|
||||
<div className="py-12 text-center text-gray-400">
|
||||
No users found
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Create User Modal */}
|
||||
{showCreateModal && (
|
||||
<CreateUserModal
|
||||
onClose={() => setShowCreateModal(false)}
|
||||
onSubmit={(data) => createMutation.mutate(data)}
|
||||
isSubmitting={createMutation.isPending}
|
||||
error={createMutation.error as Error | null}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Edit User Modal */}
|
||||
{editingUser && (
|
||||
<EditUserModal
|
||||
user={editingUser}
|
||||
onClose={() => setEditingUser(null)}
|
||||
onSubmit={(payload) =>
|
||||
updateMutation.mutate({ userId: editingUser.id, payload })
|
||||
}
|
||||
isSubmitting={updateMutation.isPending}
|
||||
error={updateMutation.error as Error | null}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Create User Modal ──────────────────────────────────────────────
|
||||
|
||||
interface CreateUserModalProps {
|
||||
onClose: () => void;
|
||||
onSubmit: (data: UserCreatePayload) => void;
|
||||
isSubmitting: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
function CreateUserModal({ onClose, onSubmit, isSubmitting, error }: CreateUserModalProps) {
|
||||
const [formData, setFormData] = useState({
|
||||
username: "",
|
||||
email: "",
|
||||
password: "",
|
||||
role: "viewer",
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const validate = () => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!formData.username.trim()) newErrors.username = "Username is required";
|
||||
if (!formData.password) newErrors.password = "Password is required";
|
||||
if (formData.password.length < 6) newErrors.password = "Password must be at least 6 characters";
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (validate()) {
|
||||
onSubmit({
|
||||
username: formData.username,
|
||||
email: formData.email || undefined,
|
||||
password: formData.password,
|
||||
role: formData.role,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
|
||||
<div className="w-full max-w-md rounded-xl border border-gray-800 bg-gray-900 p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-white">Create New User</h3>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-white">
|
||||
<X className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-lg border border-red-500/30 bg-red-900/20 p-3">
|
||||
<p className="text-sm text-red-400">{error.message}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Username *</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.username}
|
||||
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
|
||||
className={`mt-1 w-full rounded-lg border bg-gray-800 px-3 py-2 text-gray-200 ${
|
||||
errors.username ? "border-red-500" : "border-gray-700"
|
||||
}`}
|
||||
/>
|
||||
{errors.username && <p className="mt-1 text-sm text-red-400">{errors.username}</p>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
value={formData.email}
|
||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Password *</label>
|
||||
<input
|
||||
type="password"
|
||||
value={formData.password}
|
||||
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
||||
className={`mt-1 w-full rounded-lg border bg-gray-800 px-3 py-2 text-gray-200 ${
|
||||
errors.password ? "border-red-500" : "border-gray-700"
|
||||
}`}
|
||||
/>
|
||||
{errors.password && <p className="mt-1 text-sm text-red-400">{errors.password}</p>}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Role</label>
|
||||
<select
|
||||
value={formData.role}
|
||||
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
>
|
||||
{ROLES.map((role) => (
|
||||
<option key={role.value} value={role.value}>
|
||||
{role.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 disabled:opacity-50"
|
||||
>
|
||||
{isSubmitting && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Create User
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Edit User Modal ──────────────────────────────────────────────
|
||||
|
||||
interface EditUserModalProps {
|
||||
user: UserOut;
|
||||
onClose: () => void;
|
||||
onSubmit: (payload: Parameters<typeof updateUser>[1]) => void;
|
||||
isSubmitting: boolean;
|
||||
error: Error | null;
|
||||
}
|
||||
|
||||
function EditUserModal({ user, onClose, onSubmit, isSubmitting, error }: EditUserModalProps) {
|
||||
const [formData, setFormData] = useState({
|
||||
email: user.email || "",
|
||||
role: user.role,
|
||||
password: "",
|
||||
});
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const payload: Parameters<typeof updateUser>[1] = {
|
||||
email: formData.email || undefined,
|
||||
role: formData.role,
|
||||
};
|
||||
if (formData.password) {
|
||||
payload.password = formData.password;
|
||||
}
|
||||
onSubmit(payload);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
|
||||
<div className="w-full max-w-md rounded-xl border border-gray-800 bg-gray-900 p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h3 className="text-lg font-semibold text-white">Edit User: {user.username}</h3>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-white">
|
||||
<X className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-lg border border-red-500/30 bg-red-900/20 p-3">
|
||||
<p className="text-sm text-red-400">{error.message}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
value={formData.email}
|
||||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">Role</label>
|
||||
<select
|
||||
value={formData.role}
|
||||
onChange={(e) => setFormData({ ...formData, role: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
>
|
||||
{ROLES.map((role) => (
|
||||
<option key={role.value} value={role.value}>
|
||||
{role.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-300">
|
||||
New Password <span className="text-gray-500">(leave blank to keep current)</span>
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={formData.password}
|
||||
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
||||
className="mt-1 w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-gray-200"
|
||||
placeholder="••••••••"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end gap-3 pt-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="rounded-lg border border-gray-700 px-4 py-2 text-sm text-gray-400 hover:bg-gray-800"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSubmitting}
|
||||
className="flex items-center gap-1.5 rounded-lg bg-cyan-600 px-4 py-2 text-sm font-medium text-white hover:bg-cyan-500 disabled:opacity-50"
|
||||
>
|
||||
{isSubmitting && <Loader2 className="h-4 w-4 animate-spin" />}
|
||||
Save Changes
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user