- Table search, status, tag and sort live in the address; Save view pins one as a favourite. - Dashboard cards can be moved and hidden per user (users.dashboard_layout, migration 0022). - Compare two to four servers' load, memory or disk on one chart. - j/k move through table rows that open a detail panel. - Share links open a read-only wall without signing in, numbers only or with names (share_links, migration 0023); the wall is split into a shared view over two data sources. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
253 lines
9.9 KiB
TypeScript
253 lines
9.9 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { api, type CurrentUser, type DomainList, type DomainRecord } from "../api/client";
|
|
import { downloadCsv } from "../utils/csv";
|
|
import { formatDateTime } from "../utils/date";
|
|
import { formatAgo } from "../utils/duration";
|
|
import { readableError } from "../utils/errors";
|
|
import { useSortable } from "../hooks/useSortable";
|
|
import { usePagination } from "../hooks/usePagination";
|
|
import SortableTh from "../components/SortableTh";
|
|
import Pagination from "../components/Pagination";
|
|
import { confirmDialog } from "../utils/dialogs";
|
|
import { ErrorNotice } from "../components/Notice";
|
|
import { SearchBox } from "../components/TableToolbar";
|
|
import { useTableSearch } from "../hooks/useTableSearch";
|
|
|
|
function statusBadge(d: DomainRecord) {
|
|
switch (d.status) {
|
|
case "expired":
|
|
return <span className="badge bg-red-lt text-red">Expired {Math.abs(d.daysLeft ?? 0)} d ago</span>;
|
|
case "expiring":
|
|
return <span className="badge bg-yellow-lt text-yellow">{d.daysLeft} d left</span>;
|
|
case "ok":
|
|
return <span className="badge bg-green-lt text-green">{d.daysLeft} d left</span>;
|
|
default:
|
|
return <span className="badge bg-secondary-lt text-secondary">Unknown</span>;
|
|
}
|
|
}
|
|
|
|
export default function Domains({ user }: { user: CurrentUser }) {
|
|
const canEdit = user.role === "admin" || user.role === "operator";
|
|
const [data, setData] = useState<DomainList | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|
const [name, setName] = useState("");
|
|
const [adding, setAdding] = useState(false);
|
|
const [refreshingAll, setRefreshingAll] = useState(false);
|
|
const [checkingId, setCheckingId] = useState<number | null>(null);
|
|
|
|
useEffect(() => {
|
|
api.domains
|
|
.list()
|
|
.then(setData)
|
|
.catch((err) => setError(readableError(err)));
|
|
}, []);
|
|
|
|
const tableSearch = useTableSearch(data?.domains);
|
|
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "expiresAt", "asc", { url: true });
|
|
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
|
|
|
|
async function add(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setError(null);
|
|
setNotice(null);
|
|
setAdding(true);
|
|
try {
|
|
const res = await api.domains.add(name.trim());
|
|
setName("");
|
|
if (res.resolvedFrom) setNotice(`${res.resolvedFrom} is part of ${res.domain.name}, so that's the registration being tracked.`);
|
|
setData(await api.domains.list());
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setAdding(false);
|
|
}
|
|
}
|
|
|
|
async function refreshAll() {
|
|
setError(null);
|
|
setNotice(null);
|
|
setRefreshingAll(true);
|
|
try {
|
|
setData(await api.domains.refresh());
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setRefreshingAll(false);
|
|
}
|
|
}
|
|
|
|
async function check(d: DomainRecord) {
|
|
setError(null);
|
|
setNotice(null);
|
|
setCheckingId(d.id);
|
|
try {
|
|
await api.domains.check(d.id);
|
|
setData(await api.domains.list());
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setCheckingId(null);
|
|
}
|
|
}
|
|
|
|
async function remove(d: DomainRecord) {
|
|
if (!(await confirmDialog({ title: "Stop tracking domain", message: `Stop tracking ${d.name}?`, confirmLabel: "Stop tracking", danger: true }))) return;
|
|
setError(null);
|
|
try {
|
|
await api.domains.remove(d.id);
|
|
setData(await api.domains.list());
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
}
|
|
}
|
|
|
|
function exportCsv() {
|
|
if (!sorted) return;
|
|
downloadCsv(
|
|
"domains.csv",
|
|
["Domain", "Expires", "Days left", "Registrar", "Source", "Origin", "Last checked", "Problem"],
|
|
sorted.map((d) => [
|
|
d.name,
|
|
d.expiresAt ?? "",
|
|
d.daysLeft ?? "",
|
|
d.registrar ?? "",
|
|
d.lookupSource ?? "",
|
|
d.origin === "zone" ? "DNS zone" : "manual",
|
|
d.lastCheckedAt ? formatDateTime(new Date(d.lastCheckedAt)) : "",
|
|
d.lastCheckError ?? "",
|
|
]),
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="d-flex align-items-center mb-3">
|
|
<h2 className="page-title mb-0">Domains</h2>
|
|
{canEdit && (
|
|
<button className="btn btn-outline-secondary ms-auto" onClick={refreshAll} disabled={refreshingAll}>
|
|
{refreshingAll ? "Checking…" : "Check all now"}
|
|
</button>
|
|
)}
|
|
</div>
|
|
<div className="text-secondary mb-3">
|
|
When each domain registration expires, read from the registry itself. Domains for the DNS zones you've added show up
|
|
here automatically; add any others by hand.
|
|
</div>
|
|
<ErrorNotice error={error} />
|
|
{notice && <div className="alert alert-info">{notice}</div>}
|
|
|
|
{canEdit && (
|
|
<form onSubmit={add} className="card mb-3">
|
|
<div className="card-body row g-2 align-items-end">
|
|
<div className="col-md-6">
|
|
<label className="form-label">Track another domain</label>
|
|
<input
|
|
className="form-control"
|
|
required
|
|
placeholder="e.g. example.se"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="col-md-3">
|
|
<button type="submit" className="btn btn-primary" disabled={adding || !name.trim()}>
|
|
{adding ? "Looking it up…" : "Add domain"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
)}
|
|
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">Registrations</h3>
|
|
<SearchBox value={tableSearch.query} onChange={tableSearch.setQuery} className="ms-3" />
|
|
<div className="card-actions">
|
|
<button className="btn btn-sm btn-outline-secondary" onClick={exportCsv} disabled={!sorted || sorted.length === 0}>
|
|
Export CSV
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="table-responsive">
|
|
<table className="table table-vcenter card-table">
|
|
<thead>
|
|
<tr>
|
|
<SortableTh<DomainRecord> label="Domain" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<DomainRecord> label="Expires" sortKeyName="expiresAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<DomainRecord> label="Registrar" sortKeyName="registrar" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<DomainRecord> label="Last checked" sortKeyName="lastCheckedAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
{canEdit && <th className="w-1">Actions</th>}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{pageItems?.map((d) => (
|
|
<tr key={d.id}>
|
|
<td>
|
|
{d.name}
|
|
<span className="badge bg-secondary-lt text-secondary ms-2">{d.origin === "zone" ? "DNS zone" : "Manual"}</span>
|
|
</td>
|
|
<td>
|
|
{d.expiresAt ? (
|
|
<>
|
|
{statusBadge(d)} <span className="text-secondary small ms-1">{d.expiresAt}</span>
|
|
</>
|
|
) : (
|
|
<span className="text-secondary">—</span>
|
|
)}
|
|
</td>
|
|
<td className="text-secondary">
|
|
{d.registrar ?? "—"}
|
|
{d.lookupSource && <span className="small ms-1">· {d.lookupSource.toUpperCase()}</span>}
|
|
</td>
|
|
<td>
|
|
{d.lastCheckedAt ? (
|
|
<span className="text-secondary" title={formatDateTime(new Date(d.lastCheckedAt))}>
|
|
{formatAgo(d.lastCheckedAt)}
|
|
</span>
|
|
) : (
|
|
<span className="text-secondary">not yet</span>
|
|
)}
|
|
{d.lastCheckError && <div className={`small ${d.expiresAt ? "text-warning" : "text-secondary"}`}>{d.lastCheckError}</div>}
|
|
</td>
|
|
{canEdit && (
|
|
<td>
|
|
<div className="btn-list flex-nowrap">
|
|
<button className="btn btn-sm btn-outline-secondary" onClick={() => check(d)} disabled={checkingId === d.id}>
|
|
{checkingId === d.id ? "Checking…" : "Check now"}
|
|
</button>
|
|
{d.origin === "manual" && (
|
|
<button className="btn btn-sm btn-outline-danger" onClick={() => remove(d)}>
|
|
Remove
|
|
</button>
|
|
)}
|
|
</div>
|
|
</td>
|
|
)}
|
|
</tr>
|
|
))}
|
|
{data && data.domains.length === 0 && (
|
|
<tr>
|
|
<td colSpan={canEdit ? 5 : 4} className="text-secondary text-center">
|
|
No domains tracked yet. Ones for your DNS zones appear after the next daily check
|
|
{canEdit ? " — or use “Check all now”" : ""}.
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<Pagination page={page} pageCount={pageCount} totalCount={totalCount} onPageChange={setPage} />
|
|
</div>
|
|
|
|
{data && (
|
|
<div className="text-secondary small">
|
|
Expiry is read daily from the registry — over RDAP where the TLD offers it, otherwise from its WHOIS server (.se and
|
|
.nu, for example). You're reminded every day from {data.warnDays} days before a domain expires until it's renewed. A
|
|
registry that doesn't publish expiry dates (.de, .eu) can be tracked but has no date to warn about.
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|