forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileViewerModal.tsx
More file actions
175 lines (163 loc) · 5.32 KB
/
Copy pathFileViewerModal.tsx
File metadata and controls
175 lines (163 loc) · 5.32 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
"use client";
import { useEffect, useCallback } from "react";
import ReactMarkdown from "react-markdown";
import { X, Download, FileText } from "lucide-react";
import type { ResearchFile } from "@/types/research";
/**
* FileViewerModal - Modal for viewing file content with markdown rendering.
*
* Features:
* - Markdown rendering via react-markdown with typography styles
* - Download button to save file content
* - Closes on backdrop click, X button, or Escape key
* - Responsive sizing with scrollable content
*/
interface FileViewerModalProps {
file: ResearchFile | null;
onClose: () => void;
}
export function FileViewerModal({ file, onClose }: FileViewerModalProps) {
// Handle Escape key to close modal
const handleKeyDown = useCallback(
(e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose();
}
},
[onClose],
);
useEffect(() => {
if (file) {
document.addEventListener("keydown", handleKeyDown);
// Prevent body scroll when modal is open
document.body.style.overflow = "hidden";
}
return () => {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "";
};
}, [file, handleKeyDown]);
// Don't render if no file selected
if (!file) return null;
// Extract filename from path
const filename = file.path.split("/").pop() || file.path;
// Download file content
const handleDownload = () => {
const blob = new Blob([file.content], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
{/* Backdrop with blur */}
<div
className="absolute inset-0 bg-black/30 backdrop-blur-sm"
onClick={onClose}
aria-hidden="true"
/>
{/* Modal container */}
<div
className="relative max-w-3xl w-full max-h-[85vh] flex flex-col"
style={{
background: "var(--color-glass-elevated)",
backdropFilter: "blur(20px)",
WebkitBackdropFilter: "blur(20px)",
padding: 0,
borderRadius: "var(--radius-2xl)",
border: "1px solid var(--color-border-glass)",
boxShadow: "0 4px 30px rgba(0, 0, 0, 0.1)",
}}
role="dialog"
aria-modal="true"
aria-labelledby="file-viewer-title"
>
{/* Header */}
<div
style={{
padding:
"var(--space-6) var(--space-6) var(--space-4) var(--space-6)",
}}
className="flex items-center justify-between border-b border-[var(--color-border-glass)]"
>
<div className="flex items-center gap-3">
<div
style={{
background:
"linear-gradient(135deg, var(--color-accent) 0%, var(--color-accent-dark) 100%)",
padding: "var(--space-3)",
borderRadius: "var(--radius-md)",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<FileText
style={{ width: "20px", height: "20px", color: "white" }}
/>
</div>
<h2
id="file-viewer-title"
style={{
fontSize: "var(--text-2xl)",
fontWeight: "var(--font-bold)",
fontFamily: "var(--font-display)",
color: "var(--color-text-primary)",
}}
className="truncate max-w-md"
>
{filename}
</h2>
</div>
<div className="flex items-center gap-2">
<button
onClick={handleDownload}
className="p-2 hover:bg-[var(--color-glass-subtle)] rounded-lg transition-colors"
aria-label="Download file"
title="Download file"
>
<Download className="w-5 h-5 text-[var(--color-text-secondary)]" />
</button>
<button
onClick={onClose}
className="p-2 hover:bg-[var(--color-glass-subtle)] rounded-lg transition-colors"
aria-label="Close modal"
title="Close (Escape)"
>
<X className="w-5 h-5 text-[var(--color-text-secondary)]" />
</button>
</div>
</div>
{/* Scrollable content with markdown rendering */}
<div
className="flex-1 overflow-y-auto"
style={{ padding: "var(--space-8)" }}
>
<div className="prose prose-sm prose-slate max-w-none">
<ReactMarkdown>{file.content}</ReactMarkdown>
</div>
</div>
{/* Footer with file path */}
<div
style={{ padding: "var(--space-3) var(--space-6)" }}
className="border-t border-[var(--color-border-glass)]"
>
<code
style={{
fontFamily: "var(--font-mono)",
fontSize: "var(--text-sm)",
color: "var(--color-text-tertiary)",
}}
>
{file.path}
</code>
</div>
</div>
</div>
);
}