import { Fragment, useEffect, useRef, useState } from 'react'; import { guideRenderer, type QuestionType } from '../../lib/guide-renderer'; import { cn } from '../../lib/classname'; type QuestionCardProps = { question: QuestionType; }; export function QuestionCard(props: QuestionCardProps) { const { question } = props; const [isAnswerVisible, setIsAnswerVisible] = useState(false); const answerRef = useRef(null); const questionRef = useRef(null); useEffect(() => { // set the height of the question width to the height of the answer // width if the answer is visible and the question height is less than // the answer height if (isAnswerVisible) { const answerHeight = answerRef.current?.clientHeight || 0; const questionHeight = questionRef.current?.clientHeight || 0; if (answerHeight > questionHeight) { questionRef.current!.style.height = `${answerHeight}px`; } } else { questionRef.current!.style.height = `auto`; } // if the user has scrolled down and the top of the answer is not // visible, scroll to the top of the answer const questionTop = (questionRef.current?.getBoundingClientRect().top || 0) - 147; if (questionTop < 0) { window.scrollTo({ top: window.scrollY + questionTop - 10, }); } }, [isAnswerVisible]); useEffect(() => { setIsAnswerVisible(false); }, [question]); return ( <>
{question.topics?.map((topic, counter) => { const totalTopics = question.topics?.length || 0; return ( {topic} {counter !== totalTopics - 1 && ( · )} ); })}

{question.question}

{guideRenderer.render(question.answer)}
); }