♻️ update machine client labels cell

This commit is contained in:
Fred KISSIE
2026-06-03 19:17:24 +02:00
parent 97aeee541a
commit 29717e19db
+10 -44
View File
@@ -39,6 +39,7 @@ import { Badge } from "./ui/badge";
import { ControlledDataTable } from "./ui/controlled-data-table"; import { ControlledDataTable } from "./ui/controlled-data-table";
import { LabelColumnFilterButton } from "./LabelColumnFilterButton"; import { LabelColumnFilterButton } from "./LabelColumnFilterButton";
import { useLocalLabels } from "@app/hooks/useLocalLabels"; import { useLocalLabels } from "@app/hooks/useLocalLabels";
import { useOptimisticLabels } from "@app/hooks/useOptimisticLabels";
export type ClientRow = { export type ClientRow = {
id: number; id: number;
@@ -607,54 +608,19 @@ function MachineClientLabelCell({
client, client,
orgId orgId
}: MachineClientLabelCellProps) { }: MachineClientLabelCellProps) {
const t = useTranslations(); const { localLabels, refresh, toggleLabel } = useOptimisticLabels({
const api = createApiClient(useEnvContext()); serverLabels: client.labels,
const [localLabels, setLocalLabels] = useLocalLabels( orgId,
client.labels, entityId: client.id,
client.id entityIdField: "clientId"
); });
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"
});
}
})();
}
return ( return (
<LabelsTableCell <LabelsTableCell
orgId={orgId} orgId={orgId}
localLabels={localLabels} selectedLabels={localLabels}
toggleLabel={toggleClientLabel} onToggleLabel={toggleLabel}
onClosePopover={() => startTransition(refresh)}
/> />
); );
} }