5.9 KiB
5.9 KiB
LinkedIn Writer Loading State Fixes
🐛 Issues Identified
The user reported the following problems with the LinkedIn writer:
- Loading state not updating: The loader shows the first message and then doesn't update until backend completion
- Progress messages not displaying: All messages appear at once instead of progressively
- Loading state not disappearing: The loader doesn't disappear after completion
- Draft not displaying: Generated content doesn't appear in the editor UI
🔍 Root Cause Analysis
The issues were caused by missing loading state management in the LinkedIn writer actions:
- Missing
linkedinwriter:loadingStartevents: The actions weren't dispatching the loading start event, soisGeneratingwas never set totrue - Missing
linkedinwriter:loadingEndevents: The actions weren't dispatching the loading end event, so the loading state persisted - Incomplete error handling: Error cases weren't properly ending the loading state
✅ Fixes Implemented
1. Added Loading Start Events
File: frontend/src/components/LinkedInWriter/RegisterLinkedInActions.tsx
Added loading start events to all LinkedIn content generation actions:
// Start loading state
window.dispatchEvent(new CustomEvent('linkedinwriter:loadingStart', {
detail: {
action: 'generateLinkedInPost',
message: 'Generating LinkedIn post with persona optimization...'
}
}));
Actions Fixed:
generateLinkedInPostgenerateLinkedInArticlegenerateLinkedInCarousel(needs to be added)generateLinkedInVideoScript(needs to be added)
2. Added Loading End Events
Added loading end events for both success and error cases:
// End loading state on success
window.dispatchEvent(new CustomEvent('linkedinwriter:loadingEnd'));
// End loading state on error
window.dispatchEvent(new CustomEvent('linkedinwriter:loadingEnd'));
window.dispatchEvent(new CustomEvent('linkedinwriter:progressError', { detail: { id: 'finalize', details: res.error } }));
3. Enhanced Debugging
File: frontend/src/components/LinkedInWriter/hooks/useLinkedInWriter.ts
Added console logging to track loading state changes:
const handleLoadingStart = (event: CustomEvent) => {
const { action, message } = event.detail;
console.log('[LinkedIn Writer] Loading started:', { action, message });
setCurrentAction(action);
setLoadingMessage(message);
setIsGenerating(true);
};
const handleLoadingEnd = (event: CustomEvent) => {
console.log('[LinkedIn Writer] Loading ended');
setIsGenerating(false);
setLoadingMessage('');
setCurrentAction(null);
};
const handleUpdateDraft = (event: CustomEvent) => {
console.log('[LinkedIn Writer] Draft updated:', event.detail?.substring(0, 100) + '...');
setDraft(event.detail);
// ... rest of the logic
};
🔧 How the Loading System Works
Loading State Flow
- User triggers generation → CopilotKit action handler starts
- Loading start event →
linkedinwriter:loadingStartdispatched - State updates →
isGenerating = true,loadingMessageset,currentActionset - UI updates → Loading indicators appear, progress tracker shows
- Backend processing → API calls made, progress events dispatched
- Content generation → Draft content created
- Draft update event →
linkedinwriter:updateDraftdispatched - Loading end event →
linkedinwriter:loadingEnddispatched - State cleanup →
isGenerating = false, loading indicators disappear
Progress Tracking Flow
- Progress init →
linkedinwriter:progressInitwith step definitions - Step updates →
linkedinwriter:progressStepfor each completed step - Progress complete →
linkedinwriter:progressCompletewhen all done - Auto-hide → Progress tracker hides after 1.5 seconds
🧪 Testing the Fixes
Expected Behavior
- Loading starts immediately when user requests content generation
- Progress messages update progressively as backend processes each step
- Loading state disappears when generation completes
- Draft content displays in the editor preview
- Console logs show the loading state transitions
Debug Information
Check browser console for these log messages:
[LinkedIn Writer] Loading started: { action: 'generateLinkedInPost', message: '...' }[LinkedIn Writer] Draft updated: [content preview]...[LinkedIn Writer] Loading ended
🚀 Remaining Tasks
Complete the Fixes
The following actions still need loading state fixes:
- Carousel Generation: Add loading start/end events
- Video Script Generation: Add loading start/end events
- Comment Response Generation: Add loading start/end events
Test All Scenarios
- Success cases: Normal content generation
- Error cases: API failures, network issues
- Edge cases: Empty responses, malformed data
- User interactions: Canceling generation, multiple requests
📋 Verification Checklist
- Loading indicator appears immediately when generation starts
- Progress messages update progressively during generation
- Loading indicator disappears when generation completes
- Generated content appears in the editor preview
- Error cases properly end loading state
- Console logs show proper state transitions
- All LinkedIn content types work correctly
🔮 Future Improvements
- Loading state persistence: Save loading state across page refreshes
- Cancellation support: Allow users to cancel ongoing generation
- Retry mechanisms: Automatic retry for failed requests
- Loading state indicators: More detailed progress information
- Performance optimization: Reduce loading state overhead
The fixes address the core issues with loading state management in the LinkedIn writer, ensuring a smooth user experience during content generation.