2026-04-20 15:10:54 +02:00
|
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { projectsApi, type ProjectFilters } from "@/lib/api/projects";
|
|
|
|
|
import type { ProjectCreate, ProjectUpdate } from "@/types";
|
|
|
|
|
|
|
|
|
|
// Query keys
|
|
|
|
|
export const projectKeys = {
|
|
|
|
|
all: ["projects"] as const,
|
|
|
|
|
lists: () => [...projectKeys.all, "list"] as const,
|
2026-06-29 05:38:21 +02:00
|
|
|
list: (filters?: ProjectFilters) =>
|
|
|
|
|
[...projectKeys.lists(), filters] as const,
|
2026-04-20 15:10:54 +02:00
|
|
|
details: () => [...projectKeys.all, "detail"] as const,
|
|
|
|
|
detail: (id: string) => [...projectKeys.details(), id] as const,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
|
export function useProjects(filters?: ProjectFilters) {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: projectKeys.list(filters),
|
|
|
|
|
queryFn: () => projectsApi.list(filters),
|
|
|
|
|
staleTime: 60000, // 1 minute
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useProject(projectId: string) {
|
|
|
|
|
return useQuery({
|
|
|
|
|
queryKey: projectKeys.detail(projectId),
|
|
|
|
|
queryFn: () => projectsApi.get(projectId),
|
|
|
|
|
enabled: !!projectId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useCreateProject() {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: (project: ProjectCreate) => projectsApi.create(project),
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: projectKeys.lists() });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useUpdateProject() {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
2026-06-29 05:38:21 +02:00
|
|
|
mutationFn: ({
|
|
|
|
|
projectId,
|
|
|
|
|
updates,
|
|
|
|
|
}: {
|
|
|
|
|
projectId: string;
|
|
|
|
|
updates: ProjectUpdate;
|
|
|
|
|
}) => projectsApi.update(projectId, updates),
|
2026-04-20 15:10:54 +02:00
|
|
|
onSuccess: (project) => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: projectKeys.lists() });
|
|
|
|
|
queryClient.setQueryData(projectKeys.detail(project.id), project);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useSetWorkspace() {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
2026-06-29 05:38:21 +02:00
|
|
|
mutationFn: ({
|
|
|
|
|
projectId,
|
|
|
|
|
localPath,
|
|
|
|
|
}: {
|
|
|
|
|
projectId: string;
|
|
|
|
|
localPath: string;
|
|
|
|
|
}) => projectsApi.setWorkspace(projectId, localPath),
|
2026-04-20 15:10:54 +02:00
|
|
|
onSuccess: (project) => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: projectKeys.lists() });
|
|
|
|
|
queryClient.setQueryData(projectKeys.detail(project.id), project);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDeactivateProject() {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
mutationFn: (projectId: string) => projectsApi.deactivate(projectId),
|
|
|
|
|
onSuccess: (project) => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: projectKeys.lists() });
|
|
|
|
|
queryClient.setQueryData(projectKeys.detail(project.id), project);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|