feat(06-03): create useFirstRunState derived hook
- Derives first-run state from cached useCategories + useTemplate queries
- Returns { isFirstRun, loading } with no additional network calls
- isFirstRun true when categories or template items array is empty
- Loading guard prevents spurious redirects during fetch
This commit is contained in:
28
src/hooks/useFirstRunState.ts
Normal file
28
src/hooks/useFirstRunState.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useCategories } from "@/hooks/useCategories"
|
||||
import { useTemplate } from "@/hooks/useTemplate"
|
||||
|
||||
/**
|
||||
* Derives first-run state from cached category and template queries.
|
||||
* No additional network calls are made — data is read from React Query cache.
|
||||
*
|
||||
* isFirstRun is true when:
|
||||
* - categories.length === 0 (user has not created any categories), OR
|
||||
* - items.length === 0 (user has no template items)
|
||||
*
|
||||
* IMPORTANT: Always check `loading` before acting on `isFirstRun`.
|
||||
* While queries are in flight, both arrays default to [] which would
|
||||
* cause isFirstRun to be true spuriously.
|
||||
*
|
||||
* Usage:
|
||||
* const { isFirstRun, loading } = useFirstRunState()
|
||||
* if (!loading && isFirstRun) { redirect to /setup }
|
||||
*/
|
||||
export function useFirstRunState() {
|
||||
const { categories, loading: catLoading } = useCategories()
|
||||
const { items, loading: tmplLoading } = useTemplate()
|
||||
|
||||
return {
|
||||
isFirstRun: categories.length === 0 || items.length === 0,
|
||||
loading: catLoading || tmplLoading,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user