import { html, useState } from "./runtime.js"; import { useCloud, money, planPeriod, DEVELOPER_EMAIL } from "./cloud.js"; import { Notice, Pill, Icon } from "./components.js"; function Shell({ children }) { return html`
${children}
`; } // --------------------------------------------------------------------------- // Sign in / create account // --------------------------------------------------------------------------- export function SignIn({ onDone }) { const cloud = useCloud(); const [mode, setMode] = useState("signin"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(""); const [error, setError] = useState(""); const [showCode, setShowCode] = useState(false); const [code, setCode] = useState(""); if (!cloud.configured) { return html`<${Shell}>
Account

Explication has no accounts yet.

Logins run on Supabase. Connect a Supabase project and this page becomes a normal sign-in screen.

<${Notice} kind="warn">No Supabase project is connected to this copy of the site.
Connect Supabase ${DEVELOPER_EMAIL} can do this.
`; } if (cloud.user) { return html`<${Account} onDone=${onDone} />`; } const submit = async (event) => { event.preventDefault(); setError(""); setMessage(""); setBusy(true); try { if (mode === "signin") await cloud.signIn(email.trim(), password); else { const result = await cloud.signUp(email.trim(), password); if (result && result.session) setMessage("Account created. You are signed in."); else setMessage("Account created. Check your email — the link signs you straight in, or paste the six-digit code below."); } } catch (err) { setError(err && err.message ? err.message : "That did not work."); } setBusy(false); }; const sendLink = async () => { setError(""); setMessage(""); if (!email.trim()) { setError("Type your email first."); return; } setBusy(true); try { await cloud.magicLink(email.trim()); setMessage("A sign-in link is on its way to " + email.trim() + "."); } catch (err) { setError(err && err.message ? err.message : "The link could not be sent."); } setBusy(false); }; const forgot = async () => { setError(""); setMessage(""); if (!email.trim()) { setError("Type your email first, then press Forgot password."); return; } setBusy(true); try { await cloud.resetPassword(email.trim()); setMessage("If " + email.trim() + " has an account, a reset link is on its way. It signs you in so you can choose a new password."); } catch (err) { setError(err && err.message ? err.message : "The reset email could not be sent."); } setBusy(false); }; const submitCode = async (event) => { event.preventDefault(); setError(""); setMessage(""); if (!email.trim()) { setError("Type your email first, then the code."); return; } if (!/^\d{6}$/.test(code.trim())) { setError("The code is six digits."); return; } setBusy(true); try { await cloud.verifyCode(email.trim(), code.trim()); setMessage("Code accepted. You're signed in."); } catch (err) { setError(err && err.message ? err.message : "That code did not work."); } setBusy(false); }; return html`<${Shell}>
Account

${mode === "signin" ? "Welcome back." : "Make an account."}

An account keeps your plan, your model budget and any downloads you own. Your writing still stays in this browser.

setEmail(e.target.value)} autocomplete="email" placeholder="you@example.com" required />
setPassword(e.target.value)} autocomplete=${mode === "signin" ? "current-password" : "new-password"} placeholder="At least 6 characters" required />
${mode === "signin" && html`
`} ${error && html`<${Notice} kind="error">${error}`} ${message && html`<${Notice} kind="info">${message}`}
No password? Send a one-time link instead.
Got a six-digit code in the email?
${showCode && html`
setCode(e.target.value.replace(/[^0-9]/g, ""))} placeholder="123456" autocomplete="one-time-code" />
`}

Plans and purchases are attached to this account. You can use every marking tool without one.

`; } // --------------------------------------------------------------------------- // Account / plan // --------------------------------------------------------------------------- function BudgetBar({ budget, plan }) { if (!plan) return null; if (!plan.cost_limit_cents) { return html`
This pack is all offline: download the models and they are yours, with no allowance to run down.
`; } const pct = budget.limit ? Math.min(100, Math.round((budget.spent / budget.limit) * 100)) : 0; return html`
Included AI this period ${100 - pct}% left
Your included AI tops up with your plan. Offline models and your own key never touch it.
`; } export function Account({ onDone }) { const cloud = useCloud(); const [busy, setBusy] = useState(false); if (!cloud.user) { return html`<${SignIn} />`; } const plan = cloud.plan; const signedOut = async () => { setBusy(true); await cloud.signOut(); setBusy(false); if (onDone) onDone(); }; return html`<${Shell}>
Account

${cloud.user.email}

${cloud.isDev ? "This address is the developer account for this site." : "Your plan, budget and downloads live here."}

Your plan

${plan ? "Change plan" : "See plans"}
${plan ? html`
${plan.name} <${Pill} tone=${plan.kind === "subscription" ? "green" : "neutral"}>${plan.kind === "one_time" ? "One-off" : "Subscription"}
${money(plan.price_cents, plan.currency)} ${planPeriod(plan)}
${cloud.entitlement && cloud.entitlement.expires_at ? "Renews " + new Date(cloud.entitlement.expires_at).toLocaleDateString("en-GB") : "Active"}
<${BudgetBar} budget=${cloud.budget} plan=${plan} />` : html`
No plan yet
Rule-based marking, chat setup and BYOK work without one.
Choose a plan
`}

Bring your own key

Your provider, your bill
Add an OpenAI-compatible endpoint in Settings and every hosted feature runs on your own key. It never touches your included allowance, so you can keep going as long as you like.
Open Settings
${cloud.isDev && html`

Developer

Developer console
Plans and prices, PayPal, models and keys, page wording, and who has access.
Open developer console
`} `; } // --------------------------------------------------------------------------- // Landing screens for email links // --------------------------------------------------------------------------- const WELCOME_COPY = { signup: { eyebrow: "Account confirmed", title: "You're in.", body: "Your email is confirmed and you're signed in. Your plan, budget and downloads stay with this account." }, magiclink: { eyebrow: "Signed in", title: "You're signed in.", body: "The link worked — no password needed." }, email_change: { eyebrow: "Address updated", title: "That's your new address.", body: "Your sign-in address has changed, and you're already signed in with it." }, session: { eyebrow: "Signed in", title: "You're in.", body: "The link worked and you're signed in." } }; function CentredCard({ children }) { return html`
${children}
`; } function Tick({ tone = "good" }) { const good = tone === "good"; return html`
<${Icon} name=${good ? "check" : "x"} size=${26} stroke=${2.2} />
`; } function Waiting() { return html`<${CentredCard}>
Signing you in…

Reading the link and setting up your session.

`; } // Where a confirmed sign-up, magic link or email change lands. export function Welcome({ kind, onDone }) { const cloud = useCloud(); const copy = WELCOME_COPY[kind] || WELCOME_COPY.magiclink; if (!cloud.ready) { return html`<${Shell}><${Waiting} />`; } if (!cloud.configured) { return html`<${Shell}> <${CentredCard}> <${Tick} tone="bad" />

No accounts yet.

This copy of the site has no Supabase project, so there is no account to confirm.

Connect Supabase
`; } if (!cloud.user) { return html`<${Shell}> <${CentredCard}> <${Tick} tone="bad" />

That link didn't sign you in.

Email links work once and expire after an hour. Sign in with your password, or send yourself a fresh one from the sign-in page.

`; } return html`<${Shell}> <${CentredCard}> <${Tick} />
${copy.eyebrow}

${copy.title}

${copy.body}

Signed in as ${cloud.user.email}

See the plans

`; } // Where a password-reset or invite link lands. The link has already signed the // person in; this screen is the missing step that sets the password. export function NewPassword({ kind, onDone }) { const cloud = useCloud(); const [password, setPassword] = useState(""); const [again, setAgain] = useState(""); const [busy, setBusy] = useState(false); const [done, setDone] = useState(false); const [error, setError] = useState(""); const submit = async (event) => { event.preventDefault(); setError(""); if (password.length < 6) { setError("Use at least six characters."); return; } if (password !== again) { setError("The two passwords don't match."); return; } setBusy(true); try { await cloud.updatePassword(password); setDone(true); } catch (err) { setError(err && err.message ? err.message : "The password could not be changed."); } setBusy(false); }; if (!cloud.ready) { return html`<${Shell}><${Waiting} />`; } if (!cloud.configured) { return html`<${Shell}> <${CentredCard}> <${Tick} tone="bad" />

No accounts yet.

This copy of the site has no Supabase project, so there is no password to change.

Connect Supabase
`; } if (!cloud.user) { return html`<${Shell}> <${CentredCard}> <${Tick} tone="bad" />

This link has expired.

Password links work once and last an hour. Ask for a new one from the sign-in page.

`; } if (done) { return html`<${Shell}> <${CentredCard}> <${Tick} />
Password changed

You're all set.

Use your new password the next time you sign in.

`; } return html`<${Shell}> <${CentredCard}>
${kind === "invite" ? "Welcome" : "Password reset"}

${kind === "invite" ? "Choose a password." : "Choose a new password."}

${kind === "invite" ? "Set a password for " + cloud.user.email + " and you're in." : "You're signed in as " + cloud.user.email + ". Pick something you'll remember."}

setPassword(e.target.value)} autocomplete="new-password" placeholder="At least 6 characters" required />
setAgain(e.target.value)} autocomplete="new-password" placeholder="Same password" required />
${error && html`<${Notice} kind="error">${error}`}
`; } // When Supabase itself reports the link is bad or used. export function LinkProblem({ onDone }) { return html`<${Shell}> <${CentredCard}> <${Tick} tone="bad" />

That link has expired.

Email links work once and last an hour. Ask for a fresh one from the sign-in page — it takes a moment.

`; } // --------------------------------------------------------------------------- // Connect Supabase // --------------------------------------------------------------------------- export function Connect() { const cloud = useCloud(); const [url, setUrl] = useState(cloud.configured ? cloud.config.url : ""); const [anonKey, setAnonKey] = useState(cloud.configured ? cloud.config.anonKey : ""); const [error, setError] = useState(""); const [done, setDone] = useState(false); const [copied, setCopied] = useState(false); const connect = async (event) => { event.preventDefault(); setError(""); const clean = url.trim().replace(/\/+$/, ""); if (!/^https?:\/\/.+/.test(clean)) { setError("The project URL looks like https://yourproject.supabase.co"); return; } if (anonKey.trim().length < 20) { setError("Paste the anon public key from Supabase → Project Settings → API."); return; } cloud.connect({ url: clean, anonKey: anonKey.trim() }); setDone(true); }; const copySchema = async () => { try { const res = await fetch("supabase/schema.sql"); const text = await res.text(); await navigator.clipboard.writeText(text); setCopied(true); setTimeout(() => setCopied(false), 2400); } catch { setError("Could not copy the schema. Open supabase/schema.sql in the folder instead."); } }; if (done || (cloud.configured && !error)) { return html`<${Shell}>
Connected

Supabase is connected.

This browser now reads accounts, plans, models and page wording from ${cloud.config.url}.

<${Pill} tone="green">Connected${cloud.config.url}
<${Notice} kind="info"> Two things left in the Supabase dashboard: run the SQL in supabase/schema.sql in the SQL editor, then paste the same URL and key into js/config.js so every visitor gets them automatically.
Open the picture guide See the plans page Developer console

This setup screen disappears from the site once a project is connected. It stays available in Developer → Supabase.

`; } return html`<${Shell}>
Setup

Connect Supabase.

Supabase holds the accounts, the plans, the shared page wording and the model budgets. Create a free project, then paste two values from Project Settings → API.

setUrl(e.target.value)} placeholder="https://abcdefgh.supabase.co" />
${error && html`<${Notice} kind="error">${error}`}
Only the anon public key goes in the browser. It is safe to publish: row-level security in the schema stops anyone but ${DEVELOPER_EMAIL} from changing plans, models or wording. Never paste the service_role key here.

Full instructions: the picture guide.

`; }