import { useState } from 'react' import { RotateCcw, Trash2 } from 'lucide-react' import { useMutation, useQueryClient } from '@tanstack/react-query' import { usePhotoStore } from '../../store/photoStore' import { useFilterStore } from '../../store/filterStore' import { usePhotosQuery } from '../../hooks/usePhotosQuery' import { discard as discardApi, photos as photosApi } from '../../services/api' import { toast } from '../ToastContainer' import { ConfirmDialog } from '../dialogs/ConfirmDialog' import { registerUndoable } from '../../store/undoStore' /** * Top-of-timeline bar visible only when the discarded filter is active. * Shows a count, lets the user restore the current selection, and lets them * permanently empty the discard pile (with confirmation). */ export function DiscardActionBar() { const flag = useFilterStore((s) => s.flag) const selectedPhotos = usePhotoStore((s) => s.selectedPhotos) const clearSelection = usePhotoStore((s) => s.clearSelection) const queryClient = useQueryClient() const { data: photos = [] } = usePhotosQuery() const [confirmOpen, setConfirmOpen] = useState(false) const [deleteSelectedOpen, setDeleteSelectedOpen] = useState(false) const restoreMutation = useMutation({ mutationFn: (ids: string[]) => discardApi.restore(ids), onSuccess: (_, ids) => { registerUndoable( `Restored ${ids.length} photo${ids.length === 1 ? '' : 's'}`, async () => { await photosApi.bulkDiscard(ids) queryClient.invalidateQueries({ queryKey: ['photos'] }) } ) clearSelection() queryClient.invalidateQueries({ queryKey: ['photos'] }) }, onError: (e: any) => toast.error('Restore failed', e.message || 'Unknown error'), }) const deleteSelectedMutation = useMutation({ mutationFn: (ids: string[]) => discardApi.deletePermanent(ids), onSuccess: (data: any) => { const count = data?.deleted ?? 0 const errors = data?.file_errors ?? 0 if (errors > 0) { toast.error( `Deleted with ${errors} error${errors > 1 ? 's' : ''}`, `${count} record${count === 1 ? '' : 's'} deleted; some files could not be removed` ) } else { toast.success( 'Permanently deleted', `${count} photo${count === 1 ? '' : 's'} removed from disk` ) } clearSelection() queryClient.invalidateQueries({ queryKey: ['photos'] }) setDeleteSelectedOpen(false) }, onError: (e: any) => toast.error('Delete failed', e.message || 'Unknown error'), }) const emptyMutation = useMutation({ mutationFn: () => discardApi.empty(), onSuccess: (data: any) => { const count = data?.deleted ?? 0 const errors = data?.file_errors ?? 0 if (errors > 0) { toast.error( `Emptied with ${errors} error${errors > 1 ? 's' : ''}`, `${count} record${count > 1 ? 's' : ''} deleted; some files could not be removed` ) } else { toast.success('Discard pile emptied', `${count} photo${count > 1 ? 's' : ''} permanently deleted`) } clearSelection() queryClient.invalidateQueries({ queryKey: ['photos'] }) setConfirmOpen(false) }, onError: (e: any) => toast.error('Empty failed', e.message || 'Unknown error'), }) if (flag !== 'discarded') return null const total = photos.length const selected = selectedPhotos.length return ( <>