diff --git a/src/main/main.ts b/src/main/main.ts index fd124e47..e02e430c 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -109,6 +109,19 @@ export const getMainWindow = () => { return mainWindow; }; +export const sendToastToRenderer = ({ + message, + type, +}: { + message: string; + type: 'success' | 'error' | 'warning' | 'info'; +}) => { + getMainWindow()?.webContents.send('toast-from-main', { + message, + type, + }); +}; + const createWinThumbarButtons = () => { if (isWindows()) { getMainWindow()?.setThumbarButtons([ diff --git a/src/main/preload/utils.ts b/src/main/preload/utils.ts index 83b56653..12f83ba7 100644 --- a/src/main/preload/utils.ts +++ b/src/main/preload/utils.ts @@ -18,12 +18,40 @@ const onRestoreQueue = (cb: (event: IpcRendererEvent, data: Partial ipcRenderer.on('renderer-restore-queue', cb); }; +const playerErrorListener = (cb: (event: IpcRendererEvent, data: { code: number }) => void) => { + ipcRenderer.on('player-error-listener', cb); +}; + +const mainMessageListener = ( + cb: ( + event: IpcRendererEvent, + data: { message: string; type: 'success' | 'error' | 'warning' | 'info' }, + ) => void, +) => { + ipcRenderer.on('toast-from-main', cb); +}; + +const logger = ( + cb: ( + event: IpcRendererEvent, + data: { + message: string; + type: 'debug' | 'verbose' | 'error' | 'warning' | 'info'; + }, + ) => void, +) => { + ipcRenderer.send('logger', cb); +}; + export const utils = { isLinux, isMacOS, isWindows, + logger, + mainMessageListener, onRestoreQueue, onSaveQueue, + playerErrorListener, restoreQueue, saveQueue, }; diff --git a/src/renderer/router/app-outlet.tsx b/src/renderer/router/app-outlet.tsx index 5b59f4e5..c62fb913 100644 --- a/src/renderer/router/app-outlet.tsx +++ b/src/renderer/router/app-outlet.tsx @@ -1,10 +1,13 @@ -import { useMemo } from 'react'; +import { useMemo, useEffect } from 'react'; import isElectron from 'is-electron'; import { Navigate, Outlet } from 'react-router-dom'; import { AppRoute } from '/@/renderer/router/routes'; import { useCurrentServer } from '/@/renderer/store'; +import { toast } from '/@/renderer/components'; const localSettings = isElectron() ? window.electron.localSettings : null; +const ipc = isElectron() ? window.electron.ipc : null; +const utils = isElectron() ? window.electron.utils : null; export const AppOutlet = () => { const currentServer = useCurrentServer(); @@ -29,6 +32,16 @@ export const AppOutlet = () => { return isActionRequired; }, [currentServer]); + useEffect(() => { + utils?.mainMessageListener((_event, data) => { + toast.show(data); + }); + + return () => { + ipc?.removeAllListeners('toast-from-main'); + }; + }, []); + if (isActionsRequired) { return (