2018-09-24 18:45:11 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Panel } from '@freecodecamp/react-bootstrap';
|
|
|
|
import Prism from 'prismjs';
|
|
|
|
|
|
|
|
const prismLang = {
|
|
|
|
css: 'css',
|
|
|
|
js: 'javascript',
|
|
|
|
jsx: 'javascript',
|
|
|
|
html: 'markup'
|
|
|
|
};
|
|
|
|
|
2020-10-15 14:43:51 +02:00
|
|
|
const SolutionViewer = ({
|
2018-09-24 18:45:11 +01:00
|
|
|
files,
|
|
|
|
solution = '// The solution is not available for this project'
|
2020-10-15 14:43:51 +02:00
|
|
|
}) =>
|
|
|
|
files && Array.isArray(files) && files.length ? (
|
|
|
|
files.map(file => (
|
|
|
|
<Panel bsStyle='primary' className='solution-viewer' key={file.ext}>
|
|
|
|
<Panel.Heading>{file.ext.toUpperCase()}</Panel.Heading>
|
2018-09-24 18:45:11 +01:00
|
|
|
<Panel.Body>
|
|
|
|
<pre>
|
|
|
|
<code
|
2020-10-15 14:43:51 +02:00
|
|
|
className={`language-${prismLang[file.ext]}`}
|
2018-09-24 18:45:11 +01:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: Prism.highlight(
|
2020-10-15 14:43:51 +02:00
|
|
|
file.contents.trim(),
|
|
|
|
Prism.languages[prismLang[file.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 = {
|
|
|
|
files: PropTypes.arrayOf(PropTypes.objectOf(PropTypes.string)),
|
|
|
|
solution: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SolutionViewer;
|