import { Fragment, useEffect, useRef, useState } from 'react'; import type { QuestionType } from '../../lib/question-group'; import { markdownToHtml } from '../../lib/markdown'; import Prism from 'prismjs'; import './PrismAtom.css'; 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) { Prism.highlightAll(); 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}

{!question.isLongAnswer && (
p]:leading-relaxed sm:text-xl`} dangerouslySetInnerHTML={{ __html: markdownToHtml(question.answer, false), }} /> )} {question.isLongAnswer && (
)}
); }