From 261fc2d41b00d95ede65a97281a44cf19b0fb907 Mon Sep 17 00:00:00 2001 From: samlhuillier Date: Sun, 24 Dec 2023 22:55:11 +0100 Subject: [PATCH] delete functionaility & all file watching event types --- electron/main/Files/Filesystem.ts | 17 +++++++++++------ electron/main/index.ts | 18 ++++++++++++------ electron/preload/index.ts | 17 ++++------------- src/components/File/FileSidebar.tsx | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/electron/main/Files/Filesystem.ts b/electron/main/Files/Filesystem.ts index c52979ed..a1e87db9 100644 --- a/electron/main/Files/Filesystem.ts +++ b/electron/main/Files/Filesystem.ts @@ -155,7 +155,7 @@ export function startWatchingDirectory( ignoreInitial: true, }); - watcher.on("add", (path) => { + const handleFileEvent = (eventType: string, path: string) => { // Check if the file extension is in the provided list (if any) if ( !extensionsToFilterFor || @@ -163,13 +163,18 @@ export function startWatchingDirectory( path.toLowerCase().endsWith(ext.toLowerCase()) ) ) { - console.log("extensions: ", extensionsToFilterFor); - console.log(`File added: ${path}`); + console.log(`extensions: `, extensionsToFilterFor); + console.log(`File ${eventType}: ${path}`); updateFileListForRenderer(win, directory); } - }); - - // Handle other events like 'change', 'unlink' if needed + }; + + watcher + .on("add", (path) => handleFileEvent("added", path)) + .on("change", (path) => handleFileEvent("changed", path)) + .on("unlink", (path) => handleFileEvent("removed", path)) + .on("addDir", (path) => handleFileEvent("directory added", path)) + .on("unlinkDir", (path) => handleFileEvent("directory removed", path)); // No 'ready' event handler is needed here, as we're ignoring initial scan } catch (error) { diff --git a/electron/main/index.ts b/electron/main/index.ts index ce942f05..3e0cf2cf 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -15,7 +15,7 @@ import * as path from "path"; import { AIModelConfig, StoreKeys, StoreSchema } from "./Store/storeConfig"; // import contextMenus from "./contextMenus"; import * as lancedb from "vectordb"; - +import * as fs from "fs"; import { RagnoteTable, maybeRePopulateTable, @@ -31,7 +31,7 @@ import { writeFileSyncRecursive, } from "./Files/Filesystem"; import { registerLLMSessionHandlers } from "./llm/llmSessionHandlers"; -import { FileInfoTree } from "./Files/Types"; +import { FileInfoNode, FileInfoTree } from "./Files/Types"; import { registerDBSessionHandlers } from "./database/dbSessionHandlers"; import { validateAIModelConfig } from "./llm/llmConfig"; import { registerStoreHandlers } from "./Store/storeHandlers"; @@ -223,19 +223,25 @@ ipcMain.on("index-files-in-directory", async (event, userDirectory: string) => { event.sender.send("indexing-complete", "success"); }); -ipcMain.on("show-context-menu-file-item", (event, menuKey) => { +ipcMain.on("show-context-menu-file-item", (event, file: FileInfoNode) => { const menu = new Menu(); menu.append( new MenuItem({ label: "Delete", click: () => { - console.log("CLICKED MENU ITEM: delete"); - // event.sender.send("context-menu-command", "delete"); + console.log(file.path); + fs.unlink(file.path, (err) => { + if (err) { + console.error("An error occurred:", err); + return; + } + console.log(`File at ${file.path} was deleted successfully.`); + }); // event.sender.send("context-menu-command", "delete"); }, }) ); - console.log("menu key: ", menuKey); + console.log("menu key: ", file); const browserWindow = BrowserWindow.fromWebContents(event.sender); if (browserWindow) { diff --git a/electron/preload/index.ts b/electron/preload/index.ts index d9316813..167c6191 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -1,6 +1,6 @@ import { contextBridge, ipcRenderer } from "electron"; import { AIModelConfig } from "electron/main/Store/storeConfig"; -import { FileInfoTree } from "electron/main/Files/Types"; +import { FileInfoNode, FileInfoTree } from "electron/main/Files/Types"; // import { FileInfo } from "electron/main/Files/Types"; import { RagnoteDBEntry } from "electron/main/database/Table"; type ReceiveCallback = (...args: any[]) => void; @@ -21,8 +21,7 @@ declare global { receive: (channel: string, callback: ReceiveCallback) => void; }; contextMenu: { - showFileItemContextMenu: (filePath: string) => void; - onMenuActionCUNT: (callback: (action: string) => void) => () => void; + showFileItemContextMenu: (filePath: FileInfoNode) => void; }; database: { search: ( @@ -143,19 +142,11 @@ contextBridge.exposeInMainWorld("electronAPI", { contextBridge.exposeInMainWorld("contextMenu", { // Function to trigger the context menu - showFileItemContextMenu: (filePath: string) => { - ipcRenderer.send("show-context-menu-file-item", filePath); + showFileItemContextMenu: (file: FileInfoNode) => { + ipcRenderer.send("show-context-menu-file-item", file); }, // Function to set up a listener for menu actions - onMenuActionCUNT: (callback: (action: string) => void) => { - const handler = (_: any, action: any) => callback(action); - ipcRenderer.on("context-menu-command", handler); - // Return a function to remove the listener - return () => { - ipcRenderer.removeListener("context-menu-command", handler); - }; - }, }); contextBridge.exposeInMainWorld("files", { diff --git a/src/components/File/FileSidebar.tsx b/src/components/File/FileSidebar.tsx index 4613a1f7..30b5d11e 100644 --- a/src/components/File/FileSidebar.tsx +++ b/src/components/File/FileSidebar.tsx @@ -166,7 +166,7 @@ const FileItem: React.FC = ({ const handleContextMenu = (e: React.MouseEvent) => { e.preventDefault(); - window.contextMenu.showFileItemContextMenu(file.path); + window.contextMenu.showFileItemContextMenu(file); }; // useEffect(() => { // const removeMenuActionListener = window.contextMenu.onMenuActionCUNT(