File size: 11,479 Bytes
34b0226
 
070c931
34b0226
 
f74c934
b74c6ae
0649f1b
 
 
 
 
34b0226
 
b74c6ae
 
 
 
 
 
 
 
 
34b0226
0649f1b
 
 
 
 
 
 
 
 
b74c6ae
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34b0226
 
 
0649f1b
34b0226
b74c6ae
 
 
 
 
 
 
 
 
 
 
 
 
34b0226
 
fa8df4c
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0649f1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070c931
 
34b0226
 
b74c6ae
34b0226
 
 
 
0649f1b
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070c931
 
 
 
 
 
 
 
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b74c6ae
 
 
 
 
 
 
 
 
34b0226
0649f1b
 
 
 
 
 
 
 
34b0226
 
 
 
 
 
bdd1640
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0649f1b
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0649f1b
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
070c931
 
34b0226
 
 
 
 
 
 
 
 
 
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
import { useState, type ReactNode } from "react";
import { Link } from "react-router-dom";
import { artifactsApi, videosApi } from "../api/endpoints";
import type { Artifact, Citation, Segment } from "../api/types";
import { useJob } from "../hooks/useJob";
import { Markdown } from "./Markdown";
import { MindMapExplorer } from "./MindMap";
import { FlashcardsBody } from "./Flashcards";
import { InfographicBody } from "./Infographic";
import { QuizBody } from "./Quiz";
import { StudyOverlay } from "./StudyOverlay";
import { ReportBody } from "./ReportBody";
import { ConfirmButton, errorMessage, formatDate, formatDuration, JobProgress, StatusPill } from "./ui";

export function CitationList({
  citations,
  onCite,
  collapsible = false,
}: {
  citations: Citation[];
  onCite?: (c: Citation) => void;
  collapsible?: boolean;
}) {
  if (!citations.length) return null;
  // The same source is listed once: its title, then one clickable number for each place it was cited.
  const groups: { key: string; title: string; cites: Citation[] }[] = [];
  for (const c of [...citations].sort((x, y) => x.marker - y.marker)) {
    const key = c.document_id || c.path || c.title;
    const group = groups.find((g) => g.key === key);
    if (group) group.cites.push(c);
    else groups.push({ key, title: c.title, cites: [c] });
  }
  const lines = (c: Citation) => `lines ${c.line_start}${c.line_end > c.line_start ? ` to ${c.line_end}` : ""}`;
  const list = (
    <ul className="cites cites-grouped">
      {groups.map((g) => (
        <li key={g.key}>
          <button type="button" className="link-btn cite-title" onClick={() => onCite?.(g.cites[0])}>
            {g.title}
          </button>
          <span className="cite-nums">
            {g.cites.map((c, i) => (
              <span key={c.marker}>
                {i > 0 && <span className="muted">, </span>}
                <button type="button" className="cite-marker cite-num mono" onClick={() => onCite?.(c)}
                        title={`${lines(c)}: ${c.span.length > 160 ? `${c.span.slice(0, 160)}...` : c.span}`}>
                  {c.marker}
                </button>
              </span>
            ))}
          </span>
        </li>
      ))}
    </ul>
  );
  if (!collapsible) return list;
  return (
    <details className="citation-disclosure">
      <summary>
        <span>Citations</span>
        <span className="citation-count mono">{citations.length}</span>
        <svg className="citation-chevron" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
          <path d="m7 10 5 5 5-5" />
        </svg>
      </summary>
      {list}
    </details>
  );
}

export function Transcript({ segments, onSeek }: { segments: Segment[]; onSeek: (t: number) => void }) {
  return (
    <ol className="transcript">
      {segments.map((s, i) => (
        <li key={i}>
          <button type="button" className="link-btn mono transcript-time" onClick={() => onSeek(s.start)}>
            {formatDuration(s.start) || "0:00"}
          </button>
          <span className={`speaker speaker-${s.speaker}`}>{s.speaker === "host_b" ? "B" : "A"}</span>
          <span>{s.text}</span>
          {s.sources.length > 0 && <span className="mono muted transcript-src"> [{s.sources.map((r) => r.title ?? r.path).join("; ")}]</span>}
        </li>
      ))}
    </ol>
  );
}

/** Cards whose buttons (Open, Share, Delete...) sit together on one line, all the same size. */
const INLINE_ACTIONS = ["summary", "report", "quiz", "flashcards", "mind_map", "launch_kit", "infographic"];

/** A summary in its card is a title and a way in: it opens full screen (like the Mind Constellation) and can be minimised. */
function SummaryBody({ artifact, onCite }: { artifact: Artifact; onCite?: (c: Citation) => void }) {
  const [open, setOpen] = useState(false);
  const c = artifact.content;
  const themes = (c.themes as string[] | undefined) ?? [];
  return (
    <div className="artifact-body">
      {themes.length > 0 && <p className="muted">{themes.length} themes · {(c.citations ?? []).length} citations</p>}
      <button type="button" className="btn btn-small" onClick={() => setOpen(true)}>Open summary</button>
      {open && (
        <StudyOverlay title={artifact.title} onClose={() => setOpen(false)}>
          {themes.length > 0 && (
            <div className="chips">
              {themes.map((t) => <span key={t} className="chip">{t}</span>)}
            </div>
          )}
          <Markdown text={String(c.summary ?? "")} citations={c.citations ?? []} onCite={onCite} />
          <CitationList citations={c.citations ?? []} onCite={onCite} />
        </StudyOverlay>
      )}
    </div>
  );
}

/** The player, image or text of a finished artifact (nothing while it is still being made). */
export function Body({ artifact, onCite }: { artifact: Artifact; onCite?: (c: Citation) => void }) {
  const [showTranscript, setShowTranscript] = useState(false);
  const [audioEl, setAudioEl] = useState<HTMLAudioElement | null>(null);
  const [exploring, setExploring] = useState(false);
  const c = artifact.content;
  if (artifact.status !== "ready") return null;
  switch (artifact.type) {
    case "summary":
      return <SummaryBody artifact={artifact} onCite={onCite} />;
    case "audio_overview":
      return (
        <div className="artifact-body">
          {artifact.url && <audio ref={setAudioEl} controls preload="metadata" src={artifact.url} className="player" />}
          {c.segments?.length > 0 && (
            <>
              <button type="button" className="link-btn" onClick={() => setShowTranscript((v) => !v)}>
                {showTranscript ? "Hide transcript" : `Show transcript (${c.segments.length} lines)`}
              </button>
              {showTranscript && (
                <Transcript
                  segments={c.segments}
                  onSeek={(t) => {
                    if (audioEl) {
                      audioEl.currentTime = t;
                      void audioEl.play();
                    }
                  }}
                />
              )}
            </>
          )}
        </div>
      );
    case "video":
      if (artifact.provider === "blog2video") {
        return (
          <div className="artifact-body">
            {artifact.url && <video controls preload="metadata" src={artifact.url} className={`player video-${c.aspect_ratio === "portrait" ? "short" : "explainer"}`} />}
            <Link className="btn btn-small" to={`/app/videos/${artifact.id}`}>
              Open editor
            </Link>
          </div>
        );
      }
      return artifact.url ? (
        <div className="artifact-body">
          <video controls preload="metadata" src={artifact.url} className={`player video-${c.style ?? "short"}`} />
        </div>
      ) : null;
    case "quote_card":
      return artifact.url ? (
        <div className="artifact-body">
          <img src={artifact.url} alt={c.quote ?? "Quote card"} className="still" />
        </div>
      ) : null;
    case "carousel":
      return (
        <div className="artifact-body slides">
          {artifact.slide_urls.map((u, i) => (
            <a key={u} href={u} target="_blank" rel="noreferrer">
              <img src={u} alt={`Slide ${i + 1}`} className="still" />
            </a>
          ))}
        </div>
      );
    case "mind_map":
      return (
        <div className="artifact-body">
          <p className="muted">{c.node_count ?? 0} stars across {c.post_count ?? 0} posts.</p>
          <button type="button" className="btn btn-small" onClick={() => setExploring(true)}>
            Open Mind Constellation
          </button>
          {exploring && <MindMapExplorer artifact={artifact} onClose={() => setExploring(false)} />}
        </div>
      );
    case "quiz":
      return <QuizBody artifact={artifact} />;
    case "flashcards":
      return <FlashcardsBody artifact={artifact} />;
    case "report":
      return <ReportBody artifact={artifact} />;
    case "infographic":
      return <InfographicBody artifact={artifact} />;
    case "launch_kit":
      return (
        <div className="artifact-body">
          <p className="muted">
            {c.claims?.length ?? 0} claims, {c.hooks?.length ?? 0} hooks, posts for 4 platforms, SEO pack and carousel.
          </p>
          <Link className="btn btn-small" to={`/app/launchpad/kits/${artifact.id}`}>
            Open Launch Kit
          </Link>
        </div>
      );
    default:
      return null;
  }
}

/** Any generated artifact: live progress while it runs, then the right player or preview. */
export function ArtifactCard({
  artifact: initial,
  onCite,
  onRemoved,
  onChanged,
  actions,
}: {
  artifact: Artifact;
  onCite?: (c: Citation) => void;
  onRemoved?: (id: string) => void;
  onChanged?: (a: Artifact) => void;
  actions?: (a: Artifact) => ReactNode;
}) {
  const [artifact, setArtifact] = useState(initial);
  const [error, setError] = useState<string | null>(null);
  const job = useJob(artifact.job, () => {
    artifactsApi.get(artifact.id).then((a) => {
      setArtifact(a);
      onChanged?.(a);
    });
  });
  const running = job && (job.status === "queued" || job.status === "running");
  const failedMessage = artifact.status === "failed" ? (artifact.content.error as string | undefined) ?? job?.error : null;

  return (
    <article className={`artifact card artifact-${artifact.type}${INLINE_ACTIONS.includes(artifact.type) ? " artifact-inline" : ""}`}>
      <header className="artifact-head">
        <div>
          <p className="eyebrow">{artifact.type_label}</p>
          <h3>{artifact.title}</h3>
          <p className="mono muted">
            {formatDate(artifact.created_at, true)}
            {artifact.content.duration_s ? ` · ${formatDuration(artifact.content.duration_s)}` : ""}
          </p>
        </div>
        <StatusPill status={running ? (job!.status === "queued" ? "queued" : "working") : artifact.status} />
      </header>
      {running && job && <JobProgress job={job} compact />}
      {failedMessage && <p className="error-text">{failedMessage}</p>}
      <Body artifact={artifact} onCite={onCite} />
      {error && <p className="error-text">{error}</p>}
      <footer className="artifact-actions">
        {actions?.(artifact)}
        {artifact.download_url && artifact.type !== "infographic" && (
          <a className="btn btn-small" href={artifact.download_url}>
            Download
          </a>
        )}
        {artifact.status === "failed" && (
          <button
            type="button"
            className="btn btn-small"
            onClick={async () => {
              setError(null);
              try {
                setArtifact(await artifactsApi.retry(artifact.id));
              } catch (e) {
                setError(errorMessage(e));
              }
            }}
          >
            Retry
          </button>
        )}
        {onRemoved && (
          <ConfirmButton
            onConfirm={async () => {
              // A blog2video video is also removed from blog2video.
              await (artifact.provider === "blog2video" ? videosApi.remove(artifact.id) : artifactsApi.remove(artifact.id));
              onRemoved(artifact.id);
            }}
          >
            Delete
          </ConfirmButton>
        )}
      </footer>
    </article>
  );
}