Files
archon/archon-ui-main/src/components/layout/hooks/useBackendHealth.ts
Wirasm 63a92cf7d7 refactor: reorganize features/shared directory for better maintainability (#730)
* refactor: reorganize features/shared directory structure

- Created organized subdirectories for better code organization:
  - api/ - API clients and HTTP utilities (renamed apiWithEtag.ts to apiClient.ts)
  - config/ - Configuration files (queryClient, queryPatterns)
  - types/ - Shared type definitions (errors)
  - utils/ - Pure utility functions (optimistic, clipboard)
  - hooks/ - Shared React hooks (already existed)

- Updated all import paths across the codebase (~40+ files)
- Updated all AI documentation in PRPs/ai_docs/ to reflect new structure
- All tests passing, build successful, no functional changes

This improves maintainability and follows vertical slice architecture patterns.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: address PR review comments and code improvements

- Update imports to use @/features alias path for optimistic utils
- Fix optimistic upload item replacement by matching on source_id instead of id
- Clean up test suite naming and remove meta-terms from comments
- Only set Content-Type header on requests with body
- Add explicit TypeScript typing to useProjectFeatures hook
- Complete Phase 4 improvements with proper query typing

* fix: address additional PR review feedback

- Clear feature queries when deleting project to prevent cache memory leaks
- Update KnowledgeCard comments to follow documentation guidelines
- Add explanatory comment for accessibility pattern in KnowledgeCard

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-22 14:59:33 +03:00

43 lines
1.5 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { callAPIWithETag } from "../../../features/shared/api/apiClient";
import { createRetryLogic, STALE_TIMES } from "../../../features/shared/config/queryPatterns";
import type { HealthResponse } from "../types";
/**
* Hook to monitor backend health status using TanStack Query
* Uses ETag caching for bandwidth reduction (~70% savings per project docs)
*/
export function useBackendHealth() {
return useQuery<HealthResponse>({
queryKey: ["backend", "health"],
queryFn: ({ signal }) => {
// Use existing ETag infrastructure with timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
// Chain signals: React Query's signal + our timeout
if (signal) {
signal.addEventListener("abort", () => controller.abort());
}
return callAPIWithETag<HealthResponse>("/api/health", {
signal: controller.signal,
}).finally(() => {
clearTimeout(timeoutId);
});
},
// Retry configuration for startup scenarios - respect 4xx but allow more attempts
retry: createRetryLogic(5),
retryDelay: (attemptIndex) => {
// Exponential backoff: 1.5s, 2.25s, 3.375s, etc.
return Math.min(1500 * 1.5 ** attemptIndex, 10000);
},
// Refetch every 30 seconds when healthy
refetchInterval: STALE_TIMES.normal,
// Keep trying to connect on window focus
refetchOnWindowFocus: true,
// Consider data fresh for 30 seconds
staleTime: STALE_TIMES.normal,
});
}