import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { ClassificationConfigView, type OrganizeSourceInput, OrganizeSourceList, OrganizeSourceView, OrganizeStockResult, } from "@umt/contracts"; import { z } from "zod"; import { apiParsed } from "./parse.ts"; export function useOrganizeSources() { return useQuery({ queryKey: ["organize-sources"], queryFn: ({ signal }) => apiParsed(OrganizeSourceList, "/organize-sources", { signal }), refetchInterval: 10_000, }); } export function useSaveOrganizeSource() { const qc = useQueryClient(); return useMutation({ mutationFn: ({ id, body }: { id: string; body: OrganizeSourceInput }) => apiParsed(OrganizeSourceView, `/organize-sources/${id}`, { method: "PUT", body }), onSuccess: () => qc.invalidateQueries({ queryKey: ["organize-sources"] }), }); } export function useRemoveOrganizeSource() { const qc = useQueryClient(); return useMutation({ mutationFn: (id: string) => apiParsed(z.object({ deleted: z.boolean() }), `/organize-sources/${id}`, { method: "DELETE" }), onSuccess: () => qc.invalidateQueries({ queryKey: ["organize-sources"] }), }); } export function useOrganizeStock() { return useMutation({ mutationFn: ({ id, path }: { id: string; path?: string }) => apiParsed(OrganizeStockResult, `/organize-sources/${id}/stock`, { method: "POST", body: { path } }), }); } export function useClassification() { return useQuery({ queryKey: ["classification"], queryFn: ({ signal }) => apiParsed(ClassificationConfigView, "/classification", { signal }), }); } export function useSaveClassification() { const qc = useQueryClient(); return useMutation({ mutationFn: (yaml: string) => apiParsed(ClassificationConfigView, "/classification", { method: "PUT", body: { yaml } }), onSuccess: (data) => qc.setQueryData(["classification"], data), }); }