Skip to content

Commit

Permalink
handle drag n drop errs
Browse files Browse the repository at this point in the history
  • Loading branch information
samlhuillier committed Nov 1, 2024
1 parent 38e9377 commit 3176f2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
13 changes: 12 additions & 1 deletion electron/main/common/windowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,18 @@ class WindowsManager {
}

appendNewErrorToDisplayInWindow(errorString: string) {
this.errorStringsToSendWindow.push(errorString)
let errorSent = false
const activeWindows = BrowserWindow.getAllWindows()
activeWindows.forEach((window) => {
if (!window.webContents.isLoading()) {
window.webContents.send('error-to-display-in-window', errorString)
errorSent = true
}
})

if (!errorSent) {
this.errorStringsToSendWindow.push(errorString)
}
}

getAndClearErrorStrings(): string[] {
Expand Down
15 changes: 13 additions & 2 deletions electron/main/filesystem/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function readFile(filePath: string): string {
return data
}

export const moveFileOrDirectoryInFileSystem = async (sourcePath: string, destinationPath: string): Promise<string> => {
export const moveFileOrDirectory = async (sourcePath: string, destinationPath: string): Promise<string> => {
await fsPromises.access(sourcePath)

let destinationStats
Expand All @@ -187,6 +187,7 @@ export const moveFileOrDirectoryInFileSystem = async (sourcePath: string, destin
} catch (error) {
// Error means destination path does not exist, which is fine
}

let resolvedDestinationPath = destinationPath
if (destinationStats && destinationStats.isFile()) {
resolvedDestinationPath = path.dirname(destinationPath)
Expand All @@ -195,7 +196,17 @@ export const moveFileOrDirectoryInFileSystem = async (sourcePath: string, destin
await fsPromises.mkdir(resolvedDestinationPath, { recursive: true })

const newPath = path.join(resolvedDestinationPath, path.basename(sourcePath))
await fsPromises.rename(sourcePath, newPath)

try {
await fsPromises.access(newPath)
throw new Error(`A file already exists at destination: ${newPath}`)
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
await fsPromises.rename(sourcePath, newPath)
} else {
throw error
}
}

return newPath
}
Expand Down
12 changes: 6 additions & 6 deletions electron/main/vector-database/tableHelperFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GetFilesInfoList,
GetFilesInfoTree,
flattenFileInfoTree,
moveFileOrDirectoryInFileSystem,
moveFileOrDirectory,
readFile,
} from '../filesystem/filesystem'
import { FileInfo, FileInfoTree } from '../filesystem/types'
Expand Down Expand Up @@ -160,11 +160,11 @@ export const addFileTreeToDBTable = async (dbTable: LanceDBTableWrapper, fileTre
export const orchestrateEntryMove = async (table: LanceDBTableWrapper, sourcePath: string, destinationPath: string) => {
const fileSystemTree = GetFilesInfoTree(sourcePath)
await removeFileTreeFromDBTable(table, fileSystemTree)
moveFileOrDirectoryInFileSystem(sourcePath, destinationPath).then((newDestinationPath) => {
if (newDestinationPath) {
addFileTreeToDBTable(table, GetFilesInfoTree(newDestinationPath))
}
})

const newDestinationPath = await moveFileOrDirectory(sourcePath, destinationPath)
if (newDestinationPath) {
await addFileTreeToDBTable(table, GetFilesInfoTree(newDestinationPath))
}
}

export function formatTimestampForLanceDB(date: Date): string {
Expand Down
17 changes: 15 additions & 2 deletions src/components/Sidebars/FileSideBar/FileSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FixedSizeList } from 'react-window'
import { isFileNodeDirectory } from '@shared/utils'
import { useFileContext } from '@/contexts/FileContext'
import FileItemRows from './FileItemRows'
import { moveFile } from '@/lib/file'

const getFilesAndIndentationsForSidebar = (
files: FileInfoTree,
Expand Down Expand Up @@ -33,6 +34,19 @@ const FileSidebar: React.FC<FileExplorerProps> = ({ lheight }) => {
const [listHeight, setListHeight] = useState(lheight ?? window.innerHeight - 50)
const { vaultFilesTree, expandedDirectories } = useFileContext()

const handleDrop = async (e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
const sourcePath = e.dataTransfer.getData('text/plain')
const destinationPath = await window.electronStore.getVaultDirectoryForWindow()
moveFile(sourcePath, destinationPath)
}

const handleDragOver = (e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
}

useEffect(() => {
const updateHeight = () => {
setListHeight(lheight ?? window.innerHeight - 50)
Expand All @@ -47,13 +61,12 @@ const FileSidebar: React.FC<FileExplorerProps> = ({ lheight }) => {
const itemCount = filesAndIndentations.length

return (
<div className="h-full grow bg-pink-200 px-1 pt-2 opacity-70">
<div className="h-full grow px-1 pt-2 opacity-70" onDrop={handleDrop} onDragOver={handleDragOver}>
<FixedSizeList
height={listHeight}
itemCount={itemCount}
itemSize={30}
width="100%"
style={{ backgroundColor: 'pink' }}
itemData={{
filesAndIndentations,
}}
Expand Down

0 comments on commit 3176f2a

Please sign in to comment.