fix: synchronize skill deletion server-side (#12)

This commit is contained in:
Felipe Domingues 2026-05-30 15:39:17 -03:00 committed by GitHub
parent fd06a4bb07
commit cd4eb53304
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 253 additions and 30 deletions

View file

@ -9,6 +9,7 @@ import { ptBR } from "date-fns/locale";
import { toast } from "sonner";
import { supabase } from "@/integrations/supabase/client";
import { syncAgentSkills } from "@/lib/sync-agent-skills";
import { deleteSkill } from "@/lib/delete-skill";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@ -76,16 +77,37 @@ export function SkillCard({ skill }: { skill: Skill }) {
};
const handleArchive = () => {
updateStatus.mutate("archived", {
onSuccess: async () => {
toast.success("Skill arquivada");
setConfirmArchive(false);
await syncRuntimeAfterMutation("Skill arquivada");
},
onError: (e: unknown) => toast.error(e instanceof Error ? e.message : "Erro ao arquivar"),
});
handleArchiveMutation.mutate("archive");
};
const handleArchiveMutation = useMutation({
mutationFn: async (action: "archive" | "delete") => {
const { data, error } = await deleteSkill(skill.id, action);
if (error) throw new Error(error.message);
if (!data?.success) throw new Error("Erro ao atualizar skill.");
return data;
},
onSuccess: (data, action) => {
if (data.runtime_sync_warning) {
toast.warning(
action === "delete"
? "Skill arquivada; exclusão final do runtime pendente."
: "Skill arquivada no painel; sync com o runtime pendente.",
);
} else {
toast.success(action === "delete" ? "Skill deletada" : "Skill arquivada");
}
if (action === "archive") {
setConfirmArchive(false);
} else {
setConfirmDelete(false);
}
qc.invalidateQueries({ queryKey: ["skills"] });
qc.invalidateQueries({ queryKey: ["user-limits"] });
},
onError: (e: unknown) => toast.error(e instanceof Error ? e.message : "Erro ao atualizar"),
});
const handleRestore = () => {
updateStatus.mutate("draft", {
onSuccess: async () => {
@ -104,15 +126,20 @@ export function SkillCard({ skill }: { skill: Skill }) {
const handleDelete = useMutation({
mutationFn: async () => {
const { error } = await supabase.from("skills").delete().eq("id", skill.id);
if (error) throw error;
const { data, error } = await deleteSkill(skill.id, "delete");
if (error) throw new Error(error.message);
if (!data?.success) throw new Error("Erro ao deletar skill.");
return data;
},
onSuccess: async () => {
toast.success("Skill deletada");
onSuccess: (data) => {
if (data.runtime_sync_warning) {
toast.warning("Skill arquivada; exclusão final do runtime pendente.");
} else {
toast.success("Skill deletada");
}
qc.invalidateQueries({ queryKey: ["skills"] });
qc.invalidateQueries({ queryKey: ["user-limits"] });
setConfirmDelete(false);
await syncRuntimeAfterMutation("Skill deletada");
},
onError: (e: unknown) => toast.error(e instanceof Error ? e.message : "Erro ao deletar"),
});

22
src/lib/delete-skill.ts Normal file
View file

@ -0,0 +1,22 @@
"use client";
import { invokeFunction } from "@/lib/invoke-function";
export interface DeleteSkillResponse {
success: boolean;
skill_id: string;
agent_instance_id: string;
archived: boolean;
deleted: boolean;
runtime_sync_warning: string | null;
}
export async function deleteSkill(
skillId: string,
action: "archive" | "delete" = "archive",
) {
return await invokeFunction<DeleteSkillResponse>("delete-skill", {
skill_id: skillId,
action,
});
}

View file

@ -12,7 +12,7 @@ import { toast } from "sonner";
import { supabase } from "@/integrations/supabase/client";
import { useAuth } from "@/hooks/use-auth";
import { useSkill } from "@/hooks/use-skills";
import { syncAgentSkills } from "@/lib/sync-agent-skills";
import { deleteSkill } from "@/lib/delete-skill";
import { SkillStatusBadge } from "@/components/mika/skills/SkillStatusBadge";
import { SkillTestPanel } from "@/components/mika/skills/SkillTestPanel";
import { Button } from "@/components/ui/button";
@ -171,25 +171,19 @@ function SkillDetailPage() {
// Archive
const archiveSkill = useMutation({
mutationFn: async () => {
const { error } = await supabase
.from("skills")
.update({ status: "archived", updated_at: new Date().toISOString() })
.eq("id", id);
if (error) throw error;
const { data, error } = await deleteSkill(id, "archive");
if (error) throw new Error(error.message);
if (!data?.success) throw new Error("Erro ao arquivar skill.");
return data;
},
onSuccess: () => {
toast.success("Skill arquivada");
onSuccess: (data) => {
if (data.runtime_sync_warning) {
toast.warning("Skill arquivada no painel; sync com o runtime pendente.");
} else {
toast.success("Skill arquivada");
}
qc.invalidateQueries({ queryKey: ["skills"] });
qc.invalidateQueries({ queryKey: ["user-limits"] });
if (skill.data?.agent_instance_id) {
void syncAgentSkills(skill.data.agent_instance_id).then(({ error }) => {
if (error) {
toast.warning("Skill arquivada, mas o sync com o container falhou.", {
description: error.message,
});
}
});
}
navigate({ to: "/painel/skills" });
},
onError: (e: unknown) => toast.error(e instanceof Error ? e.message : "Erro"),