Feat: Add Edit Label button to Agent Actions

This commit is contained in:
ypratama committed 2026-09-02 17:10:43 +07:00
1 parent 3524588e42
commit 19197ea9fa
3 files changed
+35 -2

No files matched your search

+12 -1
View File
@@ -4,7 +4,7 @@ import { Column } from "@/components/ui/DataTable";
import { Agent } from "@/lib/actions/agentsCore";
import { Badge } from "@/components/ui/Badge";
import { resolveAgentLabel, type ManagedUser } from "@/lib/admin-api";
import { Loader2, Trash2, CheckCircle2, XCircle, ChevronRight, UserCog, Eye, MapPin, Network } from "lucide-react";
import { Loader2, Trash2, CheckCircle2, XCircle, ChevronRight, UserCog, Eye, MapPin, Network, Edit2 } from "lucide-react";
interface GetColumnsProps {
role: string | null;
@@ -18,6 +18,7 @@ interface GetColumnsProps {
handleViewAs: (uuid: string, label: string) => void;
openDelete: (agent: Agent) => void;
handleEditLocation?: (agentUuid: string, agentLabel: string) => void;
handleEditLabel?: (agentUuid: string, currentLabel: string) => void;
handleEditSubnet?: (agentUuid: string, agentLabel: string) => void;
/** Set of agent UUIDs that already have a location saved */
locationUuids?: Set<string>;
@@ -35,6 +36,7 @@ export function getAgentColumns({
handleViewAs,
openDelete,
handleEditLocation,
handleEditLabel,
handleEditSubnet,
locationUuids = new Set(),
}: GetColumnsProps): Column<Agent>[] {
@@ -128,6 +130,15 @@ export function getAgentColumns({
</button>
{(role === 'SUPER_ADMIN' || role === 'TENANT_ADMIN') && (
<>
{handleEditLabel && (
<button
onClick={(e) => { e.stopPropagation(); handleEditLabel(uuid, agentLabel); }}
className="p-1 text-blue-400 hover:text-blue-300 hover:bg-blue-950/30 rounded transition-colors"
title="Edit Label Agent"
>
<Edit2 className="w-4 h-4" />
</button>
)}
<button
onClick={(e) => { e.stopPropagation(); setAccountModalAgent({ uuid, label: agentLabel }); setAccountModalOpen(true); }}
className="p-1 text-emerald-400 hover:text-emerald-300 hover:bg-emerald-950/30 rounded transition-colors"
+2
View File
@@ -60,6 +60,7 @@ export default function AgentsPage() {
handleViewAs,
openDelete,
handleEditLocation,
handleEditLabel,
} = useAgentsData();
const [subnetModalOpen, setSubnetModalOpen] = useState(false);
@@ -82,6 +83,7 @@ export default function AgentsPage() {
handleViewAs,
openDelete,
handleEditLocation,
handleEditLabel,
handleEditSubnet,
locationUuids: new Set(locations.map((l: any) => l.agent_uuid)),
});
+21 -1
View File
@@ -2,7 +2,7 @@
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { getAgents } from "@/lib/actions/agents";
import { getAgents, updateAgent } from "@/lib/actions/agents";
import { Agent } from "@/lib/actions/agentsCore";
import { getAdminUsers, startViewAs, getViewAsHeaders, type ManagedUser } from "@/lib/admin-api";
import { useTimeFilter } from "@/contexts/TimeFilterContext";
@@ -194,6 +194,25 @@ export function useAgentsData() {
setIsLocModalOpen(true);
};
const handleEditLabel = async (agentUuid: string, currentLabel: string) => {
const newLabel = window.prompt("Masukkan label baru untuk agent ini:", currentLabel);
if (newLabel !== null && newLabel.trim() !== "" && newLabel !== currentLabel) {
try {
const result = await updateAgent(agentUuid, newLabel.trim());
if (result.success) {
// Update local state without waiting for full refresh to feel snappy
setAgents(prev => prev.map(a =>
(a.uuid === agentUuid || a.serial === agentUuid) ? { ...a, label: newLabel.trim() } : a
));
} else {
window.alert("Gagal mengubah label: " + result.message);
}
} catch (err: any) {
window.alert("Terjadi kesalahan: " + err.message);
}
}
};
return {
role,
agents,
@@ -236,5 +255,6 @@ export function useAgentsData() {
handleViewAs,
openDelete,
handleEditLocation,
handleEditLabel,
};
}