11'use client' ;
22
3- import { RefObject , useEffect , useLayoutEffect , useRef , useState } from 'react' ;
3+ import { RefObject , useContext , useEffect , useLayoutEffect , useRef , useState } from 'react' ;
44import { Clipboard } from 'react-feather' ;
55
66import { usePlausibleEvent } from 'sentry-docs/hooks/usePlausibleEvent' ;
77
88import styles from './code-blocks.module.scss' ;
99
10+ import { CodeContext } from '../codeContext' ;
1011import { makeHighlightBlocks } from '../codeHighlights' ;
1112import { makeKeywordsClickable } from '../codeKeywords' ;
1213
@@ -53,6 +54,7 @@ function getCopiableText(element: HTMLDivElement) {
5354export function CodeBlock ( { filename, language, children} : CodeBlockProps ) {
5455 const [ showCopied , setShowCopied ] = useState ( false ) ;
5556 const codeRef = useRef < HTMLDivElement > ( null ) ;
57+ const codeContext = useContext ( CodeContext ) ;
5658
5759 // Show the copy button after js has loaded
5860 // otherwise the copy button will not work
@@ -61,11 +63,17 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
6163 const [ isMounted , setIsMounted ] = useState ( false ) ;
6264 const { emit} = usePlausibleEvent ( ) ;
6365
66+ // Extract isLoading to avoid dependency array issues
67+ const isContextLoading = codeContext ?. isLoading ?? true ;
68+
6469 // Use useLayoutEffect to set isMounted synchronously before browser paints
6570 // This ensures keyword interpolation happens before the user sees anything
71+ // Wait for codeKeywords to be loaded before enabling keyword interpolation
6672 useLayoutEffect ( ( ) => {
67- setIsMounted ( true ) ;
68- } , [ ] ) ;
73+ if ( codeContext && ! isContextLoading ) {
74+ setIsMounted ( true ) ;
75+ }
76+ } , [ codeContext , isContextLoading ] ) ;
6977
7078 useEffect ( ( ) => {
7179 setShowCopyButton ( true ) ;
0 commit comments