Files
Claude-Code-Workflow/ccw/frontend/src/hooks/useProjectOverview.ts
catlog22 e5252f8a77 feat: add useApiSettings hook for managing API settings, including providers, endpoints, cache, and model pools
- Implemented hooks for CRUD operations on providers and endpoints.
- Added cache management hooks for cache stats and settings.
- Introduced model pool management hooks for high availability and load balancing.
- Created localization files for English and Chinese translations of API settings.
2026-02-01 23:14:55 +08:00

92 lines
2.7 KiB
TypeScript

// ========================================
// useProjectOverview Hook
// ========================================
// TanStack Query hook for project overview data
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { fetchProjectOverview, updateProjectGuidelines, type ProjectGuidelines } from '../lib/api';
import { useWorkflowStore, selectProjectPath } from '@/stores/workflowStore';
// Query key factory
export const projectOverviewKeys = {
all: ['projectOverview'] as const,
detail: (path?: string) => [...projectOverviewKeys.all, 'detail', path] as const,
};
// Default stale time: 5 minutes
const STALE_TIME = 5 * 60 * 1000;
export interface UseProjectOverviewOptions {
/** Override default stale time (ms) */
staleTime?: number;
/** Enable/disable the query */
enabled?: boolean;
}
/**
* Hook for fetching project overview data
*
* @example
* ```tsx
* const { projectOverview, isLoading } = useProjectOverview();
* ```
*/
export function useProjectOverview(options: UseProjectOverviewOptions = {}) {
const { staleTime = STALE_TIME, enabled = true } = options;
const projectPath = useWorkflowStore(selectProjectPath);
const queryEnabled = enabled && !!projectPath;
const query = useQuery({
queryKey: projectOverviewKeys.detail(),
queryFn: () => fetchProjectOverview(projectPath),
staleTime,
enabled: queryEnabled,
retry: 2,
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 10000),
});
return {
projectOverview: query.data,
isLoading: query.isLoading,
error: query.error,
refetch: query.refetch,
};
}
// ========== Mutations ==========
export interface UseUpdateGuidelinesReturn {
updateGuidelines: (guidelines: ProjectGuidelines) => Promise<{ success: boolean; guidelines?: ProjectGuidelines; error?: string }>;
isUpdating: boolean;
error: Error | null;
}
/**
* Hook for updating project guidelines
*
* @example
* ```tsx
* const { updateGuidelines, isUpdating } = useUpdateGuidelines();
* await updateGuidelines({ conventions: { ... }, constraints: { ... } });
* ```
*/
export function useUpdateGuidelines(): UseUpdateGuidelinesReturn {
const queryClient = useQueryClient();
const projectPath = useWorkflowStore(selectProjectPath);
const mutation = useMutation({
mutationFn: (guidelines: ProjectGuidelines) => updateProjectGuidelines(guidelines, projectPath),
onSuccess: () => {
// Invalidate project overview cache to trigger refetch
queryClient.invalidateQueries({ queryKey: projectOverviewKeys.detail() });
},
});
return {
updateGuidelines: mutation.mutateAsync,
isUpdating: mutation.isPending,
error: mutation.error,
};
}