diff --git a/frontend/components/q&a/AccordionList.tsx b/frontend/components/q&a/AccordionList.tsx index e0496b42..c0e52f85 100644 --- a/frontend/components/q&a/AccordionList.tsx +++ b/frontend/components/q&a/AccordionList.tsx @@ -191,9 +191,21 @@ function renderTextNodes( return nodes.map((node, i) => renderTextNode(node, i, highlight)); } +function resolveCodeContent( + block: Pick +): string { + if (typeof block.content === 'string') return block.content; + if (typeof block.code === 'string') return block.code; + return ''; +} + function renderCodeBlock(block: CodeBlockEntry, index: number): ReactNode { return ( - + ); } @@ -236,7 +248,11 @@ function renderListItemChildren( if ('type' in child && child.type === 'code') { return ( - + ); } diff --git a/frontend/components/q&a/CodeBlock.tsx b/frontend/components/q&a/CodeBlock.tsx index 6bd4965f..e621c4b4 100644 --- a/frontend/components/q&a/CodeBlock.tsx +++ b/frontend/components/q&a/CodeBlock.tsx @@ -6,25 +6,27 @@ import { Highlight, themes } from 'prism-react-renderer'; import { useState } from 'react'; type Props = { - code: string; + code?: string | null; language?: string | null; }; export default function CodeBlock({ code, language }: Props) { const { resolvedTheme } = useTheme(); const [copied, setCopied] = useState(false); + const safeCode = typeof code === 'string' ? code : ''; const theme = resolvedTheme === 'dark' ? themes.nightOwl : themes.github; const handleCopy = async () => { - await navigator.clipboard.writeText(code); + if (!safeCode) return; + await navigator.clipboard.writeText(safeCode); setCopied(true); setTimeout(() => setCopied(false), 1500); }; return ( diff --git a/frontend/components/q&a/types.ts b/frontend/components/q&a/types.ts index 7c9b550f..d034de33 100644 --- a/frontend/components/q&a/types.ts +++ b/frontend/components/q&a/types.ts @@ -18,7 +18,8 @@ export type TextNode = { export type CodeBlock = { type: 'code'; language: string | null; - content: string; + content?: string; + code?: string; }; export type ListEntry = ListItemBlock | ListItemChild;