import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { reposApi } from '../api'; export function useRepositories(sortBy: 'updated' | 'created' | 'name' = 'updated') { return useQuery({ queryKey: ['repositories', sortBy], queryFn: () => reposApi.listRepositories(sortBy), }); } export function useFiles( owner: string, repo: string, path: string = '', branch: string = '', extensions: string[] = ['md', 'mdx'] ) { return useQuery({ queryKey: ['files', owner, repo, path, branch, extensions], queryFn: () => reposApi.listFiles(owner, repo, path, branch, extensions), enabled: !!owner && !!repo, // Extended cache configuration for performance staleTime: 5 * 60 * 1000, // 5 min - aligns with backend cache gcTime: 10 * 60 * 1000, // 10 min - keep in cache longer refetchOnMount: false, // Don't refetch if data is fresh refetchOnReconnect: false, // Don't refetch on reconnect }); } export function useFileContent( owner: string, repo: string, path: string, branch: string = '' ) { return useQuery({ queryKey: ['file-content', owner, repo, path, branch], queryFn: () => reposApi.getFileContent(owner, repo, path, branch), enabled: !!owner && !!repo && !!path, }); } export function useCreateFile(owner: string, repo: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ path, content, message }: { path: string; content?: string; message?: string }) => reposApi.createFile(owner, repo, path, content, message), onSuccess: () => { // Invalidate and refetch files query and pending changes queryClient.invalidateQueries({ queryKey: ['files', owner, repo] }); queryClient.invalidateQueries({ queryKey: ['pending-changes', owner, repo] }); }, }); } export function useCreateFolder(owner: string, repo: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ path, message }: { path: string; message?: string }) => reposApi.createFolder(owner, repo, path, message), onSuccess: () => { // Invalidate and refetch files query and pending changes queryClient.invalidateQueries({ queryKey: ['files', owner, repo] }); queryClient.invalidateQueries({ queryKey: ['pending-changes', owner, repo] }); }, }); } export function useRenameItem(owner: string, repo: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ oldPath, newPath, message }: { oldPath: string; newPath: string; message?: string }) => reposApi.renameItem(owner, repo, oldPath, newPath, message), onSuccess: () => { // Invalidate and refetch files query and pending changes queryClient.invalidateQueries({ queryKey: ['files', owner, repo] }); queryClient.invalidateQueries({ queryKey: ['pending-changes', owner, repo] }); }, }); } export function usePendingChanges(owner: string, repo: string) { return useQuery({ queryKey: ['pending-changes', owner, repo], queryFn: () => reposApi.getPendingChanges(owner, repo), enabled: !!owner && !!repo, refetchInterval: 5000, // Refetch every 5 seconds to keep in sync }); } export function useDiscardChanges(owner: string, repo: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: () => reposApi.discardChanges(owner, repo), onSuccess: () => { // Invalidate and refetch all related queries queryClient.invalidateQueries({ queryKey: ['pending-changes', owner, repo] }); queryClient.invalidateQueries({ queryKey: ['files', owner, repo] }); queryClient.invalidateQueries({ queryKey: ['branchStatus', owner, repo] }); }, }); }