Skip to content

Commit fa7f3ff

Browse files
committed
feat: add file download/export capability from sidebar
1 parent 0465990 commit fa7f3ff

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/components/sidebar.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
Edit2,
1313
MoreVertical,
1414
FilePlus,
15-
FolderPlus
15+
FolderPlus,
16+
Download
1617
} from "lucide-react";
1718
import { cn } from "@/lib/utils";
1819
import { FileNode, FileSystemContextType } from "@/hooks/use-file-system";
@@ -163,6 +164,11 @@ function FileTreeNode({ node, fileSystem, depth }: FileTreeNodeProps) {
163164
</button>
164165
</>
165166
)}
167+
{node.type === 'file' && (
168+
<button onClick={handleDownload} className="p-0.5 hover:bg-background rounded" title="Download">
169+
<Download size={12} />
170+
</button>
171+
)}
166172
<button onClick={handleRename} className="p-0.5 hover:bg-background rounded" title="Rename">
167173
<Edit2 size={12} />
168174
</button>

src/hooks/use-file-system.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ export function useFileSystem() {
188188
);
189189
}, [files]);
190190

191+
const downloadFile = useCallback((id: string) => {
192+
const node = files[id];
193+
if (!node || node.type !== 'file') return;
194+
195+
const blob = new Blob([node.content], { type: 'text/markdown' });
196+
const url = URL.createObjectURL(blob);
197+
const a = document.createElement('a');
198+
a.href = url;
199+
a.download = node.name;
200+
document.body.appendChild(a);
201+
a.click();
202+
document.body.removeChild(a);
203+
URL.revokeObjectURL(url);
204+
}, [files]);
205+
191206
return {
192207
files,
193208
activeFileId,
@@ -199,5 +214,6 @@ export function useFileSystem() {
199214
selectFile,
200215
toggleFolder,
201216
searchFiles,
217+
downloadFile,
202218
};
203219
}

0 commit comments

Comments
 (0)