'use client' import { useState, useEffect } from 'react' import { claimLists, getClaimableLists, type ClaimableList } from '@/lib/migration-actions' import Modal from './Modal' import LoadingSpinner from './LoadingSpinner' interface ClaimListsModalProps { isOpen: boolean onClose: () => void onComplete: () => void } /** * Modal for claiming existing anonymous lists after registration * Requirements: 10.2, 10.5 - Migration UI for users to claim existing lists */ export default function ClaimListsModal({ isOpen, onClose, onComplete }: ClaimListsModalProps) { const [claimableLists, setClaimableLists] = useState([]) const [selectedLists, setSelectedLists] = useState>(new Set()) const [loading, setLoading] = useState(true) const [claiming, setClaiming] = useState(false) const [error, setError] = useState(null) useEffect(() => { if (isOpen) { loadClaimableLists() } }, [isOpen]) const loadClaimableLists = async () => { try { setLoading(true) setError(null) // Get list IDs from localStorage const storedListIds = getStoredListIds() if (storedListIds.length === 0) { setClaimableLists([]) setLoading(false) return } // Fetch lists from database const lists = await getClaimableLists(storedListIds) setClaimableLists(lists) // Select all lists by default setSelectedLists(new Set(lists.map(l => l.id))) } catch (err) { console.error('Error loading claimable lists:', err) setError('Failed to load your lists. Please try again.') } finally { setLoading(false) } } const getStoredListIds = (): string[] => { try { // Check for list IDs in localStorage // This assumes the app previously stored list IDs locally const stored = localStorage.getItem('anonymousListIds') if (stored) { return JSON.parse(stored) } return [] } catch (err) { console.error('Error reading localStorage:', err) return [] } } const toggleListSelection = (listId: string) => { const newSelection = new Set(selectedLists) if (newSelection.has(listId)) { newSelection.delete(listId) } else { newSelection.add(listId) } setSelectedLists(newSelection) } const handleClaimLists = async () => { try { setClaiming(true) setError(null) const listIds = Array.from(selectedLists) const result = await claimLists(listIds) if (result.success) { // Clear localStorage after successful claim localStorage.removeItem('anonymousListIds') onComplete() } else { setError(result.error) } } catch (err) { console.error('Error claiming lists:', err) setError('Failed to claim lists. Please try again.') } finally { setClaiming(false) } } const handleSkip = () => { // User chose not to claim lists localStorage.removeItem('anonymousListIds') onClose() } if (!isOpen) return null return (
{loading ? (
) : claimableLists.length === 0 ? (

No lists found to claim.

) : ( <>

We found {claimableLists.length} list{claimableLists.length !== 1 ? 's' : ''} from your previous session. Would you like to add {claimableLists.length !== 1 ? 'them' : 'it'} to your account?

{error && (
{error}
)}
{claimableLists.map((list) => (
) }