import { useState, useCallback, useEffect } from 'react'; import { startScreenshare, stopScreenshare, isScreensharing, type ScreenshareOptions } from '../lib/daily'; export interface UseScreenshareResult { /** Whether screenshare is currently active */ isSharing: boolean; /** Whether a screenshare operation is in progress */ isLoading: boolean; /** Error message if screenshare failed */ error: string | null; /** Reference to the opened deck window */ deckWindow: Window | null; /** Start screensharing with a deck URL */ startSharing: (deckUrl: string, options?: Partial) => Promise; /** Stop the current screenshare */ stopSharing: () => Promise; /** Clear any error messages */ clearError: () => void; } /** * React hook for managing Slidev deck screensharing via Daily.co * * This hook provides a simple interface for: * - Starting screenshare with a deck URL (opens popup + initiates Daily screenshare) * - Stopping screenshare * - Tracking screenshare status * - Handling errors (popup blockers, permissions, etc.) * * @example * ```tsx * function PresentationControls() { * const { isSharing, startSharing, stopSharing, error } = useScreenshare(); * * const handlePresent = async () => { * await startSharing('https://buzztrail.github.io/buzztrail-presentations/abc123/'); * }; * * return ( *
* {error &&
{error}
} * {isSharing ? ( * * ) : ( * * )} *
* ); * } * ``` */ export function useScreenshare(): UseScreenshareResult { const [isSharing, setIsSharing] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [deckWindow, setDeckWindow] = useState(null); // Check initial screenshare status useEffect(() => { const checkStatus = () => { const sharing = isScreensharing(); setIsSharing(sharing); }; checkStatus(); // Poll for status changes (Daily.co events would be better but this is simpler) const interval = setInterval(checkStatus, 1000); return () => clearInterval(interval); }, []); // Cleanup deck window on unmount useEffect(() => { return () => { if (deckWindow && !deckWindow.closed) { deckWindow.close(); } }; }, [deckWindow]); const startSharing = useCallback( async (deckUrl: string, options?: Partial) => { setIsLoading(true); setError(null); try { const result = await startScreenshare({ deckUrl, ...options, }); if (result.success) { setIsSharing(true); setDeckWindow(result.windowHandle || null); } else { setError(result.error || 'Failed to start screenshare'); setIsSharing(false); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'An unexpected error occurred'; setError(errorMessage); setIsSharing(false); } finally { setIsLoading(false); } }, [] ); const stopSharing = useCallback(async () => { setIsLoading(true); setError(null); try { await stopScreenshare(); setIsSharing(false); // Close the deck window if it's still open if (deckWindow && !deckWindow.closed) { deckWindow.close(); setDeckWindow(null); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to stop screenshare'; setError(errorMessage); } finally { setIsLoading(false); } }, [deckWindow]); const clearError = useCallback(() => { setError(null); }, []); return { isSharing, isLoading, error, deckWindow, startSharing, stopSharing, clearError, }; } /** * Hook for automatic screenshare after deck generation * * This hook automatically triggers screenshare when a deck URL becomes available. * Useful for the "generate and present" flow. * * @param deckUrl - The deck URL to present (will trigger when it changes from null to a value) * @param autoStart - Whether to automatically start sharing when deckUrl is set * * @example * ```tsx * function AutoPresentation() { * const [deckUrl, setDeckUrl] = useState(null); * const { isSharing, error } = useAutoScreenshare(deckUrl, true); * * const generateDeck = async () => { * const result = await generateDeckAPI(); * setDeckUrl(result.deckUrl); // This will trigger auto-screenshare * }; * * return ; * } * ``` */ export function useAutoScreenshare( deckUrl: string | null, autoStart = true ): Omit { const screenshare = useScreenshare(); useEffect(() => { if (autoStart && deckUrl && !screenshare.isSharing && !screenshare.isLoading) { void screenshare.startSharing(deckUrl); } }, [deckUrl, autoStart, screenshare]); return { isSharing: screenshare.isSharing, isLoading: screenshare.isLoading, error: screenshare.error, deckWindow: screenshare.deckWindow, stopSharing: screenshare.stopSharing, clearError: screenshare.clearError, }; }