Spaces:
Running

File size: 13,695 Bytes
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { React, html, useEffect, useState } from "./runtime.js";
import { useApp } from "./store.js";
import { useEngine } from "./engine.js";
import { useCloud } from "./cloud.js";
import { levelTitle, wordCount } from "./marking.js";

// ---------------------------------------------------------------------------
// Icons
// ---------------------------------------------------------------------------

const PATHS = {
  home: ["M3 10.5 12 3l9 7.5V20a1 1 0 0 1-1 1h-5v-6H9v6H4a1 1 0 0 1-1-1z"],
  chat: ["M21 11.5a8.4 8.4 0 0 1-8.5 8.5H7l-4 3v-7.2A8.5 8.5 0 1 1 21 11.5z"],
  pen: ["M12 20h9", "M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z"],
  book: ["M4 19.5A2.5 2.5 0 0 1 6.5 17H20", "M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"],
  feather: ["M20 4a6.5 6.5 0 0 0-9.2 0L4 10.8V20h9.2L20 13.2A6.5 6.5 0 0 0 20 4z", "M4 20 15 9"],
  sparkle: ["M12 3l1.9 4.6L18.5 9.5l-4.6 1.9L12 16l-1.9-4.6L5.5 9.5l4.6-1.9z", "M19 16l.7 1.8L21.5 19l-1.8.7L19 21.5l-.7-1.8L16.5 19l1.8-.7z"],
  file: ["M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z", "M14 2v6h6", "M8 13h8", "M8 17h5"],
  clock: ["M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z", "M12 6v6l4 2"],
  list: ["M8 6h13", "M8 12h13", "M8 18h13", "M3 6h.01", "M3 12h.01", "M3 18h.01"],
  settings: ["M12 15.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z", "M19.4 15a1.7 1.7 0 0 0 .3 1.9l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.9-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1-1.5 1.7 1.7 0 0 0-1.9.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.9 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.3-1.9l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.9.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.9-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.9V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"],
  menu: ["M3 6h18", "M3 12h18", "M3 18h18"],
  plus: ["M12 5v14", "M5 12h14"],
  up: ["M12 19V5", "M5 12l7-7 7 7"],
  down: ["M12 5v14", "M19 12l-7 7-7-7"],
  download: ["M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4", "M7 10l5 5 5-5", "M12 15V3"],
  check: ["M20 6 9 17l-5-5"],
  x: ["M18 6 6 18", "M6 6l12 12"],
  lock: ["M5 11h14v10H5z", "M8 11V7a4 4 0 0 1 8 0v4"],
  refresh: ["M21 12a9 9 0 1 1-2.6-6.4", "M21 3v6h-6"],
  chevron: ["M9 6l6 6-6 6"],
  camera: ["M3 7h3l2-3h8l2 3h3v13H3z", "M12 17a4 4 0 1 0 0-8 4 4 0 0 0 0 8z"],
  external: ["M14 3h7v7", "M21 3l-9 9", "M21 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5"],
  trash: ["M3 6h18", "M8 6V4h8v2", "M19 6l-1 14H6L5 6"],
  user: ["M20 21v-1a7 7 0 0 0-14 0v1", "M12 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8z"],
  tag: ["M20.6 13.4 12 22l-9-9V4h9z", "M7.5 7.5h.01"],
  code: ["M16 18l6-6-6-6", "M8 6l-6 6 6 6"]
};

export function Icon({ name, size = 16, stroke = 1.8 }) {
  const paths = PATHS[name] || PATHS.file;
  return html`<svg
    width=${size}
    height=${size}
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width=${stroke}
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
  >${paths.map((d, i) => html`<path key=${i} d=${d}></path>`)}</svg>`;
}

// ---------------------------------------------------------------------------
// Brand
// ---------------------------------------------------------------------------

export function Mark({ size = 32 }) {
  return html`<div class="mark" style=${{ width: size + "px", height: size + "px" }} aria-label="Explication">
    <div class="mark-lines"><span></span><span></span><span></span></div>
  </div>`;
}

export function Brand({ small = false }) {
  return html`<div class="brand">
    <${Mark} size=${small ? 28 : 32} />
    <div>
      <div class="brand-name" style=${{ fontSize: small ? "17px" : "19px" }}>Explication</div>
      ${!small && html`<div class="brand-sub">GCSE English Literature</div>`}
    </div>
  </div>`;
}

// ---------------------------------------------------------------------------
// Navigation
// ---------------------------------------------------------------------------

export const SECTIONS = [
  { id: "dashboard", title: "Today", group: "Progress", icon: "home" },
  { id: "chat", title: "Chat", group: "Write", icon: "chat" },
  { id: "coach", title: "Paragraph coach", group: "Write", icon: "pen" },
  { id: "essay", title: "Essay", group: "Write", icon: "book" },
  { id: "improve", title: "Improve", group: "Write", icon: "sparkle" },
  { id: "poetry", title: "Unseen poetry", group: "Study", icon: "feather" },
  { id: "modelAnswer", title: "Model answers", group: "Study", icon: "file" },
  { id: "mockExam", title: "Mock exam", group: "Study", icon: "clock" },
  { id: "process", title: "Process log", group: "Study", icon: "list" },
  { id: "settings", title: "Settings", group: "Tools", icon: "settings" },
  { id: "pricing", title: "Plans", group: "Account", icon: "tag" },
  { id: "account", title: "Account", group: "Account", icon: "user" },
  { id: "developer", title: "Developer", group: "Account", icon: "code" }
];

const GROUPS = ["Progress", "Write", "Study", "Tools", "Account"];

function NavList({ current, onNavigate }) {
  const app = useApp();
  const cloud = useCloud();
  return html`<div class="nav">
    ${GROUPS.map(
      (group) => html`<div class="nav-group" key=${group}>
        <div class="nav-label">${group}</div>
        ${SECTIONS.filter((s) => s.group === group && (s.id !== "developer" || cloud.isDev)).map(
          (s) => html`<button
            key=${s.id}
            class=${"nav-row" + (current === s.id ? " active" : "")}
            onClick=${() => onNavigate(s.id)}
          >
            <${Icon} name=${s.icon} />
            <span>${s.title}</span>
            ${current === s.id && html`<i class="dot"></i>`}
          </button>`
        )}
      </div>`
    )}
    <div class="side-card">
      <div class="row">
        <span class="mini-label">Review</span>
        <${Icon} name="lock" size=${11} />
      </div>
      <div class="name">Rule-based · offline</div>
      <div class="meta">Marks in this browser. No upload.</div>
    </div>
    <div class="side-foot">
      <${Icon} name="lock" size=${12} />
      <span>Your drafts stay in this browser. An account is only for plans and downloads.</span>
    </div>
  </div>`;
}

export function Sidebar({ current, onNavigate }) {
  return html`<aside class="sidebar">
    <div class="sidebar-head">
      <${Brand} />
      <button class="side-cta" onClick=${() => onNavigate("coach")}>
        <${Icon} name="plus" size=${15} /> New feedback
      </button>
    </div>
    <${NavList} current=${current} onNavigate=${onNavigate} />
  </aside>`;
}

export function Drawer({ current, onNavigate, onClose }) {
  useEffect(() => {
    const onKey = (e) => e.key === "Escape" && onClose();
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [onClose]);
  return html`<${React.Fragment}>
    <div class="scrim" onClick=${onClose}></div>
    <div class="drawer">
      <div class="sidebar-head">
        <div class="row between">
          <${Brand} small=${true} />
          <button class="icon-btn" onClick=${onClose} aria-label="Close menu"><${Icon} name="x" /></button>
        </div>
        <button class="side-cta" onClick=${() => { onNavigate("coach"); onClose(); }}>
          <${Icon} name="plus" size=${15} /> New feedback
        </button>
      </div>
      <${NavList} current=${current} onNavigate=${(id) => { onNavigate(id); onClose(); }} />
    </div>
  <//>`;
}

export function TopBar({ section, onMenu, onNavigate }) {
  const app = useApp();
  const engine = useEngine();
  const cloud = useCloud();
  const title = (SECTIONS.find((s) => s.id === section) || SECTIONS[0]).title;
  return html`<header class="topbar">
    <button class="icon-btn menu-btn" onClick=${onMenu} aria-label="Open menu"><${Icon} name="menu" size=${18} /></button>
    <h1>${title}</h1>
    <div class="spacer"></div>
    <span class="pill green" title="Credits">✦ ${app.credits}</span>
    <button class="status-pill ${engine.canGenerate ? "ready" : ""}" onClick=${() => onNavigate("settings")} title="Model status">
      <i class="dot"></i>${engine.canGenerate ? "Model ready" : "Rules"}
    </button>
    ${cloud.user
      ? html`<button class="account-chip" onClick=${() => onNavigate("account")} title=${cloud.user.email}>
          <span class="avatar">${String(cloud.user.email || "?").slice(0, 1).toUpperCase()}</span>
          <span class="who">${cloud.plan ? cloud.plan.name : "No plan"}</span>
        </button>`
      : html`<button class="btn small" onClick=${() => onNavigate("account")}>Sign in</button>`}
    <button class="icon-btn" onClick=${() => onNavigate("coach")} aria-label="New feedback"><${Icon} name="plus" size=${17} /></button>
  </header>`;
}

// ---------------------------------------------------------------------------
// Small pieces
// ---------------------------------------------------------------------------

export function Notice({ kind = "info", children }) {
  return html`<div class=${"notice " + kind}><${Icon} name=${kind === "error" ? "x" : "lock"} size=${12} /> <div>${children}</div></div>`;
}

export function Pill({ children, tone = "neutral" }) {
  return html`<span class=${"pill " + tone}>${children}</span>`;
}

export function ScoreRing({ total, max }) {
  const pct = max ? Math.round((total / max) * 100) : 0;
  return html`<div class="ring" style=${{ "--pct": pct }}>
    <div class="num">${total}</div>
    <div class="den">/${max}</div>
  </div>`;
}

export function CriterionList({ criteria }) {
  return html`<div>
    ${criteria.map(
      (c) => html`<div class="criterion" key=${c.id}>
        <div class="head">
          <span class="name">${c.title}</span>
          <span class="score">${c.score}/${c.max}</span>
        </div>
        <div class="bar"><i style=${{ width: (c.max ? (c.score / c.max) * 100 : 0) + "%" }}></i></div>
        ${c.note && html`<div class="note">${c.note}</div>`}
      </div>`
    )}
  </div>`;
}

export function FeedbackPanel({ feedback, onSave, saved }) {
  if (!feedback) {
    return html`<div class="card empty">
      <${Mark} size=${36} />
      <div style=${{ fontSize: "12px", maxWidth: "280px", margin: "0 auto", lineHeight: 1.55 }}>
        Mark your writing and the feedback appears here: a mark for every criterion, then one thing to change.
      </div>
    </div>`;
  }
  return html`<div class="card fade-in">
    <div class="row between" style=${{ alignItems: "flex-start" }}>
      <div class="score-card">
        <${ScoreRing} total=${feedback.total} max=${feedback.max} />
        <div>
          <div class="eyebrow">${feedback.engine === "model" ? "Model mark" : "Offline review"}</div>
          <div style=${{ fontFamily: "var(--serif)", fontSize: "18px", fontWeight: 600, marginTop: "2px" }}>
            ${feedback.total >= feedback.max * 0.8 ? "Strong work." : feedback.total >= feedback.max * 0.55 ? "Solid, with room to sharpen." : "A start to build on."}
          </div>
          <div class="tiny faint">${feedback.criteria.reduce((s, c) => s + c.max, 0)} marks available</div>
        </div>
      </div>
      ${onSave &&
      html`<button class="btn small" onClick=${onSave} disabled=${saved}>
        <${Icon} name="check" size=${12} /> ${saved ? "Saved" : "Save"}
      </button>`}
    </div>

    <div class="divider"></div>
    <${CriterionList} criteria=${feedback.criteria} />

    ${feedback.strengths.length > 0 &&
    html`<div class="stack" style=${{ marginTop: "12px" }}>
      <div class="callout green"><strong>What works.</strong> ${feedback.strengths.join(" ")}</div>
    </div>`}

    ${feedback.improvements.length > 0 &&
    html`<div class="stack" style=${{ marginTop: "8px" }}>
      <div class="callout amber"><strong>Next. </strong>${feedback.improvements.join(" ")}</div>
    </div>`}

    ${feedback.nextStep && html`<div class="callout blue" style=${{ marginTop: "8px" }}><strong>Do this once. </strong>${feedback.nextStep}</div>`}

    ${feedback.quotes && feedback.quotes.length > 0 &&
    html`<div style=${{ marginTop: "14px" }}>
      <div class="eyebrow" style=${{ marginBottom: "6px" }}>Quotations checked</div>
      <div class="stack">
        ${feedback.quotes.map(
          (q, i) => html`<div class="row" key=${i} style=${{ alignItems: "flex-start", fontSize: "11px" }}>
            <span class="pill ${q.inExtract === false ? "warn" : "green"}">${q.inExtract === false ? "Check" : q.inExtract === true ? "Found" : "No extract"}</span>
            <span class="muted" style=${{ flex: 1 }}>“${q.text}” ${q.note ? "— " + q.note : ""}</span>
          </div>`
        )}
      </div>
    </div>`}
  </div>`;
}

// ---------------------------------------------------------------------------
// Hooks
// ---------------------------------------------------------------------------

export function usePersistent(key, initial) {
  const [value, setValue] = useState(() => {
    try {
      const raw = localStorage.getItem(key);
      return raw ? JSON.parse(raw) : initial;
    } catch {
      return initial;
    }
  });
  useEffect(() => {
    try {
      localStorage.setItem(key, JSON.stringify(value));
    } catch {
      // ignore
    }
  }, [key, value]);
  return [value, setValue];
}

export function download(filename, text, type = "text/markdown") {
  const blob = new Blob([text], { type });
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  setTimeout(() => URL.revokeObjectURL(url), 1000);
}

export { wordCount, levelTitle };