2018-09-24 18:45:11 +01:00
|
|
|
import { Panel } from '@freecodecamp/react-bootstrap';
|
|
|
|
import Prism from 'prismjs';
|
2021-08-02 15:39:40 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2018-09-24 18:45:11 +01:00
|
|
|
|
|
|
|
const prismLang = {
|
|
|
|
css: 'css',
|
|
|
|
js: 'javascript',
|
|
|
|
jsx: 'javascript',
|
|
|
|
html: 'markup'
|
|
|
|
};
|
|
|
|
|
2020-10-15 14:43:51 +02:00
|
|
|
const SolutionViewer = ({
|
2021-08-12 19:48:28 +01:00
|
|
|
challengeFiles,
|
2018-09-24 18:45:11 +01:00
|
|
|
solution = '// The solution is not available for this project'
|
2020-10-15 14:43:51 +02:00
|
|
|
}) =>
|
2021-08-12 19:48:28 +01:00
|
|
|
challengeFiles?.length ? (
|
|
|
|
challengeFiles.map(challengeFile => (
|
|
|
|
<Panel
|
|
|
|
bsStyle='primary'
|
|
|
|
className='solution-viewer'
|
|
|
|
key={challengeFile.ext}
|
|
|
|
>
|
|
|
|
<Panel.Heading>{challengeFile.ext.toUpperCase()}</Panel.Heading>
|
2018-09-24 18:45:11 +01:00
|
|
|
<Panel.Body>
|
|
|
|
<pre>
|
|
|
|
<code
|
2021-08-12 19:48:28 +01:00
|
|
|
className={`language-${prismLang[challengeFile.ext]}`}
|
2018-09-24 18:45:11 +01:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: Prism.highlight(
|
2021-08-12 19:48:28 +01:00
|
|
|
challengeFile.contents.trim(),
|
|
|
|
Prism.languages[prismLang[challengeFile.ext]]
|
2018-09-24 18:45:11 +01:00
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</pre>
|
|
|
|
</Panel.Body>
|
|
|
|
</Panel>
|
2020-10-15 14:43:51 +02:00
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<Panel
|
|
|
|
bsStyle='primary'
|
|
|
|
className='solution-viewer'
|
|
|
|
key={solution.slice(0, 10)}
|
|
|
|
>
|
|
|
|
<Panel.Heading>JS</Panel.Heading>
|
|
|
|
<Panel.Body>
|
|
|
|
<pre>
|
|
|
|
<code
|
|
|
|
className='language-markup'
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: Prism.highlight(
|
|
|
|
solution.trim(),
|
|
|
|
Prism.languages.js,
|
|
|
|
'javascript'
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</pre>
|
|
|
|
</Panel.Body>
|
|
|
|
</Panel>
|
2018-09-24 18:45:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
SolutionViewer.displayName = 'SolutionViewer';
|
|
|
|
SolutionViewer.propTypes = {
|
2021-08-12 19:48:28 +01:00
|
|
|
challengeFiles: PropTypes.array,
|
2018-09-24 18:45:11 +01:00
|
|
|
solution: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SolutionViewer;
|