/** * Component: BookDate Settings Widget * Documentation: documentation/features/bookdate.md */ 'use client'; import { useState, useEffect } from 'react'; interface SettingsWidgetProps { isOpen: boolean; onClose: () => void; isOnboarding?: boolean; // If true, this is first-time onboarding onOnboardingComplete?: () => void; // Called when onboarding is saved } export function SettingsWidget({ isOpen, onClose, isOnboarding = false, onOnboardingComplete }: SettingsWidgetProps) { const [libraryScope, setLibraryScope] = useState<'full' | 'rated'>('full'); const [customPrompt, setCustomPrompt] = useState(''); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [successMessage, setSuccessMessage] = useState(null); // Load current preferences useEffect(() => { if (isOpen) { loadPreferences(); } }, [isOpen]); const loadPreferences = async () => { setLoading(true); setError(null); try { const accessToken = localStorage.getItem('accessToken'); const response = await fetch('/api/bookdate/preferences', { headers: { 'Authorization': `Bearer ${accessToken}`, }, }); if (!response.ok) { throw new Error('Failed to load preferences'); } const data = await response.json(); setLibraryScope(data.libraryScope || 'full'); setCustomPrompt(data.customPrompt || ''); } catch (error: any) { console.error('Load preferences error:', error); setError(error.message || 'Failed to load preferences'); } finally { setLoading(false); } }; const handleSave = async () => { setSaving(true); setError(null); setSuccessMessage(null); try { const accessToken = localStorage.getItem('accessToken'); const trimmedPrompt = customPrompt.trim(); const response = await fetch('/api/bookdate/preferences', { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${accessToken}`, }, body: JSON.stringify({ libraryScope, customPrompt: trimmedPrompt || null, // Send null if empty onboardingComplete: isOnboarding ? true : undefined, }), }); if (!response.ok) { const data = await response.json(); throw new Error(data.error || 'Failed to save preferences'); } setSuccessMessage('Preferences saved successfully!'); // If this is onboarding, call the completion callback after a short delay if (isOnboarding && onOnboardingComplete) { setTimeout(() => { onOnboardingComplete(); onClose(); }, 500); } else { // Clear success message after 3 seconds for normal saves setTimeout(() => { setSuccessMessage(null); }, 3000); } } catch (error: any) { console.error('Save preferences error:', error); setError(error.message || 'Failed to save preferences'); } finally { setSaving(false); } }; if (!isOpen) return null; return ( <> {/* Backdrop */}
{/* Settings Panel */}
{/* Header */}

{isOnboarding ? 'Welcome to BookDate!' : 'BookDate Preferences'}

{isOnboarding && (

Customize your recommendations before we begin

)}
{!isOnboarding && ( )}
{/* Loading State */} {loading ? (
) : ( <> {/* Error Message */} {error && (
{error}
)} {/* Success Message */} {successMessage && (
{successMessage}
)} {/* Library Scope */}
{/* Custom Prompt */}