@biolibs/bionet-panels 0.2.0 → 0.3.1

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/places.tsx ADDED
@@ -0,0 +1,170 @@
1
+ "use client";
2
+
3
+ /** The places: where things are kept, which is inside which, and putting a tube somewhere. */
4
+ import type { ItemState, PlaceState } from "@biolibs/bionet";
5
+ import { useAct, useLab } from "@biolibs/bionet-react";
6
+ import { slotFrom, slotName } from "@biolibs/bionet-space";
7
+ import { Ask } from "./parts.js";
8
+
9
+ /** The kinds of place offered when adding one. Any other kind a lab's log holds is shown as it is. */
10
+ export const PLACE_KINDS = ["room", "freezer", "fridge", "shelf", "rack", "box", "bench", "instrument"] as const;
11
+
12
+ /**
13
+ * Where a place is, said the way a person would: every place it is inside, then itself.
14
+ *
15
+ * @param places - The lab's places.
16
+ * @param id - The place.
17
+ * @returns Something like `Main lab › −80 › Shelf 2 › Box 7`, or `""` if the lab has no such place.
18
+ */
19
+ export function placePath(places: readonly Pick<PlaceState, "place" | "name" | "parent">[], id: string): string {
20
+ const names: string[] = [];
21
+ const passed = new Set<string>();
22
+ for (let at: string | null = id; at !== null && !passed.has(at);) {
23
+ const one = places.find((each) => each.place === at);
24
+ if (one === undefined) break;
25
+ passed.add(at);
26
+ names.unshift(one.name);
27
+ at = one.parent;
28
+ }
29
+ return names.join(" › ");
30
+ }
31
+
32
+ /**
33
+ * Where an item is, in words.
34
+ *
35
+ * @param item - The item.
36
+ * @param places - The lab's places.
37
+ * @returns Something like `−80 › Box 7 · C4`, or `nowhere recorded`.
38
+ */
39
+ export function whereWords(item: Pick<ItemState, "where">, places: readonly Pick<PlaceState, "place" | "name" | "parent">[]): string {
40
+ if (item.where === null) return "nowhere recorded";
41
+ const path = placePath(places, item.where.place) || "a place the lab no longer has";
42
+ return item.where.slot === null ? path : `${path} · ${slotName(item.where.slot)}`;
43
+ }
44
+
45
+ /**
46
+ * The places in the order a tree is read: each one followed by what is inside it.
47
+ *
48
+ * @param places - The lab's places.
49
+ * @returns Each place still in use, with how deep it is.
50
+ */
51
+ export function placeTree(places: readonly PlaceState[]): { place: PlaceState; depth: number }[] {
52
+ const out: { place: PlaceState; depth: number }[] = [];
53
+ const walk = (parent: string | null, depth: number): void => {
54
+ for (const place of places) {
55
+ if (place.parent !== parent || place.retired) continue;
56
+ out.push({ place, depth });
57
+ walk(place.place, depth + 1);
58
+ }
59
+ };
60
+ walk(null, 0);
61
+ return out;
62
+ }
63
+
64
+ /** Every place as a tree, what needs looking at, and a form to add a place. Draws nothing until the lab has arrived. */
65
+ export function Places() {
66
+ const lab = useLab()?.lab;
67
+ const act = useAct();
68
+ if (lab === null || lab === undefined) return null;
69
+ const tree = placeTree(lab.places);
70
+ const live = lab.items.filter((item) => !item.retired);
71
+ const holding = (id: string): number => live.filter((item) => item.where?.place === id).length;
72
+ return (
73
+ <>
74
+ {lab.crowded.length > 0 && (
75
+ <section className="card">
76
+ <h2>Somebody needs to look</h2>
77
+ <p className="dim small">The lab has more than one tube recorded in each of these positions. Two people put them there without knowing of each other. Move whichever is not really there.</p>
78
+ <ul className="plainlist">
79
+ {lab.crowded.map((one) => (
80
+ <li key={`${one.place} ${one.slot.row} ${one.slot.column}`} className="flagged">
81
+ <strong>{placePath(lab.places, one.place)} · {slotName(one.slot)}</strong> <span className="badge">{one.items.length} tubes</span>
82
+ </li>
83
+ ))}
84
+ </ul>
85
+ </section>
86
+ )}
87
+ <section className="card">
88
+ <h2>Where things are kept</h2>
89
+ {tree.length === 0 ? <p className="empty">No places yet. Add the first below: a freezer, then the boxes in it.</p> : (
90
+ <table>
91
+ <thead><tr><th>Place</th><th>Kind</th><th>Positions</th><th>Tubes here</th><th /></tr></thead>
92
+ <tbody>
93
+ {tree.map(({ place, depth }) => (
94
+ <tr key={place.place} className={place.adrift ? "flagged" : undefined}>
95
+ <td style={{ paddingLeft: `${0.6 + depth * 1.2}rem` }}>
96
+ {place.name}
97
+ {place.adrift && <span className="badge" title="Two people each moved this place and another into one another. Move one of them to settle it.">needs moving</span>}
98
+ </td>
99
+ <td className="dim">{place.kind}</td>
100
+ <td className="dim">{place.grid === null ? "" : `${place.grid.rows} × ${place.grid.columns}`}</td>
101
+ <td>{holding(place.place) || ""}</td>
102
+ <td><button type="button" className="plain" onClick={() => void act("retirePlace", { place: place.place })}>retire</button></td>
103
+ </tr>
104
+ ))}
105
+ </tbody>
106
+ </table>
107
+ )}
108
+ </section>
109
+ <section className="card">
110
+ <h2>Add a place</h2>
111
+ <p className="dim small">A place is anything something is kept in or on. Only a box has positions; give it rows and columns and its tubes can be found by letter and number.</p>
112
+ <Ask className="stack" onAsk={(data) => {
113
+ const rows = Number(data.get("rows") ?? "");
114
+ const columns = Number(data.get("columns") ?? "");
115
+ const parent = String(data.get("parent") ?? "");
116
+ return act("addPlace", { kind: String(data.get("kind") ?? ""), name: String(data.get("name") ?? ""), parent: parent === "" ? null : parent,
117
+ grid: rows > 0 && columns > 0 ? { rows, columns } : null });
118
+ }}>
119
+ <label>Kind<select name="kind" defaultValue="box">{PLACE_KINDS.map((kind) => <option key={kind} value={kind}>{kind}</option>)}</select></label>
120
+ <label>Name<input name="name" placeholder="Box 7" required maxLength={200} /></label>
121
+ <label>Inside
122
+ <select name="parent" defaultValue="">
123
+ <option value="">nothing</option>
124
+ {tree.map(({ place, depth }) => <option key={place.place} value={place.place}>{" ".repeat(depth)}{place.name}</option>)}
125
+ </select>
126
+ </label>
127
+ <div className="row tight">
128
+ <input name="rows" type="number" min={1} max={100} step={1} placeholder="Rows" aria-label="Rows" className="num" />
129
+ <input name="columns" type="number" min={1} max={100} step={1} placeholder="Columns" aria-label="Columns" className="num" />
130
+ <span className="dim small">for a box</span>
131
+ </div>
132
+ <button type="submit">Add place</button>
133
+ </Ask>
134
+ </section>
135
+ </>
136
+ );
137
+ }
138
+
139
+ /**
140
+ * Where one tube is, and a way to put it somewhere.
141
+ *
142
+ * @param props - `item` is the tube.
143
+ */
144
+ export function PutItem({ item }: { item: ItemState }) {
145
+ const lab = useLab()?.lab;
146
+ const act = useAct();
147
+ if (lab === null || lab === undefined) return null;
148
+ const tree = placeTree(lab.places);
149
+ return (
150
+ <div className="where">
151
+ <span className={item.where === null ? "dim small" : "small"}>{whereWords(item, lab.places)}</span>
152
+ {tree.length > 0 && (
153
+ <Ask className="row tight" onAsk={async (data) => {
154
+ const place = String(data.get("place") ?? "");
155
+ const written = String(data.get("slot") ?? "").trim();
156
+ const slot = written === "" ? null : slotFrom(written);
157
+ // A position that is not written like one is sent as it is not: the lab refuses it and says why.
158
+ return act("putItem", { item: item.item, place: place === "" ? null : place, slot: written !== "" && slot === null ? { row: -1, column: -1 } : slot });
159
+ }}>
160
+ <select name="place" defaultValue={item.where?.place ?? ""} aria-label="Place">
161
+ <option value="">nowhere</option>
162
+ {tree.map(({ place, depth }) => <option key={place.place} value={place.place}>{" ".repeat(depth)}{place.name}</option>)}
163
+ </select>
164
+ <input name="slot" placeholder="A1" aria-label="Position" className="num" maxLength={5} />
165
+ <button type="submit" className="quiet">Put</button>
166
+ </Ask>
167
+ )}
168
+ </div>
169
+ );
170
+ }
package/src/window.tsx CHANGED
@@ -10,34 +10,51 @@
10
10
  * It walks a person through the three places they can be: nobody has said who is at this device,
11
11
  * the device is in no lab, the device is waiting to be let into one — and then the lab itself.
12
12
  */
13
- import { useState } from "react";
13
+ import { useState, type ReactNode } from "react";
14
14
  import { Logo, Mark, useAct, useLab } from "@biolibs/bionet-react";
15
15
  import { Freezer } from "./freezer.js";
16
16
  import { History } from "./history.js";
17
17
  import { Ask, CopyContext, DeviceKey, SyncStatus, Trouble } from "./parts.js";
18
+ import { RestoreKey, RestoreLab, SaveContext } from "./backup.js";
18
19
  import { People } from "./people.js";
20
+ import { Places } from "./places.js";
19
21
  import { Sharing } from "./sharing.js";
20
22
 
21
- /** The four things a member looks at. */
22
- export const TABS = ["freezer", "people", "history", "sharing"] as const;
23
+ /** The things a member looks at. */
24
+ export const TABS = ["freezer", "places", "people", "history", "sharing"] as const;
23
25
 
24
26
  /** One of them. */
25
27
  export type Tab = (typeof TABS)[number];
26
28
 
29
+ /** A view a host adds to the window beside its own, such as a drawing of the lab. */
30
+ export interface ExtraTab {
31
+ /** What tells it apart. */
32
+ id: string;
33
+ /** What its tab says. */
34
+ label: string;
35
+ /** What it shows. It is inside the lab's provider, so it may use the hooks. */
36
+ view: ReactNode;
37
+ }
38
+
27
39
  /**
28
40
  * The window.
29
41
  *
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.
42
+ * @param props - `tab` is which view to open on; `copy` puts words on the clipboard and `save` hands
43
+ * over a file, for a host that cannot use the browser's own way; `extra` are views the host adds,
44
+ * shown after the window's own.
32
45
  */
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>;
46
+ export function LabWindow({ tab: first = "freezer", copy, save, extra = [] }: {
47
+ tab?: Tab | string; copy?: (words: string) => Promise<void>; save?: (name: string, text: string) => Promise<boolean | void>; extra?: readonly ExtraTab[];
48
+ }) {
49
+ let window = <Body first={first} extra={extra} />;
50
+ if (save !== undefined) window = <SaveContext.Provider value={save}>{window}</SaveContext.Provider>;
51
+ if (copy !== undefined) window = <CopyContext.Provider value={copy}>{window}</CopyContext.Provider>;
52
+ return <div className="bionet">{window}</div>;
36
53
  }
37
54
 
38
- function Body({ first }: { first: Tab }) {
55
+ function Body({ first, extra }: { first: string; extra: readonly ExtraTab[] }) {
39
56
  const state = useLab();
40
- const [tab, setTab] = useState<Tab>(first);
57
+ const [tab, setTab] = useState<string>(first);
41
58
  if (state === null) return <main className="centre"><p className="dim">Opening the lab…</p></main>;
42
59
  if (state.device.person === null) return <main className="centre"><Logo /><Trouble /><Who /></main>;
43
60
  if (state.lab === null) return <main className="centre"><Logo /><Trouble /><NoLab /></main>;
@@ -46,6 +63,7 @@ function Body({ first }: { first: Tab }) {
46
63
  const lab = state.lab;
47
64
  const label: Record<Tab, string> = {
48
65
  freezer: `Freezer · ${lab.items.filter((item) => !item.retired).length}`,
66
+ places: `Places · ${lab.places.filter((place) => !place.retired).length}`,
49
67
  people: `People · ${lab.roster.filter((key) => key.removed === null).length}`,
50
68
  history: `History · ${lab.entries.length}`,
51
69
  sharing: "Sharing",
@@ -64,13 +82,16 @@ function Body({ first }: { first: Tab }) {
64
82
  </header>
65
83
  <nav>
66
84
  {TABS.map((one) => <button key={one} type="button" className={one === tab ? "tab on" : "tab"} onClick={() => setTab(one)}>{label[one]}</button>)}
85
+ {extra.map((one) => <button key={one.id} type="button" className={one.id === tab ? "tab on" : "tab"} onClick={() => setTab(one.id)}>{one.label}</button>)}
67
86
  </nav>
68
87
  <main>
69
88
  <Trouble />
70
89
  {tab === "freezer" && <Freezer />}
90
+ {tab === "places" && <Places />}
71
91
  {tab === "people" && <People />}
72
92
  {tab === "history" && <History />}
73
93
  {tab === "sharing" && <Sharing />}
94
+ {extra.find((one) => one.id === tab)?.view}
74
95
  </main>
75
96
  </div>
76
97
  );
@@ -115,6 +136,8 @@ export function NoLab() {
115
136
  <label>Invitation<input name="invite" placeholder="wss://your-lab.example/sync#…" required /></label>
116
137
  <button type="submit">Join</button>
117
138
  </Ask>
139
+ <RestoreKey />
140
+ <RestoreLab />
118
141
  </section>
119
142
  </div>
120
143
  );