File size: 3,429 Bytes
1eec5a6
52739ad
d9f07b6
44215ae
52739ad
1eec5a6
44215ae
 
 
 
 
 
 
 
 
 
 
 
1eec5a6
 
d9f07b6
44215ae
 
1eec5a6
 
 
52739ad
 
 
1eec5a6
 
 
 
 
 
 
 
52739ad
 
 
 
 
 
 
44215ae
 
 
 
 
 
 
 
 
52739ad
44215ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52739ad
1eec5a6
52739ad
1eec5a6
d9f07b6
44215ae
 
 
d9f07b6
 
 
 
 
 
 
 
 
 
 
52739ad
1eec5a6
 
 
44215ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from "react";
import DocumentUploader from "./DocumentUploader";
import Chat from "./Chat";
import Dashboard from "./Dashboard";
import { getHealth } from "./api";

// Minimal hash routing keeps the dashboard on its own URL (/#/dashboard)
// without pulling in a router dependency.
function useHashRoute() {
  const [route, setRoute] = useState(window.location.hash || "#/");
  useEffect(() => {
    const onChange = () => setRoute(window.location.hash || "#/");
    window.addEventListener("hashchange", onChange);
    return () => window.removeEventListener("hashchange", onChange);
  }, []);
  return route;
}

export default function App() {
  const [status, setStatus] = useState("loading");
  const [doc, setDoc] = useState(null);
  const route = useHashRoute();
  const onDashboard = route.startsWith("#/dashboard");

  useEffect(() => {
    let cancelled = false;
    getHealth()
      .then((s) => !cancelled && setStatus(s))
      .catch(() => !cancelled && setStatus("unreachable"));
    return () => {
      cancelled = true;
    };
  }, []);

  const ok = status === "ok";

  return (
    <div className="min-h-screen bg-slate-50 text-slate-800">
      <header className="border-b border-slate-200 bg-white">
        <div className="mx-auto flex max-w-3xl items-center justify-between px-6 py-4">
          <div>
            <h1 className="text-xl font-bold tracking-tight">DocuAsk</h1>
            <p className="text-xs text-slate-500">Upload a document, ask questions</p>
          </div>
          <div className="flex items-center gap-4">
            <nav className="flex gap-1 text-sm">
              <NavLink href="#/" active={!onDashboard}>
                Chat
              </NavLink>
              <NavLink href="#/dashboard" active={onDashboard}>
                Dashboard
              </NavLink>
            </nav>
            <span
              className="flex items-center gap-1.5 text-xs text-slate-500"
              title={`API status: ${status}`}
            >
              <span
                className={`inline-block h-2.5 w-2.5 rounded-full ${
                  status === "loading"
                    ? "animate-pulse bg-amber-400"
                    : ok
                    ? "bg-emerald-500"
                    : "bg-red-500"
                }`}
                aria-hidden="true"
              />
              <span className="hidden sm:inline">API {status}</span>
            </span>
          </div>
        </div>
      </header>

      <main className="mx-auto max-w-xl px-6 py-10">
        {onDashboard ? (
          <Dashboard />
        ) : doc ? (
          <Chat doc={doc} onReset={() => setDoc(null)} />
        ) : (
          <div className="mx-auto max-w-md">
            <h2 className="mb-1 text-lg font-semibold">Add a document</h2>
            <p className="mb-5 text-sm text-slate-500">
              Upload a PDF or paste text. We extract and index it so you can ask
              questions about it.
            </p>
            <DocumentUploader onReady={setDoc} />
          </div>
        )}
      </main>
    </div>
  );
}

function NavLink({ href, active, children }) {
  return (
    <a
      href={href}
      className={`rounded-lg px-3 py-1.5 font-medium transition ${
        active
          ? "bg-slate-100 text-slate-800"
          : "text-slate-500 hover:bg-slate-50"
      }`}
    >
      {children}
    </a>
  );
}