-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFileViewShell.tsx
More file actions
132 lines (121 loc) · 4.45 KB
/
FileViewShell.tsx
File metadata and controls
132 lines (121 loc) · 4.45 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
import { useNavigate } from "@tanstack/react-router";
import { FileCodeIcon, XIcon } from "lucide-react";
import { useCallback, useEffect } from "react";
import { useAppSettings } from "~/appSettings";
import { useCodeViewerStore } from "~/codeViewerStore";
import { useTheme } from "~/hooks/useTheme";
import type { CodeContextSelection } from "../CodeMirrorViewer";
import { CodeViewerFileContent, CodeViewerTabStrip } from "../CodeViewerPanel";
import { Button } from "../ui/button";
export function FileViewShell(props: { initialCwd: string; initialPath: string | null }) {
const { resolvedTheme } = useTheme();
const navigate = useNavigate();
const { settings } = useAppSettings();
const tabs = useCodeViewerStore((state) => state.tabs);
const activeTabId = useCodeViewerStore((state) => state.activeTabId);
const setActiveTab = useCodeViewerStore((state) => state.setActiveTab);
const closeTab = useCodeViewerStore((state) => state.closeTab);
const closeAllTabs = useCodeViewerStore((state) => state.closeAllTabs);
const openFile = useCodeViewerStore((state) => state.openFile);
const setPendingContext = useCodeViewerStore((state) => state.setPendingContext);
// On mount, ensure the initial file is open as a tab
useEffect(() => {
if (props.initialPath) {
openFile(props.initialCwd, props.initialPath);
}
// Only run on mount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const activeTab = tabs.find((tab) => tab.tabId === activeTabId);
const onSelectTab = useCallback(
(tabId: string) => {
setActiveTab(tabId);
const tab = tabs.find((t) => t.tabId === tabId);
if (tab) {
void navigate({
to: "/file-view",
search: { cwd: tab.cwd, path: tab.relativePath },
replace: true,
});
}
},
[setActiveTab, tabs, navigate],
);
const onCloseTab = useCallback(
async (tabId: string) => {
const tab = tabs.find((item) => item.tabId === tabId);
if (!tab) return;
if (tab.isDirty && !settings.codeViewerAutosave) {
const confirmed = window.confirm(`Discard unsaved changes in "${tab.label}"?`);
if (!confirmed) return;
}
closeTab(tabId);
if (tabs.length <= 1) {
await navigate({ to: "/" });
}
},
[closeTab, navigate, settings.codeViewerAutosave, tabs],
);
const onCloseAll = useCallback(async () => {
if (tabs.some((tab) => tab.isDirty) && !settings.codeViewerAutosave) {
const confirmed = window.confirm("Discard unsaved changes in all open files?");
if (!confirmed) return;
}
closeAllTabs();
await navigate({ to: "/" });
}, [closeAllTabs, navigate, settings.codeViewerAutosave, tabs]);
const onAddContext = useCallback(
(ctx: CodeContextSelection) => {
setPendingContext({
filePath: ctx.filePath,
fromLine: ctx.fromLine,
toLine: ctx.toLine,
});
},
[setPendingContext],
);
return (
<div className="flex h-full w-full flex-col">
{/* Tab bar */}
<div className="flex items-center justify-between gap-2 border-b border-border px-4 h-12">
<CodeViewerTabStrip
tabs={tabs}
activeTabId={activeTabId}
onSelectTab={onSelectTab}
onCloseTab={onCloseTab}
onCloseAll={onCloseAll}
/>
<div className="flex shrink-0 items-center gap-2 [-webkit-app-region:no-drag]">
<Button
size="icon-xs"
variant="ghost"
onClick={onCloseAll}
aria-label="Close all open files"
title="Close all open files"
>
<XIcon className="size-4" />
</Button>
</div>
</div>
{/* Content */}
<div className="flex min-h-0 flex-1 justify-center overflow-y-auto">
<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.tabId}
cwd={activeTab.cwd}
relativePath={activeTab.relativePath}
resolvedTheme={resolvedTheme}
onAddContext={onAddContext}
/>
)}
</div>
</div>
</div>
);
}