diff --git a/components/admin-dashboard.tsx b/components/admin-dashboard.tsx index 9a092a8..cbe6d18 100644 --- a/components/admin-dashboard.tsx +++ b/components/admin-dashboard.tsx @@ -1,39 +1,9 @@ "use client" import * as React from "react" -import { - Database, - Settings, - Shield, - Table, - Plus, - Search, - MoreHorizontal, - Edit, - Trash2, - Eye, - FileText, - Cog, - LogOut, - Key, -} from "lucide-react" +import { Database, Settings, Shield, LogOut } from "lucide-react" import { Button } from "@/components/ui/button" -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" -import { Input } from "@/components/ui/input" -import { Label } from "@/components/ui/label" -import { Badge } from "@/components/ui/badge" -import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle, - DialogTrigger, -} from "@/components/ui/dialog" -import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" -import { Table as TableComponent, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Sidebar, SidebarContent, @@ -47,41 +17,10 @@ import { SidebarProvider, SidebarTrigger, } from "@/components/ui/sidebar" -import { Textarea } from "@/components/ui/textarea" -import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" -import { Switch } from "@/components/ui/switch" -import { Alert, AlertDescription } from "@/components/ui/alert" - -interface TableMetadata { - id: string - name: string - type: "base" | "auth" | "view" - created: string - updated: string - rules: { - list: string - get: string - add: string - update: string - delete: string - } - fields?: Array<{ - name: string - type: string - primaryKey: boolean - nullable: boolean - unique?: boolean - isFile?: boolean - }> - sql?: string -} - -interface Admin { - id: string - email: string - created: string - updated: string -} +import { ApiClient, type TableMetadata, type Admin } from "@/lib/api" +import { DatabaseSection } from "./database/database-section" +import { AdminsSection } from "./admins/admins-section" +import { SettingsSection } from "./settings/settings-section" interface AdminDashboardProps { token: string @@ -91,46 +30,27 @@ interface AdminDashboardProps { export function AdminDashboard({ token, onLogout }: AdminDashboardProps) { const [mounted, setMounted] = React.useState(false) const [activeSection, setActiveSection] = React.useState("database") - const [selectedTable, setSelectedTable] = React.useState(null) - const [searchTerm, setSearchTerm] = React.useState("") const [tables, setTables] = React.useState([]) const [admins, setAdmins] = React.useState([]) const [loading, setLoading] = React.useState(true) + const apiClient = React.useMemo(() => new ApiClient(token, onLogout), [token, onLogout]) + React.useEffect(() => { setMounted(true) loadData() }, []) - const apiCall = async (endpoint: string, options: RequestInit = {}) => { - const response = await fetch(endpoint, { - ...options, - headers: { - ...options.headers, - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - }) - - if (response.status === 401) { - onLogout() - return null - } - - if (!response.ok) { - throw new Error(`API call failed: ${response.statusText}`) - } - - return response.json() - } - const loadData = async () => { try { setLoading(true) - const [tablesData, adminsData] = await Promise.all([apiCall("/api/v1/tables"), apiCall("/api/v1/admins")]) + const [tablesData, adminsData] = await Promise.all([ + apiClient.call("/api/v1/tables"), + apiClient.call("/api/v1/admins"), + ]) - if (tablesData) setTables(tablesData) - if (adminsData) setAdmins(adminsData) + setTables(tablesData) + setAdmins(adminsData) } catch (error) { console.error("Failed to load data:", error) } finally { @@ -161,8 +81,6 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) { }, ] - const filteredTables = tables.filter((table) => table.name.toLowerCase().includes(searchTerm.toLowerCase())) - if (!mounted) { return (
@@ -196,13 +114,7 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) { {sidebarItems.map((item) => ( - { - setActiveSection(item.id) - setSelectedTable(null) - }} - isActive={activeSection === item.id} - > + setActiveSection(item.id)} isActive={activeSection === item.id}> {item.title} @@ -237,19 +149,10 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) { ) : ( <> {activeSection === "database" && ( - + )} {activeSection === "admins" && ( - + )} {activeSection === "settings" && } @@ -260,941 +163,3 @@ export function AdminDashboard({ token, onLogout }: AdminDashboardProps) { ) } - -function AddTableDialog({ - apiCall, - onTablesUpdate, -}: { - apiCall: (endpoint: string, options?: RequestInit) => Promise - onTablesUpdate: (tables: TableMetadata[]) => void -}) { - const [tableType, setTableType] = React.useState<"base" | "auth" | "view">("base") - const [tableName, setTableName] = React.useState("") - const [tableDescription, setTableDescription] = React.useState("") - const [columns, setColumns] = React.useState< - Array<{ - name: string - type: string - primaryKey: boolean - nullable: boolean - unique?: boolean - isFile?: boolean - isSystem?: boolean - }> - >([]) - const [sqlQuery, setSqlQuery] = React.useState("") - const [isLoading, setIsLoading] = React.useState(false) - const [open, setOpen] = React.useState(false) - - const dataTypes = [ - "string", - "int", - "bigint", - "double", - "float", - "boolean", - "date", - "datetime", - "timestamp", - "json", - "xml", - "text", - "varchar", - "char", - "binary", - "uuid", - ] - - React.useEffect(() => { - if (tableType === "base") { - setColumns([ - { name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true }, - { name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true }, - { name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true }, - ]) - } else if (tableType === "auth") { - setColumns([ - { name: "id", type: "string", primaryKey: true, nullable: false, isSystem: true }, - { name: "email", type: "string", primaryKey: false, nullable: false, isSystem: true }, - { name: "password", type: "string", primaryKey: false, nullable: false, isSystem: true }, - { name: "created", type: "datetime", primaryKey: false, nullable: false, isSystem: true }, - { name: "updated", type: "datetime", primaryKey: false, nullable: false, isSystem: true }, - ]) - } else { - setColumns([]) - } - }, [tableType]) - - const addColumn = () => { - setColumns([...columns, { name: "", type: "string", primaryKey: false, nullable: true, isSystem: false }]) - } - - const removeColumn = (index: number) => { - const column = columns[index] - if (columns.length > 1 && !column.isSystem) { - setColumns(columns.filter((_, i) => i !== index)) - } - } - - const updateColumn = (index: number, field: string, value: any) => { - const updatedColumns = columns.map((col, i) => (i === index ? { ...col, [field]: value } : col)) - setColumns(updatedColumns) - } - - const handleSubmit = async () => { - if (!tableName.trim()) return - - setIsLoading(true) - try { - const tableData: Partial = { - name: tableName, - type: tableType, - rules: { - list: "", - get: "", - add: tableType === "view" ? "" : "", - update: tableType === "view" ? "" : "", - delete: tableType === "view" ? "" : "", - }, - } - - if (tableType === "view") { - tableData.sql = sqlQuery - } else { - tableData.fields = columns - } - - await apiCall("/api/v1/tables", { - method: "POST", - body: JSON.stringify(tableData), - }) - - // Refresh tables list - const updatedTables = await apiCall("/api/v1/tables") - onTablesUpdate(updatedTables) - - // Reset form - setTableName("") - setTableDescription("") - setSqlQuery("") - setOpen(false) - } catch (error) { - console.error("Failed to create table:", error) - } finally { - setIsLoading(false) - } - } - - return ( - - - - - - - Create New Table - Create a new database table with custom columns and settings. - - -
-
-
- - setTableName(e.target.value)} - /> -
-
- - -
-
- - {tableType === "view" ? ( -
- -