Spaces:
Running

explication / js /app.js
quantilimited's picture
Serve the paid Explication build with Paragraph model downloads
30247fe
Raw History Blame Contribute Delete
6.82 kB
import { React, ReactDOM, html, useEffect, useState } from "./runtime.js";
import { AppProvider, useApp } from "./store.js";
import { EngineProvider } from "./engine.js";
import { CloudProvider, useCloud } from "./cloud.js";
import { EditModeProvider, useEditMode } from "./copy.js";
import { Sidebar, Drawer, TopBar, SECTIONS, Icon } from "./components.js";
import { Dashboard, Chat, Coach, Essay, Poetry, Improve } from "./screens.js";
import { ModelAnswers, MockExam, ProcessLog, Settings } from "./screens-extra.js";
import { Pricing } from "./pricing.js";
import { Account, Connect, Welcome, NewPassword, LinkProblem } from "./auth.js";
import { Developer } from "./admin.js";
// Screens that exist but are not in the sidebar: reachable by link, never listed.
const HIDDEN_ROUTES = ["connect", "welcome", "new-password", "link-problem"];
function isRoute(id) {
return SECTIONS.some((s) => s.id === id) || HIDDEN_ROUTES.includes(id);
}
function currentSection() {
const raw = (window.location.hash || "").replace(/^#\/?/, "");
return isRoute(raw) ? raw : "dashboard";
}
// Supabase comes back from an email link with the session, or an error, in the
// address itself (#access_token=…&type=signup). Read that once at startup,
// before the router touches the URL, so a confirmed sign-up gets its own
// landing screen instead of being dropped straight on the dashboard.
function readArrival() {
const raw = (window.location.hash || "") + "&" + (window.location.search || "");
if (/(?:^|[?&#])error=/.test(raw)) return "error";
const type = (raw.match(/(?:^|[?&#])type=([a-z_]+)/) || [])[1] || "";
if (["signup", "magiclink", "email_change", "recovery", "invite"].includes(type)) return type;
if (/access_token=/.test(raw) || /[?&]code=/.test(raw)) return "session";
return "";
}
const ARRIVAL = readArrival();
const ARRIVAL_ROUTE =
ARRIVAL === "error"
? "link-problem"
: ARRIVAL === "recovery" || ARRIVAL === "invite"
? "new-password"
: ARRIVAL
? "welcome"
: "";
function SetupBanner({ onConnect, onDismiss }) {
return html`<div class="setup-banner">
<${Icon} name="sparkle" size=${15} />
<div class="setup-copy">
<strong>This copy of Explication has no accounts yet.</strong>
<span> Connect Supabase to add sign-in, plans and shared page edits. Until then everything uses this browser.</span>
</div>
<button class="btn small primary" onClick=${onConnect}>Connect Supabase</button>
<button class="icon-btn" onClick=${onDismiss} aria-label="Dismiss"><${Icon} name="x" size=${14} /></button>
</div>`;
}
function EditBar({ onDone, onSettings }) {
return html`<div class="edit-bar">
<span class="dot-live"></span>
<span><strong>Edit mode.</strong> Click any highlighted wording to change it for everyone.</span>
<button class="btn small" onClick=${onSettings}>Changed wording</button>
<button class="btn small primary" onClick=${onDone}>Done</button>
</div>`;
}
function Shell() {
const cloud = useCloud();
const [section, setSection] = useState(() => ARRIVAL_ROUTE || currentSection());
const [arrival, setArrival] = useState(ARRIVAL_ROUTE);
const [drawer, setDrawer] = useState(false);
const { editing, setEditing } = useEditMode();
const [dismissed, setDismissed] = useState(() => {
try {
return localStorage.getItem("explication.setup.dismissed") === "1";
} catch {
return false;
}
});
const navigate = (id) => {
if (!isRoute(id)) id = "dashboard";
setArrival("");
setSection(id);
if (window.location.hash !== "#/" + id) window.location.hash = "#/" + id;
window.scrollTo({ top: 0, behavior: "instant" });
};
useEffect(() => {
const onHash = () => {
const hash = window.location.hash || "";
// Supabase wipes its token from the address once it has read it, which
// fires a hashchange with no route. Keep the arrival screen on show.
if (arrival && !hash.startsWith("#/")) return;
if (arrival) setArrival("");
setSection(currentSection());
};
window.addEventListener("hashchange", onHash);
if (!arrival && !window.location.hash) window.location.hash = "#/dashboard";
return () => window.removeEventListener("hashchange", onHash);
}, [arrival]);
// A first-run visitor who has already been to the setup screen is not asked again.
const showSetup = !cloud.configured && !dismissed && section !== "connect" && !arrival;
const dismissSetup = () => {
setDismissed(true);
try {
localStorage.setItem("explication.setup.dismissed", "1");
} catch {
// ignore
}
};
const screen = (() => {
switch (section) {
case "chat":
return html`<${Chat} />`;
case "coach":
return html`<${Coach} />`;
case "essay":
return html`<${Essay} />`;
case "poetry":
return html`<${Poetry} />`;
case "improve":
return html`<${Improve} />`;
case "modelAnswer":
return html`<${ModelAnswers} />`;
case "mockExam":
return html`<${MockExam} />`;
case "process":
return html`<${ProcessLog} />`;
case "settings":
return html`<${Settings} />`;
case "pricing":
return html`<${Pricing} />`;
case "account":
return html`<${Account} onDone=${() => navigate("dashboard")} />`;
case "connect":
return html`<${Connect} />`;
case "welcome":
return html`<${Welcome} kind=${ARRIVAL} onDone=${() => navigate("dashboard")} />`;
case "new-password":
return html`<${NewPassword} kind=${ARRIVAL} onDone=${() => navigate("account")} />`;
case "link-problem":
return html`<${LinkProblem} onDone=${() => navigate("account")} />`;
case "developer":
return html`<${Developer} />`;
default:
return html`<${Dashboard} onNavigate=${navigate} />`;
}
})();
return html`<div class="app">
<${Sidebar} current=${section} onNavigate=${navigate} />
<div class="main">
${showSetup && html`<${SetupBanner} onConnect=${() => navigate("connect")} onDismiss=${dismissSetup} />`}
<${TopBar} section=${section} onMenu=${() => setDrawer(true)} onNavigate=${navigate} />
${screen}
</div>
${drawer && html`<${Drawer} current=${section} onNavigate=${navigate} onClose=${() => setDrawer(false)} />`}
${editing && html`<${EditBar} onDone=${() => setEditing(false)} onSettings=${() => navigate("developer")} />`}
</div>`;
}
function Root() {
return html`<${CloudProvider}>
<${AppProvider}>
<${EngineProvider}>
<${EditModeProvider}>
<${Shell} />
<//>
<//>
<//>
<//>`;
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(html`<${Root} />`);