Spaces:
Running

File size: 6,815 Bytes
64378fa
 
 
 
 
 
 
 
30247fe
64378fa
 
 
 
 
 
 
30247fe
64378fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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} />`);