-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCodeViewerPanel.tsx
More file actions
200 lines (186 loc) · 6.78 KB
/
Copy pathCodeViewerPanel.tsx
File metadata and controls
200 lines (186 loc) · 6.78 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { useQuery } from "@tanstack/react-query";
import { FileCodeIcon, XIcon } from "lucide-react";
import { memo, useCallback } from "react";
import { useCodeViewerStore, type CodeViewerTab } from "~/codeViewerStore";
import { useTheme } from "~/hooks/useTheme";
import { projectReadFileQueryOptions } from "~/lib/projectReactQuery";
import { cn, isMacPlatform } from "~/lib/utils";
import { isMarkdownPreviewFilePath } from "~/markdownPreview";
import { CodeMirrorViewer, type CodeContextSelection } from "./CodeMirrorViewer";
import { DiffPanelLoadingState } from "./DiffPanelShell";
import { MarkdownPreview } from "./MarkdownPreview";
import { isElectron } from "~/env";
import { Button } from "./ui/button";
function CodeViewerTabStrip(props: {
tabs: CodeViewerTab[];
activeTabPath: string | null;
onSelectTab: (relativePath: string) => void;
onCloseTab: (relativePath: string) => void;
onCloseAll: () => void;
}) {
return (
<div className="flex min-w-0 flex-1 items-center gap-0.5 overflow-x-auto [-webkit-app-region:no-drag]">
{props.tabs.map((tab) => {
const isActive = tab.relativePath === props.activeTabPath;
return (
<div
key={tab.relativePath}
className={cn(
"group flex max-w-[180px] shrink-0 items-center gap-1.5 rounded-md border px-2 py-1 text-[11px] transition-colors",
isActive
? "border-border bg-accent text-accent-foreground"
: "border-transparent text-muted-foreground/70 hover:border-border/60 hover:text-foreground/80",
)}
>
<button
type="button"
className="min-w-0 flex-1 truncate text-left font-mono"
onClick={() => props.onSelectTab(tab.relativePath)}
title={tab.relativePath}
>
{tab.label}
</button>
<button
type="button"
className="shrink-0 rounded-sm p-0.5 opacity-0 transition-opacity hover:bg-accent/80 group-hover:opacity-100"
onClick={(event) => {
event.stopPropagation();
props.onCloseTab(tab.relativePath);
}}
aria-label={`Close ${tab.label}`}
>
<XIcon className="size-3" />
</button>
</div>
);
})}
</div>
);
}
const CodeViewerFileContent = memo(function CodeViewerFileContent(props: {
cwd: string;
relativePath: string;
resolvedTheme: "light" | "dark";
onAddContext: (ctx: CodeContextSelection) => void;
}) {
const query = useQuery(
projectReadFileQueryOptions({
cwd: props.cwd,
relativePath: props.relativePath,
}),
);
if (query.isLoading) {
return <DiffPanelLoadingState label="Loading file..." />;
}
if (query.isError) {
const message = query.error instanceof Error ? query.error.message : "Failed to load file.";
return (
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-destructive/80">
{message}
</div>
);
}
if (!query.data?.contents && query.data?.contents !== "") {
return (
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
No content available.
</div>
);
}
return (
<div className="min-h-0 flex-1 overflow-hidden">
{query.data.truncated && (
<div className="border-b border-amber-500/30 bg-amber-500/10 px-3 py-1 text-[11px] text-amber-700 dark:text-amber-300/90">
File is larger than 1MB. Showing truncated content.
</div>
)}
{isMarkdownPreviewFilePath(props.relativePath) ? (
<MarkdownPreview contents={query.data.contents} />
) : (
<CodeMirrorViewer
contents={query.data.contents}
filePath={props.relativePath}
resolvedTheme={props.resolvedTheme}
onAddContext={props.onAddContext}
/>
)}
</div>
);
});
export default function CodeViewerPanel() {
const { resolvedTheme } = useTheme();
const tabs = useCodeViewerStore((state) => state.tabs);
const activeTabPath = useCodeViewerStore((state) => state.activeTabPath);
const setActiveTab = useCodeViewerStore((state) => state.setActiveTab);
const closeTab = useCodeViewerStore((state) => state.closeTab);
const closeViewer = useCodeViewerStore((state) => state.close);
const setPendingContext = useCodeViewerStore((state) => state.setPendingContext);
const activeTab = tabs.find((tab) => tab.relativePath === activeTabPath);
const onSelectTab = useCallback(
(relativePath: string) => setActiveTab(relativePath),
[setActiveTab],
);
const onCloseTab = useCallback((relativePath: string) => closeTab(relativePath), [closeTab]);
const onAddContext = useCallback(
(ctx: CodeContextSelection) => {
setPendingContext({
filePath: ctx.filePath,
fromLine: ctx.fromLine,
toLine: ctx.toLine,
});
},
[setPendingContext],
);
const modKey = isMacPlatform(navigator.platform) ? "\u2318" : "Ctrl+";
return (
<div className="flex h-full w-full flex-col bg-background">
{/* Header */}
<div
className={cn(
"flex items-center justify-between gap-2 border-b border-border px-4",
isElectron ? "drag-region h-[52px]" : "h-12",
)}
>
<CodeViewerTabStrip
tabs={tabs}
activeTabPath={activeTabPath}
onSelectTab={onSelectTab}
onCloseTab={onCloseTab}
onCloseAll={closeViewer}
/>
<div className="flex shrink-0 items-center gap-2 [-webkit-app-region:no-drag]">
<span className="hidden text-[10px] text-muted-foreground/50 sm:inline">
Select code + {modKey}L to add context
</span>
<Button
size="icon-xs"
variant="ghost"
onClick={closeViewer}
aria-label="Close code viewer"
>
<XIcon className="size-4" />
</Button>
</div>
</div>
{/* Content */}
<div className="flex min-h-0 flex-1 justify-center overflow-hidden">
<div className="h-full w-full max-w-5xl">
{!activeTab ? (
<div className="flex h-full flex-col items-center justify-center gap-2 px-5 text-center text-muted-foreground/60">
<FileCodeIcon className="size-8 opacity-40" />
<p className="text-xs">Click a file in the sidebar to view it here.</p>
</div>
) : (
<CodeViewerFileContent
key={activeTab.relativePath}
cwd={activeTab.cwd}
relativePath={activeTab.relativePath}
resolvedTheme={resolvedTheme}
onAddContext={onAddContext}
/>
)}
</div>
</div>
</div>
);
}