Add bulk actions to Secrets, IP Addresses, and Tailscale devices
One row at a time was tedious for anything beyond a handful of items. New useSelection hook (id-based Set, so picks made on page 1 survive moving to page 2 rather than resetting per page) backs a checkbox column and a bulk-actions bar on each of the three tables: - Secrets / IP Addresses: bulk delete. - Tailscale devices: bulk authorize/deauthorize/remove. No new backend endpoints — each bulk action is a client-side Promise.allSettled loop over the existing single-item endpoints, so one failure doesn't block the rest, and the bar reports how many (if any) failed. Selection clears after a bulk action completes or when switching Tailscale integrations (device ids aren't comparable across different tailnets). Verified the selection hook's Set logic directly (extracted as pure functions, no React renderer needed): select-all/deselect-all toggling, individual toggle, and — the case most likely to have a subtle bug — that selecting items on one page and then selecting different items on another page keeps both, with toggleAll on either page only ever touching that page's own ids. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e081eecba4
commit
35979cb043
4 files changed
+216
-3
No files matched your search
+52
-1
@@ -6,6 +6,7 @@ import { useSortable } from "../hooks/useSortable";
|
||||
import SortableTh from "../components/SortableTh";
|
||||
import { usePagination } from "../hooks/usePagination";
|
||||
import Pagination from "../components/Pagination";
|
||||
import { useSelection } from "../hooks/useSelection";
|
||||
|
||||
const emptyForm: IpamInput = { ipAddress: "", label: "", vendor: "", location: "", notes: "" };
|
||||
|
||||
@@ -25,6 +26,8 @@ export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [syncing, setSyncing] = useState<"tailscale" | "proxmox" | null>(null);
|
||||
const [syncResult, setSyncResult] = useState<string | null>(null);
|
||||
const selection = useSelection<number>();
|
||||
const [bulkDeleting, setBulkDeleting] = useState(false);
|
||||
|
||||
function load() {
|
||||
api.ipam
|
||||
@@ -105,6 +108,23 @@ export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
}
|
||||
}
|
||||
|
||||
async function bulkDelete() {
|
||||
const ids = Array.from(selection.selected);
|
||||
if (ids.length === 0) return;
|
||||
if (!confirm(`Delete ${ids.length} IP address${ids.length !== 1 ? "es" : ""}?`)) return;
|
||||
setError(null);
|
||||
setBulkDeleting(true);
|
||||
try {
|
||||
const results = await Promise.allSettled(ids.map((id) => api.ipam.remove(id)));
|
||||
const failed = results.filter((r) => r.status === "rejected").length;
|
||||
if (failed > 0) setError(`${failed} of ${ids.length} deletions failed.`);
|
||||
selection.clear();
|
||||
load();
|
||||
} finally {
|
||||
setBulkDeleting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function runSync(source: "tailscale" | "proxmox") {
|
||||
setError(null);
|
||||
setSyncResult(null);
|
||||
@@ -246,10 +266,31 @@ export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{canEdit && selection.count > 0 && (
|
||||
<div className="card-header bg-blue-lt d-flex align-items-center gap-2">
|
||||
<span>{selection.count} selected</span>
|
||||
<button className="btn btn-sm btn-outline-danger" onClick={bulkDelete} disabled={bulkDeleting}>
|
||||
{bulkDeleting ? "Deleting…" : "Delete selected"}
|
||||
</button>
|
||||
<button className="btn btn-sm btn-link" onClick={selection.clear}>
|
||||
Clear selection
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table">
|
||||
<thead>
|
||||
<tr>
|
||||
{canEdit && (
|
||||
<th className="w-1">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="form-check-input m-0"
|
||||
checked={(pageItems ?? []).length > 0 && (pageItems ?? []).every((e) => selection.isSelected(e.id))}
|
||||
onChange={() => selection.toggleAll((pageItems ?? []).map((e) => e.id))}
|
||||
/>
|
||||
</th>
|
||||
)}
|
||||
<SortableTh<IpamEntry> label="IP address" sortKeyName="ipAddress" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<IpamEntry> label="Label" sortKeyName="label" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<IpamEntry> label="Vendor" sortKeyName="vendor" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
@@ -261,6 +302,16 @@ export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
<tbody>
|
||||
{(pageItems ?? []).map((entry) => (
|
||||
<tr key={entry.id}>
|
||||
{canEdit && (
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="form-check-input m-0"
|
||||
checked={selection.isSelected(entry.id)}
|
||||
onChange={() => selection.toggle(entry.id)}
|
||||
/>
|
||||
</td>
|
||||
)}
|
||||
<td>
|
||||
<span className={`badge ${isIpv6(entry.ipAddress) ? "bg-purple-lt" : "bg-blue-lt"} me-2`}>
|
||||
{isIpv6(entry.ipAddress) ? "IPv6" : "IPv4"}
|
||||
@@ -292,7 +343,7 @@ export default function Ipam({ user }: { user: CurrentUser }) {
|
||||
))}
|
||||
{(sorted ?? []).length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={canEdit ? 6 : 5} className="text-secondary text-center">
|
||||
<td colSpan={canEdit ? 7 : 5} className="text-secondary text-center">
|
||||
No IP addresses tracked yet.
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
Reference in new issue
Block a user