Skip to content

Migrate manual data fetching to React Query hooks #89

Description

@JohnRDOrazio

Context

The codebase already uses React Query (@tanstack/react-query v5) in 5 hooks (analytics, search, cross-references, etc.), but 13 hooks and most page components still use manual useState/useEffect fetch patterns. This causes duplicate network requests and inconsistent caching.

The most impactful example: projectApi.get() is called independently from 9 different page components — navigating between project pages triggers redundant fetches that React Query's shared cache would eliminate.

Phase 1: useProject hook (completed in PR #86)

  • Create useProject(projectId) hook wrapping projectApi.get() in useQuery
  • Create derivePermissions() helper to centralize permission logic
  • Migrate viewer page, dashboard, pull-requests, suggestions, suggestion review, and useProjectViewer (6 of 9 call sites)
  • Migrate settings page (3 remaining calls — complex due to post-mutation state updates)
  • Add invalidateQueries calls after project mutations in settings page

Phase 2: Secondary Data Queries (future)

  • Migrate useRemoteSync.ts to useQuery + refetchInterval for polling
  • Extract PR count, lint summary, and normalization status fetches from useProjectViewer into standalone useQuery hooks
  • Create factory hooks for projectOntologyApi searches

Phase 3: Complex Queries (optional, higher risk)

  • Evaluate refactoring useOntologyTree.ts to use React Query with per-node queries
  • Evaluate refactoring useGraphData.ts to coordinate multiple queries
  • Implement proper invalidateQueries strategy for entity create/update/delete mutations

Not suitable for migration

  • useSuggestionSession.ts — state machine with strict lifecycle ordering
  • useAutoSave.ts / useEntityAutoSave.ts — local-first draft management
  • useTreeDragDrop.ts — optimistic UI with manual rollback
  • useKeyboardShortcuts.ts, useFilteredTree.ts — UI state, not data fetching

Benefits

  • Deduplication — redundant projectApi.get() calls share a single cache entry
  • Stale-while-revalidate — instant page transitions with background refresh
  • DevTools — React Query DevTools for debugging query state and cache
  • Automatic refetch — on window focus/reconnect
  • Simplified error handling — centralized via query error state

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions