Overview
TheuseCVDraft hook manages CV draft state with automatic saving to localStorage, cloud synchronization, and three-way merge conflict detection. It handles both authenticated and guest user workflows.
Import
Usage
Parameters
Firebase authenticated user object. Pass
null for guest users.Indicates whether authentication state is still loading. Prevents premature data loading.
Template to apply when loading CV data. Overrides stored template preference.Options:
'default' | 'rhyhorn' | 'nexus'Error callback with user-friendly messages and context.Contexts:
'load'- Failed to load CV data'save'- Failed to save to cloud'sync'- Failed to sync after authentication
Return Value
true while initial data is loading. Use to show loading states.true when local and server data conflict. Indicates user needs to resolve conflicts.Manually save current form data to localStorage draft.Behavior:
- Only saves if data has changed
- Updates internal tracking reference
- Safe to call frequently
Save current form data to Firebase cloud storage.Returns:
true on success, false on failureSide effects:- Clears localStorage draft on success
- Updates base version for merge tracking
- Sets
hasPendingSavetofalse
Check for conflicts between local draft and server data.Returns:Use cases:
- Before syncing after login
- When
hasPendingSaveis true - Manual conflict detection
Sync data immediately after user authentication (login/signup).Parameters:
newUser- Newly authenticated Firebase user
- Compares local draft with server data
- If conflict detected, sets
hasPendingSavetotrue - If no conflict, loads server data or uploads local data
- Updates form with resolved data
Features
Auto-Save
Auto-saves draft every 5 seconds when content changes:- Meaningful content exists (personal info, experiences, etc.)
- Template or section order changes
- Data differs from last saved state
Data Validation
Automatically sanitizes and validates loaded data:Conflict Detection
Uses three-way merge strategy:- Base - Last successfully synced version
- Local - Current local draft
- Server - Current server state
- Local draft has no meaningful content
- Local and server data are identical
- User is not authenticated
Error Handling
Provides user-friendly error messages:Dependencies
Requires React Hook Form context:State Management
Internal Refs
Load Priority
- Authenticated users: Server data → Local draft (if conflict)
- Guest users: Local draft only
- After login: Sync local draft with server
Best Practices
Handle Loading States
Resolve Conflicts
Save Before Navigation
Related
- useCVVersions - Version control operations
- CVData - CV data structure
- MergeResult - Conflict resolution types