import { useMutation } from "@tanstack/react-query"; import { ENDPOINT } from "@/shared/config"; import { parseFilenameFromResponse } from "../lib/parse-filename-from-response"; import { attachService } from "./attach-service"; const attachBaseKey = [ENDPOINT.ATTACH_SERVICE.ATTACHMENTS] as const; const attachDownloadBaseKey = [ENDPOINT.ATTACH_SERVICE.DOWNLOAD] as const; export function useDeleteAttachment() { return useMutation({ mutationKey: [...attachBaseKey, "delete"], mutationFn: (atflId: string) => attachService.deleteAttachmentByAtflId(atflId), }); } export function useDownloadAttachmentByAtflCd() { return useMutation({ mutationKey: [...attachDownloadBaseKey, "download"], mutationFn: async (atflCd: string) => { const response = await attachService.downloadAttachmentByAtflCd(atflCd); const filename = parseFilenameFromResponse(response); const blob = new Blob([response.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); window.URL.revokeObjectURL(url); document.body.removeChild(link); }, }); }