mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
feat(renderer): useMenuAction hook (native menu IPC -> command store)
- bridge between main-process menu events and the command dispatcher - optional transform: convert raw IPC payload to command args - extends ipc.ts with ipc.menu.on() helper - TDD: 5 unit tests covering subscription, payload transform, no-op, unmount
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { useEffect } from 'react';
|
||||
import { ipc } from '@/lib/ipc';
|
||||
import { useCommandStore } from '@/stores/command-store';
|
||||
import type { CommandId } from '@/stores/command-store';
|
||||
|
||||
type Transform<Args = unknown> = (...payload: unknown[]) => Args;
|
||||
|
||||
/**
|
||||
* Bridge a native-menu IPC channel to a command-store dispatch.
|
||||
*
|
||||
* When the main process sends `channel` (e.g. `'file-save'`), the optional
|
||||
* `transform` converts the payload to the command's args, then the command
|
||||
* `commandId` is dispatched through the command store.
|
||||
*
|
||||
* The hook cleans up its IPC subscription on unmount.
|
||||
*/
|
||||
export function useMenuAction<Args = unknown>(
|
||||
channel: string,
|
||||
commandId: CommandId,
|
||||
transform?: Transform<Args>
|
||||
): void {
|
||||
useEffect(() => {
|
||||
const unsubscribe = ipc.menu.on(channel, (...payload: unknown[]) => {
|
||||
const args = transform ? transform(...payload) : (payload[0] as Args);
|
||||
useCommandStore.getState().dispatch(commandId, args);
|
||||
});
|
||||
return unsubscribe;
|
||||
}, [channel, commandId, transform]);
|
||||
}
|
||||
Reference in New Issue
Block a user