Skip to content

Commit

Permalink
Merge pull request #393 from reorproject/jp/chat-cleanup
Browse files Browse the repository at this point in the history
Jp/chat cleanup
  • Loading branch information
joseplayero authored Sep 7, 2024
2 parents 1b1bd85 + 92c98b2 commit 7253c03
Show file tree
Hide file tree
Showing 39 changed files with 1,383 additions and 1,556 deletions.
9 changes: 5 additions & 4 deletions electron/main/electron-store/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const registerStoreHandlers = (store: Store<StoreSchema>, windowsManager:
store.set(StoreKeys.hasUserOpenedAppBefore, true)
})

ipcMain.handle('get-all-chat-histories', (event) => {
ipcMain.handle('get-all-chats', (event) => {
const vaultDir = windowsManager.getVaultDirectoryForWinContents(event.sender)

if (!vaultDir) {
Expand All @@ -143,7 +143,7 @@ export const registerStoreHandlers = (store: Store<StoreSchema>, windowsManager:
return chatHistoriesCorrespondingToVault
})

ipcMain.handle('update-chat-history', (event, newChat: Chat) => {
ipcMain.handle('update-chat', (event, newChat: Chat) => {
const vaultDir = windowsManager.getVaultDirectoryForWinContents(event.sender)
const allChatHistories = store.get(StoreKeys.ChatHistories)
if (!vaultDir) {
Expand All @@ -166,7 +166,7 @@ export const registerStoreHandlers = (store: Store<StoreSchema>, windowsManager:
event.sender.send('update-chat-histories', chatHistoriesCorrespondingToVault)
})

ipcMain.handle('get-chat-history', (event, chatId: string) => {
ipcMain.handle('get-chat', (event, chatId: string) => {
const vaultDir = windowsManager.getVaultDirectoryForWinContents(event.sender)
if (!vaultDir) {
return null
Expand All @@ -176,7 +176,8 @@ export const registerStoreHandlers = (store: Store<StoreSchema>, windowsManager:
return vaultChatHistories.find((chat) => chat.id === chatId)
})

ipcMain.handle('remove-chat-history-at-id', (event, chatID: string) => {
ipcMain.handle('delete-chat-at-id', (event, chatID: string | undefined) => {
if (!chatID) return
const vaultDir = windowsManager.getVaultDirectoryForWinContents(event.sender)

if (!vaultDir) {
Expand Down
87 changes: 2 additions & 85 deletions electron/main/electron-utils/ipcHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { app, BrowserWindow, dialog, ipcMain, Menu, MenuItem, shell } from 'electron'
import { app, ipcMain, shell } from 'electron'
import Store from 'electron-store'

import WindowsManager from '../common/windowManager'
import { StoreKeys, StoreSchema } from '../electron-store/storeConfig'
import { ChatHistoryMetadata } from '@/components/Chat/hooks/use-chat-history'
import { FileInfoNode } from '../filesystem/types'
import { StoreSchema } from '../electron-store/storeConfig'

const registerElectronUtilsHandlers = (
store: Store<StoreSchema>,
Expand All @@ -13,87 +11,6 @@ const registerElectronUtilsHandlers = (
url: string | undefined,
indexHtml: string,
) => {
ipcMain.handle('show-context-menu-file-item', async (event, file: FileInfoNode) => {
const menu = new Menu()

menu.append(
new MenuItem({
label: 'Delete',
click: () =>
dialog
.showMessageBox({
type: 'question',
title: 'Delete File',
message: `Are you sure you want to delete "${file.name}"?`,
buttons: ['Yes', 'No'],
})
.then((confirm) => {
if (confirm.response === 0) {
event.sender.send('delete-file-listener', file.path)
}
}),
}),
)
menu.append(
new MenuItem({
label: 'Rename',
click: () => {
event.sender.send('rename-file-listener', file.path)
},
}),
)

menu.append(
new MenuItem({
label: 'Create a flashcard set',
click: () => {
event.sender.send('create-flashcard-file-listener', file.path)
},
}),
)

menu.append(
new MenuItem({
label: 'Add file to chat context',
click: () => {
event.sender.send('add-file-to-chat-listener', file.path)
},
}),
)

const browserWindow = BrowserWindow.fromWebContents(event.sender)
if (browserWindow) {
menu.popup({ window: browserWindow })
}
})

ipcMain.handle('show-chat-menu-item', (event, chatRow: ChatHistoryMetadata) => {
const menu = new Menu()

menu.append(
new MenuItem({
label: 'Delete Chat',
click: () => {
const vaultDir = windowsManager.getVaultDirectoryForWinContents(event.sender)

if (!vaultDir) {
return
}

const chatHistoriesMap = store.get(StoreKeys.ChatHistories)
const allChatHistories = chatHistoriesMap[vaultDir] || []
const filteredChatHistories = allChatHistories.filter((item) => item.id !== chatRow.id)
chatHistoriesMap[vaultDir] = filteredChatHistories
store.set(StoreKeys.ChatHistories, chatHistoriesMap)
event.sender.send('update-chat-histories', chatHistoriesMap[vaultDir] || [])
},
}),
)

const browserWindow = BrowserWindow.fromWebContents(event.sender)
if (browserWindow) menu.popup({ window: browserWindow })
})

ipcMain.handle('open-external', (event, _url) => {
shell.openExternal(_url)
})
Expand Down
13 changes: 5 additions & 8 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
LLMGenerationParameters,
Tab,
} from 'electron/main/electron-store/storeConfig'
import { FileInfoNode, FileInfoTree, RenameFileProps, WriteFileProps } from 'electron/main/filesystem/types'
import { FileInfoTree, RenameFileProps, WriteFileProps } from 'electron/main/filesystem/types'
import { DBEntry, DBQueryResult } from 'electron/main/vector-database/schema'

import { ChatHistoryMetadata } from '@/components/Chat/hooks/use-chat-history'
import { Chat } from '@/components/Chat/types'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -40,8 +39,6 @@ const electronUtils = {
getPlatform: createIPCHandler<() => Promise<string>>('get-platform'),
openNewWindow: createIPCHandler<() => Promise<void>>('open-new-window'),
getReorAppVersion: createIPCHandler<() => Promise<string>>('get-reor-app-version'),
showFileItemContextMenu: createIPCHandler<(file: FileInfoNode) => Promise<void>>('show-context-menu-file-item'),
showChatItemContext: createIPCHandler<(chatRow: ChatHistoryMetadata) => Promise<void>>('show-chat-menu-item'),
}

const electronStore = {
Expand Down Expand Up @@ -71,10 +68,10 @@ const electronStore = {
setSpellCheckMode: createIPCHandler<(isSpellCheck: boolean) => Promise<void>>('set-spellcheck-mode'),
getHasUserOpenedAppBefore: createIPCHandler<() => Promise<boolean>>('has-user-opened-app-before'),
setHasUserOpenedAppBefore: createIPCHandler<() => Promise<void>>('set-user-has-opened-app-before'),
getAllChatHistories: createIPCHandler<() => Promise<Chat[]>>('get-all-chat-histories'),
updateChatHistory: createIPCHandler<(chatHistory: Chat) => Promise<void>>('update-chat-history'),
removeChatHistoryAtID: createIPCHandler<(chatID: string) => Promise<void>>('remove-chat-history-at-id'),
getChatHistory: createIPCHandler<(chatID: string) => Promise<Chat>>('get-chat-history'),
getAllChats: createIPCHandler<() => Promise<Chat[]>>('get-all-chats'),
updateChat: createIPCHandler<(chat: Chat) => Promise<void>>('update-chat'),
deleteChatAtID: createIPCHandler<(chatID: string) => Promise<void>>('delete-chat-at-id'),
getChat: createIPCHandler<(chatID: string) => Promise<Chat>>('get-chat'),
getSBCompact: createIPCHandler<() => Promise<boolean>>('get-sb-compact'),
setSBCompact: createIPCHandler<(isSBCompact: boolean) => Promise<void>>('set-sb-compact'),
getEditorFlexCenter: createIPCHandler<() => Promise<boolean>>('get-editor-flex-center'),
Expand Down
4 changes: 2 additions & 2 deletions src/components/Chat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const ChatInput: React.FC<ChatInputProps> = ({
handleSubmitNewMessage,
loadingResponse,
}) => (
<div className="relative flex h-titlebar w-full items-center justify-center bg-dark-gray-c-eleven p-10">
<div className="flex h-titlebar w-full items-center justify-center bg-dark-gray-c-eleven p-10">
<div className=" relative bottom-5 flex w-full max-w-3xl">
<div className="w-full rounded-lg border-2 border-solid border-neutral-700 p-3 focus-within:ring-1 focus-within:ring-[#8c8c8c]">
<div className="relative flex h-full pr-8">
<div className="flex h-full pr-8">
<textarea
onKeyDown={(e) => {
if (userTextFieldInput && !e.shiftKey && e.key === 'Enter') {
Expand Down
Loading

0 comments on commit 7253c03

Please sign in to comment.