feat: add list controls and DNS check history
This commit is contained in:
parent
11b16ac3cf
commit
24351eb897
14 changed files with 511 additions and 28 deletions
|
|
@ -12,6 +12,7 @@ import { Label } from "@minmon/ui/components/label";
|
|||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
|
|
@ -28,8 +29,8 @@ import {
|
|||
} from "@minmon/ui/components/table";
|
||||
import { Textarea } from "@minmon/ui/components/textarea";
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import { Check, ChevronRight, Trash2 } from "lucide-react";
|
||||
import { useEffect, useState, type ReactNode } from "react";
|
||||
import { Check, ChevronLeft, ChevronRight, Trash2, X } from "lucide-react";
|
||||
import { useEffect, useId, useMemo, useState, type ReactNode } from "react";
|
||||
|
||||
type BadgeVariant = "neutral" | "success" | "warning" | "destructive" | "info";
|
||||
|
||||
|
|
@ -80,6 +81,7 @@ export function ResourceListCard<TItem extends { id: string }>({
|
|||
items,
|
||||
emptyTitle,
|
||||
emptyDescription,
|
||||
controls,
|
||||
}: {
|
||||
title: string;
|
||||
description: string;
|
||||
|
|
@ -87,15 +89,139 @@ export function ResourceListCard<TItem extends { id: string }>({
|
|||
items: TItem[];
|
||||
emptyTitle: string;
|
||||
emptyDescription: string;
|
||||
controls?: {
|
||||
search: {
|
||||
placeholder: string;
|
||||
getText: (item: TItem) => string;
|
||||
};
|
||||
filter?: {
|
||||
label: string;
|
||||
allLabel: string;
|
||||
options: readonly { value: string; label: string }[];
|
||||
getValue: (item: TItem) => string | string[];
|
||||
};
|
||||
initialPageSize?: number;
|
||||
pageSizeOptions?: readonly number[];
|
||||
};
|
||||
}) {
|
||||
const allFilterValue = "__all";
|
||||
const controlId = useId();
|
||||
const searchInputId = `${controlId}-search`;
|
||||
const filterInputId = `${controlId}-filter`;
|
||||
const pageSizeOptions = controls?.pageSizeOptions ?? [5, 10, 25];
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [filterValue, setFilterValue] = useState(allFilterValue);
|
||||
const [pageSize, setPageSize] = useState(controls?.initialPageSize ?? 10);
|
||||
const [page, setPage] = useState(1);
|
||||
|
||||
const filteredItems = useMemo(() => {
|
||||
if (!controls) return items;
|
||||
|
||||
const normalizedQuery = searchQuery.trim().toLocaleLowerCase();
|
||||
|
||||
return items.filter((item) => {
|
||||
const matchesSearch =
|
||||
!normalizedQuery || controls.search.getText(item).toLocaleLowerCase().includes(normalizedQuery);
|
||||
const itemFilterValue = controls.filter?.getValue(item);
|
||||
const matchesFilter =
|
||||
!controls.filter ||
|
||||
filterValue === allFilterValue ||
|
||||
(Array.isArray(itemFilterValue)
|
||||
? itemFilterValue.includes(filterValue)
|
||||
: itemFilterValue === filterValue);
|
||||
|
||||
return matchesSearch && matchesFilter;
|
||||
});
|
||||
}, [controls, filterValue, items, searchQuery]);
|
||||
|
||||
const pageCount = controls ? Math.max(1, Math.ceil(filteredItems.length / pageSize)) : 1;
|
||||
const currentPage = Math.min(page, pageCount);
|
||||
const visibleItems = controls
|
||||
? filteredItems.slice((currentPage - 1) * pageSize, currentPage * pageSize)
|
||||
: filteredItems;
|
||||
const hasActiveControls = Boolean(searchQuery || filterValue !== allFilterValue);
|
||||
const selectedFilterLabel = controls?.filter
|
||||
? filterValue === allFilterValue
|
||||
? controls.filter.allLabel
|
||||
: controls.filter.options.find((option) => option.value === filterValue)?.label
|
||||
: undefined;
|
||||
const firstVisibleItem = filteredItems.length ? (currentPage - 1) * pageSize + 1 : 0;
|
||||
const lastVisibleItem = Math.min(currentPage * pageSize, filteredItems.length);
|
||||
|
||||
useEffect(() => {
|
||||
if (page > pageCount) {
|
||||
setPage(pageCount);
|
||||
}
|
||||
}, [page, pageCount]);
|
||||
|
||||
function resetControls() {
|
||||
setSearchQuery("");
|
||||
setFilterValue(allFilterValue);
|
||||
setPage(1);
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="py-0 pt-4 gap-0">
|
||||
<CardHeader className="border-b">
|
||||
<CardTitle>{title}</CardTitle>
|
||||
<CardDescription>{description}</CardDescription>
|
||||
{controls && items.length ? (
|
||||
<div className="flex flex-col gap-2 pt-2 sm:flex-row sm:items-center">
|
||||
<div className="min-w-0 flex-1">
|
||||
<Label htmlFor={searchInputId} className="sr-only">
|
||||
Search {title.toLocaleLowerCase()}
|
||||
</Label>
|
||||
<Input
|
||||
id={searchInputId}
|
||||
type="search"
|
||||
aria-label={`Search ${title.toLocaleLowerCase()}`}
|
||||
value={searchQuery}
|
||||
placeholder={controls.search.placeholder}
|
||||
onChange={(event) => {
|
||||
setSearchQuery(event.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{controls.filter ? (
|
||||
<div className="sm:w-44">
|
||||
<Label htmlFor={filterInputId} className="sr-only">
|
||||
{controls.filter.label}
|
||||
</Label>
|
||||
<Select
|
||||
value={filterValue}
|
||||
onValueChange={(value) => {
|
||||
setFilterValue(value ?? allFilterValue);
|
||||
setPage(1);
|
||||
}}
|
||||
>
|
||||
<SelectTrigger id={filterInputId} className="w-full" aria-label={controls.filter.label}>
|
||||
<SelectValue>{selectedFilterLabel}</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem value={allFilterValue}>{controls.filter.allLabel}</SelectItem>
|
||||
{controls.filter.options.map((option) => (
|
||||
<SelectItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
) : null}
|
||||
{hasActiveControls ? (
|
||||
<Button type="button" variant="ghost" onClick={resetControls}>
|
||||
<X data-icon="inline-start" />
|
||||
Clear
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</CardHeader>
|
||||
<CardContent className="px-0">
|
||||
{items.length ? (
|
||||
{visibleItems.length ? (
|
||||
<TableWrapper className="border-0">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
|
|
@ -108,7 +234,7 @@ export function ResourceListCard<TItem extends { id: string }>({
|
|||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{items.map((item) => (
|
||||
{visibleItems.map((item) => (
|
||||
<TableRow key={item.id}>
|
||||
{columns.map((column) => (
|
||||
<TableCell key={column.key} className={column.className}>
|
||||
|
|
@ -120,10 +246,74 @@ export function ResourceListCard<TItem extends { id: string }>({
|
|||
</TableBody>
|
||||
</Table>
|
||||
</TableWrapper>
|
||||
) : items.length ? (
|
||||
<EmptyState
|
||||
title="No matching results"
|
||||
description="Try another search term or clear the current filter."
|
||||
action={
|
||||
<Button type="button" variant="outline" onClick={resetControls}>
|
||||
Clear search and filter
|
||||
</Button>
|
||||
}
|
||||
className="border-0"
|
||||
/>
|
||||
) : (
|
||||
<EmptyState title={emptyTitle} description={emptyDescription} className="border-0" />
|
||||
)}
|
||||
</CardContent>
|
||||
{controls && items.length ? (
|
||||
<CardFooter className="flex-col items-stretch gap-3 border-t py-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<p className="text-xs text-muted-foreground tabular-nums" aria-live="polite">
|
||||
Showing {firstVisibleItem}–{lastVisibleItem} of {filteredItems.length}
|
||||
</p>
|
||||
<div className="flex items-center justify-between gap-2 sm:justify-end">
|
||||
<Select
|
||||
value={String(pageSize)}
|
||||
onValueChange={(value) => {
|
||||
if (!value) return;
|
||||
setPageSize(Number(value));
|
||||
setPage(1);
|
||||
}}
|
||||
>
|
||||
<SelectTrigger aria-label="Rows per page">
|
||||
<SelectValue>{pageSize} per page</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
{pageSizeOptions.map((option) => (
|
||||
<SelectItem key={option} value={String(option)}>
|
||||
{option} per page
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<span className="min-w-20 text-center text-xs text-muted-foreground tabular-nums">
|
||||
Page {currentPage} of {pageCount}
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
aria-label="Previous page"
|
||||
disabled={currentPage === 1}
|
||||
onClick={() => setPage((current) => Math.max(1, current - 1))}
|
||||
>
|
||||
<ChevronLeft />
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
aria-label="Next page"
|
||||
disabled={currentPage === pageCount}
|
||||
onClick={() => setPage((current) => Math.min(pageCount, current + 1))}
|
||||
>
|
||||
<ChevronRight />
|
||||
</Button>
|
||||
</div>
|
||||
</CardFooter>
|
||||
) : null}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
|
@ -329,11 +519,13 @@ export function SelectField({
|
|||
</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{options.map((option) => (
|
||||
<SelectItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
<SelectGroup>
|
||||
{options.map((option) => (
|
||||
<SelectItem key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FieldGroup>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue