refactor(app): Phase B.1 — extract pre-React bootstrap into src/app/ (#555)

main.tsx shrinks from 56 -> 17 LOC. New module surface:

  - src/app/windowKind.ts: cached getWindowKind() detector,
    replaces the global __PSY_WINDOW_LABEL__ string everywhere
  - src/app/bootstrap.ts: pushUserAgentToBackend +
    pushLoggingModeToBackend + runPreReactBootstrap orchestrator

App.tsx + playerStore.ts now read getWindowKind() instead of poking
window.__PSY_WINDOW_LABEL__ directly. Behaviour-preserving.
This commit is contained in:
Frank Stellmacher
2026-05-12 01:39:39 +02:00
committed by GitHub
parent d3a8160b37
commit 0cd8998dc9
8 changed files with 298 additions and 46 deletions
+45
View File
@@ -0,0 +1,45 @@
import { invoke } from '@tauri-apps/api/core';
import { installQueueUndoHotkey } from '../store/playerStore';
import { getWindowKind } from './windowKind';
/** Sync backend HTTP User-Agent from the main webview once at startup. */
export function pushUserAgentToBackend(): void {
try {
if (getWindowKind() !== 'main') return;
const ua = window.navigator.userAgent?.trim();
if (ua) {
void invoke('set_subsonic_wire_user_agent', { userAgent: ua, windowLabel: 'main' });
}
} catch {
// Ignore in non-Tauri runtimes.
}
}
/**
* Push the persisted logging mode to Rust before React mounts. Zustand rehydrate
* runs after first paint; AppShell's useEffect can miss the user's persisted
* `loggingMode` until then — but waveform/audio may already run. Matches the
* `psysonic-auth` localStorage key.
*/
export function pushLoggingModeToBackend(): void {
try {
const raw = localStorage.getItem('psysonic-auth');
if (!raw) return;
const parsed = JSON.parse(raw) as { state?: { loggingMode?: string } };
const mode = parsed.state?.loggingMode;
if (mode === 'off' || mode === 'normal' || mode === 'debug') {
void invoke('set_logging_mode', { mode });
}
} catch {
// Ignore parse / non-Tauri.
}
}
/** Orchestrates everything that must run before React mounts. */
export function runPreReactBootstrap(): void {
// Pre-warm the window-kind cache so subsequent reads are sync + safe.
getWindowKind();
pushUserAgentToBackend();
pushLoggingModeToBackend();
installQueueUndoHotkey();
}