175 lines
8.3 KiB
TypeScript
175 lines
8.3 KiB
TypeScript
import { useEffect, useState, type ReactNode } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import type { IpcOutput } from '@shared/ipc/contract.ts';
|
|
import type { Copies } from '@shared/schemas/install.ts';
|
|
import type { LaunchState } from '@shared/schemas/launch.ts';
|
|
import type { Settings } from '@shared/schemas/settings.ts';
|
|
import { Sheet } from './components/Sheet.tsx';
|
|
import { applyLanguage, resolveLanguage } from './i18n/index.ts';
|
|
import { ipcInvoke, ipcOn } from './ipc.ts';
|
|
import { AboutPage } from './pages/about/AboutPage.tsx';
|
|
import { CopyMaintenance } from './pages/copies/CopyMaintenance.tsx';
|
|
import { CopyList } from './pages/copies/CopyList.tsx';
|
|
import { CopySummary } from './pages/copies/CopySummary.tsx';
|
|
import { FeatureStatus } from './pages/copies/FeatureStatus.tsx';
|
|
import { BackgroundSwitch } from './pages/home/BackgroundSwitch.tsx';
|
|
import { HomeMain } from './pages/home/HomeMain.tsx';
|
|
import { HomeSide } from './pages/home/HomeSide.tsx';
|
|
import { homeBar } from './pages/home/home-bar.ts';
|
|
import { useInstallWizard } from './pages/install/useInstallWizard.ts';
|
|
import { WizardMain } from './pages/install/WizardMain.tsx';
|
|
import { WizardSide } from './pages/install/WizardSide.tsx';
|
|
import { wizardBar } from './pages/install/wizard-bar.ts';
|
|
import { DeveloperSettings } from './pages/settings/DeveloperSettings.tsx';
|
|
import { DiagnosticsSettings } from './pages/settings/DiagnosticsSettings.tsx';
|
|
import { GeneralSettings } from './pages/settings/GeneralSettings.tsx';
|
|
import { LaunchSettings } from './pages/settings/LaunchSettings.tsx';
|
|
import { Background } from './shell/Background.tsx';
|
|
import { BottomBar } from './shell/BottomBar.tsx';
|
|
import type { BarModel } from './shell/bar-model.ts';
|
|
import { Footer } from './shell/Footer.tsx';
|
|
import { COPY_SECTIONS, navigate, SETTINGS_SECTIONS, START_ROUTE, type Destination, type Route } from './shell/navigation.ts';
|
|
import { SubNav } from './shell/SubNav.tsx';
|
|
import { TitleBar } from './shell/TitleBar.tsx';
|
|
import { useBackgrounds } from './shell/useBackgrounds.ts';
|
|
import { useErrorMessage } from './useErrorMessage.ts';
|
|
|
|
interface AppProps {
|
|
readonly info: IpcOutput<'app:getInfo'>;
|
|
readonly initialSettings: Settings;
|
|
readonly initialCopies: Copies;
|
|
}
|
|
|
|
interface View {
|
|
readonly side: ReactNode;
|
|
readonly main: ReactNode;
|
|
readonly bar: BarModel;
|
|
}
|
|
|
|
/**
|
|
* The window: title bar, side panel, main area, bottom bar, footer. Each page fills the side
|
|
* panel and the main area; the bottom bar belongs to the current task (the install wizard while
|
|
* it runs, otherwise install / launch).
|
|
*/
|
|
export function App({ info, initialSettings, initialCopies }: AppProps) {
|
|
const { t } = useTranslation();
|
|
const { message, report } = useErrorMessage();
|
|
const [settings, setSettings] = useState(initialSettings);
|
|
const [registry, setRegistry] = useState(initialCopies);
|
|
const record = registry.copies.find((copy) => copy.id === registry.current) ?? null;
|
|
const [launch, setLaunch] = useState<LaunchState>({ phase: 'ready' });
|
|
const [route, setRoute] = useState<Route>(START_ROUTE);
|
|
const supported = info.supportedGameVersions;
|
|
|
|
const wizard = useInstallWizard({ onInstalled: () => setRoute(START_ROUTE) });
|
|
|
|
const backgrounds = useBackgrounds(record?.copyPath ?? null, settings, report);
|
|
|
|
useEffect(() => ipcOn('event:settings-changed', setSettings), []);
|
|
useEffect(() => ipcOn('event:copies-changed', setRegistry), []);
|
|
useEffect(() => applyLanguage(resolveLanguage(settings.language, info.systemLocale)), [settings.language, info.systemLocale]);
|
|
|
|
useEffect(() => {
|
|
let mounted = true;
|
|
let receivedEvent = false;
|
|
const dispose = ipcOn('event:launch-state', (state) => {
|
|
receivedEvent = true;
|
|
setLaunch(state);
|
|
});
|
|
ipcInvoke('launch:getState').then((state) => {
|
|
if (mounted && !receivedEvent) setLaunch(state);
|
|
}).catch(report);
|
|
return () => { mounted = false; dispose(); };
|
|
}, [report]);
|
|
|
|
const go = (to: Destination): void => setRoute((current) => navigate(current, to));
|
|
const selectCopy = (id: string, details = false): void => {
|
|
ipcInvoke('copies:setCurrent', { id }).then(() => {
|
|
if (details) go({ page: 'copies', section: 'summary' });
|
|
}).catch(report);
|
|
};
|
|
const perCopyStatus = ['version-changed', 'failed', 'port-busy', 'server-exited'].includes(launch.phase);
|
|
const launchStatus: LaunchState = perCopyStatus && launch.copyId && launch.copyId !== record?.id ? { phase: 'ready' } : launch;
|
|
const home = homeBar(record, launchStatus, t, wizard.start, {
|
|
start: () => { ipcInvoke('launch:start').catch(report); },
|
|
stop: () => { ipcInvoke('launch:stop').catch(report); },
|
|
checkVersion: () => go({ page: 'copies', section: 'features' }),
|
|
});
|
|
|
|
const view = ((): View => {
|
|
if (wizard.active) {
|
|
return { side: <WizardSide step={wizard.step} />, main: <WizardMain wizard={wizard} supportedVersions={supported} />, bar: wizardBar(wizard, t) };
|
|
}
|
|
const homeSide = <HomeSide record={record} copies={registry.copies} supportedVersions={supported} onNavigate={go} onSelectCopy={selectCopy} />;
|
|
switch (route.page) {
|
|
case 'home':
|
|
return {
|
|
side: homeSide,
|
|
main: (
|
|
<>
|
|
<BackgroundSwitch pictures={backgrounds.pictures} shown={backgrounds.shown} onError={report} />
|
|
<HomeMain record={record} />
|
|
</>
|
|
),
|
|
bar: home,
|
|
};
|
|
case 'about':
|
|
return { side: homeSide, main: <AboutPage launcherVersion={info.version} supportedVersions={supported} />, bar: home };
|
|
case 'settings': {
|
|
const sections = SETTINGS_SECTIONS.filter((section) => info.development || section !== 'dev');
|
|
const content = {
|
|
general: <GeneralSettings settings={settings} pictures={backgrounds.pictures} onError={report} />,
|
|
launch: <LaunchSettings onError={report} />,
|
|
diag: <DiagnosticsSettings onError={report} serverRunning={launch.phase === 'running'} />,
|
|
dev: <DeveloperSettings settings={settings} onError={report} />,
|
|
};
|
|
return {
|
|
side: <SubNav sections={sections} active={route.settings} label={(section) => t(`settings.sections.${section}`)} onSelect={(section) => go({ page: 'settings', section })} />,
|
|
main: content[route.settings],
|
|
bar: home,
|
|
};
|
|
}
|
|
case 'copies': {
|
|
const side = <SubNav sections={COPY_SECTIONS} active={route.copies} label={(section) => t(`copies.sections.${section}`)} onSelect={(section) => go({ page: 'copies', section })} />;
|
|
if (route.copies === 'list') {
|
|
return { side, main: <CopyList registry={registry} supportedVersions={supported} onSelect={selectCopy} onAdd={wizard.start} />, bar: home };
|
|
}
|
|
if (!record) {
|
|
return {
|
|
side,
|
|
main: (
|
|
<Sheet title={t(`copies.sections.${route.copies}`)}>
|
|
<p className="lead">{t('copies.none')}</p>
|
|
<button type="button" className="button button-primary" onClick={wizard.start}>{t('bar.install')}</button>
|
|
</Sheet>
|
|
),
|
|
bar: home,
|
|
};
|
|
}
|
|
if (route.copies === 'maintain') {
|
|
return { side, main: <CopyMaintenance key={record.id} record={record} onError={report} />, bar: home };
|
|
}
|
|
const main = route.copies === 'features'
|
|
? <FeatureStatus record={record} supportedVersions={supported} onError={report} />
|
|
: <CopySummary record={record} supportedVersions={supported} onShowFeatures={() => go({ page: 'copies', section: 'features' })} onError={report} />;
|
|
return { side, main, bar: home };
|
|
}
|
|
}
|
|
})();
|
|
|
|
const paged = wizard.active || route.page !== 'home';
|
|
return (
|
|
<div className={paged ? 'shell shell-paged' : 'shell'}>
|
|
<Background artUrl={backgrounds.shown?.url ?? null} />
|
|
<TitleBar page={route.page} navigationEnabled={!wizard.active} language={settings.language} onNavigate={(page) => go({ page })} onError={report} />
|
|
<aside className="side">{view.side}</aside>
|
|
<main className="main">
|
|
{view.main}
|
|
{message && <p className="app-error" role="alert">{message}</p>}
|
|
</main>
|
|
<BottomBar model={view.bar} />
|
|
<Footer launcherVersion={info.version} gameVersion={record?.gameVersion ?? null} copyName={record?.name ?? null} />
|
|
</div>
|
|
);
|
|
}
|