Add listeners and preload for log/toast from main

This commit is contained in:
jeffvli 2024-02-12 14:02:59 -08:00
parent 69f82a9427
commit b69290f9f2
3 changed files with 55 additions and 1 deletions

View File

@ -109,6 +109,19 @@ export const getMainWindow = () => {
return mainWindow; return mainWindow;
}; };
export const sendToastToRenderer = ({
message,
type,
}: {
message: string;
type: 'success' | 'error' | 'warning' | 'info';
}) => {
getMainWindow()?.webContents.send('toast-from-main', {
message,
type,
});
};
const createWinThumbarButtons = () => { const createWinThumbarButtons = () => {
if (isWindows()) { if (isWindows()) {
getMainWindow()?.setThumbarButtons([ getMainWindow()?.setThumbarButtons([

View File

@ -18,12 +18,40 @@ const onRestoreQueue = (cb: (event: IpcRendererEvent, data: Partial<PlayerState>
ipcRenderer.on('renderer-restore-queue', cb); 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 = { export const utils = {
isLinux, isLinux,
isMacOS, isMacOS,
isWindows, isWindows,
logger,
mainMessageListener,
onRestoreQueue, onRestoreQueue,
onSaveQueue, onSaveQueue,
playerErrorListener,
restoreQueue, restoreQueue,
saveQueue, saveQueue,
}; };

View File

@ -1,10 +1,13 @@
import { useMemo } from 'react'; import { useMemo, useEffect } from 'react';
import isElectron from 'is-electron'; import isElectron from 'is-electron';
import { Navigate, Outlet } from 'react-router-dom'; import { Navigate, Outlet } from 'react-router-dom';
import { AppRoute } from '/@/renderer/router/routes'; import { AppRoute } from '/@/renderer/router/routes';
import { useCurrentServer } from '/@/renderer/store'; import { useCurrentServer } from '/@/renderer/store';
import { toast } from '/@/renderer/components';
const localSettings = isElectron() ? window.electron.localSettings : null; const localSettings = isElectron() ? window.electron.localSettings : null;
const ipc = isElectron() ? window.electron.ipc : null;
const utils = isElectron() ? window.electron.utils : null;
export const AppOutlet = () => { export const AppOutlet = () => {
const currentServer = useCurrentServer(); const currentServer = useCurrentServer();
@ -29,6 +32,16 @@ export const AppOutlet = () => {
return isActionRequired; return isActionRequired;
}, [currentServer]); }, [currentServer]);
useEffect(() => {
utils?.mainMessageListener((_event, data) => {
toast.show(data);
});
return () => {
ipc?.removeAllListeners('toast-from-main');
};
}, []);
if (isActionsRequired) { if (isActionsRequired) {
return ( return (
<Navigate <Navigate