import Heading from '../components/Heading'; import { useState } from 'preact/hooks'; import useSWR from 'swr'; import Button from '../components/Button'; import axios from 'axios'; export default function Export() { const { data: config } = useSWR('config'); const [camera, setCamera] = useState('select'); const [playback, setPlayback] = useState('select'); const [message, setMessage] = useState({ text: '', error: false }); const currentDate = new Date(); currentDate.setHours(0, 0, 0, 0); const offsetMs = currentDate.getTimezoneOffset() * 60 * 1000; const localDate = new Date(currentDate.getTime() - offsetMs); const localISODate = localDate.toISOString().split('T')[0]; const [startDate, setStartDate] = useState(localISODate); const [startTime, setStartTime] = useState('00:00'); const [endDate, setEndDate] = useState(localISODate); const [endTime, setEndTime] = useState('23:59'); const onHandleExport = () => { if (camera == 'select') { setMessage({ text: 'A camera needs to be selected.', error: true }); return; } if (playback == 'select') { setMessage({ text: 'A playback factor needs to be selected.', error: true }); return; } if (!startDate || !startTime || !endDate || !endTime) { setMessage({ text: 'A start and end time needs to be selected', error: true }); return; } const start = new Date(`${startDate}T${startTime}`).getTime() / 1000; const end = new Date(`${endDate}T${endTime}`).getTime() / 1000; if (end <= start) { setMessage({ text: 'The end time must be after the start time.', error: true }); return; } axios .post(`export/${camera}/start/${start}/end/${end}`, { playback }) .then(() => { setMessage({ text: 'Successfully started export. View the file in the /exports folder.', error: false }); }) .catch((error) => { setMessage({ text: `Failed to start export: ${error.response.data.message}`, error: true }); }); }; return (