a6cbcc273e
Made-with: Cursor
32 lines
844 B
TypeScript
32 lines
844 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import { ipcChannels } from '../../../shared/ipc/contracts';
|
|
import type { EffectsEvent, EffectsState } from '../../../shared/types';
|
|
import { getDndApi } from '../dndApi';
|
|
|
|
export function useEffectsState(): readonly [
|
|
EffectsState | null,
|
|
{ dispatch: (event: EffectsEvent) => Promise<void> },
|
|
] {
|
|
const api = getDndApi();
|
|
const [state, setState] = useState<EffectsState | null>(null);
|
|
|
|
useEffect(() => {
|
|
void api.invoke(ipcChannels.effects.getState, {}).then((r) => {
|
|
setState(r.state);
|
|
});
|
|
return api.on(ipcChannels.effects.stateChanged, ({ state: next }) => {
|
|
setState(next);
|
|
});
|
|
}, [api]);
|
|
|
|
return [
|
|
state,
|
|
{
|
|
dispatch: async (event) => {
|
|
await api.invoke(ipcChannels.effects.dispatch, { event });
|
|
},
|
|
},
|
|
] as const;
|
|
}
|