import { useEffect } from "react"; import { events } from "@/bindings"; import { SWRConfiguration } from "swr"; type ExtractCallback = ( payload: Parameters[0]>[0]["payload"] ) => void; type SWRConfigTyp = { suspense: true } & SWRConfiguration; export const sharedSwrConfig: SWRConfigTyp = { suspense: true, dedupingInterval: 100 }; /** * Convenience hook that does useEffect for a Tauri event and handles unsubscribing on unmount */ export function useTauriEvent( tauriEvent: E, cb: ExtractCallback ) { useEffect(() => { const unlisten = events[tauriEvent].listen((e) => { cb(e.payload); }); return () => { unlisten.then((f) => f()); }; }, [tauriEvent, cb]); }