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
@@ -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 TYPE_LABELS: Record<SecretRecord["type"], string> = {
|
||||
api_token: "API Token",
|
||||
@@ -39,6 +40,8 @@ export default function Secrets({ user }: { user: CurrentUser }) {
|
||||
const [editingId, setEditingId] = useState<number | null>(null);
|
||||
const [form, setForm] = useState<SecretInput>(emptyForm);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const selection = useSelection<number>();
|
||||
const [bulkDeleting, setBulkDeleting] = useState(false);
|
||||
|
||||
function load() {
|
||||
api.secrets
|
||||
@@ -112,6 +115,23 @@ export default function Secrets({ user }: { user: CurrentUser }) {
|
||||
}
|
||||
}
|
||||
|
||||
async function bulkDelete() {
|
||||
const ids = Array.from(selection.selected);
|
||||
if (ids.length === 0) return;
|
||||
if (!confirm(`Delete ${ids.length} secret${ids.length !== 1 ? "s" : ""}?`)) return;
|
||||
setError(null);
|
||||
setBulkDeleting(true);
|
||||
try {
|
||||
const results = await Promise.allSettled(ids.map((id) => api.secrets.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);
|
||||
}
|
||||
}
|
||||
|
||||
const { sorted, sortKey, sortDir, requestSort } = useSortable(filtered);
|
||||
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
|
||||
|
||||
@@ -244,10 +264,31 @@ export default function Secrets({ 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((s) => selection.isSelected(s.id))}
|
||||
onChange={() => selection.toggleAll((pageItems ?? []).map((s) => s.id))}
|
||||
/>
|
||||
</th>
|
||||
)}
|
||||
<SortableTh<SecretRecord> label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<SecretRecord> label="Type" sortKeyName="type" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
||||
<SortableTh<SecretRecord>
|
||||
@@ -278,6 +319,16 @@ export default function Secrets({ user }: { user: CurrentUser }) {
|
||||
<tbody>
|
||||
{(pageItems ?? []).map((s) => (
|
||||
<tr key={s.id}>
|
||||
{canEdit && (
|
||||
<td>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="form-check-input m-0"
|
||||
checked={selection.isSelected(s.id)}
|
||||
onChange={() => selection.toggle(s.id)}
|
||||
/>
|
||||
</td>
|
||||
)}
|
||||
<td>{s.name}</td>
|
||||
<td>{TYPE_LABELS[s.type]}</td>
|
||||
<td>{s.expiryDate}</td>
|
||||
@@ -302,7 +353,7 @@ export default function Secrets({ user }: { user: CurrentUser }) {
|
||||
))}
|
||||
{(sorted ?? []).length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={canEdit ? 7 : 6} className="text-secondary text-center">
|
||||
<td colSpan={canEdit ? 8 : 6} className="text-secondary text-center">
|
||||
No secrets tracked yet.
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
Reference in new issue
Block a user