@biolibs/bionet-panels 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/window.tsx ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ * A whole lab application, in one component.
3
+ *
4
+ * It holds no state of its own about the lab. It is told the lab as it stands, every time it
5
+ * changes, and draws it; a button asks for one thing to be done and waits to be told how the lab
6
+ * changed. So what is on screen is always what the signed logs say, and never what a form hoped.
7
+ *
8
+ * It walks a person through the three places they can be: nobody has said who is at this device,
9
+ * the device is in no lab, the device is waiting to be let into one — and then the lab itself.
10
+ */
11
+ import { useState } from "react";
12
+ import { Logo, Mark, useAct, useLab } from "@biolibs/bionet-react";
13
+ import { Freezer } from "./freezer.js";
14
+ import { History } from "./history.js";
15
+ import { Ask, CopyContext, DeviceKey, SyncStatus, Trouble } from "./parts.js";
16
+ import { People } from "./people.js";
17
+ import { Sharing } from "./sharing.js";
18
+
19
+ /** The four things a member looks at. */
20
+ export const TABS = ["freezer", "people", "history", "sharing"] as const;
21
+
22
+ /** One of them. */
23
+ export type Tab = (typeof TABS)[number];
24
+
25
+ /**
26
+ * The window.
27
+ *
28
+ * @param props - `tab` is which view to open on; `copy` puts words on the clipboard, for a host that
29
+ * cannot use the browser's own way.
30
+ */
31
+ export function LabWindow({ tab: first = "freezer", copy }: { tab?: Tab; copy?: (words: string) => Promise<void> }) {
32
+ const window = <Body first={first} />;
33
+ return <div className="bionet">{copy === undefined ? window : <CopyContext.Provider value={copy}>{window}</CopyContext.Provider>}</div>;
34
+ }
35
+
36
+ function Body({ first }: { first: Tab }) {
37
+ const state = useLab();
38
+ const [tab, setTab] = useState<Tab>(first);
39
+ if (state === null) return <main className="centre"><p className="dim">Opening the lab…</p></main>;
40
+ if (state.device.person === null) return <main className="centre"><Logo /><Trouble /><Who /></main>;
41
+ if (state.lab === null) return <main className="centre"><Logo /><Trouble /><NoLab /></main>;
42
+ if (!state.lab.member) return <main className="centre"><Logo /><Trouble /><Waiting /></main>;
43
+
44
+ const lab = state.lab;
45
+ const label: Record<Tab, string> = {
46
+ freezer: `Freezer · ${lab.items.filter((item) => !item.retired).length}`,
47
+ people: `People · ${lab.roster.filter((key) => key.removed === null).length}`,
48
+ history: `History · ${lab.entries.length}`,
49
+ sharing: "Sharing",
50
+ };
51
+ return (
52
+ <div className="shell">
53
+ <header>
54
+ <div className="title">
55
+ <Mark size={34} />
56
+ <div>
57
+ <h1>{lab.name ?? "A lab"}</h1>
58
+ <p className="dim small">{state.device.person} · {lab.keeper ? "keeper" : "member"} · this device <code>{state.device.fingerprint}</code></p>
59
+ </div>
60
+ </div>
61
+ <SyncStatus peer={state.peer} />
62
+ </header>
63
+ <nav>
64
+ {TABS.map((one) => <button key={one} type="button" className={one === tab ? "tab on" : "tab"} onClick={() => setTab(one)}>{label[one]}</button>)}
65
+ </nav>
66
+ <main>
67
+ <Trouble />
68
+ {tab === "freezer" && <Freezer />}
69
+ {tab === "people" && <People />}
70
+ {tab === "history" && <History />}
71
+ {tab === "sharing" && <Sharing />}
72
+ </main>
73
+ </div>
74
+ );
75
+ }
76
+
77
+ /** Asking who is at this device, before anything else. */
78
+ export function Who() {
79
+ const act = useAct();
80
+ return (
81
+ <section className="card narrow">
82
+ <h1>Who is at this device?</h1>
83
+ <p className="dim">Every change you make is signed by a key made on this device, and shown to your lab under this name. The key never leaves this computer.</p>
84
+ <Ask onAsk={(data) => act("setPerson", String(data.get("name") ?? ""))} className="row">
85
+ <input name="name" placeholder="Your name" autoFocus required maxLength={200} />
86
+ <button type="submit">Continue</button>
87
+ </Ask>
88
+ </section>
89
+ );
90
+ }
91
+
92
+ /** Founding a lab, or joining one. */
93
+ export function NoLab() {
94
+ const state = useLab();
95
+ const act = useAct();
96
+ if (state === null) return null;
97
+ return (
98
+ <div className="pair">
99
+ <section className="card">
100
+ <h2>Found a lab</h2>
101
+ <p className="dim">You become its first keeper. It works on this device straight away, with or without a network.</p>
102
+ <Ask onAsk={(data) => act("found", { name: String(data.get("name") ?? ""), peer: String(data.get("peer") ?? "") })} className="stack">
103
+ <label>What is it called?<input name="name" placeholder="The Wren Lab" required maxLength={200} /></label>
104
+ <label><span>Its web peer, if it has one (optional)</span><input name="peer" placeholder="wss://your-lab.example/sync" /></label>
105
+ <button type="submit">Found the lab</button>
106
+ </Ask>
107
+ </section>
108
+ <section className="card">
109
+ <h2>Join a lab</h2>
110
+ <p className="dim">Send a keeper this device's key, and paste the invitation they send back. The lab arrives once they have added you.</p>
111
+ <DeviceKey device={state.device} />
112
+ <Ask onAsk={(data) => act("join", String(data.get("invite") ?? ""))} className="stack">
113
+ <label>Invitation<input name="invite" placeholder="wss://your-lab.example/sync#…" required /></label>
114
+ <button type="submit">Join</button>
115
+ </Ask>
116
+ </section>
117
+ </div>
118
+ );
119
+ }
120
+
121
+ /** Waiting for a keeper to add this device. */
122
+ export function Waiting() {
123
+ const state = useLab();
124
+ if (state === null) return null;
125
+ return (
126
+ <section className="card narrow">
127
+ <h1>Waiting to be let in</h1>
128
+ <p>This device has asked to join a lab, and none of the lab has been sent to it yet. A keeper has to add this device's key, and then it arrives on its own.</p>
129
+ <DeviceKey device={state.device} />
130
+ <SyncStatus peer={state.peer} />
131
+ {state.peer.problem !== null && <p className="dim small">{state.peer.problem}</p>}
132
+ </section>
133
+ );
134
+ }