mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-11-30 10:19:37 +00:00
1365 lines
63 KiB
TypeScript
1365 lines
63 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { Plus, Trash2, Cog, X, ChevronDown, ChevronUp, GripVertical, Lock, AlertTriangle } from "lucide-react"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
import { Input } from "@/components/ui/input"
|
|
import { Label } from "@/components/ui/label"
|
|
import { Badge } from "@/components/ui/badge"
|
|
import {
|
|
Drawer,
|
|
DrawerContent,
|
|
DrawerDescription,
|
|
DrawerFooter,
|
|
DrawerHeader,
|
|
DrawerTitle,
|
|
} from "@/components/ui/drawer"
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog"
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
|
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import { Textarea } from "@/components/ui/textarea"
|
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
import { Switch } from "@/components/ui/switch"
|
|
import type { ApiClient, AuthMethods, TableMetadata, ForeignKeyConfig } from "@/lib/api"
|
|
import { authMethodLabel, getApiClientError, normalizeAuthMethods } from "@/lib/api"
|
|
import { dataTypes, intPrecisions, DEFAULT_INT_PRECISION } from "@/lib/constants"
|
|
import {
|
|
appendPrecisionToFieldPayload,
|
|
applyFieldTypeChange,
|
|
normalizeFieldTypeForEditor,
|
|
schemaFieldsNeedMigration,
|
|
} from "@/lib/field-types"
|
|
import { migrateSchemaFieldsIfNeeded } from "@/lib/schema-migration"
|
|
import { OAuthEntityPanel } from "@/components/oauth/oauth-entity-panel"
|
|
import { useToast } from "@/hooks/use-toast"
|
|
import { useNavigate } from "react-router-dom"
|
|
import { ROUTES } from "@/lib/routes"
|
|
|
|
|
|
import { cn } from "@/lib/utils"
|
|
import { formatConstraintDefaultForInput, normalizeConstraintsForFieldType } from "@/lib/schema-constraints"
|
|
|
|
interface TableConfigDrawerProps {
|
|
table: TableMetadata
|
|
apiClient: ApiClient
|
|
open: boolean
|
|
onClose: () => void
|
|
onTableUpdate: (table: TableMetadata) => void
|
|
onTablesUpdate?: (tables: TableMetadata[]) => void
|
|
tables?: TableMetadata[] // List of existing tables for foreign key entity selection
|
|
}
|
|
|
|
// The update structure for tables is a s follows
|
|
/*
|
|
{
|
|
name: "", // can be different from existing one
|
|
has_api: <bool>,
|
|
addRule: "",
|
|
getRule: "",
|
|
listRule: "",
|
|
updateRule: "",
|
|
deleteRule: "",
|
|
deletedFields: [], // List of column names to delete
|
|
fields: [], array of fields, changes to particular fields will be effected, new fields will be created
|
|
}
|
|
*/
|
|
|
|
function transformFieldsForEditor(fields: TableMetadata["schema"]["fields"] | undefined) {
|
|
return (
|
|
fields?.map((col) => {
|
|
const normalized = normalizeFieldTypeForEditor(col)
|
|
return {
|
|
...col,
|
|
type: normalized.type,
|
|
precision: normalized.precision,
|
|
old_name: col.name,
|
|
// Flatten constraints for UI
|
|
primaryKey: col.primary_key || false,
|
|
minValue: col.constraints?.min_value || null,
|
|
maxValue: col.constraints?.max_value || null,
|
|
defaultValue: col.constraints?.default_value || null,
|
|
validator: col.constraints?.validator || null,
|
|
foreign_key: col.foreign_key,
|
|
}
|
|
}) || []
|
|
)
|
|
}
|
|
|
|
function normalizeRuleMode(mode: string | undefined) {
|
|
// Convert empty string to "admin" for UI (Select doesn't allow empty string values)
|
|
return mode === "" || !mode ? "admin" : mode
|
|
}
|
|
|
|
// NOTE: We don't support changing of field names yet!
|
|
export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpdate, onTablesUpdate, tables = [] }: TableConfigDrawerProps) {
|
|
const [columns, setColumns] = React.useState<any[]>([])
|
|
const [rules, setRules] = React.useState<{
|
|
list: { mode: string; expr: string }
|
|
get: { mode: string; expr: string }
|
|
add: { mode: string; expr: string }
|
|
update: { mode: string; expr: string }
|
|
delete: { mode: string; expr: string }
|
|
}>({
|
|
list: { mode: "admin", expr: "" },
|
|
get: { mode: "admin", expr: "" },
|
|
add: { mode: "admin", expr: "" },
|
|
update: { mode: "admin", expr: "" },
|
|
delete: { mode: "admin", expr: "" },
|
|
})
|
|
const [isLoading, setIsLoading] = React.useState(false)
|
|
const [activeTab, setActiveTab] = React.useState("schema")
|
|
const [hasUnsavedChanges, setHasUnsavedChanges] = React.useState(false)
|
|
const [expandedFields, setExpandedFields] = React.useState<Set<string>>(new Set())
|
|
const [deletedColumns, setDeletedColumns] = React.useState<string[]>([]) // Track deleted field IDs
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = React.useState(false)
|
|
const [isDeleting, setIsDeleting] = React.useState(false)
|
|
const [authMethods, setAuthMethods] = React.useState<AuthMethods>(() =>
|
|
normalizeAuthMethods(table.schema.auth),
|
|
)
|
|
const [savingAuthMethod, setSavingAuthMethod] = React.useState<keyof AuthMethods | null>(null)
|
|
const wasOpenRef = React.useRef(false)
|
|
const { toast } = useToast()
|
|
const navigate = useNavigate()
|
|
const isAuthEntity = table.schema.type === "auth"
|
|
|
|
// Get unique key for field (use id if exists, otherwise generate one)
|
|
const getFieldKey = (field: any, index: number) => field.id || `temp-${index}`
|
|
|
|
// Only hydrate drawer state when it opens. Parent table updates (auth toggles,
|
|
// schema saves) must not reset the active tab or feel like a close/reopen.
|
|
React.useEffect(() => {
|
|
const justOpened = open && !wasOpenRef.current
|
|
wasOpenRef.current = open
|
|
if (!justOpened) {
|
|
return
|
|
}
|
|
|
|
setColumns(transformFieldsForEditor(table.schema?.fields))
|
|
setRules({
|
|
list: {
|
|
mode: normalizeRuleMode(table.schema.rules?.list?.mode),
|
|
expr: table.schema.rules?.list?.expr || "",
|
|
},
|
|
get: {
|
|
mode: normalizeRuleMode(table.schema.rules?.get?.mode),
|
|
expr: table.schema.rules?.get?.expr || "",
|
|
},
|
|
add: {
|
|
mode: normalizeRuleMode(table.schema.rules?.add?.mode),
|
|
expr: table.schema.rules?.add?.expr || "",
|
|
},
|
|
update: {
|
|
mode: normalizeRuleMode(table.schema.rules?.update?.mode),
|
|
expr: table.schema.rules?.update?.expr || "",
|
|
},
|
|
delete: {
|
|
mode: normalizeRuleMode(table.schema.rules?.delete?.mode),
|
|
expr: table.schema.rules?.delete?.expr || "",
|
|
},
|
|
})
|
|
setAuthMethods(normalizeAuthMethods(table.schema.auth))
|
|
setActiveTab("schema")
|
|
setHasUnsavedChanges(false)
|
|
setExpandedFields(new Set())
|
|
setDeletedColumns([])
|
|
|
|
void (async () => {
|
|
try {
|
|
const fresh = await apiClient.call<TableMetadata>(`/api/v1/schemas/${table.schema.name}`)
|
|
if ((fresh as { error?: unknown[] })?.error?.length) {
|
|
return
|
|
}
|
|
if (!schemaFieldsNeedMigration(fresh.schema?.fields ?? [])) {
|
|
return
|
|
}
|
|
|
|
const migrated = await migrateSchemaFieldsIfNeeded(apiClient, fresh)
|
|
if (migrated) {
|
|
setColumns(transformFieldsForEditor(migrated.schema?.fields))
|
|
onTableUpdate(migrated)
|
|
} else {
|
|
setHasUnsavedChanges(true)
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to migrate legacy field types:", error)
|
|
setHasUnsavedChanges(true)
|
|
}
|
|
})()
|
|
}, [open, table, apiClient, onTableUpdate])
|
|
|
|
const addColumn = () => {
|
|
setColumns([...columns, {
|
|
id: `field-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
|
name: "",
|
|
type: "string",
|
|
precision: null,
|
|
primary_key: false,
|
|
primaryKey: false, // UI format
|
|
required: true,
|
|
system: false,
|
|
unique: false,
|
|
constraints: {
|
|
default_value: null,
|
|
max_value: null,
|
|
min_value: null,
|
|
validator: null,
|
|
},
|
|
minValue: null, // UI format
|
|
maxValue: null, // UI format
|
|
defaultValue: null, // UI format
|
|
validator: null, // UI format
|
|
old_name: null
|
|
}])
|
|
setHasUnsavedChanges(true)
|
|
}
|
|
|
|
// Remove existing column by ID
|
|
const removeColumn = (index: number) => {
|
|
const column = columns[index]
|
|
const isSystemColumn = column.system
|
|
|
|
if (!(columns.length > 1 && !isSystemColumn))
|
|
return
|
|
|
|
// Ensure the field we are deleting existed/exists in the table already
|
|
if (isAnExistingField(index)) {
|
|
// Find the original field from the table schema to get its ID
|
|
const originalField = table.schema.fields.find((f: any) => f.name === column.old_name || f.name === column.name)
|
|
if (originalField?.id && !deletedColumns.includes(originalField.id)) {
|
|
// Add the field ID to the delete array
|
|
setDeletedColumns([...deletedColumns, originalField.id]);
|
|
}
|
|
}
|
|
setColumns(columns.filter((_, i) => i !== index))
|
|
}
|
|
|
|
const isAnExistingField = (index: number) => {
|
|
const col_old_name = columns[index]?.old_name
|
|
if (!col_old_name) return false;
|
|
for (const f of table.schema.fields) {
|
|
if (f.name === col_old_name) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Update existing column data
|
|
const updateColumn = (index: number, field: string, value: any) => {
|
|
const updatedColumns = columns.map((col, i) => {
|
|
if (i !== index) {
|
|
return col
|
|
}
|
|
|
|
if (field === "type") {
|
|
const typeUpdate = applyFieldTypeChange(col.type, value, col.precision)
|
|
return {
|
|
...col,
|
|
type: typeUpdate.type,
|
|
precision: typeUpdate.precision,
|
|
}
|
|
}
|
|
|
|
return { ...col, [field]: value }
|
|
})
|
|
|
|
setColumns(updatedColumns)
|
|
setHasUnsavedChanges(true)
|
|
}
|
|
|
|
// Get fields for a selected entity (for foreign key field selector)
|
|
const getEntityFields = (entityName: string): any[] => {
|
|
const entity = tables.find(t => t.schema.name === entityName)
|
|
return entity?.schema.fields || []
|
|
}
|
|
|
|
const handleSaveSchema = async () => {
|
|
setIsLoading(true)
|
|
try {
|
|
// Transform UI format (camelCase flat) to API format (with constraints and snake_case)
|
|
const transformedFields: any[] = []
|
|
for (const col of columns) {
|
|
const rawConstraints = {
|
|
default_value: col.defaultValue !== undefined ? col.defaultValue : col.constraints?.default_value,
|
|
min_value: col.minValue !== undefined ? col.minValue : col.constraints?.min_value,
|
|
max_value: col.maxValue !== undefined ? col.maxValue : col.constraints?.max_value,
|
|
validator:
|
|
col.validator !== undefined ? col.validator : (col.constraints?.validator ?? null),
|
|
}
|
|
const normalized = normalizeConstraintsForFieldType(col.type, rawConstraints, col.precision)
|
|
if (!normalized.ok) {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "Invalid constraints",
|
|
description: `${col.name || "Field"}: ${normalized.message}`,
|
|
duration: 5000,
|
|
})
|
|
setIsLoading(false)
|
|
return
|
|
}
|
|
if (col.type === "int" && !col.precision) {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "Invalid field",
|
|
description: `${col.name || "Field"}: integer fields require a precision.`,
|
|
duration: 5000,
|
|
})
|
|
setIsLoading(false)
|
|
return
|
|
}
|
|
const fieldData: any = {
|
|
id: col.id,
|
|
name: col.name,
|
|
type: col.type,
|
|
primary_key: col.primaryKey !== undefined ? col.primaryKey : col.primary_key,
|
|
required: col.required,
|
|
system: col.system || false,
|
|
unique: col.unique || false,
|
|
constraints: normalized.constraints,
|
|
...(col.old_name && { old_name: col.old_name }),
|
|
}
|
|
appendPrecisionToFieldPayload(fieldData, col)
|
|
if (col.foreign_key && col.foreign_key.entity && col.foreign_key.field) {
|
|
fieldData.foreign_key = col.foreign_key
|
|
}
|
|
transformedFields.push(fieldData)
|
|
}
|
|
|
|
// Add deleted fields to the fields array with op: "delete"
|
|
const deletedFields = deletedColumns.map((fieldId: string) => ({
|
|
id: fieldId,
|
|
op: "delete"
|
|
}))
|
|
|
|
// Combine regular fields and deleted fields in the fields array
|
|
const allFields = [...transformedFields, ...deletedFields]
|
|
|
|
const body = { fields: allFields };
|
|
console.log(body)
|
|
// return
|
|
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/schemas/${table.schema.name}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
// If the request failed, throw the error here
|
|
if ((updatedTable as any)?.error?.length > 0) throw (updatedTable as any).error
|
|
|
|
onTableUpdate(updatedTable)
|
|
setColumns(transformFieldsForEditor(updatedTable.schema?.fields))
|
|
setDeletedColumns([])
|
|
setHasUnsavedChanges(false)
|
|
toast({
|
|
title: "Table Updated",
|
|
description: "Table fields updated successfully.",
|
|
duration: 3000,
|
|
})
|
|
} catch (error) {
|
|
console.error("Failed to update schema:", error)
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
// Update rule changes
|
|
const handleSaveRules = async () => {
|
|
setIsLoading(true)
|
|
try {
|
|
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/schemas/${table.schema.name}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({
|
|
rules: {
|
|
list: {
|
|
mode: rules.list.mode === "admin" ? "" : (rules.list.mode || ""),
|
|
expr: rules.list.expr || "",
|
|
},
|
|
get: {
|
|
mode: rules.get.mode === "admin" ? "" : (rules.get.mode || ""),
|
|
expr: rules.get.expr || "",
|
|
},
|
|
add: {
|
|
mode: rules.add.mode === "admin" ? "" : (rules.add.mode || ""),
|
|
expr: rules.add.expr || "",
|
|
},
|
|
update: {
|
|
mode: rules.update.mode === "admin" ? "" : (rules.update.mode || ""),
|
|
expr: rules.update.expr || "",
|
|
},
|
|
delete: {
|
|
mode: rules.delete.mode === "admin" ? "" : (rules.delete.mode || ""),
|
|
expr: rules.delete.expr || "",
|
|
},
|
|
},
|
|
}),
|
|
})
|
|
|
|
// If the request failed, throw the error here
|
|
if ((updatedTable as any)?.error?.length > 0) throw (updatedTable as any).error
|
|
|
|
onTableUpdate(updatedTable)
|
|
toast({
|
|
title: "Table Updated",
|
|
description: "Table access rules updated successfully!",
|
|
duration: 3000,
|
|
})
|
|
setHasUnsavedChanges(false)
|
|
} catch (error) {
|
|
console.error("Failed to update rules:", error)
|
|
toast({
|
|
title: "Error",
|
|
description: error instanceof Error ? error.message : "Failed to update access rules",
|
|
variant: "destructive",
|
|
duration: 5000,
|
|
})
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
// Helper to update a specific rule
|
|
const updateRule = (ruleKey: keyof typeof rules, field: "mode" | "expr", value: string) => {
|
|
setRules({
|
|
...rules,
|
|
[ruleKey]: {
|
|
...rules[ruleKey],
|
|
[field]: value,
|
|
},
|
|
})
|
|
setHasUnsavedChanges(true)
|
|
}
|
|
|
|
const handleAuthMethodToggle = async (method: keyof AuthMethods, enabled: boolean) => {
|
|
const previous = authMethods
|
|
setAuthMethods({ ...authMethods, [method]: enabled })
|
|
setSavingAuthMethod(method)
|
|
|
|
try {
|
|
const updatedTable = await apiClient.call<TableMetadata>(`/api/v1/schemas/${table.schema.name}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify({
|
|
auth: { [method]: enabled },
|
|
}),
|
|
})
|
|
|
|
const apiError = getApiClientError(updatedTable)
|
|
if (apiError) {
|
|
throw new Error(apiError)
|
|
}
|
|
|
|
const nextMethods = normalizeAuthMethods(updatedTable.schema?.auth ?? { ...previous, [method]: enabled })
|
|
setAuthMethods(nextMethods)
|
|
onTableUpdate({
|
|
...updatedTable,
|
|
schema: {
|
|
...updatedTable.schema,
|
|
auth: nextMethods,
|
|
},
|
|
})
|
|
|
|
toast({
|
|
title: enabled ? "Setting Enabled" : "Setting Disabled",
|
|
description: `${authMethodLabel(method)} ${enabled ? "enabled" : "disabled"} for ${table.schema.name}.`,
|
|
duration: 3000,
|
|
})
|
|
} catch (error) {
|
|
console.error("Failed to update auth methods:", error)
|
|
setAuthMethods(previous)
|
|
toast({
|
|
title: "Error",
|
|
description: error instanceof Error ? error.message : "Failed to update auth methods",
|
|
variant: "destructive",
|
|
duration: 5000,
|
|
})
|
|
} finally {
|
|
setSavingAuthMethod(null)
|
|
}
|
|
}
|
|
|
|
const isSystemColumn = (column: any) => {
|
|
return column.system
|
|
}
|
|
|
|
const handleClose = () => {
|
|
if (hasUnsavedChanges) {
|
|
if (confirm("You have unsaved changes. Are you sure you want to close?")) {
|
|
setHasUnsavedChanges(false)
|
|
setDeletedColumns([])
|
|
onClose()
|
|
}
|
|
} else {
|
|
onClose()
|
|
}
|
|
}
|
|
|
|
const handleDeleteSchema = async () => {
|
|
setIsDeleting(true)
|
|
try {
|
|
const res: any = await apiClient.call(`/api/v1/schemas/${table.schema.name}`, {
|
|
method: "DELETE",
|
|
})
|
|
|
|
if (res?.error?.length > 0) throw res.error
|
|
|
|
// Fetch updated tables list
|
|
if (onTablesUpdate) {
|
|
const response: any = await apiClient.call("/api/v1/schemas")
|
|
if (response?.error?.length > 0) throw response.error
|
|
|
|
let updatedTables: TableMetadata[] = []
|
|
if (Array.isArray(response)) {
|
|
updatedTables = response
|
|
} else if (response?.data && Array.isArray(response.data)) {
|
|
updatedTables = response.data
|
|
}
|
|
onTablesUpdate(updatedTables)
|
|
}
|
|
|
|
toast({
|
|
title: "Entity Deleted",
|
|
description: `The entity '${table.schema.name}' has been deleted successfully.`,
|
|
duration: 3000,
|
|
})
|
|
|
|
// Navigate to entities list or another entity
|
|
navigate(ROUTES.entities)
|
|
onClose()
|
|
} catch (error: any) {
|
|
toast({
|
|
variant: "destructive",
|
|
title: "Error",
|
|
description: error?.message || "Failed to delete entity",
|
|
duration: 5000,
|
|
})
|
|
} finally {
|
|
setIsDeleting(false)
|
|
setDeleteDialogOpen(false)
|
|
}
|
|
}
|
|
|
|
const toggleFieldExpanded = (fieldKey: string) => {
|
|
setExpandedFields((prev) => {
|
|
const next = new Set(prev)
|
|
if (next.has(fieldKey)) {
|
|
next.delete(fieldKey)
|
|
} else {
|
|
next.add(fieldKey)
|
|
}
|
|
return next
|
|
})
|
|
}
|
|
|
|
return (
|
|
<Drawer open={open} onOpenChange={handleClose}>
|
|
<DrawerContent side="right" className="w-[900px] max-w-[95vw]">
|
|
<DrawerHeader>
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<Cog className="h-5 w-5" />
|
|
<DrawerTitle>Configure {table.schema.name} Table</DrawerTitle>
|
|
</div>
|
|
<Button variant="ghost" size="sm" onClick={onClose}>
|
|
<X className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
<DrawerDescription>
|
|
{isAuthEntity
|
|
? "Manage fields, login methods, OAuth providers, and access rules"
|
|
: "Manage entity fields and access control rules"}
|
|
</DrawerDescription>
|
|
</DrawerHeader>
|
|
|
|
<div className="flex-1 overflow-hidden">
|
|
<ScrollArea className="h-full">
|
|
<div className="p-6">
|
|
<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
|
|
<TabsList className={`grid w-full mb-6 ${isAuthEntity ? "grid-cols-3" : "grid-cols-2"}`}>
|
|
<TabsTrigger value="schema">Fields</TabsTrigger>
|
|
{isAuthEntity && <TabsTrigger value="auth">Auth</TabsTrigger>}
|
|
<TabsTrigger value="rules">Access Rules</TabsTrigger>
|
|
</TabsList>
|
|
|
|
<TabsContent value="schema" className="space-y-6">
|
|
<div>
|
|
<Label className="text-base font-medium">Fields</Label>
|
|
<p className="text-sm text-muted-foreground">Define the fields for your entity</p>
|
|
</div>
|
|
|
|
{table.schema.type === "view" ? (
|
|
<div className="p-4 bg-muted rounded-lg">
|
|
<h5 className="font-medium mb-3">SQL Query</h5>
|
|
<ScrollArea className="h-32">
|
|
<pre className="text-sm whitespace-pre-wrap">
|
|
<code>{table.schema?.sql}</code>
|
|
</pre>
|
|
</ScrollArea>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{columns.map((column, index) => {
|
|
const fieldKey = getFieldKey(column, index)
|
|
const isSystem = isSystemColumn(column)
|
|
return (
|
|
<div
|
|
key={fieldKey}
|
|
className={cn(
|
|
"border rounded-lg p-4 space-y-3 transition-colors",
|
|
isSystem && "bg-muted/30 border-muted"
|
|
)}
|
|
>
|
|
{/* Field Header - Name and Type */}
|
|
<div className="flex items-center gap-3">
|
|
<GripVertical className="h-4 w-4 text-muted-foreground cursor-move flex-shrink-0" />
|
|
{isSystem && (
|
|
<Lock className="h-4 w-4 text-muted-foreground flex-shrink-0" />
|
|
)}
|
|
<div className="flex-1 grid grid-cols-2 gap-3">
|
|
<div>
|
|
<Input
|
|
placeholder="Field name"
|
|
value={column.name}
|
|
onChange={(e) => updateColumn(index, "name", e.target.value)}
|
|
disabled={isSystem}
|
|
className={isSystem ? "bg-muted" : ""}
|
|
/>
|
|
</div>
|
|
<div className={column.type === "int" ? "grid grid-cols-2 gap-2" : ""}>
|
|
<Select
|
|
value={column.type}
|
|
onValueChange={(value) => updateColumn(index, "type", value)}
|
|
disabled={isSystem}
|
|
>
|
|
<SelectTrigger className={isSystem ? "bg-muted" : ""}>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{dataTypes.map((type) => (
|
|
<SelectItem key={type} value={type}>
|
|
{type.toUpperCase()}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
{column.type === "int" && (
|
|
<Select
|
|
value={column.precision ?? DEFAULT_INT_PRECISION}
|
|
onValueChange={(value) => updateColumn(index, "precision", value)}
|
|
disabled={isSystem}
|
|
>
|
|
<SelectTrigger className={isSystem ? "bg-muted" : ""}>
|
|
<SelectValue placeholder="Precision" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{intPrecisions.map((precision) => (
|
|
<SelectItem key={precision} value={precision}>
|
|
{precision}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
)}
|
|
</div>
|
|
</div>
|
|
{!isSystem && (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() => removeColumn(index)}
|
|
className="flex-shrink-0"
|
|
>
|
|
<Trash2 className="h-4 w-4" />
|
|
</Button>
|
|
)}
|
|
{isSystem && (
|
|
<Badge variant="outline" className="text-xs flex-shrink-0">
|
|
System
|
|
</Badge>
|
|
)}
|
|
</div>
|
|
|
|
{/* Collapsible Advanced Options */}
|
|
{!isSystem && (
|
|
<Collapsible open={expandedFields.has(fieldKey)} onOpenChange={() => toggleFieldExpanded(fieldKey)}>
|
|
<CollapsibleTrigger asChild>
|
|
<Button variant="ghost" size="sm" className="w-full justify-between">
|
|
<span className="text-xs text-muted-foreground">Advanced Options</span>
|
|
{expandedFields.has(fieldKey) ? (
|
|
<ChevronUp className="h-4 w-4" />
|
|
) : (
|
|
<ChevronDown className="h-4 w-4" />
|
|
)}
|
|
</Button>
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent className="space-y-3 pt-2">
|
|
<div className="grid grid-cols-3 gap-3">
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
id={`pk-${fieldKey}`}
|
|
checked={column.primaryKey}
|
|
onCheckedChange={(checked) => updateColumn(index, "primaryKey", checked as boolean)}
|
|
/>
|
|
<Label htmlFor={`pk-${fieldKey}`} className="text-xs cursor-pointer">
|
|
Primary Key
|
|
</Label>
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
id={`unique-${fieldKey}`}
|
|
checked={column.unique || false}
|
|
onCheckedChange={(checked) => updateColumn(index, "unique", checked as boolean)}
|
|
/>
|
|
<Label htmlFor={`unique-${fieldKey}`} className="text-xs cursor-pointer">
|
|
Unique
|
|
</Label>
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
id={`required-${fieldKey}`}
|
|
checked={column.required || false}
|
|
onCheckedChange={(checked) => updateColumn(index, "required", checked as boolean)}
|
|
/>
|
|
<Label htmlFor={`required-${fieldKey}`} className="text-xs cursor-pointer">
|
|
Required
|
|
</Label>
|
|
</div>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`min-${fieldKey}`} className="text-xs">
|
|
Min Value
|
|
</Label>
|
|
<Input
|
|
id={`min-${fieldKey}`}
|
|
type="number"
|
|
placeholder="Min"
|
|
value={
|
|
column.minValue === null || column.minValue === undefined
|
|
? ""
|
|
: String(column.minValue)
|
|
}
|
|
onChange={(e) =>
|
|
updateColumn(index, "minValue", e.target.value ? Number.parseFloat(e.target.value) : null)
|
|
}
|
|
className="h-8 text-xs"
|
|
/>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`max-${fieldKey}`} className="text-xs">
|
|
Max Value
|
|
</Label>
|
|
<Input
|
|
id={`max-${fieldKey}`}
|
|
type="number"
|
|
placeholder="Max"
|
|
value={
|
|
column.maxValue === null || column.maxValue === undefined
|
|
? ""
|
|
: String(column.maxValue)
|
|
}
|
|
onChange={(e) =>
|
|
updateColumn(index, "maxValue", e.target.value ? Number.parseFloat(e.target.value) : null)
|
|
}
|
|
className="h-8 text-xs"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`default-${fieldKey}`} className="text-xs">
|
|
Default Value
|
|
</Label>
|
|
<Input
|
|
id={`default-${fieldKey}`}
|
|
placeholder="Default value"
|
|
value={formatConstraintDefaultForInput(column.type, column.defaultValue ?? column.constraints?.default_value)}
|
|
onChange={(e) => updateColumn(index, "defaultValue", e.target.value)}
|
|
className="h-8 text-xs"
|
|
/>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`validator-${fieldKey}`} className="text-xs">
|
|
Validator
|
|
</Label>
|
|
<Input
|
|
id={`validator-${fieldKey}`}
|
|
placeholder="Validator pattern"
|
|
value={column.validator || ""}
|
|
onChange={(e) => updateColumn(index, "validator", e.target.value || null)}
|
|
className="h-8 text-xs"
|
|
/>
|
|
</div>
|
|
|
|
{/* Foreign Key Configuration */}
|
|
<div className="space-y-3 pt-2 border-t">
|
|
<Label className="text-xs font-semibold">Foreign Key</Label>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`fk-entity-${fieldKey}`} className="text-xs">
|
|
Entity
|
|
</Label>
|
|
<Select
|
|
value={column.foreign_key?.entity || ""}
|
|
onValueChange={(value) => {
|
|
updateColumn(index, "foreign_key", {
|
|
...column.foreign_key,
|
|
entity: value,
|
|
field: "", // Reset field when entity changes
|
|
} as ForeignKeyConfig)
|
|
}}
|
|
>
|
|
<SelectTrigger className="h-8 text-xs">
|
|
<SelectValue placeholder="Select entity" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{tables
|
|
.filter(t => t.schema.name !== table.schema.name) // Don't allow self-reference
|
|
.map((t) => (
|
|
<SelectItem key={t.schema.name} value={t.schema.name}>
|
|
{t.schema.name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`fk-field-${fieldKey}`} className="text-xs">
|
|
Field
|
|
</Label>
|
|
<Select
|
|
value={column.foreign_key?.field || ""}
|
|
onValueChange={(value) => {
|
|
updateColumn(index, "foreign_key", {
|
|
...column.foreign_key,
|
|
field: value,
|
|
} as ForeignKeyConfig)
|
|
}}
|
|
disabled={!column.foreign_key?.entity}
|
|
>
|
|
<SelectTrigger className="h-8 text-xs" disabled={!column.foreign_key?.entity}>
|
|
<SelectValue placeholder="Select field" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{column.foreign_key?.entity
|
|
? getEntityFields(column.foreign_key.entity).map((f: any) => (
|
|
<SelectItem key={f.id} value={f.name}>
|
|
{f.name}
|
|
</SelectItem>
|
|
))
|
|
: null}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`fk-on-update-${fieldKey}`} className="text-xs">
|
|
On Update
|
|
</Label>
|
|
<Select
|
|
value={column.foreign_key?.on_update || undefined}
|
|
onValueChange={(value) => {
|
|
updateColumn(index, "foreign_key", {
|
|
...column.foreign_key,
|
|
on_update: value === "none" ? undefined : value,
|
|
} as ForeignKeyConfig)
|
|
}}
|
|
disabled={!column.foreign_key?.entity || !column.foreign_key?.field}
|
|
>
|
|
<SelectTrigger className="h-8 text-xs" disabled={!column.foreign_key?.entity || !column.foreign_key?.field}>
|
|
<SelectValue placeholder="Optional" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="none">None</SelectItem>
|
|
<SelectItem value="CASCADE">CASCADE</SelectItem>
|
|
<SelectItem value="SET NULL">SET NULL</SelectItem>
|
|
<SelectItem value="RESTRICT">RESTRICT</SelectItem>
|
|
<SelectItem value="NO ACTION">NO ACTION</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label htmlFor={`fk-on-delete-${fieldKey}`} className="text-xs">
|
|
On Delete
|
|
</Label>
|
|
<Select
|
|
value={column.foreign_key?.on_delete || undefined}
|
|
onValueChange={(value) => {
|
|
updateColumn(index, "foreign_key", {
|
|
...column.foreign_key,
|
|
on_delete: value === "none" ? undefined : value,
|
|
} as ForeignKeyConfig)
|
|
}}
|
|
disabled={!column.foreign_key?.entity || !column.foreign_key?.field}
|
|
>
|
|
<SelectTrigger className="h-8 text-xs" disabled={!column.foreign_key?.entity || !column.foreign_key?.field}>
|
|
<SelectValue placeholder="Optional" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="none">None</SelectItem>
|
|
<SelectItem value="CASCADE">CASCADE</SelectItem>
|
|
<SelectItem value="SET NULL">SET NULL</SelectItem>
|
|
<SelectItem value="RESTRICT">RESTRICT</SelectItem>
|
|
<SelectItem value="NO ACTION">NO ACTION</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
)}
|
|
</div>
|
|
)
|
|
})}
|
|
|
|
{/* Add Field Button */}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
className="w-full border-dashed"
|
|
onClick={addColumn}
|
|
>
|
|
<Plus className="h-4 w-4 mr-2" />
|
|
Add Field
|
|
</Button>
|
|
</div>
|
|
)}
|
|
|
|
{table.schema.type !== "view" && (
|
|
<div className="text-sm text-muted-foreground p-3 bg-muted/50 rounded-lg">
|
|
<div>
|
|
<Badge variant="outline" className="text-xs mr-2">
|
|
System
|
|
</Badge>
|
|
System columns are automatically managed and cannot be modified.
|
|
</div>
|
|
<div className="mt-2"></div>
|
|
<div>
|
|
<Badge variant="outline" className="text-xs mr-2">
|
|
Table Type
|
|
</Badge>
|
|
Once a table is created, we dont support changing table field types.
|
|
</div>
|
|
</div>
|
|
)}
|
|
</TabsContent>
|
|
|
|
{isAuthEntity && (
|
|
<TabsContent value="auth" className="space-y-6">
|
|
<div>
|
|
<Label className="text-base font-medium">Login methods</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Control which sign-in options are available for this auth entity. Changes save immediately and
|
|
only affect new logins.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="space-y-3">
|
|
<div className="flex items-center justify-between rounded-lg border p-4">
|
|
<div className="space-y-0.5 pr-4">
|
|
<Label className="text-base">Password</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Email/identity and password via <code className="bg-muted px-1 rounded text-xs">/login</code>
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
checked={authMethods.password}
|
|
disabled={savingAuthMethod !== null}
|
|
onCheckedChange={(checked) => handleAuthMethodToggle("password", checked)}
|
|
aria-label="Enable password login"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between rounded-lg border p-4">
|
|
<div className="space-y-0.5 pr-4">
|
|
<Label className="text-base">Email sign-in codes (OTP)</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Send a 6-digit code to the user's email. Requires SMTP in application settings.
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
checked={authMethods.otp}
|
|
disabled={savingAuthMethod !== null}
|
|
onCheckedChange={(checked) => handleAuthMethodToggle("otp", checked)}
|
|
aria-label="Enable email sign-in codes"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between rounded-lg border p-4">
|
|
<div className="space-y-0.5 pr-4">
|
|
<Label className="text-base">OAuth</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Third-party providers. After enabling, configure and enable providers below.
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
checked={authMethods.oauth}
|
|
disabled={savingAuthMethod !== null}
|
|
onCheckedChange={(checked) => handleAuthMethodToggle("oauth", checked)}
|
|
aria-label="Enable OAuth login"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<Label className="text-base font-medium">Email verification</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Require users to verify their email before a new login. Existing sessions keep working.
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between rounded-lg border p-4">
|
|
<div className="space-y-0.5 pr-4">
|
|
<Label className="text-base">Require email verification</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
When on, password, OTP, and OAuth logins return{" "}
|
|
<code className="bg-muted px-1 rounded text-xs">403</code> until{" "}
|
|
<code className="bg-muted px-1 rounded text-xs">email_verified</code> is true. Needs SMTP and
|
|
Site Domain for verification emails.
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
checked={authMethods.requireEmailVerification}
|
|
disabled={savingAuthMethod !== null}
|
|
onCheckedChange={(checked) => handleAuthMethodToggle("requireEmailVerification", checked)}
|
|
aria-label="Require email verification before login"
|
|
/>
|
|
</div>
|
|
|
|
{(authMethods.otp || authMethods.requireEmailVerification) && (
|
|
<div className="rounded-lg border bg-muted/30 p-4 text-sm text-muted-foreground space-y-1">
|
|
{authMethods.otp && (
|
|
<p>
|
|
OTP requires SMTP (<strong className="text-foreground">host</strong> and{" "}
|
|
<strong className="text-foreground">from</strong>) under Settings.
|
|
</p>
|
|
)}
|
|
{authMethods.requireEmailVerification && (
|
|
<p>
|
|
Verification emails also need <strong className="text-foreground">Site Domain</strong> set
|
|
under Settings (General).
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{authMethods.oauth ? (
|
|
<div className="space-y-4 pt-2 border-t">
|
|
<OAuthEntityPanel
|
|
entityName={table.schema.name}
|
|
apiClient={apiClient}
|
|
isActive={open && activeTab === "auth"}
|
|
/>
|
|
</div>
|
|
) : (
|
|
<div className="rounded-lg border bg-muted/30 p-4 text-sm text-muted-foreground">
|
|
Enable <strong className="text-foreground">OAuth</strong> above to register providers and turn
|
|
them on for this entity.
|
|
</div>
|
|
)}
|
|
</TabsContent>
|
|
)}
|
|
|
|
<TabsContent value="rules" className="space-y-6">
|
|
<div>
|
|
<h4 className="text-lg font-medium mb-2">Access Control Rules</h4>
|
|
<p className="text-sm text-muted-foreground mb-6">
|
|
Configure who can access and modify records in this entity
|
|
</p>
|
|
<div className="space-y-6">
|
|
{/* List Rule */}
|
|
<div className="border rounded-lg p-4 space-y-3">
|
|
<div>
|
|
<Label htmlFor="list-mode" className="text-sm font-medium">
|
|
List Records
|
|
</Label>
|
|
<p className="text-xs text-muted-foreground mt-0.5">Controls who can list all records</p>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Select
|
|
value={rules.list.mode}
|
|
onValueChange={(value) => updateRule("list", "mode", value)}
|
|
>
|
|
<SelectTrigger id="list-mode" className="w-full">
|
|
<SelectValue placeholder="Select access mode" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="admin">Admin Only (Default)</SelectItem>
|
|
<SelectItem value="public">Public</SelectItem>
|
|
<SelectItem value="auth">Authenticated Users</SelectItem>
|
|
<SelectItem value="custom">Custom Expression</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{rules.list.mode === "custom" && (
|
|
<div>
|
|
<Textarea
|
|
placeholder='e.g., "auth.role == \"admin\"" or "auth.id == record.owner_id"'
|
|
value={rules.list.expr}
|
|
onChange={(e) => updateRule("list", "expr", e.target.value)}
|
|
className="mt-2 font-mono text-sm"
|
|
rows={3}
|
|
/>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Enter a custom expression to evaluate access
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Get Rule */}
|
|
<div className="border rounded-lg p-4 space-y-3">
|
|
<div>
|
|
<Label htmlFor="get-mode" className="text-sm font-medium">
|
|
Get Record
|
|
</Label>
|
|
<p className="text-xs text-muted-foreground mt-0.5">Controls who can view individual records</p>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Select
|
|
value={rules.get.mode}
|
|
onValueChange={(value) => updateRule("get", "mode", value)}
|
|
>
|
|
<SelectTrigger id="get-mode" className="w-full">
|
|
<SelectValue placeholder="Select access mode" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="admin">Admin Only (Default)</SelectItem>
|
|
<SelectItem value="public">Public</SelectItem>
|
|
<SelectItem value="auth">Authenticated Users</SelectItem>
|
|
<SelectItem value="custom">Custom Expression</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{rules.get.mode === "custom" && (
|
|
<div>
|
|
<Textarea
|
|
placeholder='e.g., "auth.id == record.user_id"'
|
|
value={rules.get.expr}
|
|
onChange={(e) => updateRule("get", "expr", e.target.value)}
|
|
className="mt-2 font-mono text-sm"
|
|
rows={3}
|
|
/>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Enter a custom expression to evaluate access
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{table.schema.type !== "view" && (
|
|
<>
|
|
{/* Add Rule */}
|
|
<div className="border rounded-lg p-4 space-y-3">
|
|
<div>
|
|
<Label htmlFor="add-mode" className="text-sm font-medium">
|
|
Add Record
|
|
</Label>
|
|
<p className="text-xs text-muted-foreground mt-0.5">Controls who can create new records</p>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Select
|
|
value={rules.add.mode}
|
|
onValueChange={(value) => updateRule("add", "mode", value)}
|
|
>
|
|
<SelectTrigger id="add-mode" className="w-full">
|
|
<SelectValue placeholder="Select access mode" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="admin">Admin Only (Default)</SelectItem>
|
|
<SelectItem value="public">Public</SelectItem>
|
|
<SelectItem value="auth">Authenticated Users</SelectItem>
|
|
<SelectItem value="custom">Custom Expression</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{rules.add.mode === "custom" && (
|
|
<div>
|
|
<Textarea
|
|
placeholder='e.g., "auth.id != None" or "auth.role == \"admin\""'
|
|
value={rules.add.expr}
|
|
onChange={(e) => updateRule("add", "expr", e.target.value)}
|
|
className="mt-2 font-mono text-sm"
|
|
rows={3}
|
|
/>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Enter a custom expression to evaluate access
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Update Rule */}
|
|
<div className="border rounded-lg p-4 space-y-3">
|
|
<div>
|
|
<Label htmlFor="update-mode" className="text-sm font-medium">
|
|
Update Record
|
|
</Label>
|
|
<p className="text-xs text-muted-foreground mt-0.5">Controls who can modify existing records</p>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Select
|
|
value={rules.update.mode}
|
|
onValueChange={(value) => updateRule("update", "mode", value)}
|
|
>
|
|
<SelectTrigger id="update-mode" className="w-full">
|
|
<SelectValue placeholder="Select access mode" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="admin">Admin Only (Default)</SelectItem>
|
|
<SelectItem value="public">Public</SelectItem>
|
|
<SelectItem value="auth">Authenticated Users</SelectItem>
|
|
<SelectItem value="custom">Custom Expression</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{rules.update.mode === "custom" && (
|
|
<div>
|
|
<Textarea
|
|
placeholder='e.g., "auth.id == record.user_id"'
|
|
value={rules.update.expr}
|
|
onChange={(e) => updateRule("update", "expr", e.target.value)}
|
|
className="mt-2 font-mono text-sm"
|
|
rows={3}
|
|
/>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Enter a custom expression to evaluate access
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Delete Rule */}
|
|
<div className="border rounded-lg p-4 space-y-3">
|
|
<div>
|
|
<Label htmlFor="delete-mode" className="text-sm font-medium">
|
|
Delete Record
|
|
</Label>
|
|
<p className="text-xs text-muted-foreground mt-0.5">Controls who can delete records</p>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Select
|
|
value={rules.delete.mode}
|
|
onValueChange={(value) => updateRule("delete", "mode", value)}
|
|
>
|
|
<SelectTrigger id="delete-mode" className="w-full">
|
|
<SelectValue placeholder="Select access mode" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="admin">Admin Only (Default)</SelectItem>
|
|
<SelectItem value="public">Public</SelectItem>
|
|
<SelectItem value="auth">Authenticated Users</SelectItem>
|
|
<SelectItem value="custom">Custom Expression</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
{rules.delete.mode === "custom" && (
|
|
<div>
|
|
<Textarea
|
|
placeholder='e.g., "auth.role == \"admin\"" or "auth.id == record.owner_id"'
|
|
value={rules.delete.expr}
|
|
onChange={(e) => updateRule("delete", "expr", e.target.value)}
|
|
className="mt-2 font-mono text-sm"
|
|
rows={3}
|
|
/>
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
Enter a custom expression to evaluate access
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
<div className="mt-6 p-4 bg-muted rounded-lg">
|
|
<h5 className="font-medium mb-3 text-sm">Access Mode Guide</h5>
|
|
<div className="space-y-2 text-sm text-muted-foreground">
|
|
<div className="flex items-start gap-2">
|
|
<Badge variant="outline" className="text-xs mt-0.5">Admin Only</Badge>
|
|
<p>Only administrators can access (default). Leave mode empty.</p>
|
|
</div>
|
|
<div className="flex items-start gap-2">
|
|
<Badge variant="outline" className="text-xs mt-0.5">Public</Badge>
|
|
<p>Anyone can access, no authentication required.</p>
|
|
</div>
|
|
<div className="flex items-start gap-2">
|
|
<Badge variant="outline" className="text-xs mt-0.5">Authenticated</Badge>
|
|
<p>Any logged-in user can access.</p>
|
|
</div>
|
|
<div className="flex items-start gap-2">
|
|
<Badge variant="outline" className="text-xs mt-0.5">Custom</Badge>
|
|
<p>Evaluate a custom expression to determine access. Use <code className="bg-background px-1 py-0.5 rounded text-xs">auth</code> for current user and <code className="bg-background px-1 py-0.5 rounded text-xs">record</code> for the record being accessed.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</TabsContent>
|
|
</Tabs>
|
|
</div>
|
|
</ScrollArea>
|
|
</div>
|
|
|
|
<DrawerFooter className="flex-col gap-3 sm:flex-row">
|
|
{activeTab === "schema" && (
|
|
<Button
|
|
type="button"
|
|
variant="destructive"
|
|
onClick={() => setDeleteDialogOpen(true)}
|
|
disabled={isDeleting}
|
|
className="w-full sm:w-auto"
|
|
>
|
|
<Trash2 className="h-4 w-4 mr-2" />
|
|
Delete Entity
|
|
</Button>
|
|
)}
|
|
{activeTab !== "auth" && (
|
|
<Button
|
|
onClick={activeTab === "schema" ? handleSaveSchema : handleSaveRules}
|
|
disabled={isLoading}
|
|
className="w-full sm:flex-1"
|
|
>
|
|
{isLoading ? "Saving..." : `Save ${activeTab === "schema" ? "Fields" : "Rules"}`}
|
|
</Button>
|
|
)}
|
|
</DrawerFooter>
|
|
|
|
{/* Delete Confirmation Dialog */}
|
|
<AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle className="flex items-center gap-2">
|
|
<AlertTriangle className="h-5 w-5 text-destructive" />
|
|
Delete Entity
|
|
</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Are you sure you want to delete the entity <strong>"{table.schema.name}"</strong>? This action cannot be undone and will permanently delete all data associated with this entity.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isDeleting}>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={handleDeleteSchema}
|
|
disabled={isDeleting}
|
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
>
|
|
{isDeleting ? "Deleting..." : "Delete"}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</DrawerContent>
|
|
</Drawer>
|
|
)
|
|
}
|