From bde3e875a7692b394b4ab2061041218aa3e4fddf Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 9 Dec 2023 09:57:58 +0700 Subject: [PATCH] refactor: define routes and auto proxying routes --- core/src/api/index.ts | 60 +++++++++++++ core/src/index.ts | 17 ++-- electron/handlers/app.ts | 45 ++++++++-- electron/handlers/download.ts | 22 ++--- electron/handlers/extension.ts | 65 +++++++------- electron/handlers/fs.ts | 146 ++++++++++++++++++-------------- electron/handlers/theme.ts | 27 ------ electron/handlers/update.ts | 7 +- electron/main.ts | 2 - electron/preload.ts | 66 +++------------ web/hooks/useCreateNewThread.ts | 3 +- 11 files changed, 255 insertions(+), 205 deletions(-) create mode 100644 core/src/api/index.ts delete mode 100644 electron/handlers/theme.ts diff --git a/core/src/api/index.ts b/core/src/api/index.ts new file mode 100644 index 0000000000..1371f7b00c --- /dev/null +++ b/core/src/api/index.ts @@ -0,0 +1,60 @@ +/** + * App Route APIs + * @description Enum of all the routes exposed by the app + */ +export enum AppRoute { + setNativeThemeLight = 'setNativeThemeLight', + setNativeThemeDark = 'setNativeThemeDark', + setNativeThemeSystem = 'setNativeThemeSystem', + appDataPath = 'appDataPath', + appVersion = 'appVersion', + openExternalUrl = 'openExternalUrl', + relaunch = 'relaunch', + openAppDirectory = 'openAppDirectory', + openFileExplore = 'openFileExplorer', + getResourcePath = 'getResourcePath', +} + +export enum AppEvent { + onAppUpdateDownloadUpdate = 'onAppUpdateDownloadUpdate', + onAppUpdateDownloadError = 'onAppUpdateDownloadError', + onAppUpdateDownloadSuccess = 'onAppUpdateDownloadSuccess', +} + +export enum DownloadRoute { + downloadFile = 'downloadFile', + pauseDownload = 'pauseDownload', + resumeDownload = 'resumeDownload', + abortDownload = 'abortDownload', +} + +export enum DownloadEvent { + onFileDownloadUpdate = 'onFileDownloadUpdate', + onFileDownloadError = 'onFileDownloadError', + onFileDownloadSuccess = 'onFileDownloadSuccess', +} + +export enum ExtensionRoute { + installExtension = 'installExtension', + uninstallExtension = 'uninstallExtension', + getActiveExtensions = 'getActiveExtensions', + updateExtension = 'updateExtension', + invokeExtensionFunc = 'invokeExtensionFunc', + baseExtensions = 'baseExtensions', + extensionPath = 'extensionPath', +} +export enum FileSystemRoute { + deleteFile = 'deleteFile', + isDirectory = 'isDirectory', + getUserSpace = 'getUserSpace', + readFile = 'readFile', + writeFile = 'writeFile', + listFiles = 'listFiles', + appendFile = 'appendFile', + readLineByLine = 'readLineByLine', + mkdir = 'mkdir', + rmdir = 'rmdir', + copyFile = 'copyFile', + getResourcePath = 'getResourcePath', + exists = 'exists', +} diff --git a/core/src/index.ts b/core/src/index.ts index ff233ffb3c..a56b6f0e13 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -2,34 +2,39 @@ * Export all types. * @module */ -export * from "./types/index"; +export * from './types/index' + +/** + * Export all routes + */ +export * from './api' /** * Export Core module * @module */ -export * from "./core"; +export * from './core' /** * Export Event module. * @module */ -export * from "./events"; +export * from './events' /** * Export Filesystem module. * @module */ -export * from "./fs"; +export * from './fs' /** * Export Extension module. * @module */ -export * from "./extension"; +export * from './extension' /** * Export all base extensions. * @module */ -export * from "./extensions/index"; +export * from './extensions/index' diff --git a/electron/handlers/app.ts b/electron/handlers/app.ts index adbc875b2e..08aeb0ab7e 100644 --- a/electron/handlers/app.ts +++ b/electron/handlers/app.ts @@ -1,17 +1,42 @@ -import { app, ipcMain, shell } from 'electron' +import { app, ipcMain, shell, nativeTheme } from 'electron' import { ModuleManager } from './../managers/module' import { join } from 'path' import { ExtensionManager } from './../managers/extension' import { WindowManager } from './../managers/window' import { userSpacePath } from './../utils/path' +import { AppRoute } from '@janhq/core' +import { getResourcePath } from './../utils/path' export function handleAppIPCs() { + /** + * Handles the "setNativeThemeLight" IPC message by setting the native theme source to "light". + * This will change the appearance of the app to the light theme. + */ + ipcMain.handle(AppRoute.setNativeThemeLight, () => { + nativeTheme.themeSource = 'light' + }) + + /** + * Handles the "setNativeThemeDark" IPC message by setting the native theme source to "dark". + * This will change the appearance of the app to the dark theme. + */ + ipcMain.handle(AppRoute.setNativeThemeDark, () => { + nativeTheme.themeSource = 'dark' + }) + + /** + * Handles the "setNativeThemeSystem" IPC message by setting the native theme source to "system". + * This will change the appearance of the app to match the system's current theme. + */ + ipcMain.handle(AppRoute.setNativeThemeSystem, () => { + nativeTheme.themeSource = 'system' + }) /** * Retrieves the path to the app data directory using the `coreAPI` object. * If the `coreAPI` object is not available, the function returns `undefined`. * @returns A Promise that resolves with the path to the app data directory, or `undefined` if the `coreAPI` object is not available. */ - ipcMain.handle('appDataPath', async (_event) => { + ipcMain.handle(AppRoute.appDataPath, async (_event) => { return app.getPath('userData') }) @@ -20,7 +45,7 @@ export function handleAppIPCs() { * @param _event - The IPC event object. * @returns The version of the app. */ - ipcMain.handle('appVersion', async (_event) => { + ipcMain.handle(AppRoute.appVersion, async (_event) => { return app.getVersion() }) @@ -29,16 +54,20 @@ export function handleAppIPCs() { * The `shell.openPath` method is used to open the directory in the user's default file explorer. * @param _event - The IPC event object. */ - ipcMain.handle('openAppDirectory', async (_event) => { + ipcMain.handle(AppRoute.openAppDirectory, async (_event) => { shell.openPath(userSpacePath) }) + ipcMain.handle(AppRoute.getResourcePath, async (_event) => { + return getResourcePath() + }) + /** * Opens a URL in the user's default browser. * @param _event - The IPC event object. * @param url - The URL to open. */ - ipcMain.handle('openExternalUrl', async (_event, url) => { + ipcMain.handle(AppRoute.openExternalUrl, async (_event, url) => { shell.openExternal(url) }) @@ -47,7 +76,7 @@ export function handleAppIPCs() { * @param _event - The IPC event object. * @param url - The URL to reload. */ - ipcMain.handle('relaunch', async (_event, url) => { + ipcMain.handle(AppRoute.relaunch, async (_event, url) => { ModuleManager.instance.clearImportedModules() if (app.isPackaged) { @@ -56,9 +85,7 @@ export function handleAppIPCs() { } else { for (const modulePath in ModuleManager.instance.requiredModules) { delete require.cache[ - require.resolve( - join(userSpacePath, 'extensions', modulePath) - ) + require.resolve(join(userSpacePath, 'extensions', modulePath)) ] } ExtensionManager.instance.setupExtensions() diff --git a/electron/handlers/download.ts b/electron/handlers/download.ts index d66f449746..6e64d23e2f 100644 --- a/electron/handlers/download.ts +++ b/electron/handlers/download.ts @@ -4,7 +4,7 @@ import { resolve, join } from 'path' import { WindowManager } from './../managers/window' import request from 'request' import { createWriteStream } from 'fs' -import { getResourcePath } from './../utils/path' +import { DownloadEvent, DownloadRoute } from '@janhq/core' const progress = require('request-progress') export function handleDownloaderIPCs() { @@ -13,7 +13,7 @@ export function handleDownloaderIPCs() { * @param _event - The IPC event object. * @param fileName - The name of the file being downloaded. */ - ipcMain.handle('pauseDownload', async (_event, fileName) => { + ipcMain.handle(DownloadRoute.pauseDownload, async (_event, fileName) => { DownloadManager.instance.networkRequests[fileName]?.pause() }) @@ -22,7 +22,7 @@ export function handleDownloaderIPCs() { * @param _event - The IPC event object. * @param fileName - The name of the file being downloaded. */ - ipcMain.handle('resumeDownload', async (_event, fileName) => { + ipcMain.handle(DownloadRoute.resumeDownload, async (_event, fileName) => { DownloadManager.instance.networkRequests[fileName]?.resume() }) @@ -32,23 +32,19 @@ export function handleDownloaderIPCs() { * @param _event - The IPC event object. * @param fileName - The name of the file being downloaded. */ - ipcMain.handle('abortDownload', async (_event, fileName) => { + ipcMain.handle(DownloadRoute.abortDownload, async (_event, fileName) => { const rq = DownloadManager.instance.networkRequests[fileName] DownloadManager.instance.networkRequests[fileName] = undefined rq?.abort() }) - ipcMain.handle('getResourcePath', async (_event) => { - return getResourcePath() - }) - /** * Downloads a file from a given URL. * @param _event - The IPC event object. * @param url - The URL to download the file from. * @param fileName - The name to give the downloaded file. */ - ipcMain.handle('downloadFile', async (_event, url, fileName) => { + ipcMain.handle(DownloadRoute.downloadFile, async (_event, url, fileName) => { const userDataPath = join(app.getPath('home'), 'jan') const destination = resolve(userDataPath, fileName) const rq = request(url) @@ -56,7 +52,7 @@ export function handleDownloaderIPCs() { progress(rq, {}) .on('progress', function (state: any) { WindowManager?.instance.currentWindow?.webContents.send( - 'onFileDownloadUpdate', + DownloadEvent.onFileDownloadUpdate, { ...state, fileName, @@ -65,7 +61,7 @@ export function handleDownloaderIPCs() { }) .on('error', function (err: Error) { WindowManager?.instance.currentWindow?.webContents.send( - 'onFileDownloadError', + DownloadEvent.onFileDownloadError, { fileName, err, @@ -75,7 +71,7 @@ export function handleDownloaderIPCs() { .on('end', function () { if (DownloadManager.instance.networkRequests[fileName]) { WindowManager?.instance.currentWindow?.webContents.send( - 'onFileDownloadSuccess', + DownloadEvent.onFileDownloadSuccess, { fileName, } @@ -83,7 +79,7 @@ export function handleDownloaderIPCs() { DownloadManager.instance.setRequest(fileName, undefined) } else { WindowManager?.instance.currentWindow?.webContents.send( - 'onFileDownloadError', + DownloadEvent.onFileDownloadError, { fileName, err: 'Download cancelled', diff --git a/electron/handlers/extension.ts b/electron/handlers/extension.ts index 4e401ad518..f73a472c1f 100644 --- a/electron/handlers/extension.ts +++ b/electron/handlers/extension.ts @@ -11,6 +11,7 @@ import { getExtension } from './../extension/store' import { removeExtension } from './../extension/store' import Extension from './../extension/extension' import { getResourcePath, userSpacePath } from './../utils/path' +import { ExtensionRoute } from '@janhq/core' export function handleExtensionIPCs() { /**MARK: General handlers */ @@ -23,7 +24,7 @@ export function handleExtensionIPCs() { * @returns The result of the invoked function. */ ipcMain.handle( - 'invokeExtensionFunc', + ExtensionRoute.invokeExtensionFunc, async (_event, modulePath, method, ...args) => { const module = require( /* webpackIgnore: true */ join(userSpacePath, 'extensions', modulePath) @@ -44,7 +45,7 @@ export function handleExtensionIPCs() { * @param _event - The IPC event object. * @returns An array of paths to the base extensions. */ - ipcMain.handle('baseExtensions', async (_event) => { + ipcMain.handle(ExtensionRoute.baseExtensions, async (_event) => { const baseExtensionPath = join(getResourcePath(), 'pre-install') return readdirSync(baseExtensionPath) .filter((file) => extname(file) === '.tgz') @@ -56,50 +57,56 @@ export function handleExtensionIPCs() { * @param _event - The IPC event extension. * @returns The path to the user's extension directory. */ - ipcMain.handle('extensionPath', async (_event) => { + ipcMain.handle(ExtensionRoute.extensionPath, async (_event) => { return join(userSpacePath, 'extensions') }) /**MARK: Extension Manager handlers */ - ipcMain.handle('installExtension', async (e, extensions) => { + ipcMain.handle(ExtensionRoute.installExtension, async (e, extensions) => { // Install and activate all provided extensions const installed = await installExtensions(extensions) return JSON.parse(JSON.stringify(installed)) }) // Register IPC route to uninstall a extension - ipcMain.handle('uninstallExtension', async (e, extensions, reload) => { - // Uninstall all provided extensions - for (const ext of extensions) { - const extension = getExtension(ext) - await extension.uninstall() - if (extension.name) removeExtension(extension.name) - } + ipcMain.handle( + ExtensionRoute.uninstallExtension, + async (e, extensions, reload) => { + // Uninstall all provided extensions + for (const ext of extensions) { + const extension = getExtension(ext) + await extension.uninstall() + if (extension.name) removeExtension(extension.name) + } - // Reload all renderer pages if needed - reload && webContents.getAllWebContents().forEach((wc) => wc.reload()) - return true - }) + // Reload all renderer pages if needed + reload && webContents.getAllWebContents().forEach((wc) => wc.reload()) + return true + } + ) // Register IPC route to update a extension - ipcMain.handle('updateExtension', async (e, extensions, reload) => { - // Update all provided extensions - const updated: Extension[] = [] - for (const ext of extensions) { - const extension = getExtension(ext) - const res = await extension.update() - if (res) updated.push(extension) - } + ipcMain.handle( + ExtensionRoute.updateExtension, + async (e, extensions, reload) => { + // Update all provided extensions + const updated: Extension[] = [] + for (const ext of extensions) { + const extension = getExtension(ext) + const res = await extension.update() + if (res) updated.push(extension) + } - // Reload all renderer pages if needed - if (updated.length && reload) - webContents.getAllWebContents().forEach((wc) => wc.reload()) + // Reload all renderer pages if needed + if (updated.length && reload) + webContents.getAllWebContents().forEach((wc) => wc.reload()) - return JSON.parse(JSON.stringify(updated)) - }) + return JSON.parse(JSON.stringify(updated)) + } + ) // Register IPC route to get the list of active extensions - ipcMain.handle('getActiveExtensions', () => { + ipcMain.handle(ExtensionRoute.getActiveExtensions, () => { return JSON.parse(JSON.stringify(getActiveExtensions())) }) } diff --git a/electron/handlers/fs.ts b/electron/handlers/fs.ts index acc0ed2dab..614461ef39 100644 --- a/electron/handlers/fs.ts +++ b/electron/handlers/fs.ts @@ -4,6 +4,7 @@ import fse from 'fs-extra' import { join } from 'path' import readline from 'readline' import { userSpacePath } from './../utils/path' +import { FileSystemRoute } from '@janhq/core' /** * Handles file system operations. @@ -15,7 +16,7 @@ export function handleFsIPCs() { * @returns A promise that resolves with the path to the user data directory. */ ipcMain.handle( - 'getUserSpace', + FileSystemRoute.getUserSpace, (): Promise => Promise.resolve(userSpacePath) ) @@ -25,12 +26,15 @@ export function handleFsIPCs() { * @param path - The path to check. * @returns A promise that resolves with a boolean indicating whether the path is a directory. */ - ipcMain.handle('isDirectory', (_event, path: string): Promise => { - const fullPath = join(userSpacePath, path) - return Promise.resolve( - fs.existsSync(fullPath) && fs.lstatSync(fullPath).isDirectory() - ) - }) + ipcMain.handle( + FileSystemRoute.isDirectory, + (_event, path: string): Promise => { + const fullPath = join(userSpacePath, path) + return Promise.resolve( + fs.existsSync(fullPath) && fs.lstatSync(fullPath).isDirectory() + ) + } + ) /** * Reads a file from the user data directory. @@ -38,17 +42,20 @@ export function handleFsIPCs() { * @param path - The path of the file to read. * @returns A promise that resolves with the contents of the file. */ - ipcMain.handle('readFile', async (event, path: string): Promise => { - return new Promise((resolve, reject) => { - fs.readFile(join(userSpacePath, path), 'utf8', (err, data) => { - if (err) { - reject(err) - } else { - resolve(data) - } + ipcMain.handle( + FileSystemRoute.readFile, + async (event, path: string): Promise => { + return new Promise((resolve, reject) => { + fs.readFile(join(userSpacePath, path), 'utf8', (err, data) => { + if (err) { + reject(err) + } else { + resolve(data) + } + }) }) - }) - }) + } + ) /** * Checks whether a file exists in the user data directory. @@ -56,7 +63,7 @@ export function handleFsIPCs() { * @param path - The path of the file to check. * @returns A promise that resolves with a boolean indicating whether the file exists. */ - ipcMain.handle('exists', async (_event, path: string) => { + ipcMain.handle(FileSystemRoute.exists, async (_event, path: string) => { return new Promise((resolve, reject) => { const fullPath = join(userSpacePath, path) fs.existsSync(fullPath) ? resolve(true) : resolve(false) @@ -71,7 +78,7 @@ export function handleFsIPCs() { * @returns A promise that resolves when the file has been written. */ ipcMain.handle( - 'writeFile', + FileSystemRoute.writeFile, async (event, path: string, data: string): Promise => { try { await fs.writeFileSync(join(userSpacePath, path), data, 'utf8') @@ -87,13 +94,16 @@ export function handleFsIPCs() { * @param path - The path of the directory to create. * @returns A promise that resolves when the directory has been created. */ - ipcMain.handle('mkdir', async (event, path: string): Promise => { - try { - fs.mkdirSync(join(userSpacePath, path), { recursive: true }) - } catch (err) { - console.error(`mkdir ${path} result: ${err}`) + ipcMain.handle( + FileSystemRoute.mkdir, + async (event, path: string): Promise => { + try { + fs.mkdirSync(join(userSpacePath, path), { recursive: true }) + } catch (err) { + console.error(`mkdir ${path} result: ${err}`) + } } - }) + ) /** * Removes a directory in the user data directory. @@ -101,13 +111,16 @@ export function handleFsIPCs() { * @param path - The path of the directory to remove. * @returns A promise that resolves when the directory is removed successfully. */ - ipcMain.handle('rmdir', async (event, path: string): Promise => { - try { - await fs.rmSync(join(userSpacePath, path), { recursive: true }) - } catch (err) { - console.error(`rmdir ${path} result: ${err}`) + ipcMain.handle( + FileSystemRoute.rmdir, + async (event, path: string): Promise => { + try { + await fs.rmSync(join(userSpacePath, path), { recursive: true }) + } catch (err) { + console.error(`rmdir ${path} result: ${err}`) + } } - }) + ) /** * Lists the files in a directory in the user data directory. @@ -116,7 +129,7 @@ export function handleFsIPCs() { * @returns A promise that resolves with an array of file names. */ ipcMain.handle( - 'listFiles', + FileSystemRoute.listFiles, async (event, path: string): Promise => { return new Promise((resolve, reject) => { fs.readdir(join(userSpacePath, path), (err, files) => { @@ -136,7 +149,7 @@ export function handleFsIPCs() { * @param filePath - The path to the file to delete. * @returns A string indicating the result of the operation. */ - ipcMain.handle('deleteFile', async (_event, filePath) => { + ipcMain.handle(FileSystemRoute.deleteFile, async (_event, filePath) => { try { await fs.unlinkSync(join(userSpacePath, filePath)) } catch (err) { @@ -151,19 +164,25 @@ export function handleFsIPCs() { * @param data - The data to append to the file. * @returns A promise that resolves when the file has been written. */ - ipcMain.handle('appendFile', async (_event, path: string, data: string) => { - try { - await fs.appendFileSync(join(userSpacePath, path), data, 'utf8') - } catch (err) { - console.error(`appendFile ${path} result: ${err}`) + ipcMain.handle( + FileSystemRoute.appendFile, + async (_event, path: string, data: string) => { + try { + await fs.appendFileSync(join(userSpacePath, path), data, 'utf8') + } catch (err) { + console.error(`appendFile ${path} result: ${err}`) + } } - }) + ) - ipcMain.handle('copyFile', async (_event, src: string, dest: string) => { - console.debug(`Copying file from ${src} to ${dest}`) + ipcMain.handle( + FileSystemRoute.copyFile, + async (_event, src: string, dest: string) => { + console.debug(`Copying file from ${src} to ${dest}`) - return fse.copySync(src, dest, { overwrite: false }) - }) + return fse.copySync(src, dest, { overwrite: false }) + } + ) /** * Reads a file line by line. @@ -171,25 +190,28 @@ export function handleFsIPCs() { * @param path - The path of the file to read. * @returns A promise that resolves with the contents of the file. */ - ipcMain.handle('readLineByLine', async (_event, path: string) => { - const fullPath = join(userSpacePath, path) + ipcMain.handle( + FileSystemRoute.readLineByLine, + async (_event, path: string) => { + const fullPath = join(userSpacePath, path) - return new Promise((res, rej) => { - try { - const readInterface = readline.createInterface({ - input: fs.createReadStream(fullPath), - }) - const lines: any = [] - readInterface - .on('line', function (line) { - lines.push(line) - }) - .on('close', function () { - res(lines) + return new Promise((res, rej) => { + try { + const readInterface = readline.createInterface({ + input: fs.createReadStream(fullPath), }) - } catch (err) { - rej(err) - } - }) - }) + const lines: any = [] + readInterface + .on('line', function (line) { + lines.push(line) + }) + .on('close', function () { + res(lines) + }) + } catch (err) { + rej(err) + } + }) + } + ) } diff --git a/electron/handlers/theme.ts b/electron/handlers/theme.ts deleted file mode 100644 index 0038002a86..0000000000 --- a/electron/handlers/theme.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { ipcMain, nativeTheme } from "electron"; - -export function handleThemesIPCs() { - /** - * Handles the "setNativeThemeLight" IPC message by setting the native theme source to "light". - * This will change the appearance of the app to the light theme. - */ - ipcMain.handle("setNativeThemeLight", () => { - nativeTheme.themeSource = "light"; - }); - - /** - * Handles the "setNativeThemeDark" IPC message by setting the native theme source to "dark". - * This will change the appearance of the app to the dark theme. - */ - ipcMain.handle("setNativeThemeDark", () => { - nativeTheme.themeSource = "dark"; - }); - - /** - * Handles the "setNativeThemeSystem" IPC message by setting the native theme source to "system". - * This will change the appearance of the app to match the system's current theme. - */ - ipcMain.handle("setNativeThemeSystem", () => { - nativeTheme.themeSource = "system"; - }); -} diff --git a/electron/handlers/update.ts b/electron/handlers/update.ts index 0bc6f5df6d..cbb34c22b5 100644 --- a/electron/handlers/update.ts +++ b/electron/handlers/update.ts @@ -1,6 +1,7 @@ import { app, dialog } from 'electron' import { WindowManager } from './../managers/window' import { autoUpdater } from 'electron-updater' +import { AppEvent } from '@janhq/core' export function handleAppUpdates() { /* Should not check for update during development */ @@ -19,7 +20,7 @@ export function handleAppUpdates() { /* App Update Completion Message */ autoUpdater.on('update-downloaded', async (_info: any) => { WindowManager.instance.currentWindow?.webContents.send( - 'onAppUpdateDownloadSuccess', + AppEvent.onAppUpdateDownloadSuccess, {} ) const action = await dialog.showMessageBox({ @@ -34,7 +35,7 @@ export function handleAppUpdates() { /* App Update Error */ autoUpdater.on('error', (info: any) => { WindowManager.instance.currentWindow?.webContents.send( - 'onAppUpdateDownloadError', + AppEvent.onAppUpdateDownloadError, {} ) }) @@ -43,7 +44,7 @@ export function handleAppUpdates() { autoUpdater.on('download-progress', (progress: any) => { console.debug('app update progress: ', progress.percent) WindowManager.instance.currentWindow?.webContents.send( - 'onAppUpdateDownloadUpdate', + AppEvent.onAppUpdateDownloadUpdate, { percent: progress.percent, } diff --git a/electron/main.ts b/electron/main.ts index 1898368661..396fbf3415 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -14,7 +14,6 @@ import { ExtensionManager } from './managers/extension' * IPC Handlers **/ import { handleDownloaderIPCs } from './handlers/download' -import { handleThemesIPCs } from './handlers/theme' import { handleExtensionIPCs } from './handlers/extension' import { handleAppIPCs } from './handlers/app' import { handleAppUpdates } from './handlers/update' @@ -79,7 +78,6 @@ function createMainWindow() { function handleIPCs() { handleFsIPCs() handleDownloaderIPCs() - handleThemesIPCs() handleExtensionIPCs() handleAppIPCs() } diff --git a/electron/preload.ts b/electron/preload.ts index 2265d77fe1..b13976f71a 100644 --- a/electron/preload.ts +++ b/electron/preload.ts @@ -2,64 +2,26 @@ * Exposes a set of APIs to the renderer process via the contextBridge object. * @module preload */ + +import { + AppEvent, + AppRoute, + DownloadEvent, + DownloadRoute, + ExtensionRoute, + FileSystemRoute, +} from '@janhq/core' const { contextBridge } = require('electron') const { ipcRenderer } = require('electron') - -// TODO: Add types / class definition instead of keys const ipcMethods = [ - // App - 'setNativeThemeLight', - 'setNativeThemeDark', - 'setNativeThemeSystem', - 'appDataPath', - 'appVersion', - 'openExternalUrl', - 'relaunch', - 'openAppDirectory', - 'openFileExplorer', - - // Downloader - 'downloadFile', - 'pauseDownload', - 'resumeDownload', - 'abortDownload', - - // Extension - 'installExtension', - 'uninstallExtension', - 'getActiveExtensions', - 'updateExtension', - 'invokeExtensionFunc', - 'baseExtensions', - 'extensionPath', - - // Filesystem - 'deleteFile', - 'isDirectory', - 'getUserSpace', - 'readFile', - 'writeFile', - 'listFiles', - 'appendFile', - 'readLineByLine', - 'mkdir', - 'rmdir', - 'copyFile', - 'getResourcePath', - 'exists', + ...Object.values(AppRoute), + ...Object.values(DownloadRoute), + ...Object.values(ExtensionRoute), + ...Object.values(FileSystemRoute), ] -const ipcEvents = [ - // Downloader - 'onFileDownloadUpdate', - 'onFileDownloadError', - 'onFileDownloadSuccess', - // App Update - 'onAppUpdateDownloadUpdate', - 'onAppUpdateDownloadError', - 'onAppUpdateDownloadSuccess', -] +const ipcEvents = [...Object.values(AppEvent), ...Object.values(DownloadEvent)] const interfaces: { [key: string]: (...args: any[]) => any } = {} diff --git a/web/hooks/useCreateNewThread.ts b/web/hooks/useCreateNewThread.ts index 2ba9adb3fb..e2f2aa35dc 100644 --- a/web/hooks/useCreateNewThread.ts +++ b/web/hooks/useCreateNewThread.ts @@ -15,7 +15,6 @@ import { threadsAtom, setActiveThreadIdAtom, threadStatesAtom, - activeThreadAtom, updateThreadAtom, } from '@/helpers/atoms/Conversation.atom' @@ -67,7 +66,7 @@ export const useCreateNewThread = () => { top_p: 0, stream: false, }, - engine: undefined + engine: undefined, }, instructions: assistant.instructions, }