Refactor: Migrate Global Store to TanStack Query
Overview
The current state management in lib/store.ts relies on globalThis.bountyStore, which introduces issues with Server-Side Rendering (SSR) and lacks modern debugging capabilities.
Since TanStack Query is already part of the project, we should migrate to it for managing both server state and local cached data. This will improve data synchronization, caching, and overall developer experience.
Goals
- Replace
globalThis-based state with TanStack Query
- Centralize server and cache state management
- Improve SSR compatibility
- Leverage built-in caching, invalidation, and devtools
Implementation Details
1. Refactor Existing Store
Update: lib/store.ts
- Deprecate:
- Migrate logic into TanStack Query patterns:
- Use
queryClient for reading/writing cached data
- Replace imperative store methods with query + mutation patterns
2. Update Hooks
hooks/use-bounty.ts
- Replace store access with:
useQuery for fetching bounty data
queryClient.getQueryData for synchronous reads (if needed)
- Ensure proper query keys are used for caching and invalidation
hooks/use-submission-draft.ts
- Replace local store logic with:
useMutation for updates
onSuccess callbacks to update or invalidate relevant queries
- Store draft data in query cache where appropriate
3. Ensure Query Client Provider
Create or update: providers/query-client-provider.tsx
- Ensure a properly configured
QueryClientProvider exists
- Add:
- Global defaults (e.g., stale time, retry behavior)
- Hydration logic for SSR (if applicable)
Files Affected
Modified
lib/store.ts
hooks/use-bounty.ts
hooks/use-submission-draft.ts
Created / Verified
providers/query-client-provider.tsx
Acceptance Criteria
Testing Notes
- Verify data consistency across components
- Test cache updates after mutations
- Validate SSR hydration (if applicable)
- Ensure no regressions in bounty or draft workflows
Additional Notes
- Use structured query keys (e.g.,
['bounty', id]) for scalability
- Avoid overusing
getQueryData; prefer useQuery where possible
- Consider enabling TanStack Query DevTools for debugging
- Be mindful of cache invalidation to prevent stale UI
Refactor: Migrate Global Store to TanStack Query
Overview
The current state management in
lib/store.tsrelies onglobalThis.bountyStore, which introduces issues with Server-Side Rendering (SSR) and lacks modern debugging capabilities.Since TanStack Query is already part of the project, we should migrate to it for managing both server state and local cached data. This will improve data synchronization, caching, and overall developer experience.
Goals
globalThis-based state with TanStack QueryImplementation Details
1. Refactor Existing Store
Update:
lib/store.tsBountyStoreDataclassqueryClientfor reading/writing cached data2. Update Hooks
hooks/use-bounty.tsuseQueryfor fetching bounty dataqueryClient.getQueryDatafor synchronous reads (if needed)hooks/use-submission-draft.tsuseMutationfor updatesonSuccesscallbacks to update or invalidate relevant queries3. Ensure Query Client Provider
Create or update:
providers/query-client-provider.tsxQueryClientProviderexistsFiles Affected
Modified
lib/store.tshooks/use-bounty.tshooks/use-submission-draft.tsCreated / Verified
providers/query-client-provider.tsxAcceptance Criteria
globalThisfor state managementBountyStoreDatais fully deprecateduseQueryuseMutationTesting Notes
Additional Notes
['bounty', id]) for scalabilitygetQueryData; preferuseQuerywhere possible