From 29717e19db1af0687b4a313b8f826f2f85ebb531 Mon Sep 17 00:00:00 2001 From: Fred KISSIE Date: Wed, 3 Jun 2026 19:17:24 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20update=20machine=20client?= =?UTF-8?q?=20labels=20cell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/MachineClientsTable.tsx | 54 +++++--------------------- 1 file changed, 10 insertions(+), 44 deletions(-) diff --git a/src/components/MachineClientsTable.tsx b/src/components/MachineClientsTable.tsx index 613c8ecba..eba0c9762 100644 --- a/src/components/MachineClientsTable.tsx +++ b/src/components/MachineClientsTable.tsx @@ -39,6 +39,7 @@ import { Badge } from "./ui/badge"; import { ControlledDataTable } from "./ui/controlled-data-table"; import { LabelColumnFilterButton } from "./LabelColumnFilterButton"; import { useLocalLabels } from "@app/hooks/useLocalLabels"; +import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels"; export type ClientRow = { id: number; @@ -607,54 +608,19 @@ function MachineClientLabelCell({ client, orgId }: MachineClientLabelCellProps) { - const t = useTranslations(); - const api = createApiClient(useEnvContext()); - const [localLabels, setLocalLabels] = useLocalLabels( - client.labels, - client.id - ); - - function toggleClientLabel( - label: SelectedLabel, - action: "attach" | "detach" - ) { - const previousLabels = localLabels; - - void (async () => { - try { - if (action === "attach") { - setLocalLabels([...previousLabels, label]); - await api.put( - `/org/${orgId}/label/${label.labelId}/attach`, - { clientId: client.id } - ); - } else { - setLocalLabels( - previousLabels.filter( - (lb) => lb.labelId !== label.labelId - ) - ); - await api.put( - `/org/${orgId}/label/${label.labelId}/detach`, - { clientId: client.id } - ); - } - } catch (e) { - setLocalLabels(previousLabels); - toast({ - title: t("error"), - description: formatAxiosError(e, t("errorOccurred")), - variant: "destructive" - }); - } - })(); - } + const { localLabels, refresh, toggleLabel } = useOptimisticLabels({ + serverLabels: client.labels, + orgId, + entityId: client.id, + entityIdField: "clientId" + }); return ( startTransition(refresh)} /> ); }