Files
gatehouse-ui/src/hooks/useOrganizations.ts
T
2026-01-16 17:31:25 +10:30

38 lines
1.4 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { api, Organization, ApiError } from "@/lib/api";
import { useAuth } from "@/contexts/AuthContext";
/**
* Custom hook for fetching user organizations using React Query.
* Provides automatic caching and deduplication of API calls.
*
* @returns Query result with organizations data, loading state, and error
*/
export function useOrganizations() {
const { isAuthenticated } = useAuth();
return useQuery<Organization[], ApiError>({
queryKey: ["organizations"],
queryFn: async () => {
console.log('[useOrganizations] Fetching organizations...');
const response = await api.users.organizations();
console.log('[useOrganizations] Response:', response);
console.log('[useOrganizations] Organizations array:', response.organizations);
return response.organizations;
},
// Only fetch when user is authenticated
enabled: isAuthenticated,
// Cache data for 5 minutes (300,000ms) before considering it stale
staleTime: 5 * 60 * 1000,
// Keep cached data in memory for 10 minutes (600,000ms)
gcTime: 10 * 60 * 1000,
// Don't retry on 403 errors (handled by QueryClient default config)
retry: (failureCount, error) => {
if (error instanceof ApiError && error.code === 403) {
return false;
}
return failureCount < 3;
},
});
}