-
-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathCodeExplorer.tsx
More file actions
147 lines (137 loc) · 4.09 KB
/
CodeExplorer.tsx
File metadata and controls
147 lines (137 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React from 'react'
import { HighlightedCodeBlock } from '~/components/markdown'
import { FileExplorer } from './FileExplorer'
import { InteractiveSandbox } from './InteractiveSandbox'
import { CodeExplorerTopBar } from './CodeExplorerTopBar'
import type { GitHubFileNode } from '~/utils/documents.server'
import type { Library } from '~/libraries'
import { twMerge } from 'tailwind-merge'
function getLanguageFromPath(path: string): string {
const ext = path.split('.').pop() || ''
const langMap: Record<string, string> = {
ts: 'typescript',
tsx: 'tsx',
js: 'javascript',
jsx: 'jsx',
mts: 'typescript',
cts: 'typescript',
mjs: 'javascript',
cjs: 'javascript',
json: 'json',
md: 'markdown',
html: 'html',
css: 'css',
scss: 'scss',
yaml: 'yaml',
yml: 'yaml',
toml: 'toml',
sh: 'bash',
bash: 'bash',
sql: 'sql',
vue: 'vue',
svelte: 'svelte',
}
return langMap[ext] || 'text'
}
interface CodeExplorerProps {
activeTab: 'code' | 'sandbox'
codeSandboxUrl: string
/** Pre-highlighted HTML from server-side Shiki */
currentCodeHtml: string
currentPath: string
examplePath: string
githubContents: GitHubFileNode[] | undefined
library: Library
prefetchFileContent: (path: string) => void
setActiveTab: (tab: 'code' | 'sandbox') => void
setCurrentPath: (path: string) => void
stackBlitzUrl: string
}
export function CodeExplorer({
activeTab,
codeSandboxUrl,
currentCodeHtml,
currentPath,
examplePath,
githubContents,
library,
prefetchFileContent,
setActiveTab,
setCurrentPath,
stackBlitzUrl,
}: CodeExplorerProps) {
const [isFullScreen, setIsFullScreen] = React.useState(false)
const [isSidebarOpen, setIsSidebarOpen] = React.useState(true)
// Add escape key handler
React.useEffect(() => {
const handleEsc = (e: KeyboardEvent) => {
if (e.key === 'Escape' && isFullScreen) {
setIsFullScreen(false)
}
}
window.addEventListener('keydown', handleEsc)
return () => window.removeEventListener('keydown', handleEsc)
}, [isFullScreen])
// Add sidebar close handler
React.useEffect(() => {
const handleCloseSidebar = () => {
setIsSidebarOpen(false)
}
window.addEventListener('closeSidebar', handleCloseSidebar)
return () => window.removeEventListener('closeSidebar', handleCloseSidebar)
}, [])
const lang = getLanguageFromPath(currentPath)
return (
<div
className={`flex flex-col min-h-[60dvh] sm:min-h-[80dvh] border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden ${
isFullScreen
? 'fixed inset-0 top-[var(--navbar-height)] z-50 bg-white dark:bg-gray-900'
: ''
}`}
>
<CodeExplorerTopBar
activeTab={activeTab}
setActiveTab={setActiveTab}
isFullScreen={isFullScreen}
setIsFullScreen={setIsFullScreen}
isSidebarOpen={isSidebarOpen}
setIsSidebarOpen={setIsSidebarOpen}
/>
<div className="relative flex-1">
<div
className={`absolute inset-0 flex ${
activeTab === 'code' ? '' : 'hidden'
}`}
>
<FileExplorer
currentPath={currentPath}
githubContents={githubContents}
isSidebarOpen={isSidebarOpen}
libraryColor={library.bgStyle}
prefetchFileContent={prefetchFileContent}
setCurrentPath={setCurrentPath}
/>
<div className="flex-1 overflow-auto relative">
<HighlightedCodeBlock
html={currentCodeHtml}
lang={lang}
isEmbedded
className={twMerge(
'h-full border-0',
isFullScreen ? 'max-h-[90dvh]' : 'max-h-[80dvh]',
)}
/>
</div>
</div>
<InteractiveSandbox
isActive={activeTab === 'sandbox'}
codeSandboxUrl={codeSandboxUrl}
stackBlitzUrl={stackBlitzUrl}
examplePath={examplePath}
libraryName={library.name}
embedEditor={library.embedEditor || 'stackblitz'}
/>
</div>
</div>
)
}