import React, { useState, useEffect, FormEvent, ChangeEvent } from 'react'; interface InputData { prompt: string; width: number; height: number; steps: number; fps: number; seed?: number; } interface OutputData { prediction: any; timestamp: string; prompt: string; } const TruncatedText = ({ text }: { text: string }) => { const [isTruncated, setIsTruncated] = useState(true); const toggleTruncated = () => { setIsTruncated(!isTruncated); }; if (text.length < 34) { return {text}; } return ( {isTruncated ? `${text.substring(0, 24)}...` : text} {isTruncated ? 'Read More' : 'Read Less'} ); }; const IndexPage = () => { const [inputData, setInputData] = useState({ prompt: '', width: 1024, height: 576, steps: 40, fps: 15, seed: undefined, }); const [output, setOutput] = useState([]); const [isLoading, setIsLoading] = useState(false); useEffect(() => { const savedOutput = localStorage.getItem('output'); if (savedOutput) { setOutput(JSON.parse(savedOutput)); } }, []); const handleChange = (e: ChangeEvent) => { const { name, value } = e.target; setInputData((prevData) => ({ ...prevData, [name]: value })); }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); try { const response = await fetch('/api/replicate', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(inputData), }); const result = await response.json(); if (response.ok) { const timestamp = new Date().toISOString(); const newOutput = { prediction: result.prediction, timestamp, prompt: inputData.prompt, }; const updatedOutput = [...output, newOutput]; setOutput(updatedOutput); localStorage.setItem('output', JSON.stringify(updatedOutput)); setInputData({ prompt: '', width: 1024, height: 576, steps: 40, fps: 15, seed: undefined, }); } } catch (error) { console.error("An error occurred:", error); } finally { setIsLoading(false); } }; return ( Video Generation App Width Height Steps FPS Seed {isLoading ? 'Generating...' : 'Generate'} {output.map((video, index) => ( Generated Video {index + 1}: { event.currentTarget.play(); event.currentTarget.setAttribute("controlsList", "nodownload"); }} onMouseOut={event => { event.currentTarget.pause(); event.currentTarget.removeAttribute("controlsList"); }} > {/* Download Button */} { try { const response = await fetch(video.prediction.output); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'video.mp4'; document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); } catch (error) { console.error("Failed to download the video", error); } }} className="absolute top-0 right-0 bg-green-600 text-white px-3 py-1 rounded" > Download Date: {new Date(video.timestamp).toLocaleDateString()} Time: {new Date(video.timestamp).toLocaleTimeString()} ))} ); }; export default IndexPage;
Date: {new Date(video.timestamp).toLocaleDateString()} Time: {new Date(video.timestamp).toLocaleTimeString()}