import { useEffect, useState } from "react"; import { invoke } from "@tauri-apps/api/core"; import { getCurrentWindow, LogicalSize } from "@tauri-apps/api/window"; import { WebviewWindow } from "@tauri-apps/api/webviewWindow"; import { useStore } from "./store"; import TitleBar from "./components/TitleBar"; import ResizeHandles from "./components/ResizeHandles"; import HomeView from "./components/HomeView"; import GuideView from "./components/GuideView"; import SettingsPanel from "./components/SettingsPanel"; import ProfileModal from "./components/ProfileModal"; import SyncOverlay from "./components/SyncOverlay"; export default function App() { const { loadProfiles, loadGuides, openGuide, setResourcesPanelCollapsed, view, syncing, syncGuides } = useStore(); const [showSettings, setShowSettings] = useState(false); const [needsProfile, setNeedsProfile] = useState(false); const [needsSync, setNeedsSync] = useState(false); async function runPhase2() { const has = await invoke("has_guides"); if (!has) { setNeedsSync(true); } else { await loadGuides(); const lastGuide = await invoke("get_setting", { key: "active_guide" }); if (lastGuide) { try { await openGuide(lastGuide); setResourcesPanelCollapsed(true); } catch { /* guide may no longer exist */ } } } } useEffect(() => { async function init() { const [savedW, savedH] = await Promise.all([ invoke("get_setting", { key: "window_width" }), invoke("get_setting", { key: "window_height" }), ]); if (savedW && savedH) { await getCurrentWindow().setSize(new LogicalSize(parseInt(savedW), parseInt(savedH))); } await loadProfiles(); const profiles = useStore.getState().profiles; if (profiles.length === 0) { setNeedsProfile(true); return; } await runPhase2(); } init(); // Persist window size on resize (debounced) const win = getCurrentWindow(); let debounce: ReturnType | null = null; const unlisten = win.onResized(async () => { if (debounce !== null) clearTimeout(debounce); debounce = setTimeout(async () => { const size = await win.innerSize(); const factor = await win.scaleFactor(); const w = Math.round(size.width / factor); const h = Math.round(size.height / factor); await invoke("set_setting", { key: "window_width", value: w.toString() }); await invoke("set_setting", { key: "window_height", value: h.toString() }); }, 500); }); const unlistenFocus = win.listen("tauri://focus", async () => { const viewer = await WebviewWindow.getByLabel("image-viewer"); if (viewer) { const isMin = await viewer.isMinimized(); if (isMin) await viewer.unminimize(); } }); const unlistenBlur = win.listen("tauri://blur", async () => { const isMin = await win.isMinimized(); if (isMin) { const viewer = await WebviewWindow.getByLabel("image-viewer"); if (viewer) await viewer.minimize(); } }); return () => { unlisten.then(f => f()); unlistenFocus.then(f => f()); unlistenBlur.then(f => f()); }; }, []); async function handleProfileCreated() { setNeedsProfile(false); await runPhase2(); } async function handleInitialSync() { if (!needsSync) return; setNeedsSync(false); await syncGuides(); } return (
setShowSettings(s => !s)} />
{view === "home" ? : }
{showSettings && setShowSettings(false)} />} {syncing && !showSettings && } {needsProfile && }
); }