feat(ui): generischer DataTable-Wrapper + Version 1.0.0
DataTable (components/DataTable.tsx) gibt jeder CRUD-Tabelle drei
Baseline-Features auf einmal:
* Search-Input (Volltext über alle string-Felder, case-insensitive)
* Pagination 25/Seite mit showSizeChanger
* Auto-sorter pro Spalte mit dataIndex (string→localeCompare,
number→subtract, boolean→bool→Number) — Spalten mit eigenem
sorter behalten den.
Sweep aller 13 CRUD-Pages auf <DataTable>: Domains, Backends,
Routing-Rules, Networks, IP-Addresses, SSL, Cluster, sechs Firewall-
Tabs. Kleine Sub-Tabellen (System-Discovered IP-Card) bleiben
auf <Table> — read-only ohne CRUD braucht keine Pagination.
i18n: common.search, common.totalRows.
Version-Bump auf 1.0.0 (User-Direktive: ohne -dev): VERSION-Datei,
Go-Literale in cmd/edgeguard-{api,ctl,scheduler}/main.go,
package.json, Sidebar-Konstante. Live deployed auf 89.163.205.6 als
edgeguard 1.0.0 (api + ui + meta).
Memory: project_versioning.md hält die Patch-Bump-Konvention fest
(Gitea Package Registry 409't bei Doppel-Upload — bei jedem Release
zuerst die VERSION inkrementieren).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
123
management-ui/src/components/DataTable.tsx
Normal file
123
management-ui/src/components/DataTable.tsx
Normal file
@@ -0,0 +1,123 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Input, Space, Table } from 'antd'
|
||||
import type { ColumnsType, TableProps } from 'antd/es/table'
|
||||
import { SearchOutlined } from '@ant-design/icons'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
// DataTable wraps AntD's Table and gives every CRUD page the same
|
||||
// baseline UX:
|
||||
//
|
||||
// * Search input above the table — filters across every string
|
||||
// field of every row, case-insensitive.
|
||||
// * Pagination on by default (25 / page, size-changer enabled).
|
||||
// * Sortable columns out of the box — any column with `dataIndex`
|
||||
// gets a default sorter inferred from the value type (string →
|
||||
// localeCompare, number → subtract). Columns that already
|
||||
// define `sorter` keep their custom one.
|
||||
//
|
||||
// Drop-in replacement: callers pass the same `columns` + `dataSource`
|
||||
// props they would to `<Table>`. Search visibility and page-size
|
||||
// can be customised via the props.
|
||||
interface DataTableProps<T> extends Omit<TableProps<T>, 'pagination'> {
|
||||
pageSize?: number
|
||||
searchPlaceholder?: string
|
||||
searchable?: boolean
|
||||
// toolbar renders to the right of the search input, useful for
|
||||
// "Add" buttons.
|
||||
toolbar?: React.ReactNode
|
||||
}
|
||||
|
||||
function inferSorter<T>(dataIndex: string | string[] | undefined) {
|
||||
if (dataIndex == null) return undefined
|
||||
const path = Array.isArray(dataIndex) ? dataIndex : [dataIndex]
|
||||
const get = (row: T): unknown => path.reduce<unknown>(
|
||||
(acc, k) => (acc != null && typeof acc === 'object' ? (acc as Record<string, unknown>)[k] : undefined),
|
||||
row as unknown,
|
||||
)
|
||||
return (a: T, b: T) => {
|
||||
const va = get(a), vb = get(b)
|
||||
if (typeof va === 'number' && typeof vb === 'number') return va - vb
|
||||
if (typeof va === 'boolean' && typeof vb === 'boolean') return Number(va) - Number(vb)
|
||||
const sa = String(va ?? '')
|
||||
const sb = String(vb ?? '')
|
||||
return sa.localeCompare(sb, undefined, { numeric: true, sensitivity: 'base' })
|
||||
}
|
||||
}
|
||||
|
||||
function flatStringValues(row: unknown): string[] {
|
||||
if (row == null) return []
|
||||
if (typeof row === 'string') return [row]
|
||||
if (typeof row === 'number' || typeof row === 'boolean') return [String(row)]
|
||||
if (Array.isArray(row)) return row.flatMap(flatStringValues)
|
||||
if (typeof row === 'object') {
|
||||
return Object.values(row as Record<string, unknown>).flatMap(flatStringValues)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
export default function DataTable<T extends object>(
|
||||
props: DataTableProps<T>,
|
||||
) {
|
||||
const { t } = useTranslation()
|
||||
const {
|
||||
dataSource,
|
||||
columns,
|
||||
pageSize = 25,
|
||||
searchPlaceholder,
|
||||
searchable = true,
|
||||
toolbar,
|
||||
...rest
|
||||
} = props
|
||||
|
||||
const [search, setSearch] = useState('')
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!search || !dataSource) return dataSource
|
||||
const q = search.toLowerCase()
|
||||
return (dataSource as readonly T[]).filter((row) =>
|
||||
flatStringValues(row).some((s) => s.toLowerCase().includes(q)),
|
||||
)
|
||||
}, [dataSource, search])
|
||||
|
||||
const enhancedCols: ColumnsType<T> = useMemo(() => {
|
||||
if (!columns) return []
|
||||
return (columns as ColumnsType<T>).map((c) => {
|
||||
// Skip if user already declared a sorter or this column has
|
||||
// no dataIndex (e.g. action columns).
|
||||
const col = c as Record<string, unknown>
|
||||
if (col.sorter || col.dataIndex == null) return c
|
||||
const sorter = inferSorter<T>(col.dataIndex as string | string[])
|
||||
if (!sorter) return c
|
||||
return { ...c, sorter }
|
||||
})
|
||||
}, [columns])
|
||||
|
||||
return (
|
||||
<Space direction="vertical" className="w-full mb-12" size="small">
|
||||
{(searchable || toolbar) && (
|
||||
<div className="flex-between mb-12">
|
||||
{searchable && (
|
||||
<Input
|
||||
prefix={<SearchOutlined />}
|
||||
placeholder={searchPlaceholder ?? t('common.search')}
|
||||
allowClear
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
style={{ maxWidth: 320 }}
|
||||
/>
|
||||
)}
|
||||
{toolbar && <div>{toolbar}</div>}
|
||||
</div>
|
||||
)}
|
||||
<Table<T>
|
||||
{...rest}
|
||||
dataSource={filtered}
|
||||
columns={enhancedCols}
|
||||
pagination={{
|
||||
pageSize,
|
||||
showSizeChanger: true,
|
||||
showTotal: (total) => t('common.totalRows', { count: total }),
|
||||
}}
|
||||
/>
|
||||
</Space>
|
||||
)
|
||||
}
|
||||
@@ -68,7 +68,7 @@ const NAV: NavSection[] = [
|
||||
},
|
||||
]
|
||||
|
||||
const VERSION = '0.0.1-dev'
|
||||
const VERSION = '1.0.0'
|
||||
|
||||
export default function Sidebar({ isOpen, onClose }: SidebarProps) {
|
||||
const { t } = useTranslation()
|
||||
|
||||
Reference in New Issue
Block a user