UNPKG

backsplash-app

Version:
43 lines (34 loc) 1.11 kB
import { useState, useEffect } from "react"; export function useAutoLaunch() { const [isEnabled, setIsEnabled] = useState(false); // Load initial state useEffect(() => { async function loadAutoLaunchState() { const enabled = await window.electron.ipcRenderer.invoke("auto-launch:get"); setIsEnabled(Boolean(enabled)); } loadAutoLaunchState(); }, []); // Listen for changes from the main process useEffect(() => { const handleAutoLaunchUpdate = (_event: any, enabled: boolean) => { setIsEnabled(Boolean(enabled)); }; window.electron.ipcRenderer.on("autoLaunch:updated", handleAutoLaunchUpdate); return () => { window.electron.ipcRenderer.removeListener("autoLaunch:updated", handleAutoLaunchUpdate); }; }, []); const setAutoLaunch = async (enabled: boolean) => { try { await window.electron.ipcRenderer.invoke("auto-launch:set", enabled); setIsEnabled(enabled); } catch (error) { console.error("Failed to update auto-launch setting:", error); } }; return { isEnabled, setAutoLaunch, }; }