Add tags to servers, with a tag filter on the Servers page

Servers can be tagged (prod, media, rack-1, ...) for grouping. Operators
and admins edit tags inline on a server's detail page; the input suggests
tags already used on other servers so the same word ends up spelled the
same way everywhere. Tags show as chips that keep one colour per tag, on
the server cards, in the Manage table (and its CSV export), and on the
detail page, where each chip links to the Servers list filtered by that
tag. The Servers page has a tag bar with counts; picking several tags
narrows to servers that have all of them. The filter lives in the URL, so
it survives a refresh and can be linked to. Tags are also matched by the
global search.

Tags are normalized on the server (trimmed, lowercased, spaces become "-",
duplicates merged, sorted); letters in any language are allowed, plus
digits and - _ . : /, at most 30 characters and 12 per server. Invalid
input is rejected with a message naming the offending tag, and nothing is
saved. Changes are audit-logged with the before and after lists.

Stored as a JSON column on servers (migration 0010). Every server response
now returns tags as an array, and the shared response shaping strips the
token hash in one place instead of five.

Verified with 27 backend checks (normalization edge cases including
Swedish letters, roles, validation, search, audit, PATCH/detail/list
shapes) and by driving the real Servers and detail pages against the real
router in a browser (filtering, editing, invalid tag, viewer view). Real
dev database mtime untouched.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5 committed 2026-09-26 03:06:29 +02:00
1 parent 4c11158e98
commit 9f1609c4ed
13 files changed
+1745 -12

No files matched your search

+70 -5
View File
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useState, type CSSProperties } from "react";
import { Link } from "react-router-dom";
import { Link, useSearchParams } from "react-router-dom";
import { api, type CurrentUser, type ServerRecord } from "../api/client";
import CopyButton from "../components/CopyButton";
import { formatDateTime } from "../utils/date";
@@ -8,6 +8,8 @@ import SortableTh from "../components/SortableTh";
import { usePagination } from "../hooks/usePagination";
import Pagination from "../components/Pagination";
import { downloadCsv } from "../utils/csv";
import { TagBadges } from "../components/ServerTags";
import { tagColorClass } from "../utils/tags";
function typeBadgeStyle(colors: Record<string, string>, type: string): CSSProperties {
const color = colors[type];
@@ -35,6 +37,9 @@ export default function Servers({ user }: { user: CurrentUser }) {
const [error, setError] = useState<string | null>(null);
const [managingServers, setManagingServers] = useState(false);
const [integrationColors, setIntegrationColors] = useState<Record<string, string>>({});
// The active tag filter lives in the URL, so a tag chip on a server's page can link straight to "everything with this tag".
const [searchParams, setSearchParams] = useSearchParams();
const selectedTags = searchParams.getAll("tag");
useEffect(() => {
api.settings.integrationColors().then((res) => setIntegrationColors(res.integrationColors)).catch(() => {});
@@ -101,12 +106,30 @@ export default function Servers({ user }: { user: CurrentUser }) {
const { pageItems: serverPageItems, page: serverPage, setPage: setServerPage, pageCount: serverPageCount, totalCount: serverTotalCount } =
usePagination(sortedServers);
const tagCounts = new Map<string, number>();
for (const s of servers) for (const t of s.tags) tagCounts.set(t, (tagCounts.get(t) ?? 0) + 1);
// A tag from the URL that no server has (any more) stays listed, so it can still be switched off.
for (const t of selectedTags) if (!tagCounts.has(t)) tagCounts.set(t, 0);
const allTags = [...tagCounts.keys()].sort((a, b) => a.localeCompare(b, "sv"));
const visibleServers = servers.filter((s) => selectedTags.every((t) => s.tags.includes(t)));
function toggleTag(tag: string) {
const next = selectedTags.includes(tag) ? selectedTags.filter((t) => t !== tag) : [...selectedTags, tag];
setSearchParams(next.length > 0 ? { tag: next } : {}, { replace: true });
}
function exportCsv() {
if (!sortedServers) return;
downloadCsv(
"servers.csv",
["Name", "Hostname", "Token prefix", "Last seen"],
sortedServers.map((s) => [s.name, s.hostname ?? "", s.apiTokenPrefix, s.lastSeenAt ? formatDateTime(new Date(s.lastSeenAt)) : "never"]),
["Name", "Hostname", "Tags", "Token prefix", "Last seen"],
sortedServers.map((s) => [
s.name,
s.hostname ?? "",
s.tags.join(" "),
s.apiTokenPrefix,
s.lastSeenAt ? formatDateTime(new Date(s.lastSeenAt)) : "never",
]),
);
}
@@ -243,6 +266,7 @@ export default function Servers({ user }: { user: CurrentUser }) {
direction={sortDir}
onSort={requestSort}
/>
<th>Tags</th>
<th>Token</th>
<SortableTh<ServerRecord>
label="Last seen"
@@ -261,6 +285,11 @@ export default function Servers({ user }: { user: CurrentUser }) {
<Link to={`/servers/${s.id}`}>{s.name}</Link>
</td>
<td>{s.hostname ?? "—"}</td>
<td>
<div className="d-flex flex-wrap gap-1">
<TagBadges tags={s.tags} />
</div>
</td>
<td className="text-secondary">{s.apiTokenPrefix}…</td>
<td>{s.lastSeenAt ? formatDateTime(new Date(s.lastSeenAt)) : "never"}</td>
<td>
@@ -280,7 +309,7 @@ export default function Servers({ user }: { user: CurrentUser }) {
))}
{sortedServers?.length === 0 && (
<tr>
<td colSpan={5} className="text-secondary text-center">
<td colSpan={6} className="text-secondary text-center">
No servers registered yet.
</td>
</tr>
@@ -307,8 +336,38 @@ export default function Servers({ user }: { user: CurrentUser }) {
</div>
</div>
) : (
<>
{allTags.length > 0 && (
<div className="d-flex flex-wrap align-items-center gap-1 mb-3">
<span className="text-secondary small me-1">Tags</span>
{allTags.map((tag) => {
const active = selectedTags.includes(tag);
return (
<button
key={tag}
type="button"
className={`badge border-0 ${active ? "bg-primary text-white" : tagColorClass(tag)}`}
aria-pressed={active}
onClick={() => toggleTag(tag)}
>
{tag} <span className="opacity-75">· {tagCounts.get(tag)}</span>
</button>
);
})}
{selectedTags.length > 0 && (
<button type="button" className="btn btn-link btn-sm p-0 ms-1" onClick={() => setSearchParams({}, { replace: true })}>
Clear
</button>
)}
</div>
)}
{visibleServers.length === 0 && (
<div className="card">
<div className="card-body text-secondary">No server has {selectedTags.length > 1 ? "all of those tags" : "that tag"}.</div>
</div>
)}
<div className="row row-cards">
{servers.map((s) => (
{visibleServers.map((s) => (
<div className="col-sm-6 col-lg-3" key={s.id}>
<Link to={`/servers/${s.id}`} className="card card-sm text-reset text-decoration-none">
<div className="card-body">
@@ -327,6 +386,11 @@ export default function Servers({ user }: { user: CurrentUser }) {
<span className="badge bg-secondary-lt text-secondary">No data</span>
)}
</div>
{s.tags.length > 0 && (
<div className="d-flex flex-wrap gap-1 mt-2">
<TagBadges tags={s.tags} />
</div>
)}
<div className="text-secondary small mt-2">
{s.lastSeenAt ? `Last seen ${formatDateTime(new Date(s.lastSeenAt))}` : "Never reported"}
</div>
@@ -335,6 +399,7 @@ export default function Servers({ user }: { user: CurrentUser }) {
</div>
))}
</div>
</>
)}
</>
);