import React, { useState, useEffect, useCallback } from 'react'; import type { SlidevControlsProps } from '../types/slidev'; import styles from '../styles/slidev-controls.module.css'; // Mock API URL - will be replaced with environment variable const MOTIA_API_URL = process.env.NEXT_PUBLIC_MOTIA_API_URL || 'http://localhost:3000'; /** * SlidevControls Component * * Manages the generation and control of Slidev presentations. * Features: * - Generate deck with loading states (30-60s GitHub Pages publish time) * - Display GitHub Pages deck URL * - Control buttons (next, prev, goto) * - Error handling with user-friendly messages * - Progress indicators */ export const SlidevControls: React.FC = ({ sessionId, onDeckGenerated, onError, }) => { const [deckUrl, setDeckUrl] = useState(null); const [deckId, setDeckId] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [progress, setProgress] = useState(0); const [currentSlide, setCurrentSlide] = useState('1'); // Simulate progress during loading (GitHub Pages publish takes 30-60s) useEffect(() => { if (!loading) { setProgress(0); return; } const interval = setInterval(() => { setProgress((prev) => { // Gradually increase progress, but never reach 100% until complete if (prev >= 90) return prev; return prev + Math.random() * 5; }); }, 1000); return () => clearInterval(interval); }, [loading]); const handleGenerate = useCallback(async () => { setLoading(true); setError(null); setProgress(0); try { // TODO: Replace with actual API call when Stream A completes const response = await fetch(`${MOTIA_API_URL}/api/slidev/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ sessionId, templateId: 'starter', data: { company: 'Demo Company', industry: 'SaaS', product: 'AI-powered platform', features: ['Feature 1', 'Feature 2', 'Feature 3'], }, }), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.error || 'Failed to generate presentation'); } const result = await response.json(); setDeckUrl(result.deckUrl); setDeckId(result.deckId); setProgress(100); if (onDeckGenerated) { onDeckGenerated(result.deckUrl, result.deckId); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'An unexpected error occurred'; setError(errorMessage); if (onError) { onError(errorMessage); } } finally { setLoading(false); } }, [sessionId, onDeckGenerated, onError]); const handleControl = useCallback(async (command: 'next' | 'prev' | 'goto', slideId?: string) => { if (!deckId) return; try { // TODO: Replace with actual API call when Stream A completes const response = await fetch(`${MOTIA_API_URL}/api/slidev/control`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ deckId, command, slideId, }), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.error || 'Failed to send control command'); } // Update current slide tracking if (command === 'goto' && slideId) { setCurrentSlide(slideId); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to control presentation'; setError(errorMessage); if (onError) { onError(errorMessage); } } }, [deckId, onError]); const handleGotoSlide = useCallback(() => { const slideNum = parseInt(currentSlide, 10); if (!isNaN(slideNum) && slideNum > 0) { void handleControl('goto', currentSlide); } }, [currentSlide, handleControl]); return (
{/* Generation State */} {!deckUrl && !loading && ( )} {/* Loading State */} {loading && (
Generating Presentation...
Publishing to GitHub Pages. This typically takes 30-60 seconds. Please wait while we prepare your presentation.
)} {/* Deck Ready State */} {deckUrl && !loading && ( <>
Presentation Ready
{deckUrl}
{/* Control Buttons */}
setCurrentSlide(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleGotoSlide()} className={styles.gotoInput} placeholder="Slide #" aria-label="Go to slide number" title="Enter slide number and press Enter" />
{/* Success Message */}
Your presentation is ready and published. Use the controls to navigate through slides during your demo.
)} {/* Error Message */} {error && (
{error}
)}
); }; export default SlidevControls;