diff --git a/src/components/solution.js b/src/components/solution.js index 8e3f2dd..c6f2f03 100644 --- a/src/components/solution.js +++ b/src/components/solution.js @@ -20,27 +20,34 @@ const Solution = ({ problemId, onClose, theme }) => { const fetchSolutions = async () => { setLoading(true); setFailed(false); - setSolutions([]); - + setSolutions([]); + setVideoUrl(''); + try { - const headers = { - 'Authorization': `token ${process.env.REACT_APP_GITHUB_TOKEN}`, - 'Accept': 'application/vnd.github.v3.raw' + 'Authorization': `token ${process.env.REACT_APP_GITHUB_TOKEN}`, + 'Accept': 'application/vnd.github.v3.raw' }; - const response = await axios.get(`https://api.github.com/repos/ahmad-masud/CompCode-Solutions/contents/${problemId}`, { headers }); - + if (response.status !== 200) { addAlert('Failed to fetch solutions', 'error'); setFailed(true); - return; + return; } - + const data = response.data; + + try { + const videoResponse = await axios.get(`https://raw.githubusercontent.com/ahmad-masud/CompCode-Solutions/main/${problemId}/video.txt`); + setVideoUrl(videoResponse.data.trim()); // Set the video URL (assumes plain text) + } catch (videoError) { + console.warn(`No video.txt found for problem ${problemId}:`, videoError.message); + } + const solutionPromises = data.map(async (solution) => { if (solution.type === 'dir') { try { @@ -48,23 +55,19 @@ const Solution = ({ problemId, onClose, theme }) => { const infoData = infoResponse.data; const codeResponse = await axios.get(`https://raw.githubusercontent.com/ahmad-masud/CompCode-Solutions/main/${problemId}/${solution.name}/code.py`); const codeData = codeResponse.data; - - if (!videoUrl && infoData.video) { - setVideoUrl(infoData.video); - } - + return { ...infoData, code: codeData, }; } catch (innerError) { console.error(`Error fetching data for solution ${solution.name}:`, innerError); - return null; + return null; } } return null; }); - + const loadedSolutions = await Promise.all(solutionPromises); setSolutions(loadedSolutions.filter((sol) => sol !== null)); } catch (err) { @@ -72,14 +75,14 @@ const Solution = ({ problemId, onClose, theme }) => { addAlert('Not available yet', 'warning'); setFailed(true); } finally { - setLoading(false); + setLoading(false); } }; - + if (problemId) { fetchSolutions(); } - }, [problemId, addAlert, videoUrl]); + }, [problemId, addAlert]); const handleCopy = (index) => { setCopiedIndex(index);