Open History to see the last commit
1import CodeMirror from '@uiw/react-codemirror'
2import CodeMirrorMerge from 'react-codemirror-merge'
3
4import { javascript } from '@codemirror/lang-javascript'
5import { json } from '@codemirror/lang-json'
6import { css } from '@codemirror/lang-css'
7import { html } from '@codemirror/lang-html'
8
9import { githubDarkInit } from '@uiw/codemirror-theme-github'
10
11const theme = githubDarkInit({
12 settings: {
13 background: 'oklch(0.07 0.005 285.823)',
14 foreground: '#c9d1d9',
15 gutterBackground: 'oklch(0.22 0.005 285.823)',
16 },
17})
18
19export default function FileViewer({
20 originalFile,
21 modifiedFile,
22 filePath,
23}: {
24 originalFile?: string
25 modifiedFile: string
26 filePath: string
27}) {
28 function getLanguage(file: string) {
29 if (file.endsWith('.js') || file.endsWith('.jsx')) {
30 return javascript({ jsx: true })
31 }
32 if (file.endsWith('.ts') || file.endsWith('.tsx')) {
33 return javascript({ typescript: true, jsx: true })
34 }
35 if (file.endsWith('.json')) {
36 return json()
37 }
38 if (file.endsWith('.css')) {
39 return css()
40 }
41 if (file.endsWith('.html')) {
42 return html()
43 }
44 return javascript()
45 }
46 const language = getLanguage(filePath)
47
48 // Display placeholder for binary files
49 const displayModified = modifiedFile.startsWith('base64::')
50 ? '<binary file>'
51 : modifiedFile
52 const displayOriginal = originalFile?.startsWith('base64::')
53 ? '<binary file>'
54 : originalFile
55
56 if (!displayOriginal || displayOriginal === displayModified) {
57 return (
58 <CodeMirror
59 value={displayModified}
60 theme={theme}
61 height="100vh"
62 width="100%"
63 readOnly
64 extensions={[language]}
65 className="text-lg"
66 />
67 )
68 }
69 return (
70 <CodeMirrorMerge orientation="a-b" theme={theme} className="text-lg">
71 <CodeMirrorMerge.Original
72 value={displayOriginal}
73 extensions={[language]}
74 />
75 <CodeMirrorMerge.Modified
76 value={displayModified}
77 extensions={[language]}
78 />
79 </CodeMirrorMerge>
80 )
81}