Skip to content

Commit

Permalink
delete functionaility & all file watching event types
Browse files Browse the repository at this point in the history
  • Loading branch information
samlhuillier committed Dec 24, 2023
1 parent d56e147 commit 261fc2d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
17 changes: 11 additions & 6 deletions electron/main/Files/Filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,26 @@ 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 ||
extensionsToFilterFor.some((ext) =>
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) {
Expand Down
18 changes: 12 additions & 6 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 4 additions & 13 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: (
Expand Down Expand Up @@ -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", {
Expand Down
2 changes: 1 addition & 1 deletion src/components/File/FileSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const FileItem: React.FC<FileInfoProps> = ({

const handleContextMenu = (e: React.MouseEvent) => {
e.preventDefault();
window.contextMenu.showFileItemContextMenu(file.path);
window.contextMenu.showFileItemContextMenu(file);
};
// useEffect(() => {
// const removeMenuActionListener = window.contextMenu.onMenuActionCUNT(
Expand Down

0 comments on commit 261fc2d

Please sign in to comment.