'use client'; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" import { faPatreon } from "@fortawesome/free-brands-svg-icons"; import { faGlobe, faTriangleExclamation } from "@fortawesome/free-solid-svg-icons"; import { useState, useEffect } from 'react'; interface IVSSProps { isMux: boolean; isB2: boolean; isIPFSSource: boolean; isIPFS240: boolean; isEntitledToCDN: boolean; setSelectedVideoSource: (option: string) => void; selectedVideoSource: string; } export function VideoSourceSelector({ isMux, isB2, isIPFSSource, isIPFS240, isEntitledToCDN, selectedVideoSource, setSelectedVideoSource, }: IVSSProps): React.JSX.Element { // Check for user's entitlements and saved preference when component mounts useEffect(() => { // Function to determine the best video source based on entitlements and preferences const determineBestVideoSource = () => { if (isEntitledToCDN) { if (selectedVideoSource === 'Mux' && isMux) { return 'Mux'; } else if (selectedVideoSource === 'B2' && isB2) { return 'B2'; } } // If the user doesn't have entitlements or their preference is not available, default to IPFS if (isIPFSSource) { return 'IPFSSource'; } else if (isIPFS240) { return 'IPFS240'; } // If no sources are available, return an empty string return ''; }; // If selectedVideoSource is unset, find the value to use if (selectedVideoSource === '') { // Load the user's saved preference from storage (e.g., local storage) const savedPreference = localStorage.getItem('videoSourcePreference'); // Check if the saved preference is valid based on entitlements and available sources if (savedPreference === 'Mux' && isMux && isEntitledToCDN) { setSelectedVideoSource('Mux'); } else if (savedPreference === 'B2' && isB2 && isEntitledToCDN) { setSelectedVideoSource('B2'); } else { // Determine the best video source if the saved preference is invalid or not available const bestSource = determineBestVideoSource(); setSelectedVideoSource(bestSource); } } }, [isMux, isB2, isIPFSSource, isIPFS240, isEntitledToCDN, selectedVideoSource, setSelectedVideoSource]); // Handle button click to change the selected video source const handleSourceClick = (source: string) => { if ( (source === 'Mux' && isMux && isEntitledToCDN) || (source === 'B2' && isB2 && isEntitledToCDN) || (source === 'IPFSSource') || (source === 'IPFS240') ) { setSelectedVideoSource(source); // Save the user's preference to storage (e.g., local storage) localStorage.setItem('videoSourcePreference', source); } }; return ( <>
) }