File size: 9,223 Bytes
34b0226
f74c934
34b0226
 
 
 
 
00a4363
34b0226
 
 
f74c934
 
 
 
 
 
 
 
 
00a4363
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00a4363
 
 
 
34b0226
 
 
00a4363
34b0226
00a4363
 
f74c934
00a4363
 
34b0226
 
 
00a4363
34b0226
 
 
00a4363
 
f74c934
 
 
 
 
00a4363
 
34b0226
 
 
f74c934
00a4363
34b0226
00a4363
 
 
34b0226
00a4363
 
 
 
 
 
 
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
00a4363
 
34b0226
 
 
 
 
 
 
 
00a4363
 
 
 
 
 
 
 
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00a4363
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
00a4363
 
 
 
 
 
34b0226
00a4363
 
34b0226
00a4363
 
 
 
 
 
 
34b0226
 
 
 
 
 
00a4363
 
34b0226
00a4363
 
 
 
34b0226
00a4363
 
 
34b0226
 
 
00a4363
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00a4363
 
 
34b0226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00a4363
 
 
 
 
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
import { useCallback, useEffect, useRef, useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { api } from "../api/client";
import { artifactsApi, jobsApi, launchpadApi, notebooksApi, sourcesApi, voiceApi } from "../api/endpoints";
import type { Artifact, CalendarItem, Job, NotebookSummary, Source } from "../api/types";
import { CheckIcon } from "../components/icons/Icons";
import { formatDate, JobProgress, StatusPill } from "../components/ui";
import { useAuth } from "../hooks/useAuth";
import { useJobMap } from "../hooks/useJob";
import { AddSource } from "./Sources";
import { PENDING_SOURCE_KEY } from "./Landing";
import { ONBOARDED_KEY } from "./Welcome";

function onboarded(): boolean {
  try {
    return localStorage.getItem(ONBOARDED_KEY) === "1";
  } catch {
    return true;
  }
}

type Plan = { name: string };

const JOB_LABELS: Record<string, string> = {
  ingest: "Syncing a source",
  import_url: "Importing an article",
  import_upload: "Importing a file",
  topics: "Mapping topics",
  voice_profile: "Building your voice profile",
  voice_clone: "Cloning your voice",
  resurface_scan: "Scoring evergreen posts",
  summary: "Writing a summary",
  audio_overview: "Recording an audio overview",
  video: "Making a video",
  quote_card: "Rendering a quote card",
  carousel: "Rendering a carousel",
  launch_kit: "Building a Launch Kit",
  render: "Rendering",
};

export default function MissionControl() {
  const { user } = useAuth();
  const [sources, setSources] = useState<Source[]>([]);
  const [notebooks, setNotebooks] = useState<NotebookSummary[]>([]);
  const [recent, setRecent] = useState<Artifact[]>([]);
  const [upcoming, setUpcoming] = useState<CalendarItem[]>([]);
  const [plan, setPlan] = useState<Plan | null>(null);
  const [hasVoice, setHasVoice] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const started = useRef(false);
  const navigate = useNavigate();

  const refresh = useCallback(async () => {
    const [s, n, p, a, c, v] = await Promise.all([
      sourcesApi.list(),
      notebooksApi.list(),
      api<{ plan: Plan }>("/api/billing/me"),
      artifactsApi.list({ limit: 6 }),
      launchpadApi.items({ start: new Date().toISOString(), status: "scheduled,draft" }),
      voiceApi.get(),
    ]);
    setSources(s);
    // First visit with nothing connected: run the guided setup once.
    if (s.length === 0 && !onboarded()) {
      navigate("/welcome", { replace: true });
      return;
    }
    setNotebooks(n);
    setPlan(p.plan);
    setRecent(a.items);
    setUpcoming(c.slice(0, 5));
    setHasVoice(Boolean(v.profile));
  }, [navigate]);

  const { jobs, watch } = useJobMap(() => refresh());

  useEffect(() => {
    refresh().catch(() => setError("Could not reach Mission Control. Is the API running?"));
    jobsApi.active().then((active) => active.forEach((j) => watch(j.id, j)));
    if (started.current) return;
    started.current = true;
    let pending: string | null = null;
    try {
      pending = sessionStorage.getItem(PENDING_SOURCE_KEY);
      sessionStorage.removeItem(PENDING_SOURCE_KEY);
    } catch {
      /* ignore */
    }
    if (pending) {
      sourcesApi.connect(pending).then(
        (res) => {
          watch(res.job.id, res.job);
          refresh();
        },
        (e) => setError(e instanceof Error ? e.message : "Could not add that source."),
      );
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [refresh]);

  const totalDocs = sources.reduce((n, s) => n + s.document_count, 0);
  const activeJobs = Object.values(jobs).filter((j: Job) => j.status === "queued" || j.status === "running" || j.status === "failed");
  const checklist = [
    { done: sources.length > 0 && totalDocs > 0, label: "Connect your archive", to: "/app/sources" },
    { done: hasVoice, label: "Build your voice profile", to: "/app/voice" },
    { done: notebooks.length > 0, label: "Open a research notebook", to: "/app/notebooks" },
    { done: recent.some((a) => a.type === "launch_kit"), label: "Make your first Launch Kit", to: "/app/launch-kit" },
    { done: upcoming.length > 0, label: "Schedule a launch", to: "/app/launchpad" },
  ];

  return (
    <div className="mc">
      <header className="mc-head">
        <div>
          <p className="eyebrow">Mission Control</p>
          <h1>Good to see you, {(user?.name ?? user?.email ?? "").split(/[ @]/)[0]}</h1>
        </div>
        {plan && <span className="badge">{plan.name} plan · early access</span>}
      </header>
      {error && <p className="error-text">{error}</p>}

      {checklist.some((c) => !c.done) && (
        <section className="card checklist">
          <p className="eyebrow">Pre-flight checklist</p>
          <ol>
            {checklist.map((c) => (
              <li key={c.label} className={c.done ? "done" : ""}>
                <span className="check-dot">{c.done && <CheckIcon size={14} />}</span>
                <Link to={c.to}>{c.label}</Link>
              </li>
            ))}
          </ol>
        </section>
      )}

      {activeJobs.length > 0 && (
        <section className="card stack">
          <h2>In flight</h2>
          {activeJobs.map((j) => (
            <div key={j.id}>
              <p className="mono muted small">{JOB_LABELS[j.kind] ?? j.kind}</p>
              <JobProgress job={j} compact />
            </div>
          ))}
        </section>
      )}

      <section className="mc-grid">
        <div className="card mc-card mc-sources">
          <div className="row between">
            <h2>Sources</h2>
            <Link to="/app/sources" className="mono muted small-link">
              Manage
            </Link>
          </div>
          <AddSource
            onAdded={(source, job) => {
              setSources((list) => [...list.filter((s) => s.id !== source.id), source]);
              watch(job.id, job);
            }}
          />
          <ul className="source-list">
            {sources.map((s) => (
              <li key={s.id} className="source-row">
                <div className="source-title">
                  <strong>{s.title ?? s.feed_url}</strong>
                  <span className="mono muted">
                    {s.is_imports ? "imports" : s.platform} · {s.document_count} posts
                  </span>
                </div>
                <StatusPill status={s.sync_status} />
              </li>
            ))}
            {sources.length === 0 && <li className="muted">No sources yet. A lone satellite, waiting for signal.</li>}
          </ul>
        </div>

        <div className="card mc-card">
          <div className="row between">
            <h2>Notebooks</h2>
            <Link to="/app/notebooks" className="mono muted small-link">
              All
            </Link>
          </div>
          <p className="muted">{totalDocs} posts indexed across your sources.</p>
          <ul className="nb-list">
            {notebooks.slice(0, 6).map((n) => (
              <li key={n.id}>
                <Link to={`/app/notebooks/${n.id}`} className="nb-link">
                  {n.title}
                </Link>
                <span className="mono muted"> {n.document_count} posts</span>
              </li>
            ))}
          </ul>
          <Link className="btn" to="/app/notebooks">
            {notebooks.length ? "Open notebooks" : "Create a notebook"}
          </Link>
        </div>

        <div className="card mc-card">
          <div className="row between">
            <h2>Recent launches</h2>
            <Link to="/app/archive" className="mono muted small-link">
              Archive
            </Link>
          </div>
          {recent.length === 0 && <p className="muted">Audio overviews, videos and launch kits will show up here.</p>}
          <ul className="recent-list">
            {recent.map((a) => (
              <li key={a.id}>
                <Link to={a.type === "launch_kit" ? `/app/launch-kit?kit=${a.id}` : a.notebook_id ? `/app/notebooks/${a.notebook_id}` : "/app/archive"}>
                  {a.title}
                </Link>
                <span className="row">
                  <span className="mono muted">{formatDate(a.created_at)}</span>
                  <StatusPill status={a.status} />
                </span>
              </li>
            ))}
          </ul>
        </div>

        <div className="card mc-card">
          <div className="row between">
            <h2>Launchpad</h2>
            <Link to="/app/launchpad" className="mono muted small-link">
              Calendar
            </Link>
          </div>
          {upcoming.length === 0 && <p className="muted">Nothing scheduled. Quiet moon tonight.</p>}
          <ul className="recent-list">
            {upcoming.map((i) => (
              <li key={i.id}>
                <Link to={`/app/launchpad?item=${i.id}`}>
                  <strong>{i.platform_label}</strong> <span className="muted">{i.content.slice(0, 70)}</span>
                </Link>
                <span className="mono muted">{formatDate(i.scheduled_at, true)}</span>
              </li>
            ))}
          </ul>
        </div>
      </section>
    </div>
  );
}