import { useEffect, useRef, useState, type ReactNode } from 'react' import { createPortal } from 'react-dom' import { setApiBase } from '../api' import { Icon } from '../lib/icons' /** * What the desktop window shows while the backend starts, and the gate that * keeps the app from rendering before it can reach the backend. * * The shell (src-tauri/src/lib.rs) opens the window at once and starts the * sidecar beside it; this asks `backend_status` and listens for * `konduktor://backend`. A normal launch is about half a second. The first * launch after an install or update can take much longer — macOS checks every * native library in the sidecar once — so after `SLOW_MS` a note says why, * and after `STUCK_MS` the user may quit or keep waiting. If the sidecar * exits, its last output is shown with Try again (`backend_restart`). * * Until the backend is ready, nothing else is mounted: App, QuitGuard and * UpdateCheck would all query an API with no address yet. So this answers * the shell's quit request itself (there is nothing unsaved to ask about), * and stops answering once ready, when QuitGuard takes over. * * In a plain browser (dev) there is no shell and Vite proxies /api, so the * children render at once. */ type BackendStatus = | { state: 'starting' } | { state: 'ready'; url: string } | { state: 'stopped'; code: number | null; signal: number | null; log: string[] } /** When the "takes longer" note appears. */ const SLOW_MS = 6_000 /** When the user is offered Quit / Keep waiting (and again after each wait). */ const STUCK_MS = 90_000 /** The cross-fade into the app. */ const FADE_MS = 450 const IN_SHELL = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window export function StartupGate({ children }: { children: ReactNode }) { const [status, setStatus] = useState(null) const [ready, setReady] = useState(!IN_SHELL) const [leaving, setLeaving] = useState(false) const [gone, setGone] = useState(!IN_SHELL) // The backend's status: asked once, then followed. An event that arrives // before the answer is newer than it, so the answer is then dropped. useEffect(() => { if (!IN_SHELL) return let unlisten: (() => void) | undefined let cancelled = false void (async () => { const [{ listen }, { invoke }] = await Promise.all([ import('@tauri-apps/api/event'), import('@tauri-apps/api/core'), ]) let heard = false const off = await listen('konduktor://backend', (e) => { heard = true setStatus(e.payload) }) if (cancelled) return off() unlisten = off const now = await invoke('backend_status') if (!heard && !cancelled) setStatus(now) })() return () => { cancelled = true unlisten?.() } }, []) // Ready: point the API at the sidecar, mount the app, fade this out over it. // Once ready it stays ready — a later crash is not a startup problem. useEffect(() => { if (status?.state !== 'ready' || ready) return setApiBase(status.url) setReady(true) setLeaving(true) const t = window.setTimeout(() => setGone(true), FADE_MS) return () => window.clearTimeout(t) }, [status, ready]) // Before the app is mounted, a quit request is answered here: nothing can // be unsaved yet. Stops at ready, so it never races QuitGuard. useEffect(() => { if (!IN_SHELL || ready) return let unlisten: (() => void) | undefined let cancelled = false void (async () => { const [{ listen }, { invoke }] = await Promise.all([ import('@tauri-apps/api/event'), import('@tauri-apps/api/core'), ]) const off = await listen('konduktor://quit-requested', async () => { await invoke('quit_ack') await invoke('quit_now') }) if (cancelled) off() else unlisten = off })() return () => { cancelled = true unlisten?.() } }, [ready]) return ( <> {ready && children} {!gone && } ) } async function shell(command: 'backend_restart' | 'quit_now') { const { invoke } = await import('@tauri-apps/api/core') await invoke(command) } function StartupScreen({ status, leaving }: { status: BackendStatus | null; leaving: boolean }) { const stopped = status?.state === 'stopped' ? status : null return createPortal(
{stopped ? : }
v{__APP_VERSION__}
, document.body, ) } /** The OS doing the checking, for the slow-launch note. */ function osName(): string { const ua = navigator.userAgent if (ua.includes('Windows')) return 'Windows' if (ua.includes('Mac')) return 'macOS' return 'your system' } function Starting() { // Timed from mount: Try again replaces the failure card with a fresh one, // so each attempt gets its own clock. const [startedAt] = useState(() => Date.now()) const [stuckAt, setStuckAt] = useState(STUCK_MS) const [elapsed, setElapsed] = useState(0) useEffect(() => { const t = window.setInterval(() => setElapsed(Date.now() - startedAt), 250) return () => window.clearInterval(t) }, [startedAt]) const slow = elapsed >= SLOW_MS const stuck = elapsed >= stuckAt return (
Konduktor
{stuck ? 'Still starting…' : 'Starting…'}

The first launch after installing or updating takes longer while {osName()} checks the app. Later launches start in a second or two.

) } function StoppedCard({ status }: { status: Extract }) { const [busy, setBusy] = useState(false) const [copied, setCopied] = useState(false) const logRef = useRef(null) const summary = status.code !== null ? `The library service exited with code ${status.code}.` : status.signal !== null ? `The library service was stopped by signal ${status.signal}.` : null const lines = status.log.length ? status.log : ['It wrote nothing before stopping.'] const details = [`Konduktor v${__APP_VERSION__}`, ...(summary ? [summary] : []), ...lines].join('\n') // Show the end of the output: that is where the error is. useEffect(() => { const el = logRef.current if (el) el.scrollTop = el.scrollHeight }, [status]) const copy = async () => { try { await navigator.clipboard.writeText(details) setCopied(true) window.setTimeout(() => setCopied(false), 2000) } catch { // No clipboard access: select the text so Cmd/Ctrl+C works. const el = logRef.current if (!el) return const range = document.createRange() range.selectNodeContents(el) window.getSelection()?.removeAllRanges() window.getSelection()?.addRange(range) } } const retry = async () => { setBusy(true) try { await shell('backend_restart') } finally { setBusy(false) } } return (

Konduktor couldn't start

Its library service stopped before it was ready. Nothing in your libraries was changed. Try again, and if it keeps happening, send the details below with your report.

        {summary && 
{summary}
} {lines.map((line, i) => (
{line || ' '}
))}
) } // ---- The mark: the app icon, with a track scrolling under a fixed playhead ---- const BEAT = 60 / 128 /** CSS px per second of audio, at the mark's 104 px. */ const SPEED = 46 /** 2 px bars with a 1 px gap: the main waveform's rhythm. */ const BAR = 2 const SLOT = 3 /** The icon's own four bands, back to front. */ const BANDS = ['#ff6a3d', '#8f6bff', '#3f9dff', '#7fe6ff'] const jitter = (i: number) => { const x = Math.sin(i * 12.9898) * 43758.5453 return x - Math.floor(x) } /** Band levels (0..1) of a generated house track at time t: a kick on every * beat, offbeat bass and hats, a clap on 2 and 4, a fill every 16 beats. */ function levels(t: number) { const n = Math.floor(t / BEAT) const pos = t - n * BEAT const in16 = n % 16 const fill = in16 >= 14 const sixteenth = BEAT / 4 const p16 = t % sixteenth const kick = fill ? 0 : Math.exp(-pos / 0.11) const bass = pos > BEAT / 2 ? 0.5 * Math.exp(-(pos - BEAT / 2) / 0.16) : 0 const roll = fill ? 0.35 + (0.5 * ((in16 - 14) * 4 + Math.floor(pos / sixteenth))) / 8 : 0 const clap = (n % 2 === 1 ? 0.8 : 0) * Math.exp(-pos / 0.07) + roll * Math.exp(-p16 / 0.05) const pad = 0.22 + 0.08 * Math.sin(t * 0.9) const hat = 0.85 * Math.exp(-Math.abs(pos - BEAT / 2) / 0.025) + 0.3 * Math.exp(-p16 / 0.02) return { low: Math.max(kick, bass), mid: Math.max(clap, pad, kick * 0.5), high: Math.max(hat, clap * 0.6) } } function drawWave(ctx: CanvasRenderingContext2D, w: number, h: number, scroll: number) { ctx.clearRect(0, 0, w, h) const mid = h / 2 const half = h * 0.34 const centre = w / 2 const first = Math.floor(scroll / SLOT) - 1 const last = first + Math.ceil(w / SLOT) + 2 for (let i = first; i <= last; i++) { const x = i * SLOT - scroll const t = (i * SLOT) / SPEED // The peak over the slot, so a kick never falls between two samples. const a = levels(t) const b = levels(t + SLOT / SPEED / 2) const low = Math.max(a.low, b.low) const md = Math.max(a.mid, b.mid) const hi = Math.max(a.high, b.high) const j = 0.9 + 0.2 * jitter(i) const h0 = half * Math.min(1, (0.16 + 0.84 * Math.max(low, md * 0.8, hi * 0.55)) * j) const h1 = Math.min(h0 * 0.86, half * (0.1 + 0.6 * md) * j) const h2 = Math.min(h1 * 0.84, half * (0.07 + 0.5 * hi) * j) const h3 = Math.min(h2 * 0.78, half * (0.04 + 0.32 * hi) * j) // Played audio, left of the playhead, sits dimmer, as on a deck. ctx.globalAlpha = x + BAR / 2 < centre ? 0.6 : 1 ;[h0, h1, h2, h3].forEach((bh, k) => { ctx.fillStyle = BANDS[k] ctx.fillRect(x, mid - bh, BAR, bh * 2) }) } ctx.globalAlpha = 1 // Bars enter and leave softly at the icon's edges. ctx.globalCompositeOperation = 'destination-in' const g = ctx.createLinearGradient(0, 0, w, 0) g.addColorStop(0, 'rgba(0,0,0,0)') g.addColorStop(0.14, '#000') g.addColorStop(0.86, '#000') g.addColorStop(1, 'rgba(0,0,0,0)') ctx.fillStyle = g ctx.fillRect(0, 0, w, h) ctx.globalCompositeOperation = 'source-over' } function StartupMark() { const canvas = useRef(null) useEffect(() => { const el = canvas.current const ctx = el?.getContext('2d') if (!el || !ctx) return const w = el.offsetWidth const h = el.offsetHeight const dpr = window.devicePixelRatio || 1 el.width = Math.round(w * dpr) el.height = Math.round(h * dpr) ctx.setTransform(dpr, 0, 0, dpr, 0, 0) // Reduced motion: one still frame. if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) { drawWave(ctx, w, h, 0) return } let raf = 0 let start: number | null = null const frame = (now: number) => { start ??= now drawWave(ctx, w, h, ((now - start) / 1000) * SPEED) raf = requestAnimationFrame(frame) } raf = requestAnimationFrame(frame) return () => cancelAnimationFrame(raf) }, []) return (
{/* Clipped to the icon's rounded square: rx 22 inside a 2-unit inset. */}
) }