Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions contexts/ResumeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import {
DEFAULT_METADATA,
DEFAULT_PERSONAL_INFO,
SECTION_TYPES,
DeepPartial,
ResumeInsert,
ResumeUpdate,
} from '@/types/resume';
import { Profile } from '@/types/profile';
import { ResumeImportService, ImportResult } from '@/lib/services/resume-import';
import { updateResumeMetadata } from '@/lib/services/resume-metadata';
import { ResumeScoringService, ScoringResult } from '@/lib/services/resume-scoring';
import { saveToLocalStorage, loadFromLocalStorage, removeFromLocalStorage } from '@/lib/storage/offline-storage';
import { saveToLocalStorage, removeFromLocalStorage } from '@/lib/storage/offline-storage';

// Context Type Definition
export interface ResumeContextType {
Expand Down Expand Up @@ -84,7 +83,7 @@ interface ResumeProviderProps {
}

// Helper function to generate unique IDs
const generateId = () => `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const generateId = () => `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;

// Helper function to create default section
const createDefaultSection = (type: SectionType, order: number): ResumeSection => {
Expand Down Expand Up @@ -299,12 +298,12 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
const errorMessage = err instanceof ResumeError ? err.message : 'Failed to save resume';
setError(errorMessage);
setSaveStatus('error');

// Keep in local storage if save failed
if (resume) {
saveToLocalStorage(resume);
}

throw err;
} finally {
setSaving(false);
Expand Down Expand Up @@ -402,10 +401,10 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
const updateResumeTitle = useCallback((title: string) => {
setResume((prev) => {
if (!prev) return null;

// Store previous state for potential rollback
optimisticStateRef.current = prev;

// Apply optimistic update immediately
return { ...prev, title };
});
Expand All @@ -423,10 +422,10 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
const sectionInfo = SECTION_TYPES[type];
setAnnouncement(`${sectionInfo.defaultTitle} section added`);
setLastAddedSectionId(newSection.id);

// Clear the lastAddedSectionId after a short delay
setTimeout(() => setLastAddedSectionId(null), 500);

// Apply optimistic update immediately
return {
...prev,
Expand Down Expand Up @@ -492,7 +491,14 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
return {
...prev,
sections: prev.sections.map((s) =>
s.id === sectionId ? { ...s, content: { ...s.content, ...content } as SectionContent } : s
s.id === sectionId
? {
...s,
content: Array.isArray(s.content) && Array.isArray(content)
? (content as SectionContent) // If both are arrays, replace directly with proper type
: { ...(s.content as object), ...(content as object) } as SectionContent, // Otherwise, merge objects
}
: s
),
};
});
Expand Down Expand Up @@ -721,7 +727,7 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
if (isSavingRef.current || saveQueueRef.current.length === 0) return;

isSavingRef.current = true;

// Get the latest resume from the queue (discard intermediate states)
const latestResume = saveQueueRef.current[saveQueueRef.current.length - 1];
saveQueueRef.current = [];
Expand Down Expand Up @@ -774,21 +780,21 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
const errorMessage = err instanceof ResumeError ? err.message : 'Failed to save resume';
setError(errorMessage);
setSaveStatus('error');

// Rollback to previous state if available
if (optimisticStateRef.current) {
setResume(optimisticStateRef.current);
setAnnouncement('Changes reverted due to save error');
}

// Keep in local storage if save failed
if (latestResume) {
saveToLocalStorage(latestResume);
}
} finally {
setSaving(false);
isSavingRef.current = false;

// Process any new items that were added to the queue while saving
if (saveQueueRef.current.length > 0) {
processSaveQueue();
Expand Down
Loading