From d0817427c1d84e4999ca4faf1e36fe7dc8ee5cc1 Mon Sep 17 00:00:00 2001 From: Leopoldadmin Date: Sat, 4 Jul 2026 00:29:39 +0000 Subject: [PATCH] feat(core): add attachment API hooks --- frontend/src/api/hooks.ts | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/frontend/src/api/hooks.ts b/frontend/src/api/hooks.ts index eb47577..fc92af1 100644 --- a/frontend/src/api/hooks.ts +++ b/frontend/src/api/hooks.ts @@ -725,3 +725,59 @@ export function useCreateSequence() { }, }); } + +// Attachments +export interface Attachment { + id: string; + entity_type: string; + entity_id: string; + filename: string; + file_path: string; + mime_type: string; + file_size: number; + uploaded_by?: string | null; + created_at?: string; + updated_at?: string; +} + +export function useAttachments(entityType?: string, entityId?: string) { + return useQuery({ + queryKey: ['attachments', entityType, entityId], + queryFn: () => { + const params = new URLSearchParams(); + if (entityType) params.set('entity_type', entityType); + if (entityId) params.set('entity_id', entityId); + return apiGet<{ items: Attachment[]; total: number }>(`/attachments?${params.toString()}`); + }, + enabled: !!entityType && !!entityId, + }); +} + +export function useUploadAttachment() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: async ({ file, entityType, entityId }: { file: File; entityType: string; entityId: string }) => { + const formData = new FormData(); + formData.append('file', file); + formData.append('entity_type', entityType); + formData.append('entity_id', entityId); + const response = await apiClient.post('/attachments', formData, { + headers: { 'Content-Type': 'multipart/form-data' }, + }); + return response.data; + }, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['attachments'] }); + }, + }); +} + +export function useDeleteAttachment() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: (id: string) => apiDelete(`/attachments/${id}`), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['attachments'] }); + }, + }); +}