import { Tabs } from "@decky/ui"; import { useEffect, useRef, useState, type FocusEvent, type ReactNode } from "react"; import { FaCube, FaFileAlt, FaGamepad, FaList, FaTools } from "react-icons/fa"; import { ConfigurationData } from "../config/configSchema"; import { useFlatpakConfiguration } from "../hooks/useFlatpakConfiguration"; import { useGameConfiguration } from "../hooks/useGameConfiguration"; import { useInstallation } from "../hooks/useLsfgHooks"; import { tabStyles } from "../styles"; import { resolveNowPlayingTarget } from "../utils/nowPlaying"; import { ConfigFileTab } from "./ConfigFileTab"; import { ConfigurationTab } from "./ConfigurationTab"; import { FlatpakNowPlayingTab } from "./FlatpakNowPlayingTab"; import { FlatpakTab } from "./FlatpakTab"; import { NowPlayingTab } from "./NowPlayingTab"; import { SetupTab } from "./SetupTab"; const tabIcons = { nowPlaying: , games: , flatpak: , configFile: , setup: , }; const DEBUG_TAB_VISIBILITY_KEY = "lsfg-debug-tab-visible-v1"; function usePersistentBoolean(key: string, defaultValue: boolean) { const [value, setValue] = useState(() => { try { const stored = localStorage.getItem(key); return stored === null ? defaultValue : stored === "true"; } catch { return defaultValue; } }); useEffect(() => { try { localStorage.setItem(key, String(value)); } catch {} }, [key, value]); return [value, setValue] as const; } export function Content() { const { config, runningConfig, targets, runningGame, setSelectedAppId, save, saveFor, enable, enableAll, repair, resetSelected, resetAll, reload, } = useGameConfiguration(); const { isInstalled, installationStatus, losslessScalingInstalled, losslessScalingStatus, steamBranchStatus, isInstalling, isUninstalling, install, uninstall, } = useInstallation(reload); const setupComplete = isInstalled && losslessScalingInstalled && steamBranchStatus?.success === true && steamBranchStatus.installed && !steamBranchStatus.needs_switch; const flatpak = useFlatpakConfiguration(setupComplete); const [tab, setTab] = useState("Setup"); const [showDebugTab, setShowDebugTab] = usePersistentBoolean(DEBUG_TAB_VISIBILITY_KEY, true); const [contentFocused, setContentFocused] = useState(false); const previousRunningWorkload = useRef(null); const runningFlatpak = flatpak.runningApp; const nowPlayingTarget = resolveNowPlayingTarget(runningGame, runningFlatpak); const hasNowPlaying = Boolean(nowPlayingTarget); const runningWorkload = nowPlayingTarget ? nowPlayingTarget.kind === "flatpak" ? `flatpak:${nowPlayingTarget.app.app_id}:${nowPlayingTarget.launcher?.appid ?? ""}` : `steam:${nowPlayingTarget.game.appid}` : null; useEffect(() => { if (!setupComplete) { setTab("Setup"); return; } setTab((current) => current === "Setup" ? (hasNowPlaying ? "NowPlaying" : "Games") : current); }, [hasNowPlaying, setupComplete]); useEffect(() => { if (!setupComplete) return; const previous = previousRunningWorkload.current; previousRunningWorkload.current = runningWorkload; if (runningWorkload && runningWorkload !== previous) setTab("NowPlaying"); else if (!runningWorkload && previous) { setTab((current) => current === "NowPlaying" ? "Games" : current); } }, [runningWorkload, setupComplete]); useEffect(() => { if (isInstalled) { void reload(); void flatpak.reload(); } }, [isInstalled, reload, flatpak.reload]); useEffect(() => { if (!showDebugTab && tab === "ConfigFile") setTab("Games"); }, [showDebugTab, tab]); const handleConfigChange = async ( fieldName: keyof ConfigurationData, value: boolean | number | string | string[], cleanupLaunchOptions = false, ) => { await save({ ...config, [fieldName]: value }, cleanupLaunchOptions); }; const setup = ( void install()} onUninstall={() => void uninstall()} /> ); const tabContent = (content: ReactNode) => ( {content} ); const nowPlaying = nowPlayingTarget?.kind === "steam" ? ( { await saveFor(nowPlayingTarget.game.appid, { ...runningConfig, [field]: value }, true); }} /> ) : nowPlayingTarget?.kind === "flatpak" ? ( ) : null; const tabs = setupComplete ? [ ...(nowPlaying ? [{ id: "NowPlaying", title: tabIcons.nowPlaying, content: tabContent(nowPlaying) }] : []), { id: "Games", title: tabIcons.games, content: tabContent( handleConfigChange(field, value, true)} onEnable={enable} onEnableAll={enableAll} onRepair={repair} onReset={resetSelected} onResetAll={resetAll} />, ), }, { id: "Flatpak", title: tabIcons.flatpak, content: tabContent( , ), }, ...(showDebugTab ? [{ id: "ConfigFile", title: tabIcons.configFile, content: tabContent() }] : []), { id: "Setup", title: tabIcons.setup, content: tabContent(setup) }, ] : [{ id: "Setup", title: tabIcons.setup, content: tabContent(setup) }]; const availableTabIds = new Set(tabs.map(({ id }) => id)); const activeTab = availableTabIds.has(tab) ? tab : setupComplete ? "Games" : "Setup"; const handleFocusCapture = (event: FocusEvent) => { const focusedElement = event.target as HTMLElement | null; setContentFocused(!focusedElement?.closest?.('[role="tab"]')); }; return ( { if (availableTabIds.has(nextTab)) setTab(nextTab); }} tabs={tabs} /> ); }