Skip to content

Commit d5e8559

Browse files
feat(client): add isDialogue prop to ChallengeTranscript component (freeCodeCamp#64831)
Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1 parent a34fad0 commit d5e8559

4 files changed

Lines changed: 68 additions & 17 deletions

File tree

client/src/templates/Challenges/components/challenge-transcript.test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,43 @@ describe('<ChallengeTranscript />', () => {
5858
expect(setSpy).not.toHaveBeenCalled();
5959
setSpy.mockRestore();
6060
});
61+
62+
it('should render the transcript as a table when isDialogue is true', () => {
63+
store.set('fcc-transcript-expanded', true);
64+
65+
render(
66+
<ChallengeTranscript
67+
{...baseProps}
68+
transcript={'Hello\nWorld'}
69+
shouldPersistExpanded={true}
70+
isDialogue={true}
71+
/>
72+
);
73+
const table = screen.getByRole('table');
74+
expect(table).toBeVisible();
75+
expect(screen.getByRole('cell', { name: 'Hello' })).toBeVisible();
76+
expect(screen.getByRole('cell', { name: 'World' })).toBeVisible();
77+
});
78+
79+
it('should render the transcript with PrismFormatted when isDialogue is false', () => {
80+
store.set('fcc-transcript-expanded', true);
81+
82+
render(
83+
<ChallengeTranscript
84+
{...baseProps}
85+
transcript='<pre><code class="language-js">console.log("hi")</code></pre>'
86+
shouldPersistExpanded={true}
87+
isDialogue={false}
88+
/>
89+
);
90+
91+
const preElement = screen.getByRole('region');
92+
expect(preElement).toBeVisible();
93+
expect(preElement.tagName).toBe('PRE');
94+
95+
// eslint-disable-next-line testing-library/no-node-access
96+
const codeElement = preElement.querySelector('code');
97+
expect(codeElement).toBeInTheDocument();
98+
expect(preElement).toHaveTextContent('console.log("hi")');
99+
});
61100
});

client/src/templates/Challenges/components/challenge-transcript.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ import { useTranslation } from 'react-i18next';
33
import { Spacer } from '@freecodecamp/ui';
44
import store from 'store';
55

6+
import PrismFormatted from './prism-formatted';
67
import './challenge-transcript.css';
78

89
interface ChallengeTranscriptProps {
910
transcript: string;
1011
shouldPersistExpanded?: boolean;
12+
isDialogue?: boolean;
1113
}
1214

1315
function ChallengeTranscript({
1416
transcript,
15-
shouldPersistExpanded
17+
shouldPersistExpanded,
18+
isDialogue
1619
}: ChallengeTranscriptProps): JSX.Element {
1720
const { t } = useTranslation();
1821

@@ -42,20 +45,24 @@ function ChallengeTranscript({
4245
{t('learn.transcript')}
4346
</summary>
4447
<Spacer size='m' />
45-
<table className='transcript-table'>
46-
<tbody>
47-
{transcript
48-
.split('\n')
49-
.filter(line => line.trim() !== '')
50-
.map((line, idx) => {
51-
return (
52-
<tr key={idx}>
53-
<td dangerouslySetInnerHTML={{ __html: line }} />
54-
</tr>
55-
);
56-
})}
57-
</tbody>
58-
</table>
48+
{isDialogue ? (
49+
<table className='transcript-table'>
50+
<tbody>
51+
{transcript
52+
.split('\n')
53+
.filter(line => line.trim() !== '')
54+
.map((line, idx) => {
55+
return (
56+
<tr key={idx}>
57+
<td dangerouslySetInnerHTML={{ __html: line }} />
58+
</tr>
59+
);
60+
})}
61+
</tbody>
62+
</table>
63+
) : (
64+
<PrismFormatted className='line-numbers' text={transcript} />
65+
)}
5966
</details>
6067
<Spacer size='m' />
6168
</>

client/src/templates/Challenges/components/scene/scene.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export function Scene({
437437
</button>
438438
)}
439439
</div>
440-
<ChallengeTranscript transcript={transcriptText} />
440+
<ChallengeTranscript transcript={transcriptText} isDialogue={true} />
441441
<Spacer size='m' />
442442
</Col>
443443
);

client/src/templates/Challenges/fill-in-the-blank/show.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,12 @@ const ShowFillInTheBlank = ({
279279
{scene && <Scene scene={scene} sceneSubject={sceneSubject} />}
280280

281281
<Col md={8} mdOffset={2} sm={10} smOffset={1} xs={12}>
282-
{transcript && <ChallengeTranscript transcript={transcript} />}
282+
{transcript && (
283+
<ChallengeTranscript
284+
transcript={transcript}
285+
isDialogue={true}
286+
/>
287+
)}
283288

284289
{instructions && (
285290
<>

0 commit comments

Comments
 (0)