mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-11-30 10:19:37 +00:00
fixes rehydration of table drawer on schema changes
This commit is contained in:
@@ -76,6 +76,32 @@ interface TableConfigDrawerProps {
|
||||
}
|
||||
*/
|
||||
|
||||
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[]>([])
|
||||
@@ -103,6 +129,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
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"
|
||||
@@ -110,51 +137,35 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
// 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(() => {
|
||||
if (open) {
|
||||
// Transform API format (with constraints) to UI format (flat camelCase)
|
||||
const transformedColumns = table.schema?.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,
|
||||
// Include foreign_key if present
|
||||
foreign_key: col.foreign_key,
|
||||
}
|
||||
}) || []
|
||||
setColumns(transformedColumns)
|
||||
// Initialize rules with mode and expr from API
|
||||
// Convert empty string to "admin" for UI (Select doesn't allow empty string values)
|
||||
const normalizeMode = (mode: string | undefined) => {
|
||||
return mode === "" || !mode ? "admin" : mode
|
||||
const justOpened = open && !wasOpenRef.current
|
||||
wasOpenRef.current = open
|
||||
if (!justOpened) {
|
||||
return
|
||||
}
|
||||
|
||||
setColumns(transformFieldsForEditor(table.schema?.fields))
|
||||
setRules({
|
||||
list: {
|
||||
mode: normalizeMode(table.schema.rules?.list?.mode),
|
||||
mode: normalizeRuleMode(table.schema.rules?.list?.mode),
|
||||
expr: table.schema.rules?.list?.expr || "",
|
||||
},
|
||||
get: {
|
||||
mode: normalizeMode(table.schema.rules?.get?.mode),
|
||||
mode: normalizeRuleMode(table.schema.rules?.get?.mode),
|
||||
expr: table.schema.rules?.get?.expr || "",
|
||||
},
|
||||
add: {
|
||||
mode: normalizeMode(table.schema.rules?.add?.mode),
|
||||
mode: normalizeRuleMode(table.schema.rules?.add?.mode),
|
||||
expr: table.schema.rules?.add?.expr || "",
|
||||
},
|
||||
update: {
|
||||
mode: normalizeMode(table.schema.rules?.update?.mode),
|
||||
mode: normalizeRuleMode(table.schema.rules?.update?.mode),
|
||||
expr: table.schema.rules?.update?.expr || "",
|
||||
},
|
||||
delete: {
|
||||
mode: normalizeMode(table.schema.rules?.delete?.mode),
|
||||
mode: normalizeRuleMode(table.schema.rules?.delete?.mode),
|
||||
expr: table.schema.rules?.delete?.expr || "",
|
||||
},
|
||||
})
|
||||
@@ -176,6 +187,7 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
|
||||
const migrated = await migrateSchemaFieldsIfNeeded(apiClient, fresh)
|
||||
if (migrated) {
|
||||
setColumns(transformFieldsForEditor(migrated.schema?.fields))
|
||||
onTableUpdate(migrated)
|
||||
} else {
|
||||
setHasUnsavedChanges(true)
|
||||
@@ -185,7 +197,6 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
setHasUnsavedChanges(true)
|
||||
}
|
||||
})()
|
||||
}
|
||||
}, [open, table, apiClient, onTableUpdate])
|
||||
|
||||
const addColumn = () => {
|
||||
@@ -346,6 +357,8 @@ export function TableConfigDrawer({ table, apiClient, open, onClose, onTableUpda
|
||||
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",
|
||||
|
||||
Reference in New Issue
Block a user