feat: add list controls and DNS check history

This commit is contained in:
Syahdan 2026-08-07 17:47:50 +07:00
parent 11b16ac3cf
commit 24351eb897
14 changed files with 511 additions and 28 deletions

View file

@ -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>

View file

@ -123,6 +123,32 @@ function RouteComponent() {
items={domains.data}
emptyTitle="No domains added"
emptyDescription="Create a domain after adding a service."
controls={{
search: {
placeholder: "Search domain, service, or IP address…",
getText: (item) =>
[
item.name,
serviceMap.get(item.serviceId),
item.expectedServerIp,
item.lastResolvedIp,
...item.lastResolvedIps,
item.remarks,
]
.filter(Boolean)
.join(" "),
},
filter: {
label: "Filter by DNS status",
allLabel: "All DNS statuses",
options: [
{ value: "MATCH", label: "Match" },
{ value: "MISMATCH", label: "Mismatch" },
{ value: "UNRESOLVED", label: "Unresolved" },
],
getValue: (item) => item.resolutionStatus,
},
}}
/>
</PageSection>
);

View file

@ -1,4 +1,4 @@
import { DetailCard, FormCard, PageSection, QueryStateCard, SelectField, TextAreaField, TextField } from "@/components/dashboard-ui";
import { DetailCard, FormCard, PageSection, QueryStateCard, ResourceListCard, SelectField, TextAreaField, TextField } from "@/components/dashboard-ui";
import { queryClient, trpc, trpcClient } from "@/utils/trpc";
import { Button } from "@minmon/ui/components/button";
import { StatusBadge } from "@minmon/ui/components/status-badge";
@ -29,6 +29,7 @@ function RouteComponent() {
const { domainId } = Route.useParams();
const navigate = useNavigate();
const domain = useQuery(trpc.domains.byId.queryOptions({ id: domainId }));
const history = useQuery(trpc.domains.history.queryOptions({ id: domainId }));
const services = useQuery(trpc.domains.serviceOptions.queryOptions());
const updateDomain = useMutation({
@ -51,6 +52,7 @@ function RouteComponent() {
);
}
await queryClient.invalidateQueries({ queryKey: trpc.domains.byId.queryKey({ id: domainId }) });
await queryClient.invalidateQueries({ queryKey: trpc.domains.history.queryKey({ id: domainId }) });
await queryClient.invalidateQueries({ queryKey: trpc.domains.list.queryKey() });
},
onError: (error) => toast.error(error.message),
@ -60,6 +62,7 @@ function RouteComponent() {
onSuccess: async () => {
toast.success("DNS check completed");
await queryClient.invalidateQueries({ queryKey: trpc.domains.byId.queryKey({ id: domainId }) });
await queryClient.invalidateQueries({ queryKey: trpc.domains.history.queryKey({ id: domainId }) });
await queryClient.invalidateQueries({ queryKey: trpc.domains.list.queryKey() });
},
onError: (error) => toast.error(error.message),
@ -119,6 +122,82 @@ function RouteComponent() {
/>
</PageSection>
<PageSection
title="DNS check history"
description="A chronological record of every DNS check run for this domain."
>
{history.isLoading ? (
<QueryStateCard title="Loading DNS history" description="Fetching earlier resolution checks." />
) : history.isError || !history.data ? (
<QueryStateCard title="DNS history unavailable" description="Earlier DNS checks could not be loaded." />
) : (
<ResourceListCard
title="Resolution timeline"
description="Compare resolved A records with the expected server IP at the time of each check."
columns={[
{
key: "checkedAt",
header: "Checked",
className: "w-48",
render: (item: (typeof history.data)[number]) => (
<span className="tabular-nums">{new Date(item.checkedAt).toLocaleString()}</span>
),
},
{
key: "status",
header: "Status",
className: "w-36",
render: (item: (typeof history.data)[number]) => (
<StatusBadge variant={dnsStatusVariant(item.status)}>{item.status}</StatusBadge>
),
},
{
key: "expectedServerIp",
header: "Expected IP",
render: (item: (typeof history.data)[number]) => item.expectedServerIp,
},
{
key: "resolvedIps",
header: "Resolved IPv4",
render: (item: (typeof history.data)[number]) => item.resolvedIps.join(", ") || "-",
},
{
key: "message",
header: "Result",
render: (item: (typeof history.data)[number]) =>
item.message ||
(item.status === "MATCH"
? "Expected IP found"
: "Expected IP not found in the A records"),
},
]}
items={history.data}
emptyTitle="No DNS checks yet"
emptyDescription="Run Check DNS to create the first history entry for this domain."
controls={{
search: {
placeholder: "Search IP address, status, or result…",
getText: (item) =>
[item.status, item.expectedServerIp, ...item.resolvedIps, item.message]
.filter(Boolean)
.join(" "),
},
filter: {
label: "Filter by DNS status",
allLabel: "All DNS statuses",
options: [
{ value: "MATCH", label: "Match" },
{ value: "MISMATCH", label: "Mismatch" },
{ value: "UNRESOLVED", label: "Unresolved" },
],
getValue: (item) => item.status,
},
initialPageSize: 5,
}}
/>
)}
</PageSection>
<form
className="grid gap-4"
onSubmit={(e) => {

View file

@ -89,6 +89,19 @@ function RouteComponent() {
items={notes.data}
emptyTitle="No notes yet"
emptyDescription="Add a note to document maintenance, incidents, or general server information."
controls={{
search: {
placeholder: "Search title, note, category, or server…",
getText: (item) =>
[item.title, item.content, item.category, serverMap.get(item.serverId)].filter(Boolean).join(" "),
},
filter: {
label: "Filter by server",
allLabel: "All servers",
options: servers.data.map((item) => ({ value: item.id, label: item.name })),
getValue: (item) => item.serverId,
},
}}
/>
</PageSection>
);

View file

@ -112,6 +112,33 @@ function RouteComponent() {
items={servers.data}
emptyTitle="No servers added"
emptyDescription="Add your first server to start managing services, notes, and domains."
controls={{
search: {
placeholder: "Search name, IP, location, or provider…",
getText: (item) =>
[
item.name,
item.primaryIpAddress,
item.secondaryIpAddress,
item.operatingSystem,
item.location,
item.provider,
item.description,
]
.filter(Boolean)
.join(" "),
},
filter: {
label: "Filter by server status",
allLabel: "All statuses",
options: [
{ value: "active", label: "Active" },
{ value: "maintenance", label: "Maintenance" },
{ value: "inactive", label: "Inactive" },
],
getValue: (item) => item.status,
},
}}
/>
</PageSection>
);

View file

@ -159,6 +159,12 @@ function RouteComponent() {
items={notes.data}
emptyTitle="No notes for this server"
emptyDescription="Add a note to document maintenance, incidents, or internal server context."
controls={{
search: {
placeholder: "Search attached notes…",
getText: (item) => [item.title, item.content, item.category].filter(Boolean).join(" "),
},
}}
/>
</PageSection>
)}

View file

@ -109,6 +109,33 @@ function RouteComponent() {
items={services.data}
emptyTitle="No services added"
emptyDescription="Create a service after adding at least one server."
controls={{
search: {
placeholder: "Search service, server, port, or protocol…",
getText: (item) =>
[
item.name,
serverMap.get(item.serverId),
item.internalPort,
item.externalPort,
item.protocol,
item.type,
item.description,
]
.filter((value) => value !== null && value !== undefined)
.join(" "),
},
filter: {
label: "Filter by service status",
allLabel: "All statuses",
options: [
{ value: "active", label: "Active" },
{ value: "maintenance", label: "Maintenance" },
{ value: "down", label: "Down" },
],
getValue: (item) => item.status,
},
}}
/>
</PageSection>
);

View file

@ -157,6 +157,18 @@ function RoleAssignmentCard({ roles, permissions }: { roles: RoleOption[]; permi
items={users.data}
emptyTitle="No users found"
emptyDescription="Users appear here after they sign up or are seeded."
controls={{
search: {
placeholder: "Search name, email, or role…",
getText: (item) => [item.name, item.email, ...item.roles.map((role) => role.name)].join(" "),
},
filter: {
label: "Filter by role",
allLabel: "All roles",
options: roles.map((role) => ({ value: role.id, label: role.name })),
getValue: (item) => item.roles.map((role) => role.id),
},
}}
/>
);
}