Scaffold Homelab Manager foundation
Monorepo (Express+TS+Drizzle/libSQL server, React+Vite+Tabler web) matching the stack used by ScheduleTaskManager and Sloth Manager. Includes Authentik OIDC login with local admin/operator/viewer roles (first user becomes admin), a generalized audit log, encrypted-at-rest storage for future integration API tokens, the DB schema for all planned modules, and the Tabler-styled app shell/nav. Also ports the Secrets (expiry tracker) and IP Addresses (IPAM) modules from Sloth Manager onto the new stack. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
commit
6bd2ed52c1
52 files changed
+8103
No files matched your search
@@ -0,0 +1,257 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { api, type CurrentUser, type IpamEntry, type IpamInput } from "../api/client";
|
||||
import { downloadCsv } from "../utils/csv";
|
||||
|
||||
const emptyForm: IpamInput = { ipAddress: "", label: "", vendor: "", location: "", notes: "" };
|
||||
|
||||
function isIpv6(ip: string) {
|
||||
return ip.includes(":");
|
||||
}
|
||||
|
||||
export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
const canEdit = user.role === "admin" || user.role === "operator";
|
||||
const [entries, setEntries] = useState<IpamEntry[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [search, setSearch] = useState("");
|
||||
const [editing, setEditing] = useState<IpamEntry | null>(null);
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [form, setForm] = useState<IpamInput>(emptyForm);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
function load() {
|
||||
api.ipam
|
||||
.list()
|
||||
.then((res) => setEntries(res.entries))
|
||||
.catch((err) => setError(String(err)));
|
||||
}
|
||||
|
||||
useEffect(load, []);
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!entries) return [];
|
||||
const q = search.trim().toLowerCase();
|
||||
if (!q) return entries;
|
||||
return entries.filter((e) =>
|
||||
[e.ipAddress, e.label, e.vendor, e.location].some((v) => (v ?? "").toLowerCase().includes(q)),
|
||||
);
|
||||
}, [entries, search]);
|
||||
|
||||
function startAdd() {
|
||||
setAdding(true);
|
||||
setEditing(null);
|
||||
setForm(emptyForm);
|
||||
}
|
||||
|
||||
function startEdit(entry: IpamEntry) {
|
||||
setEditing(entry);
|
||||
setAdding(false);
|
||||
setForm({
|
||||
ipAddress: entry.ipAddress,
|
||||
label: entry.label ?? "",
|
||||
vendor: entry.vendor ?? "",
|
||||
location: entry.location ?? "",
|
||||
notes: entry.notes ?? "",
|
||||
});
|
||||
}
|
||||
|
||||
function cancelForm() {
|
||||
setAdding(false);
|
||||
setEditing(null);
|
||||
setForm(emptyForm);
|
||||
}
|
||||
|
||||
async function submit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setSaving(true);
|
||||
try {
|
||||
if (editing) {
|
||||
const { ipAddress: _ip, ...rest } = form;
|
||||
await api.ipam.update(editing.id, rest);
|
||||
} else {
|
||||
await api.ipam.create(form);
|
||||
}
|
||||
cancelForm();
|
||||
load();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : String(err));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(entry: IpamEntry) {
|
||||
if (!confirm(`Delete IP address "${entry.ipAddress}"?`)) return;
|
||||
setError(null);
|
||||
try {
|
||||
await api.ipam.remove(entry.id);
|
||||
load();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}
|
||||
|
||||
function exportCsv() {
|
||||
downloadCsv(
|
||||
"ip-addresses.csv",
|
||||
["IP Address", "Label", "Vendor", "Location", "Notes", "DNS Records"],
|
||||
filtered.map((e) => [
|
||||
e.ipAddress,
|
||||
e.label ?? "",
|
||||
e.vendor ?? "",
|
||||
e.location ?? "",
|
||||
e.notes ?? "",
|
||||
e.matchingDnsRecords.join("; "),
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
const showForm = adding || editing;
|
||||
|
||||
return (
|
||||
<>
|
||||
<h2 className="page-title mb-3">IP Addresses</h2>
|
||||
{error && <div className="alert alert-danger">{error}</div>}
|
||||
|
||||
{canEdit && showForm && (
|
||||
<div className="card mb-3">
|
||||
<div className="card-header">
|
||||
<h3 className="card-title">{editing ? `Edit ${editing.ipAddress}` : "Add an IP address"}</h3>
|
||||
</div>
|
||||
<form onSubmit={submit}>
|
||||
<div className="card-body row g-3">
|
||||
<div className="col-md-3">
|
||||
<label className="form-label">IP address</label>
|
||||
<input
|
||||
className="form-control"
|
||||
required
|
||||
disabled={!!editing}
|
||||
placeholder="203.0.113.10 or 2001:db8::1"
|
||||
value={form.ipAddress}
|
||||
onChange={(e) => setForm({ ...form, ipAddress: e.target.value })}
|
||||
/>
|
||||
{!editing && <div className="form-hint">Cannot be changed after creation.</div>}
|
||||
</div>
|
||||
<div className="col-md-3">
|
||||
<label className="form-label">Label</label>
|
||||
<input
|
||||
className="form-control"
|
||||
placeholder="Web Server 1"
|
||||
value={form.label}
|
||||
onChange={(e) => setForm({ ...form, label: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-md-2">
|
||||
<label className="form-label">Vendor / provider</label>
|
||||
<input
|
||||
className="form-control"
|
||||
placeholder="Hetzner"
|
||||
value={form.vendor}
|
||||
onChange={(e) => setForm({ ...form, vendor: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-md-2">
|
||||
<label className="form-label">Location</label>
|
||||
<input
|
||||
className="form-control"
|
||||
placeholder="Frankfurt"
|
||||
value={form.location}
|
||||
onChange={(e) => setForm({ ...form, location: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-md-2">
|
||||
<label className="form-label">Notes</label>
|
||||
<input
|
||||
className="form-control"
|
||||
value={form.notes}
|
||||
onChange={(e) => setForm({ ...form, notes: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="card-footer d-flex gap-2">
|
||||
<button type="submit" className="btn btn-primary" disabled={saving}>
|
||||
{editing ? "Save changes" : "Add IP address"}
|
||||
</button>
|
||||
<button type="button" className="btn" onClick={cancelForm}>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<div className="d-flex flex-wrap gap-2 align-items-center w-100">
|
||||
<input
|
||||
className="form-control"
|
||||
style={{ maxWidth: 280 }}
|
||||
placeholder="Search IP, label, vendor, location…"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
/>
|
||||
{canEdit && !showForm && (
|
||||
<button className="btn btn-primary" onClick={startAdd}>
|
||||
Add IP address
|
||||
</button>
|
||||
)}
|
||||
<button className="btn btn-outline-secondary ms-auto" onClick={exportCsv}>
|
||||
Export CSV
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>IP address</th>
|
||||
<th>Label</th>
|
||||
<th>Vendor</th>
|
||||
<th>Location</th>
|
||||
<th>DNS records</th>
|
||||
{canEdit && <th className="w-1">Actions</th>}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{filtered.map((entry) => (
|
||||
<tr key={entry.id}>
|
||||
<td>
|
||||
<span className={`badge ${isIpv6(entry.ipAddress) ? "bg-purple-lt" : "bg-blue-lt"} me-2`}>
|
||||
{isIpv6(entry.ipAddress) ? "IPv6" : "IPv4"}
|
||||
</span>
|
||||
{entry.ipAddress}
|
||||
</td>
|
||||
<td>{entry.label ?? "—"}</td>
|
||||
<td>{entry.vendor ?? "—"}</td>
|
||||
<td>{entry.location ?? "—"}</td>
|
||||
<td className="text-secondary">
|
||||
{entry.matchingDnsRecords.length > 0 ? entry.matchingDnsRecords.join(", ") : "—"}
|
||||
</td>
|
||||
{canEdit && (
|
||||
<td>
|
||||
<div className="btn-list flex-nowrap">
|
||||
<button className="btn btn-sm" onClick={() => startEdit(entry)}>
|
||||
Edit
|
||||
</button>
|
||||
<button className="btn btn-sm btn-outline-danger" onClick={() => remove(entry)}>
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
)}
|
||||
</tr>
|
||||
))}
|
||||
{filtered.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={canEdit ? 6 : 5} className="text-secondary text-center">
|
||||
No IP addresses tracked yet.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user