Skip to content

Commit 19813bf

Browse files
committed
fix: resolve interpolation race condition
1 parent 3f80590 commit 19813bf

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/components/codeBlock/index.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use client';
22

3-
import {RefObject, useEffect, useLayoutEffect, useRef, useState} from 'react';
3+
import {RefObject, useContext, useEffect, useLayoutEffect, useRef, useState} from 'react';
44
import {Clipboard} from 'react-feather';
55

66
import {usePlausibleEvent} from 'sentry-docs/hooks/usePlausibleEvent';
77

88
import styles from './code-blocks.module.scss';
99

10+
import {CodeContext} from '../codeContext';
1011
import {makeHighlightBlocks} from '../codeHighlights';
1112
import {makeKeywordsClickable} from '../codeKeywords';
1213

@@ -53,6 +54,7 @@ function getCopiableText(element: HTMLDivElement) {
5354
export 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

Comments
 (0)