import { message } from 'antd'; import { observer } from 'mobx-react-lite'; import React from 'react'; import withPageTitle from 'HOCs/withPageTitle'; import withPermissions from 'App/components/hocs/withPermissions'; import { useStore } from 'App/mstore'; import { numberWithCommas } from 'App/utils'; import { Loader, NoContent, Pagination } from 'UI'; import AnimatedSVG, { ICONS } from 'Shared/AnimatedSVG/AnimatedSVG'; import EmptyPage from './EmptyPage'; import SpotListItem from './SpotListItem'; import SpotsListHeader from './SpotsListHeader'; function SpotsList() { const [selectedSpots, setSelectedSpots] = React.useState([]); const [isEmptyState, setIsEmpty] = React.useState(false); const { spotStore } = useStore(); React.useEffect(() => { void spotStore.fetchSpots(); }, []); const onPageChange = (page: number) => { spotStore.setPage(page); void spotStore.fetchSpots(); }; const onDelete = async (spotId: string) => { await spotStore.deleteSpot([spotId]); setSelectedSpots(selectedSpots.filter((s) => s !== spotId)); }; const batchDelete = async () => { const deletedCount = selectedSpots.length; await spotStore.deleteSpot(selectedSpots); setSelectedSpots([]); const remainingItemsOnPage = spotStore.spots.length - deletedCount; if (remainingItemsOnPage <= 0 && spotStore.page > 1) { spotStore.setPage(spotStore.page - 1); await spotStore.fetchSpots(); } else { await spotStore.fetchSpots(); } message.success( `${deletedCount} Spot${deletedCount > 1 ? 's' : ''} deleted successfully.` ); }; const onRename = (id: string, newName: string) => { return spotStore.updateSpot(id, { name: newName }); }; const onVideo = (id: string) => { return spotStore.getVideo(id); }; const handleSelectSpot = (spotId: string, isSelected: boolean) => { if (isSelected) { setSelectedSpots((prev) => [...prev, spotId]); } else { setSelectedSpots((prev) => prev.filter((id) => id !== spotId)); } }; const isSpotSelected = (spotId: string) => selectedSpots.includes(spotId); const clearSelection = () => { setSelectedSpots([]); }; const isLoading = spotStore.isLoading; const isEmpty = isEmptyState || spotStore.total === 0 && spotStore.query === '' return (
setIsEmpty(!isEmptyState)} />
{isEmpty ? ( isLoading ? ( ) : ( ) ) : ( <>
No Matching Results.
} >
{spotStore.spots.map((spot) => ( onDelete(spot.spotId)} onRename={onRename} onVideo={onVideo} onSelect={(checked: boolean) => handleSelectSpot(spot.spotId, checked) } isSelected={isSpotSelected(spot.spotId)} /> ))}
Showing{' '} {(spotStore.page - 1) * spotStore.limit + 1} {' '} to{' '} {(spotStore.page - 1) * spotStore.limit + spotStore.spots.length} {' '} of{' '} {numberWithCommas(spotStore.total)} {' '} spots.
)}
); } export default withPermissions(['SPOT'])(withPageTitle('Spot List - OpenReplay')(observer(SpotsList)));