Skip to content

Conversation

@rohilsurana
Copy link
Member

Summary

Migrate usePreferences hook from REST client to Connect RPC

Behavior Changes

Fetching Behavior

Before (REST client):

  • Used useEffect with fetchPreferences in dependency array - could cause infinite loops if not memoized correctly
  • Manual state management with useState for preferences
  • fetchPreferences() returned Promise<V1Beta1Preference[] | undefined> - required await
  • No caching - every component mount triggered a new network request
  • Errors were caught and logged, but returned empty array [] (no error state exposed)

After (Connect RPC):

  • Uses React Query's useQuery with enabled: autoFetch - prevents infinite loops
  • Automatic caching - subsequent mounts use cached data (no redundant requests)
  • fetchPreferences() returns void - triggers refetch via React Query
  • Cache is shared across all components using this hook
  • React Query provides built-in error state (available via error if exposed)

Update Behavior

Before (REST client):

  • After successful update, directly updated local state with setPreferences(getFormattedData(data))
  • No automatic refetch - components using the same hook elsewhere wouldn't see the update
  • Returned updated preferences array Promise<V1Beta1Preference[] | undefined>
  • On error, logged to console and returned empty array [] (silently failed)

After (Connect RPC):

  • After successful update, invalidates React Query cache for listCurrentUserPreferences
  • Automatic background refetch triggers - all components using this hook see the update
  • Returns Promise<void> - doesn't return the data (fetched automatically)
  • On error, logs to console and re-throws error for caller to handle
  • Runtime type validation added via create(CreateCurrentUserPreferencesRequestSchema, { bodies })

Caching & Synchronization

Before:

  • ❌ No caching - same preferences fetched multiple times
  • ❌ No synchronization - updating in one component didn't update others
  • ❌ Manual state management across components

After:

  • ✅ Automatic caching via React Query
  • ✅ Global synchronization - updates propagate to all components
  • ✅ Shared cache reduces network requests

Status Management

Before:

status: 'idle' | 'fetching' | 'loading'  // manually managed with setState
isLoading: status === 'loading'          // derived from status
isFetching: status === 'fetching'        // derived from status

After:

isFetching: from useQuery                // tracks fetch operations
isLoading: from useMutation             // tracks update operations  
status: computed from both              // 'loading' | 'fetching' | 'idle'

Key Improvements

  1. Eliminates race conditions: React Query handles concurrent requests
  2. Better performance: Caching reduces unnecessary network calls
  3. Global state sync: Updates propagate automatically
  4. Type safety: Runtime validation with protobuf schemas
  5. Cleaner code: No manual state/effect management

@vercel
Copy link

vercel bot commented Oct 17, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
frontier Ready Ready Preview Comment Oct 17, 2025 11:48am

@coveralls
Copy link

Pull Request Test Coverage Report for Build 18591773693

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 36.996%

Totals Coverage Status
Change from base Build 18588917885: 0.0%
Covered Lines: 14678
Relevant Lines: 39675

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants