import React from "@moonlight-mod/wp/react"; import * as Components from "@moonlight-mod/wp/discord/components/common/index"; import { useStateFromStores, useStateFromStoresObject } from "@moonlight-mod/wp/discord/packages/flux"; import spacepack from "@moonlight-mod/wp/spacepack_spacepack"; import { MoonbaseSettingsStore } from "@moonlight-mod/wp/moonbase_stores"; import { RepositoryManifest, UpdateState } from "../types"; const { Button, TabBar } = Components; const TabBarClasses = spacepack.findByCode(/\.exports={tabBar:"tabBar_[a-z0-9]+",tabBarItem:"tabBarItem_[a-z0-9]+"}/)[0] .exports; const logger = moonlight.getLogger("moonbase/crashScreen"); type ErrorState = { error: Error; info: { componentStack: string; }; __moonlight_update?: UpdateState; }; type WrapperProps = { action: React.ReactNode; state: ErrorState; }; type UpdateCardProps = { id: number; ext: { version: string; download: string; updateManifest: RepositoryManifest; }; }; const updateStrings: Record = { [UpdateState.Ready]: "A new version of moonlight is available.", [UpdateState.Working]: "Updating moonlight...", [UpdateState.Installed]: "Updated moonlight. Click Reload to apply changes.", [UpdateState.Failed]: "Failed to update moonlight. Please use the installer." }; const buttonStrings: Record = { [UpdateState.Ready]: "Update moonlight", [UpdateState.Working]: "Updating moonlight...", [UpdateState.Installed]: "", [UpdateState.Failed]: "Update failed" }; const extensionButtonStrings: Record = { [UpdateState.Ready]: "Update", [UpdateState.Working]: "Updating...", [UpdateState.Installed]: "Updated", [UpdateState.Failed]: "Update failed" }; function ExtensionUpdateCard({ id, ext }: UpdateCardProps) { const [state, setState] = React.useState(UpdateState.Ready); const installed = useStateFromStores([MoonbaseSettingsStore], () => MoonbaseSettingsStore.getExtension(id), [id]); return (
{ext.updateManifest.meta?.name ?? ext.updateManifest.id}
{`v${installed?.manifest?.version ?? "???"} -> v${ ext.version }`}
); } export function wrapAction({ action, state }: WrapperProps) { const [tab, setTab] = React.useState("crash"); const { updates, updateCount } = useStateFromStoresObject([MoonbaseSettingsStore], () => { const { updates } = MoonbaseSettingsStore; return { updates: Object.entries(updates), updateCount: Object.keys(updates).length }; }); return (
{action} setTab(v)} > Crash Details {`Extension Updates (${updateCount})`} {tab === "crash" ? (
            
              {state.error.stack}
              {"\n\nComponent stack:"}
              {state.info.componentStack}
            
          
) : null} {tab === "extensions" ? (
{updates.map(([id, ext]) => ( ))}
) : null}
); } export function UpdateText({ state, setState }: { state: ErrorState; setState: (state: ErrorState) => void }) { if (!state.__moonlight_update) { setState({ ...state, __moonlight_update: UpdateState.Ready }); } const newVersion = useStateFromStores([MoonbaseSettingsStore], () => MoonbaseSettingsStore.newVersion); return newVersion == null ? null : (

{state.__moonlight_update !== undefined ? updateStrings[state.__moonlight_update] : ""}

); } export function UpdateButton({ state, setState }: { state: ErrorState; setState: (state: ErrorState) => void }) { const newVersion = useStateFromStores([MoonbaseSettingsStore], () => MoonbaseSettingsStore.newVersion); return newVersion == null || state.__moonlight_update === UpdateState.Installed || state.__moonlight_update === undefined ? null : ( ); }