Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(datatable): per-column menu on every table, with inferred filter types

Juan Denisd75e1a164716
diff --git a/frontend/src/components/ds/DataTable.jsx b/frontend/src/components/ds/DataTable.jsx
index 9a0d44ad..6fdc998f 100644
--- a/frontend/src/components/ds/DataTable.jsx
+++ b/frontend/src/components/ds/DataTable.jsx
@@ -1,5 +1,5 @@
-import { useEffect, useMemo, useRef, useState } from 'react';
-import { ChevronUp, ChevronDown, ChevronRight } from 'lucide-react';
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { ChevronUp, ChevronDown, ChevronRight, Filter } from 'lucide-react';
import {
Table,
TableBody,
@@ -13,6 +13,8 @@ import { cn } from '@/lib/utils';
import { applyTableSorts, nextSorts, useTableSort } from '@/hooks/useTableSort';
import { useColumnVisibility } from '@/hooks/useColumnVisibility';
import EmptyState from '../EmptyState';
+import ColumnMenu from './grid/ColumnMenu';
+import { applyFilters, isDecorative, withInferredTypes } from './grid/fields';
/**
* Declarative data table built on top of the shadcn/ui Table primitives.
@@ -68,9 +70,20 @@ export function DataTable({
onSortsChange,
// Controlled column visibility (optional).
hiddenKeys: controlledHiddenKeys,
+ onHiddenKeysChange,
// Grouping (optional; controlled via groupBy + onGroupByChange).
groupBy: controlledGroupBy,
onGroupByChange,
+ // Per-column header menu (sort · group · filter · move · hide). On by
+ // default: it is the table's own affordance, not a page feature.
+ columnMenu = true,
+ // Column filter rules. Uncontrolled by default — pass both to mirror the
+ // rules in a chip bar or a filter drawer.
+ filters: controlledFilters,
+ onFiltersChange,
+ // Column order (visible order). Uncontrolled by default.
+ columnOrder: controlledOrder,
+ onColumnOrderChange,
// Selection (optional, controlled).
selectable = false,
selectedKeys,
@@ -100,23 +113,99 @@ export function DataTable({
? (key, additive) => onSortsChange(nextSorts(sorts, key, additive))
: internal.toggleSort
), [onSortsChange, sorts, internal.toggleSort]);
+ const setSorts = onSortsChange ?? internal.setSorts;
+
+ // The header menu picks a DIRECTION rather than cycling, so it replaces the
+ // whole sort stack. Shift+click on the header is still how you stack levels.
+ const setSortDirection = useCallback(
+ (key, direction) => setSorts([{ key, direction }]),
+ [setSorts],
+ );
+
+ const [openMenu, setOpenMenu] = useState(null);
const internalCols = useColumnVisibility({
storageKey: storageKey ? `${storageKey}-cols` : undefined,
});
const hiddenKeys = controlledHiddenKeys ?? internalCols.hiddenKeys;
+ const setHiddenKeys = onHiddenKeysChange
+ ?? (controlledHiddenKeys ? null : internalCols.setHiddenKeys);
const [internalGroupBy, setInternalGroupBy] = useState(null);
const groupBy = controlledGroupBy !== undefined ? controlledGroupBy : internalGroupBy;
+ const setGroupBy = onGroupByChange ?? setInternalGroupBy;
+
+ // ---- column filters (header menu) --------------------------------------
+ // Types are inferred from the data for every column that doesn't declare
+ // one, so the ~50 tables written before the field model existed get
+ // filtering without touching a single call site.
+ const typedColumns = useMemo(
+ () => (columnMenu ? withInferredTypes(columns, data) : columns),
+ [columns, data, columnMenu],
+ );
+
+ const [internalFilters, setInternalFilters] = useState({ match: 'all', rules: [] });
+ const filters = controlledFilters ?? internalFilters;
+ const setFilters = onFiltersChange ?? setInternalFilters;
+
+ // Every setter below is called with an explicit VALUE, never an updater
+ // function: half of them may be a controlled `onXChange` prop from a page,
+ // which is a plain `(next) => void` and would receive the function itself.
+ const putRule = useCallback((key, rule) => {
+ setFilters({
+ ...filters,
+ rules: [...filters.rules.filter((r) => r.field !== key), ...(rule ? [rule] : [])],
+ });
+ }, [setFilters, filters]);
+
+ // ---- column order (header menu: move left / right) ---------------------
+ const [internalOrder, setInternalOrder] = useState(null);
+ const order = controlledOrder ?? internalOrder;
+ const setOrder = onColumnOrderChange ?? setInternalOrder;
+
+ const orderedColumns = useMemo(() => {
+ if (!order?.length) return typedColumns;
+ const byKey = new Map(typedColumns.map((c) => [c.key, c]));
+ const moved = order.map((k) => byKey.get(k)).filter(Boolean);
+ // Columns absent from a stale saved order keep their declared position.
+ const rest = typedColumns.filter((c) => !order.includes(c.key));
+ return [...moved, ...rest];
+ }, [typedColumns, order]);
const visibleColumns = useMemo(
- () => columns.filter((c) => !hiddenKeys.includes(c.key)),
- [columns, hiddenKeys],
+ () => orderedColumns.filter((c) => !hiddenKeys.includes(c.key)),
+ [orderedColumns, hiddenKeys],
+ );
+
+ const moveColumn = useCallback((key, delta) => {
+ const keys = visibleColumns.map((c) => c.key);
+ const from = keys.indexOf(key);
+ const to = from + delta;
+ if (from < 0 || to < 0 || to >= keys.length) return;
+ keys.splice(to, 0, ...keys.splice(from, 1));
+ setOrder(keys);
+ }, [visibleColumns, setOrder]);
+
+ const hideColumn = useCallback((key) => {
+ if (!hiddenKeys.includes(key)) setHiddenKeys?.([...hiddenKeys, key]);
+ putRule(key, null);
+ if (groupBy === key) setGroupBy?.(null);
+ }, [setHiddenKeys, hiddenKeys, putRule, setGroupBy, groupBy]);
+
+ // Group BY a column (header menu). Distinct from `toggleGroup` below, which
+ // collapses/expands one already-rendered group row.
+ const toggleGroupBy = useCallback((key) => {
+ setGroupBy?.(groupBy === key ? null : key);
+ }, [setGroupBy, groupBy]);
+
+ const filteredData = useMemo(
+ () => (filters?.rules?.length ? applyFilters(data, filters, typedColumns) : data),
+ [data, filters, typedColumns],
);
const sortedData = useMemo(
- () => (sortable ? applyTableSorts(data, sorts, columns) : data),
- [data, sorts, sortable, columns],
+ () => (sortable ? applyTableSorts(filteredData, sorts, columns) : filteredData),
+ [filteredData, sorts, sortable, columns],
);
// ---- grouping ----------------------------------------------------------
@@ -288,10 +377,12 @@ export function DataTable({
/>
</TableHead>
)}
- {visibleColumns.map((column) => {
+ {visibleColumns.map((column, columnIndex) => {
const sortIndex = sorts.findIndex((s) => s.key === column.key);
const isSorted = sortIndex !== -1;
const canSort = sortable && column.sortable;
+ const rule = filters.rules.find((r) => r.field === column.key) || null;
+ const showMenu = columnMenu && !isDecorative(column);
return (
<TableHead
key={column.key}
@@ -299,6 +390,8 @@ export function DataTable({
column.className,
canSort && 'is-sortable',
isSorted && 'is-sorted',
+ rule && 'is-filtered',
+ showMenu && 'has-menu',
)}
style={column.width ? { width: column.width } : undefined}
onClick={(event) => handleHeaderClick(event, column)}
@@ -327,14 +420,49 @@ export function DataTable({
)}
</span>
)}
+ {rule && <Filter size={11} className="sk-dtable__filter-mark" />}
</span>
+ {showMenu && (
+ <ColumnMenu
+ column={column}
+ rows={data}
+ sortDir={isSorted ? sorts[sortIndex].direction : null}
+ grouped={groupBy === column.key}
+ rule={rule}
+ canMoveLeft={columnIndex > 0}
+ canMoveRight={columnIndex < visibleColumns.length - 1}
+ canHide={!!setHiddenKeys && column.hideable !== false}
+ onSort={canSort ? setSortDirection : undefined}
+ onToggleGroup={column.groupable ? toggleGroupBy : undefined}
+ onPutRule={putRule}
+ onMove={moveColumn}
+ onHide={hideColumn}
+ open={openMenu === column.key}
+ onOpenChange={(open) => setOpenMenu(open ? column.key : null)}
+ />
+ )}
</TableHead>
);
})}
</TableRow>
</TableHeader>
<TableBody>
- {groups
+ {/* Filtered to nothing is NOT the empty state: the header
+ has to stay on screen, because the control that undoes
+ it lives in the header menu. */}
+ {sortedData.length === 0 && data.length > 0 ? (
+ <TableRow className="sk-dtable__nomatch">
+ <TableCell colSpan={columnCount}>
+ <span>No rows match the active column filters.</span>
+ <button
+ type="button"
+ onClick={() => setFilters({ match: filters.match, rules: [] })}
+ >
+ Clear filters
+ </button>
+ </TableCell>
+ </TableRow>
+ ) : groups
? groups.map((group) => {
const isCollapsed = collapsedGroups.has(group.key);
const label = groupColumn.groupLabel
diff --git a/frontend/src/styles/components/_design-system.scss b/frontend/src/styles/components/_design-system.scss
index 31983aa5..d7b9b979 100644
--- a/frontend/src/styles/components/_design-system.scss
+++ b/frontend/src/styles/components/_design-system.scss
@@ -347,6 +347,7 @@ $sk-tones: ('accent', 'green', 'cyan', 'amber', 'red', 'violet');
font-size: 13px;
thead th {
+ position: relative;
text-align: left;
font-family: var(--mono);
font-size: 10.5px;
@@ -356,6 +357,42 @@ $sk-tones: ('accent', 'green', 'cyan', 'amber', 'red', 'violet');
font-weight: 600;
padding: 11px 16px;
border-bottom: 1px solid var(--border);
+
+ // Header cells that carry a "⋮" reserve its lane, so the label never
+ // reflows when the button appears on hover.
+ &.has-menu { padding-right: 28px; }
+ &.is-filtered { color: var(--accent-bright); }
+ &:hover .sk-colmenu-btn { opacity: 1; }
+ &.is-filtered .sk-colmenu-btn { opacity: 1; color: var(--accent-bright); }
+ }
+
+ // Top-lit header, matching the DataGrid surface, so the two table
+ // implementations read as one component.
+ thead { background: linear-gradient(180deg, var(--surface-2), var(--surface)); }
+
+ &__filter-mark { color: var(--accent-bright); flex-shrink: 0; }
+
+ // Filtered-to-nothing: the header stays, because the control that undoes it
+ // lives in the header menu.
+ &__nomatch td {
+ padding: 34px 16px !important;
+ text-align: center;
+ color: var(--text-faint);
+ font-size: 12.5px;
+
+ button {
+ margin-left: 10px;
+ padding: 5px 10px;
+ border: 1px solid var(--border);
+ border-radius: 7px;
+ background: var(--surface-2);
+ color: var(--text-dim);
+ font-size: 12px;
+ font-family: inherit;
+ cursor: pointer;
+
+ &:hover { border-color: var(--accent-dim); color: var(--accent-bright); }
+ }
}
tbody td { padding: 13px 16px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tbody tr { transition: background 0.12s; }
@@ -1053,12 +1090,19 @@ $sk-tones: ('accent', 'green', 'cyan', 'amber', 'red', 'violet');
gap: 8px;
flex-shrink: 0;
- // House rule: the search box is ALWAYS the right-most control in the
- // top bar, whatever order a page happens to publish its actions in.
- // Enforced here rather than by convention at each call site, so it
- // cannot drift and new pages get it for free. (Flex order defaults to
- // 0, so lifting only the search field is enough.)
+ // House rule: the top bar has FIXED SLOTS, not a publication order.
+ // Whatever order a page happens to render its actions in, they land in
+ // the same places on every page:
+ //
+ // [ page actions ] [ search ] [ filter · overflow ]
+ //
+ // Enforced here rather than by convention at each call site so it
+ // cannot drift and new pages get it for free. The point is muscle
+ // memory: the search box must sit in the same pixels on /domains as on
+ // /services, and must not move when a page gains or loses a button.
+ > * { order: 0; }
> .sk-searchfield { order: 1; }
+ > .sk-gridicon { order: 2; }
}
// Routed sub-nav (the ex-sidebar sub-items). The nav is the flex-fill