import { h } from 'preact'; import { Link } from 'preact-router/match'; import { closestTo, format, isEqual, parseISO } from 'date-fns'; import ActivityIndicator from '../components/ActivityIndicator'; import Button from '../components/Button'; import Calendar from '../components/Calendar'; import Carousel from '../components/Carousel'; import Heading from '../components/Heading'; import VideoPlayer from '../components/VideoPlayer'; import { FetchStatus, useApiHost, useRecording } from '../api'; export default function Recording({ camera, date, hour }) { const apiHost = useApiHost(); const { data, status } = useRecording(camera); if (status !== FetchStatus.LOADED) { return ; } const recordingDates = data.map((item) => item.date); const selectedDate = closestTo( date ? parseISO(date) : new Date(), recordingDates.map((i) => parseISO(i)) ); const selectedKey = format(selectedDate, 'yyyy-MM-dd'); const [year, month, day] = selectedKey.split('-'); const calendar = []; const buttons = []; const playlist = []; const hours = []; for (const item of data) { const date = parseISO(item.date); const events = item.recordings.map((i) => i.events); calendar.push( a + b)} selected={isEqual(selectedDate, date)} /> ); if (item.date == selectedKey) { for (const recording of item.recordings) { buttons.push( ); playlist.push({ name: `${selectedKey} ${recording.hour}:00`, description: `${camera} recording @ ${recording.hour}:00.`, sources: [ { src: `${apiHost}/vod/${year}-${month}/${day}/${recording.hour}/${camera}/index.m3u8`, type: 'application/vnd.apple.mpegurl', }, ], }); hours.push(recording.hour); } } } const selectedHour = hours.indexOf(hour); if (this.player !== undefined) { this.player.playlist([]); this.player.playlist(playlist); this.player.playlist.autoadvance(0); if (selectedHour !== -1) { this.player.playlist.currentItem(selectedHour); } } const selectDate = (flkty) => { flkty.select(recordingDates.indexOf(selectedKey), false, true); }; const selectHour = (flkty) => { flkty.select(selectedHour, false, true); }; return (
{camera} Recordings {calendar} { if (player.playlist) { player.playlist(playlist); player.playlist.autoadvance(0); if (selectedHour !== -1) { player.playlist.currentItem(selectedHour); } this.player = player; } }} /> {buttons}
); }