import type { CSSProperties } from "react"; import { useEffect, useState } from "react"; import { openWorldshaperStudioWindow } from "./worldshaperStudio/windowing"; import { CHANGELOG_SECTIONS, CHANGELOG_SPLASH_FOOTNOTE, CHANGELOG_SPLASH_KICKER, CHANGELOG_SPLASH_TITLE, CHANGELOG_SPLASH_VERSION, } from "./worldshaperStudio/changelogData"; import type { ChangelogItem } from "./worldshaperStudio/changelogData"; import launcherBackground from "../background.png"; type WorldDefaultPayload = { worldId?: string; world?: { id?: string; }; }; type LaunchState = "ready" | "opening" | "opened" | "blocked" | "error"; type BoardTab = "news" | "requests"; type LauncherRequest = { id: string; sourceSubmissionId?: string; title: string; status: "pending" | "active"; category: string; tags: string[]; sourceText: string; summary: string; implementationNotes: string; createdAt: string; updatedAt: string; }; type LauncherRequestsPayload = { requests?: LauncherRequest[]; }; const DEFAULT_EDITOR_WORLD_ID_FALLBACK = "overworld"; async function resolveDefaultWorldId(): Promise { const response = await fetch("/api/world-default"); if (!response.ok) { throw new Error(`Failed to load default world (${response.status}).`); } const payload = await response.json() as WorldDefaultPayload; const resolvedWorldId = String(payload.worldId || payload.world?.id || "").trim(); return resolvedWorldId || DEFAULT_EDITOR_WORLD_ID_FALLBACK; } async function fetchJsonOrThrow(input: RequestInfo | URL, init?: RequestInit): Promise { const response = await fetch(input, init); if (!response.ok) { let detail = `Request failed (${response.status}).`; try { const payload = await response.json() as { error?: string }; detail = String(payload?.error || detail); } catch { // Ignore JSON parse failures and fall back to status text. } throw new Error(detail); } return response.json() as Promise; } function formatRequestTimestamp(value: string): string { const parsed = Date.parse(String(value || "")); if (!Number.isFinite(parsed)) { return "Saved recently"; } return new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit", }).format(parsed); } function formatRequestStatusLabel(status: "pending" | "active"): string { return status === "active" ? "Active" : "Pending"; } function openStudioPopup(worldId: string): boolean { const popup = openWorldshaperStudioWindow(worldId, window, { worldId }); return Boolean(popup); } function openRepo(): void { window.location.assign("https://repo.andraxion.net/"); } function WorldshaperLauncher() { const [launchState, setLaunchState] = useState("ready"); const [status, setStatus] = useState("Launch Worldshaper Studio in its floating window."); const [error, setError] = useState(""); const [worldId, setWorldId] = useState(DEFAULT_EDITOR_WORLD_ID_FALLBACK); const [activeBoardTab, setActiveBoardTab] = useState("news"); const [requests, setRequests] = useState([]); const [requestsLoading, setRequestsLoading] = useState(true); const [requestsError, setRequestsError] = useState(""); const [requestDraftOpen, setRequestDraftOpen] = useState(false); const [requestDraft, setRequestDraft] = useState(""); const [requestSubmitting, setRequestSubmitting] = useState(false); const [requestMutatingId, setRequestMutatingId] = useState(""); const [requestFilter, setRequestFilter] = useState("all"); const [expandedRequestIds, setExpandedRequestIds] = useState([]); useEffect(() => { let cancelled = false; void resolveDefaultWorldId() .then((resolvedWorldId) => { if (cancelled) { return; } setWorldId(resolvedWorldId); }) .catch(() => { if (cancelled) { return; } setWorldId(DEFAULT_EDITOR_WORLD_ID_FALLBACK); }); return () => { cancelled = true; }; }, []); useEffect(() => { let cancelled = false; async function loadRequests() { setRequestsLoading(true); try { const payload = await fetchJsonOrThrow("/api/launcher-requests"); if (cancelled) { return; } setRequests(Array.isArray(payload.requests) ? payload.requests : []); setRequestsError(""); } catch (nextError: unknown) { if (cancelled) { return; } setRequestsError(String(nextError || "Failed to load requests.")); } finally { if (!cancelled) { setRequestsLoading(false); } } } void loadRequests(); return () => { cancelled = true; }; }, []); async function handleLaunch(): Promise { setError(""); const nextWorldId = worldId || DEFAULT_EDITOR_WORLD_ID_FALLBACK; setLaunchState("opening"); setStatus(`Opening Worldshaper Studio for ${nextWorldId}...`); try { const resolvedWorldId = nextWorldId || await resolveDefaultWorldId().catch(() => DEFAULT_EDITOR_WORLD_ID_FALLBACK); setWorldId(resolvedWorldId); setStatus(`Opening Worldshaper Studio for ${resolvedWorldId}...`); if (openStudioPopup(resolvedWorldId)) { setLaunchState("opened"); setStatus("Worldshaper Studio opened in a separate window."); return; } setLaunchState("blocked"); setStatus("Your browser blocked the studio window. Use the launch button again after allowing popups."); } catch (nextError: unknown) { const nextErrorText = String(nextError || "Failed to prepare Worldshaper Studio."); setLaunchState("error"); setError(nextErrorText); setStatus("Worldshaper Studio unavailable."); } } async function handleAddRequest(): Promise { const text = requestDraft.trim(); if (!text) { setRequestsError("Write a request before saving it."); return; } setRequestSubmitting(true); try { const payload = await fetchJsonOrThrow("/api/launcher-requests", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ text }), }); setRequests(Array.isArray(payload.requests) ? payload.requests : []); setRequestDraft(""); setRequestDraftOpen(false); setRequestsError(""); } catch (nextError: unknown) { setRequestsError(String(nextError || "Failed to save request.")); } finally { setRequestSubmitting(false); } } function handleToggleExpandedRequest(requestId: string): void { setExpandedRequestIds((current) => ( current.includes(requestId) ? current.filter((entry) => entry !== requestId) : [...current, requestId] )); } async function handleDeleteRequest(requestEntry: LauncherRequest): Promise { const confirmed = window.confirm(`Delete this request?\n\n${requestEntry.title}`); if (!confirmed) { return; } setRequestMutatingId(requestEntry.id); try { const payload = await fetchJsonOrThrow(`/api/launcher-requests/${encodeURIComponent(requestEntry.id)}`, { method: "DELETE", }); setRequests(Array.isArray(payload.requests) ? payload.requests : []); setRequestsError(""); setExpandedRequestIds((current) => current.filter((entry) => entry !== requestEntry.id)); } catch (nextError: unknown) { setRequestsError(String(nextError || "Failed to delete request.")); } finally { setRequestMutatingId(""); } } const isBusy = launchState === "opening"; const requestCount = requests.length; const pendingRequestCount = requests.filter((entry) => entry.status === "pending").length; const activeRequestCount = requests.filter((entry) => entry.status === "active").length; const requestTags = Array.from(new Set( requests .flatMap((entry) => Array.isArray(entry.tags) ? entry.tags : []) .map((entry) => String(entry || "").trim()) .filter(Boolean), )).sort((a, b) => a.localeCompare(b)); const filteredRequests = requests.filter((entry) => { if (requestFilter === "status:pending") { return entry.status === "pending"; } if (requestFilter === "status:active") { return entry.status === "active"; } if (requestFilter.startsWith("tag:")) { const tag = requestFilter.slice(4); return entry.status === "active" && entry.tags.includes(tag); } return true; }); const boardHint = activeBoardTab === "news" ? "Latest announcements" : `${pendingRequestCount} pending, ${activeRequestCount} active`; return (
Worldshaper Studio
Floating editor launch

New RPG

Worldshaper Studio

{status}

{launchState === "blocked" ? (

Allow the popup, then use the studio button again to launch the floating editor window.

) : null} {launchState === "opened" ? (

The studio is open in its own slim window. This page stays behind as your release board and relaunch point.

) : null} {launchState === "ready" ? (

The editor is designed to live in its own floating window, so the launcher keeps the first step clean.

) : null} {error ?

{error}

: null}
Worldshaper Board
{boardHint}
{activeBoardTab === "news" ? (
{CHANGELOG_SPLASH_KICKER}
{CHANGELOG_SPLASH_TITLE}
Release {CHANGELOG_SPLASH_VERSION}
{CHANGELOG_SECTIONS.map((section) => (

{section.title}

    {section.items.map((item, index) => { const key = `${section.title}-${index}`; const normalizedItem: ChangelogItem = item; if (typeof normalizedItem === "string") { return
  • {normalizedItem}
  • ; } return (
  • {normalizedItem.text}
    {normalizedItem.note ?
    {normalizedItem.note}
    : null}
  • ); })}
))}
{CHANGELOG_SPLASH_FOOTNOTE}
) : (
Shared Request Board
Requests
{requestCount} saved request{requestCount === 1 ? "" : "s"}: {pendingRequestCount} pending and {activeRequestCount} active.
{requestDraftOpen ? (