File size: 2,187 Bytes
a968ec4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { api } from '@/lib/api/client'
import toast from 'react-hot-toast'
export interface Project {
id: string
name: string
type: 'image' | 'video' | 'batch'
status: 'draft' | 'processing' | 'completed'
thumbnail: string
filesCount: number
createdAt: string
updatedAt: string
metadata?: {
width?: number
height?: number
duration?: number
format?: string
}
}
export function useProjects(params?: {
page?: number
limit?: number
search?: string
}) {
return useQuery({
queryKey: ['projects', params],
queryFn: () => api.projects.list(params),
staleTime: 5 * 60 * 1000, // 5 minutes
})
}
export function useProject(id: string) {
return useQuery({
queryKey: ['project', id],
queryFn: () => api.projects.get(id),
enabled: !!id,
})
}
export function useCreateProject() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (data: Partial<Project>) => api.projects.create(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['projects'] })
toast.success('Project created successfully')
},
onError: () => {
toast.error('Failed to create project')
},
})
}
export function useUpdateProject() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ id, data }: { id: string; data: Partial<Project> }) =>
api.projects.update(id, data),
onSuccess: (_, { id }) => {
queryClient.invalidateQueries({ queryKey: ['project', id] })
queryClient.invalidateQueries({ queryKey: ['projects'] })
toast.success('Project updated successfully')
},
onError: () => {
toast.error('Failed to update project')
},
})
}
export function useDeleteProject() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (id: string) => api.projects.delete(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['projects'] })
toast.success('Project deleted successfully')
},
onError: () => {
toast.error('Failed to delete project')
},
})
} |