@biolibs/bionet-panels 0.0.0-stage → 0.2.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/README.md +89 -2
- package/dist/freezer.d.ts +21 -0
- package/dist/freezer.d.ts.map +1 -0
- package/dist/freezer.js +32 -0
- package/dist/freezer.js.map +1 -0
- package/dist/history.d.ts +14 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +48 -0
- package/dist/history.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/parts.d.ts +36 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +61 -0
- package/dist/parts.js.map +1 -0
- package/dist/people.d.ts +3 -0
- package/dist/people.d.ts.map +1 -0
- package/dist/people.js +15 -0
- package/dist/people.js.map +1 -0
- package/dist/public.d.ts +77 -0
- package/dist/public.d.ts.map +1 -0
- package/dist/public.js +73 -0
- package/dist/public.js.map +1 -0
- package/dist/sharing.d.ts +12 -0
- package/dist/sharing.d.ts.map +1 -0
- package/dist/sharing.js +35 -0
- package/dist/sharing.js.map +1 -0
- package/dist/window.d.ts +21 -0
- package/dist/window.d.ts.map +1 -0
- package/dist/window.js +72 -0
- package/dist/window.js.map +1 -0
- package/package.json +55 -4
- package/panels.css +61 -0
- package/src/freezer.tsx +101 -0
- package/src/history.tsx +70 -0
- package/src/index.ts +30 -0
- package/src/parts.tsx +80 -0
- package/src/people.tsx +52 -0
- package/src/public.tsx +175 -0
- package/src/sharing.tsx +71 -0
- package/src/window.tsx +136 -0
package/src/parts.tsx
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/** The small pieces the panels are made of. */
|
|
4
|
+
import { createContext, useContext, useState, type FormEvent, type ReactNode } from "react";
|
|
5
|
+
import type { LabSnapshot, PeerView } from "@biolibs/bionet-client";
|
|
6
|
+
import { useTrouble } from "@biolibs/bionet-react";
|
|
7
|
+
|
|
8
|
+
/** How to put words on the clipboard. A host that cannot use the browser's own way supplies one. */
|
|
9
|
+
export const CopyContext = createContext<(words: string) => Promise<void>>((words) => navigator.clipboard.writeText(words));
|
|
10
|
+
|
|
11
|
+
/** A form that asks for one thing and clears itself if it worked. */
|
|
12
|
+
export function Ask({ onAsk, children, className }: { onAsk: (data: FormData) => Promise<{ ok: boolean }>; children: ReactNode; className?: string }) {
|
|
13
|
+
const [busy, setBusy] = useState(false);
|
|
14
|
+
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
|
15
|
+
event.preventDefault();
|
|
16
|
+
const form = event.currentTarget;
|
|
17
|
+
setBusy(true);
|
|
18
|
+
const { ok } = await onAsk(new FormData(form));
|
|
19
|
+
setBusy(false);
|
|
20
|
+
if (ok) form.reset();
|
|
21
|
+
};
|
|
22
|
+
return <form className={className} onSubmit={submit} aria-busy={busy}><fieldset disabled={busy}>{children}</fieldset></form>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A button that copies some words and says that it did. */
|
|
26
|
+
export function Copy({ words, label = "Copy" }: { words: string; label?: string }) {
|
|
27
|
+
const copy = useContext(CopyContext);
|
|
28
|
+
const [done, setDone] = useState(false);
|
|
29
|
+
return (
|
|
30
|
+
<button type="button" className="quiet" onClick={() => { void copy(words).then(() => { setDone(true); setTimeout(() => setDone(false), 1500); }); }}>
|
|
31
|
+
{done ? "Copied" : label}
|
|
32
|
+
</button>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The last change that was refused, with a way to put it away. Draws nothing when there is none. */
|
|
37
|
+
export function Trouble() {
|
|
38
|
+
const { trouble, dismiss } = useTrouble();
|
|
39
|
+
if (trouble === null) return null;
|
|
40
|
+
return <p role="alert" className="trouble">{trouble}<button type="button" className="plain" onClick={dismiss}>dismiss</button></p>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How the connection to the lab's peer is going, in the words a person would want.
|
|
45
|
+
*
|
|
46
|
+
* @param peer - The connection.
|
|
47
|
+
* @returns One line.
|
|
48
|
+
*/
|
|
49
|
+
export function syncWords(peer: PeerView): string {
|
|
50
|
+
const waiting = peer.pending === 0 ? "" : ` · ${peer.pending} ${peer.pending === 1 ? "change" : "changes"} to send`;
|
|
51
|
+
if (peer.url === null) return "No peer set · working on this device only";
|
|
52
|
+
switch (peer.status) {
|
|
53
|
+
case "synced": return "In step with the lab";
|
|
54
|
+
case "syncing": return "Syncing…";
|
|
55
|
+
case "connecting": return `Reaching the lab's peer…${waiting}`;
|
|
56
|
+
case "waiting": return "Waiting to be let in";
|
|
57
|
+
default: return `Offline${waiting}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A dot and a line saying how the connection to the lab's peer is going. */
|
|
62
|
+
export function SyncStatus({ peer }: { peer: PeerView }) {
|
|
63
|
+
return (
|
|
64
|
+
<div className="sync" title={peer.problem ?? peer.url ?? ""}>
|
|
65
|
+
<span className={`dot ${peer.url === null ? "offline" : peer.status}`} />
|
|
66
|
+
<span>{syncWords(peer)}</span>
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** This device's key, to be sent to a keeper. */
|
|
72
|
+
export function DeviceKey({ device }: { device: LabSnapshot["device"] }) {
|
|
73
|
+
return (
|
|
74
|
+
<div className="keybox">
|
|
75
|
+
<div><span className="label">This device's key</span><Copy words={device.key} label="Copy key" /></div>
|
|
76
|
+
<code className="wrap">{device.key}</code>
|
|
77
|
+
<p className="dim small">Fingerprint <code>{device.fingerprint}</code>. It is safe to send: it lets a lab recognise this device, and nothing else.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
package/src/people.tsx
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/** The people: who is in the lab, and letting somebody in. */
|
|
4
|
+
import { useAct, useLab } from "@biolibs/bionet-react";
|
|
5
|
+
import { Ask, Copy } from "./parts.js";
|
|
6
|
+
|
|
7
|
+
/** The roster, and for a keeper the way to add somebody. Draws nothing until the lab has arrived. */
|
|
8
|
+
export function People() {
|
|
9
|
+
const state = useLab();
|
|
10
|
+
const act = useAct();
|
|
11
|
+
const lab = state?.lab;
|
|
12
|
+
if (state === null || lab === null || lab === undefined) return null;
|
|
13
|
+
return (
|
|
14
|
+
<>
|
|
15
|
+
<section className="card">
|
|
16
|
+
<h2>Who is in the lab</h2>
|
|
17
|
+
<table>
|
|
18
|
+
<thead><tr><th>Person</th><th>Role</th><th>Device</th><th /></tr></thead>
|
|
19
|
+
<tbody>
|
|
20
|
+
{lab.roster.map((one) => (
|
|
21
|
+
<tr key={one.key} className={one.removed === null ? undefined : "gone"}>
|
|
22
|
+
<td>{one.person}{one.you && <span className="badge calm">this device</span>}</td>
|
|
23
|
+
<td>{one.removed !== null ? "removed" : one.founder ? "keeper · founder" : one.role}</td>
|
|
24
|
+
<td><code>{one.fingerprint}</code></td>
|
|
25
|
+
<td>{one.removed !== null && <span className="dim small">log kept up to entry {one.removed.cutoff + 1}</span>}</td>
|
|
26
|
+
</tr>
|
|
27
|
+
))}
|
|
28
|
+
</tbody>
|
|
29
|
+
</table>
|
|
30
|
+
<p className="dim small">A device is known by its key. One person with two computers appears twice, once for each.</p>
|
|
31
|
+
</section>
|
|
32
|
+
{lab.keeper ? (
|
|
33
|
+
<section className="card">
|
|
34
|
+
<h2>Add somebody</h2>
|
|
35
|
+
<p className="dim small">Ask them for their device's key, add it here, and send them the invitation. Their app fills in once this change has reached the lab's peer.</p>
|
|
36
|
+
<Ask onAsk={(data) => act("addMember", { key: String(data.get("key") ?? ""), person: String(data.get("person") ?? ""), role: String(data.get("role") ?? "member") })} className="stack">
|
|
37
|
+
<label>Their device's key<input name="key" placeholder="43 letters and digits" required spellCheck={false} /></label>
|
|
38
|
+
<label>Their name<input name="person" required maxLength={200} /></label>
|
|
39
|
+
<label>As<select name="role"><option value="member">a member, who can change the freezer</option><option value="keeper">a keeper, who can also change who is in the lab</option></select></label>
|
|
40
|
+
<button type="submit">Add to the lab</button>
|
|
41
|
+
</Ask>
|
|
42
|
+
{state.peer.invite !== null ? (
|
|
43
|
+
<div className="keybox">
|
|
44
|
+
<div><span className="label">Invitation to send them</span><Copy words={state.peer.invite} label="Copy invitation" /></div>
|
|
45
|
+
<code className="wrap">{state.peer.invite}</code>
|
|
46
|
+
</div>
|
|
47
|
+
) : <p className="dim small">Set the lab's web peer under Sharing to have an invitation to send.</p>}
|
|
48
|
+
</section>
|
|
49
|
+
) : <p className="dim small">Only a keeper can change who is in the lab.</p>}
|
|
50
|
+
</>
|
|
51
|
+
);
|
|
52
|
+
}
|
package/src/public.tsx
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a lab shows the world, and what it can see of another lab.
|
|
3
|
+
*
|
|
4
|
+
* These are for a lab's public page. They take what was fetched and checked as plain values and
|
|
5
|
+
* draw it; none of them fetches, signs or holds state, so they can be drawn on a server.
|
|
6
|
+
*
|
|
7
|
+
* They also carry the wording. What a signature settles, and what it does not, has to be said the
|
|
8
|
+
* same way on every lab's page — a page that words it itself will sooner or later claim more than
|
|
9
|
+
* the checks support.
|
|
10
|
+
*/
|
|
11
|
+
import type { ReactNode } from "react";
|
|
12
|
+
import { settled, type FetchedInventory, type InventoryPart } from "@biolibs/bionet";
|
|
13
|
+
import type { SignatureCheck } from "@biolibs/opencloning";
|
|
14
|
+
import { Mark } from "@biolibs/bionet-react";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The parts in one list whose checksums are not in another.
|
|
18
|
+
*
|
|
19
|
+
* @param mine - What this lab holds.
|
|
20
|
+
* @param theirs - What the other lab holds.
|
|
21
|
+
* @returns What they have that this lab does not. Names play no part: a part is its checksum.
|
|
22
|
+
*/
|
|
23
|
+
export function onlyTheirs(mine: readonly InventoryPart[], theirs: readonly InventoryPart[]): InventoryPart[] {
|
|
24
|
+
const have = new Set(mine.map((part) => part.checksum));
|
|
25
|
+
return theirs.filter((part) => !have.has(part.checksum));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The parts two lists both hold, by checksum.
|
|
30
|
+
*
|
|
31
|
+
* @param mine - What this lab holds.
|
|
32
|
+
* @param theirs - What the other lab holds.
|
|
33
|
+
* @returns This lab's entries for the parts both have.
|
|
34
|
+
*/
|
|
35
|
+
export function heldByBoth(mine: readonly InventoryPart[], theirs: readonly InventoryPart[]): InventoryPart[] {
|
|
36
|
+
const have = new Set(theirs.map((part) => part.checksum));
|
|
37
|
+
return mine.filter((part) => have.has(part.checksum));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The frame of a lab's public page: the mark, the lab's name, and a line about what it is.
|
|
42
|
+
*
|
|
43
|
+
* @param props - `name` and `note` are the lab's; `children` is the rest of the page.
|
|
44
|
+
*/
|
|
45
|
+
export function LabPage({ name, note, children }: { name: string; note?: string | null; children: ReactNode }) {
|
|
46
|
+
return (
|
|
47
|
+
<div className="bionet">
|
|
48
|
+
<main className="page">
|
|
49
|
+
<div className="title">
|
|
50
|
+
<Mark size={40} />
|
|
51
|
+
<h1>{name}</h1>
|
|
52
|
+
</div>
|
|
53
|
+
{note !== null && note !== undefined && <p className="dim">{note}</p>}
|
|
54
|
+
<p className="small dim">
|
|
55
|
+
This lab publishes what it holds at{" "}
|
|
56
|
+
<a href="/.well-known/bionet.json"><code>/.well-known/bionet.json</code></a>, signed, so that a
|
|
57
|
+
cloning plan can be acted on and not just read. Parts are named by checksum, because a name
|
|
58
|
+
agrees with nothing.
|
|
59
|
+
</p>
|
|
60
|
+
{children}
|
|
61
|
+
</main>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A table of parts: name, length, and whether each is on offer. `checksums` adds that column. */
|
|
67
|
+
export function PartsTable({ parts, checksums = false }: { parts: readonly InventoryPart[]; checksums?: boolean }) {
|
|
68
|
+
return (
|
|
69
|
+
<table>
|
|
70
|
+
<thead><tr><th>Part</th><th>Length</th>{checksums && <th>Checksum</th>}<th>On offer</th></tr></thead>
|
|
71
|
+
<tbody>
|
|
72
|
+
{parts.map((part) => (
|
|
73
|
+
<tr key={part.checksum}>
|
|
74
|
+
<td>{part.name}{part.note !== null && <><br /><span className="dim small">{part.note}</span></>}</td>
|
|
75
|
+
<td className="nowrap"><code>{part.length} bp{part.circular ? " ○" : ""}</code></td>
|
|
76
|
+
{checksums && <td className="dim"><code>{part.checksum.slice(0, 22)}…</code></td>}
|
|
77
|
+
<td className={part.available ? "good" : "bad"}>{part.available ? "yes" : "no"}</td>
|
|
78
|
+
</tr>
|
|
79
|
+
))}
|
|
80
|
+
</tbody>
|
|
81
|
+
</table>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* What this lab holds, and what the signature over that list settles.
|
|
87
|
+
*
|
|
88
|
+
* @param props - `parts` is the published list; `checks` is one check per signature over it;
|
|
89
|
+
* `origin` is the address this page is served from; `bound` is whether the list names that address.
|
|
90
|
+
*/
|
|
91
|
+
export function Held({ parts, checks, origin, bound }: {
|
|
92
|
+
parts: readonly InventoryPart[]; checks: readonly SignatureCheck[]; origin: string; bound: boolean;
|
|
93
|
+
}) {
|
|
94
|
+
const holds = checks.length > 0 && checks.every((check) => check.ok);
|
|
95
|
+
return (
|
|
96
|
+
<section>
|
|
97
|
+
<h2>What we hold</h2>
|
|
98
|
+
{parts.length === 0 ? <p className="dim">Nothing is listed at the moment.</p> : <PartsTable parts={parts} checksums />}
|
|
99
|
+
<p className="small dim">
|
|
100
|
+
{checks.length === 0 ? "This list is not signed, so it claims nothing about who published it." : (
|
|
101
|
+
<>
|
|
102
|
+
Signed by {checks.map((check) => <code key={check.public_key}>{check.fingerprint} </code>)}
|
|
103
|
+
{holds ? "— the signature holds over this list as served." : "— the signature does not hold."}
|
|
104
|
+
</>
|
|
105
|
+
)}{" "}
|
|
106
|
+
Bound to <code>{origin}</code>: {bound ? "yes" : "no"}.
|
|
107
|
+
</p>
|
|
108
|
+
</section>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The caveat every reader is owed, in the same words everywhere. */
|
|
113
|
+
export function WhatASignatureSettles() {
|
|
114
|
+
return (
|
|
115
|
+
<p className="small dim">
|
|
116
|
+
None of that says this lab is who it says it is. The address does that work, and only as far as
|
|
117
|
+
whoever typed it already trusted it. What a signature settles is that this list has not changed
|
|
118
|
+
since it was published, and that it was published by whoever holds that key.
|
|
119
|
+
</p>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Another lab that answered, beside what this lab holds.
|
|
125
|
+
*
|
|
126
|
+
* @param props - `found` is what was fetched from the other lab and checked; `mine` is this lab's list.
|
|
127
|
+
*/
|
|
128
|
+
export function PeerAnswered({ found, mine }: { found: FetchedInventory; mine: readonly InventoryPart[] }) {
|
|
129
|
+
const theirs = found.signed.inventory.parts;
|
|
130
|
+
const only = onlyTheirs(mine, theirs);
|
|
131
|
+
const both = heldByBoth(mine, theirs);
|
|
132
|
+
return (
|
|
133
|
+
<>
|
|
134
|
+
<p>
|
|
135
|
+
<strong>{found.signed.inventory.lab.name}</strong> answered at <code>{found.origin}</code> with{" "}
|
|
136
|
+
{theirs.length} {theirs.length === 1 ? "part" : "parts"}.{" "}
|
|
137
|
+
{settled(found)
|
|
138
|
+
? <span className="good">Its signature holds and it was served from the address it names.</span>
|
|
139
|
+
: <span className="bad">Its signature or its address does not check out, so nothing below is settled.</span>}
|
|
140
|
+
</p>
|
|
141
|
+
<WhatASignatureSettles />
|
|
142
|
+
<h3>What they have that we do not</h3>
|
|
143
|
+
{only.length === 0 ? <p className="small">Nothing. Everything they list, we hold too.</p> : <PartsTable parts={only} />}
|
|
144
|
+
<h3>What we both have</h3>
|
|
145
|
+
{both.length === 0 ? <p className="small">Nothing in common, by checksum.</p> : (
|
|
146
|
+
<p className="small">
|
|
147
|
+
{both.map((part) => <code key={part.checksum}>{part.name} </code>)}
|
|
148
|
+
— matched by checksum, so it does not matter what either of us calls them.
|
|
149
|
+
</p>
|
|
150
|
+
)}
|
|
151
|
+
<p className="small dim">
|
|
152
|
+
Fetched from that lab when this page was drawn, not copied. Nothing here fetches a part: finding
|
|
153
|
+
out who has a plasmid is as far as software goes, and the rest is an email and a padded envelope.
|
|
154
|
+
</p>
|
|
155
|
+
</>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Another lab that did not answer.
|
|
161
|
+
*
|
|
162
|
+
* @param props - `address` is where it was asked for; `why` is what went wrong.
|
|
163
|
+
*/
|
|
164
|
+
export function PeerSilent({ address, why }: { address: string; why: string }) {
|
|
165
|
+
return (
|
|
166
|
+
<>
|
|
167
|
+
<p>We point at <code>{address}</code> and it did not answer.</p>
|
|
168
|
+
<p className="small dim">
|
|
169
|
+
The reason given was: {why}. A lab that cannot be reached is not a lab that is lying — it may be
|
|
170
|
+
down, or moved, or never have existed. All this page can honestly say is that the address did
|
|
171
|
+
not answer when it was asked.
|
|
172
|
+
</p>
|
|
173
|
+
</>
|
|
174
|
+
);
|
|
175
|
+
}
|
package/src/sharing.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/** Sharing: what other labs are shown, and the lab's web peer. */
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import type { ItemState, PartState } from "@biolibs/bionet";
|
|
6
|
+
import { useAct, useLab } from "@biolibs/bionet-react";
|
|
7
|
+
import { Ask, Copy } from "./parts.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The parts other labs would be shown: those with a tube that has not been retired and is not empty.
|
|
11
|
+
*
|
|
12
|
+
* @param parts - Every part.
|
|
13
|
+
* @param items - Every item.
|
|
14
|
+
* @returns The parts that would be listed, in order.
|
|
15
|
+
*/
|
|
16
|
+
export function listedParts(parts: readonly PartState[], items: readonly ItemState[]): PartState[] {
|
|
17
|
+
const inStock = new Set(items.filter((item) => !item.retired && item.amount > 0).map((item) => item.part));
|
|
18
|
+
return parts.filter((part) => inStock.has(part.checksum));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** What would be published, a button to publish it, and the peer's settings. Draws nothing until the lab has arrived. */
|
|
22
|
+
export function Sharing() {
|
|
23
|
+
const state = useLab();
|
|
24
|
+
const act = useAct();
|
|
25
|
+
const [said, setSaid] = useState<string | null>(null);
|
|
26
|
+
const lab = state?.lab;
|
|
27
|
+
if (state === null || lab === null || lab === undefined) return null;
|
|
28
|
+
const listed = listedParts(lab.parts, lab.items);
|
|
29
|
+
const publish = async () => {
|
|
30
|
+
setSaid("Publishing…");
|
|
31
|
+
const answer = await act("publish");
|
|
32
|
+
const result = answer.ok ? (answer.result as { ok: boolean; reason: string | null; parts: number }) : { ok: false, reason: answer.error, parts: 0 };
|
|
33
|
+
setSaid(result.ok ? `Published: ${result.parts} ${result.parts === 1 ? "part" : "parts"}, signed by this device.` : `Not published. ${result.reason ?? ""}`);
|
|
34
|
+
};
|
|
35
|
+
return (
|
|
36
|
+
<>
|
|
37
|
+
<section className="card">
|
|
38
|
+
<h2>What other labs are shown</h2>
|
|
39
|
+
<p className="dim small">Only this: which parts the lab has in stock, and whether each can be asked for. Tubes, amounts and who did what stay inside the lab.</p>
|
|
40
|
+
{listed.length === 0 ? <p className="dim">Nothing would be listed: no part has a tube with anything in it.</p> : (
|
|
41
|
+
<table>
|
|
42
|
+
<thead><tr><th>Part</th><th>Length</th><th>Other labs may ask for it</th></tr></thead>
|
|
43
|
+
<tbody>{listed.map((part) => <tr key={part.checksum}><td>{part.name}</td><td>{part.length} bp</td><td>{part.offered ? "yes" : "no"}</td></tr>)}</tbody>
|
|
44
|
+
</table>
|
|
45
|
+
)}
|
|
46
|
+
<div className="row">
|
|
47
|
+
<button type="button" onClick={() => void publish()} disabled={state.peer.url === null}>Publish this</button>
|
|
48
|
+
{said !== null && <span className="small">{said}</span>}
|
|
49
|
+
</div>
|
|
50
|
+
<p className="dim small">
|
|
51
|
+
{state.peer.origin === null ? "Set the lab's web peer below to have somewhere to publish." : <>Published at <code>{state.peer.origin}/.well-known/bionet.json</code>, signed by this device's key.</>}
|
|
52
|
+
{state.published !== null && <> Last published from this device {new Date(state.published).toLocaleString()}.</>}
|
|
53
|
+
</p>
|
|
54
|
+
</section>
|
|
55
|
+
<section className="card">
|
|
56
|
+
<h2>The lab's web peer</h2>
|
|
57
|
+
<p className="dim small">A server at the lab's address that holds a copy of the lab and passes changes between members who are never online together. It has no key, so it cannot change anything.</p>
|
|
58
|
+
<Ask onAsk={(data) => act("setPeer", String(data.get("peer") ?? ""))} className="row">
|
|
59
|
+
<input name="peer" placeholder="wss://your-lab.example/sync" defaultValue={state.peer.url ?? ""} spellCheck={false} />
|
|
60
|
+
<button type="submit" className="quiet">Use this peer</button>
|
|
61
|
+
</Ask>
|
|
62
|
+
{state.peer.problem !== null && <p className="note">{state.peer.problem}</p>}
|
|
63
|
+
<div className="keybox">
|
|
64
|
+
<div><span className="label">This lab's ID</span>{lab.id !== null && <Copy words={lab.id} label="Copy ID" />}</div>
|
|
65
|
+
<code className="wrap">{lab.id}</code>
|
|
66
|
+
<p className="dim small">Whoever runs the lab's peer sets this as <code>BIONET_LAB</code>, so that it serves this lab and no other.</p>
|
|
67
|
+
</div>
|
|
68
|
+
</section>
|
|
69
|
+
</>
|
|
70
|
+
);
|
|
71
|
+
}
|
package/src/window.tsx
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A whole lab application, in one component.
|
|
5
|
+
*
|
|
6
|
+
* It holds no state of its own about the lab. It is told the lab as it stands, every time it
|
|
7
|
+
* changes, and draws it; a button asks for one thing to be done and waits to be told how the lab
|
|
8
|
+
* changed. So what is on screen is always what the signed logs say, and never what a form hoped.
|
|
9
|
+
*
|
|
10
|
+
* It walks a person through the three places they can be: nobody has said who is at this device,
|
|
11
|
+
* the device is in no lab, the device is waiting to be let into one — and then the lab itself.
|
|
12
|
+
*/
|
|
13
|
+
import { useState } from "react";
|
|
14
|
+
import { Logo, Mark, useAct, useLab } from "@biolibs/bionet-react";
|
|
15
|
+
import { Freezer } from "./freezer.js";
|
|
16
|
+
import { History } from "./history.js";
|
|
17
|
+
import { Ask, CopyContext, DeviceKey, SyncStatus, Trouble } from "./parts.js";
|
|
18
|
+
import { People } from "./people.js";
|
|
19
|
+
import { Sharing } from "./sharing.js";
|
|
20
|
+
|
|
21
|
+
/** The four things a member looks at. */
|
|
22
|
+
export const TABS = ["freezer", "people", "history", "sharing"] as const;
|
|
23
|
+
|
|
24
|
+
/** One of them. */
|
|
25
|
+
export type Tab = (typeof TABS)[number];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The window.
|
|
29
|
+
*
|
|
30
|
+
* @param props - `tab` is which view to open on; `copy` puts words on the clipboard, for a host that
|
|
31
|
+
* cannot use the browser's own way.
|
|
32
|
+
*/
|
|
33
|
+
export function LabWindow({ tab: first = "freezer", copy }: { tab?: Tab; copy?: (words: string) => Promise<void> }) {
|
|
34
|
+
const window = <Body first={first} />;
|
|
35
|
+
return <div className="bionet">{copy === undefined ? window : <CopyContext.Provider value={copy}>{window}</CopyContext.Provider>}</div>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function Body({ first }: { first: Tab }) {
|
|
39
|
+
const state = useLab();
|
|
40
|
+
const [tab, setTab] = useState<Tab>(first);
|
|
41
|
+
if (state === null) return <main className="centre"><p className="dim">Opening the lab…</p></main>;
|
|
42
|
+
if (state.device.person === null) return <main className="centre"><Logo /><Trouble /><Who /></main>;
|
|
43
|
+
if (state.lab === null) return <main className="centre"><Logo /><Trouble /><NoLab /></main>;
|
|
44
|
+
if (!state.lab.member) return <main className="centre"><Logo /><Trouble /><Waiting /></main>;
|
|
45
|
+
|
|
46
|
+
const lab = state.lab;
|
|
47
|
+
const label: Record<Tab, string> = {
|
|
48
|
+
freezer: `Freezer · ${lab.items.filter((item) => !item.retired).length}`,
|
|
49
|
+
people: `People · ${lab.roster.filter((key) => key.removed === null).length}`,
|
|
50
|
+
history: `History · ${lab.entries.length}`,
|
|
51
|
+
sharing: "Sharing",
|
|
52
|
+
};
|
|
53
|
+
return (
|
|
54
|
+
<div className="shell">
|
|
55
|
+
<header>
|
|
56
|
+
<div className="title">
|
|
57
|
+
<Mark size={34} />
|
|
58
|
+
<div>
|
|
59
|
+
<h1>{lab.name ?? "A lab"}</h1>
|
|
60
|
+
<p className="dim small">{state.device.person} · {lab.keeper ? "keeper" : "member"} · this device <code>{state.device.fingerprint}</code></p>
|
|
61
|
+
</div>
|
|
62
|
+
</div>
|
|
63
|
+
<SyncStatus peer={state.peer} />
|
|
64
|
+
</header>
|
|
65
|
+
<nav>
|
|
66
|
+
{TABS.map((one) => <button key={one} type="button" className={one === tab ? "tab on" : "tab"} onClick={() => setTab(one)}>{label[one]}</button>)}
|
|
67
|
+
</nav>
|
|
68
|
+
<main>
|
|
69
|
+
<Trouble />
|
|
70
|
+
{tab === "freezer" && <Freezer />}
|
|
71
|
+
{tab === "people" && <People />}
|
|
72
|
+
{tab === "history" && <History />}
|
|
73
|
+
{tab === "sharing" && <Sharing />}
|
|
74
|
+
</main>
|
|
75
|
+
</div>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Asking who is at this device, before anything else. */
|
|
80
|
+
export function Who() {
|
|
81
|
+
const act = useAct();
|
|
82
|
+
return (
|
|
83
|
+
<section className="card narrow">
|
|
84
|
+
<h1>Who is at this device?</h1>
|
|
85
|
+
<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>
|
|
86
|
+
<Ask onAsk={(data) => act("setPerson", String(data.get("name") ?? ""))} className="row">
|
|
87
|
+
<input name="name" placeholder="Your name" autoFocus required maxLength={200} />
|
|
88
|
+
<button type="submit">Continue</button>
|
|
89
|
+
</Ask>
|
|
90
|
+
</section>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Founding a lab, or joining one. */
|
|
95
|
+
export function NoLab() {
|
|
96
|
+
const state = useLab();
|
|
97
|
+
const act = useAct();
|
|
98
|
+
if (state === null) return null;
|
|
99
|
+
return (
|
|
100
|
+
<div className="pair">
|
|
101
|
+
<section className="card">
|
|
102
|
+
<h2>Found a lab</h2>
|
|
103
|
+
<p className="dim">You become its first keeper. It works on this device straight away, with or without a network.</p>
|
|
104
|
+
<Ask onAsk={(data) => act("found", { name: String(data.get("name") ?? ""), peer: String(data.get("peer") ?? "") })} className="stack">
|
|
105
|
+
<label>What is it called?<input name="name" placeholder="The Wren Lab" required maxLength={200} /></label>
|
|
106
|
+
<label><span>Its web peer, if it has one (optional)</span><input name="peer" placeholder="wss://your-lab.example/sync" /></label>
|
|
107
|
+
<button type="submit">Found the lab</button>
|
|
108
|
+
</Ask>
|
|
109
|
+
</section>
|
|
110
|
+
<section className="card">
|
|
111
|
+
<h2>Join a lab</h2>
|
|
112
|
+
<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>
|
|
113
|
+
<DeviceKey device={state.device} />
|
|
114
|
+
<Ask onAsk={(data) => act("join", String(data.get("invite") ?? ""))} className="stack">
|
|
115
|
+
<label>Invitation<input name="invite" placeholder="wss://your-lab.example/sync#…" required /></label>
|
|
116
|
+
<button type="submit">Join</button>
|
|
117
|
+
</Ask>
|
|
118
|
+
</section>
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Waiting for a keeper to add this device. */
|
|
124
|
+
export function Waiting() {
|
|
125
|
+
const state = useLab();
|
|
126
|
+
if (state === null) return null;
|
|
127
|
+
return (
|
|
128
|
+
<section className="card narrow">
|
|
129
|
+
<h1>Waiting to be let in</h1>
|
|
130
|
+
<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>
|
|
131
|
+
<DeviceKey device={state.device} />
|
|
132
|
+
<SyncStatus peer={state.peer} />
|
|
133
|
+
{state.peer.problem !== null && <p className="dim small">{state.peer.problem}</p>}
|
|
134
|
+
</section>
|
|
135
|
+
);
|
|
136
|
+
}
|