This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathFileSidebar.tsx
More file actions
113 lines (104 loc) · 4 KB
/
Copy pathFileSidebar.tsx
File metadata and controls
113 lines (104 loc) · 4 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
import React, { useEffect, useState } from 'react'
import { FileInfoNode, FileInfoTree } from 'electron/main/filesystem/types'
import { FixedSizeList } from 'react-window'
import { isFileNodeDirectory } from '@shared/utils'
import { XStack, YStack } from 'tamagui'
import { HiOutlinePencilAlt } from 'react-icons/hi'
import { VscNewFolder } from 'react-icons/vsc'
import { useFileContext } from '@/contexts/FileContext'
import { useModalOpeners } from '@/contexts/ModalContext'
import { useContentContext } from '@/contexts/ContentContext'
import FileItemRows from './FileItemRows'
const getFilesAndIndentationsForSidebar = (
files: FileInfoTree,
expandedDirectories: Map<string, boolean>,
indentation = 0,
): { file: FileInfoNode; indentation: number }[] => {
let filesAndIndexes: { file: FileInfoNode; indentation: number }[] = []
files.forEach((file) => {
filesAndIndexes.push({ file, indentation })
if (isFileNodeDirectory(file) && expandedDirectories.has(file.path) && expandedDirectories.get(file.path)) {
if (file.children) {
filesAndIndexes = [
...filesAndIndexes,
...getFilesAndIndentationsForSidebar(file.children, expandedDirectories, indentation + 1),
]
}
}
})
return filesAndIndexes
}
interface FileExplorerProps {
lheight?: number
}
const FileSidebar: React.FC<FileExplorerProps> = ({ lheight }) => {
// const { state, actions } = useThemeManager()
const [listHeight, setListHeight] = useState(lheight ?? window.innerHeight - 50)
const { vaultFilesTree, expandedDirectories, renameFile, setSelectedDirectory } = useFileContext()
const { setIsNewDirectoryModalOpen } = useModalOpeners()
const { createUntitledNote } = useContentContext()
const handleDrop = async (e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
const sourcePath = e.dataTransfer.getData('text/plain')
const destinationDirectory = await window.electronStore.getVaultDirectoryForWindow()
const destinationPath = await window.path.join(destinationDirectory, await window.path.basename(sourcePath))
renameFile(sourcePath, destinationPath)
}
const handleDragOver = (e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
}
const handleClick = () => {
setSelectedDirectory(null)
}
useEffect(() => {
const updateHeight = () => {
setListHeight(lheight ?? window.innerHeight - 50)
}
window.addEventListener('resize', updateHeight)
return () => {
window.removeEventListener('resize', updateHeight)
}
}, [lheight])
const filesAndIndentations = getFilesAndIndentationsForSidebar(vaultFilesTree, expandedDirectories)
const itemCount = filesAndIndentations.length
return (
<YStack className="h-full grow px-1 pt-2" backgroundColor="$gray3">
<XStack className="mb-1 items-center justify-start gap-1 px-1">
<YStack
alignItems="center"
justifyContent="center"
hoverStyle={{ backgroundColor: '$gray7' }}
className="size-5 cursor-pointer rounded opacity-60 hover:opacity-90"
onPress={() => createUntitledNote()}
>
<HiOutlinePencilAlt className="text-gray-300" color="gray" size={16} title="New Note" />
</YStack>
<YStack
alignItems="center"
justifyContent="center"
hoverStyle={{ backgroundColor: '$gray7' }}
className="size-5 cursor-pointer rounded opacity-60 hover:opacity-90"
onPress={() => setIsNewDirectoryModalOpen(true)}
>
<VscNewFolder className="text-gray-300" color="gray" size={16} title="New Directory" />
</YStack>
</XStack>
<div onDrop={handleDrop} onDragOver={handleDragOver} onClick={handleClick}>
<FixedSizeList
height={listHeight}
itemCount={itemCount}
itemSize={30}
width="100%"
itemData={{
filesAndIndentations,
}}
>
{FileItemRows}
</FixedSizeList>
</div>
</YStack>
)
}
export default FileSidebar