@fungi.computer/oyster 0.14.1 → 0.15.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.
@@ -1,16 +1,146 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useCallback, useEffect, useId, useRef, useState } from "react";
3
+ import { Disclosure } from "@fungi.computer/caps/components/disclosure";
4
+ import { Alert } from "@fungi.computer/caps/components/alert";
3
5
  import { Button } from "@fungi.computer/caps/components/button";
4
6
  import { Card, CardContent } from "@fungi.computer/caps/components/card";
5
- import { Checkbox } from "@fungi.computer/caps/components/checkbox";
6
7
  import { DataList, DataRow } from "@fungi.computer/caps/components/data-list";
7
8
  import { Form, FormFooter } from "@fungi.computer/caps/components/field";
8
- import { IconCheckbox } from "@fungi.computer/caps/components/icon-checkbox";
9
+ import { Switch } from "@fungi.computer/caps/components/switch";
10
+ import { LoadingState } from "@fungi.computer/caps/blocks/loading-state";
11
+ import { PermissionGrid, } from "@fungi.computer/caps/blocks/permission-grid";
9
12
  import { Status } from "@fungi.computer/caps/components/status";
10
- import { Display, ScreenCell, ScreenCells, } from "@fungi.computer/caps/components/surface";
11
13
  import { Sigil } from "@fungi.computer/sigils";
12
14
  import { permissionCatalog } from "../apps/catalog.js";
13
15
  import { draftOf, editPermissions, permissionScreen, planGrantChange, rebaseDraft, } from "../apps/permissions.js";
16
+ const groupLabels = {
17
+ computers: "Computers",
18
+ agents: "Agents",
19
+ generation: "Generation",
20
+ bazaar: "Bazaar",
21
+ data: "Data",
22
+ workflows: "Workflows",
23
+ git: "Git",
24
+ channels: "Channels",
25
+ apps: "Apps",
26
+ shell: "Shell",
27
+ other: "Other",
28
+ };
29
+ const gridHeadings = {
30
+ capability: "Permission",
31
+ state: "State",
32
+ scope: "Applies to",
33
+ control: "Change",
34
+ };
35
+ /** "publishDraft" and "publish_draft" read as "Publish draft". */
36
+ function methodLabel(operation) {
37
+ const words = operation
38
+ .replace(/([a-z0-9])([A-Z])/gu, "$1 $2")
39
+ .replace(/[._-]+/gu, " ")
40
+ .trim()
41
+ .toLowerCase();
42
+ return words.charAt(0).toUpperCase() + words.slice(1);
43
+ }
44
+ /** One grid row per grant: a label, its state in words and a labelled switch. */
45
+ function grantRow(context, input) {
46
+ return {
47
+ id: input.id,
48
+ mark: _jsx(Sigil, { name: input.sigil }),
49
+ label: input.label,
50
+ state: input.on ? "allowed" : "denied",
51
+ group: input.chip,
52
+ stateLabel: input.stateLabel ?? (input.on ? "On" : "Off"),
53
+ scope: input.scopeName,
54
+ control: (_jsx(Switch, { id: input.pending ? `grant-${input.id}` : undefined, "aria-label": `${input.scopeName}: ${input.label}`, "aria-describedby": context.describedBy(input.capability), checked: input.on, disabled: context.disabled || input.locked, onCheckedChange: input.onChange })),
55
+ };
56
+ }
57
+ /** The rows for one resource (or "all") of a scoped group. */
58
+ function resourceRows(context, group, resource) {
59
+ const key = resource.resource === "all"
60
+ ? "all"
61
+ : `${resource.resource.kind}:${resource.resource.id}`;
62
+ return resource.cells.flatMap((cell) => {
63
+ const column = group.columns.find((item) => item.capability === cell.capability);
64
+ if (column === undefined)
65
+ return [];
66
+ const id = `${key}:${cell.capability}`;
67
+ return [
68
+ grantRow(context, {
69
+ id,
70
+ capability: cell.capability,
71
+ sigil: column.sigil,
72
+ label: column.label,
73
+ chip: groupLabels[group.id],
74
+ scopeName: resource.label,
75
+ stateLabel: cell.state === "via-all" ? "On for all" : undefined,
76
+ pending: column.pending && cell.state === "off",
77
+ on: cell.state !== "off",
78
+ locked: cell.state === "via-all",
79
+ onChange: (on) => {
80
+ context.edit(resource.resource === "all"
81
+ ? { kind: "all", capability: cell.capability, on }
82
+ : {
83
+ kind: "resource",
84
+ capability: cell.capability,
85
+ resource: resource.resource,
86
+ on,
87
+ });
88
+ },
89
+ }),
90
+ ];
91
+ });
92
+ }
93
+ /** Switches and App-call operations, which apply to the App rather than a resource. */
94
+ function appRows(context, group) {
95
+ return [
96
+ ...group.switches.map((column) => {
97
+ const locked = permissionCatalog[column.capability].consent === "affordance";
98
+ return grantRow(context, {
99
+ id: column.capability,
100
+ capability: column.capability,
101
+ sigil: column.sigil,
102
+ label: column.label,
103
+ chip: groupLabels[group.id],
104
+ scopeName: "This App",
105
+ stateLabel: locked ? "Always on" : undefined,
106
+ pending: column.pending && !column.on,
107
+ on: column.on,
108
+ locked,
109
+ onChange: (on) => {
110
+ context.edit({ kind: "switch", capability: column.capability, on });
111
+ },
112
+ });
113
+ }),
114
+ ...group.operations.map((operation) => grantRow(context, {
115
+ id: `${operation.capability}:${operation.operation}`,
116
+ capability: operation.capability,
117
+ sigil: permissionCatalog["apps.call"].sigil,
118
+ label: methodLabel(operation.operation),
119
+ chip: groupLabels[group.id],
120
+ scopeName: "Other Apps",
121
+ pending: false,
122
+ on: operation.on,
123
+ locked: false,
124
+ onChange: (on) => {
125
+ context.edit({
126
+ kind: "operation",
127
+ capability: operation.capability,
128
+ operation: operation.operation,
129
+ on,
130
+ });
131
+ },
132
+ })),
133
+ ];
134
+ }
135
+ /** Every row of a group: its resources' grants, then the App's own switches. */
136
+ function groupRows(context, group) {
137
+ return [
138
+ ...group.rows
139
+ .filter((resource) => resource.cells.some((cell) => cell.state !== "via-all"))
140
+ .flatMap((resource) => resourceRows(context, group, resource)),
141
+ ...appRows(context, group),
142
+ ];
143
+ }
14
144
  /** One editor; each App owns its draft and pending-request lifetime on reuse. */
15
145
  export function AppPermissionsView(props) {
16
146
  return _jsx(PermissionEditor, { ...props }, props.appKey);
@@ -23,10 +153,13 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
23
153
  const [retired, setRetired] = useState(false);
24
154
  const [refreshNeeded, setRefreshNeeded] = useState(false);
25
155
  const [message, setMessage] = useState(null);
156
+ const [saved, setSaved] = useState(false);
26
157
  const [loadError, setLoadError] = useState(false);
27
158
  const [reload, setReload] = useState(0);
159
+ const [otherOpen, setOtherOpen] = useState(false);
160
+ const asked = useRef(null);
161
+ const describeId = useId();
28
162
  const mounted = useRef(true);
29
- const legendId = useId();
30
163
  const retire = useCallback(() => {
31
164
  setRetired(true);
32
165
  setUncertain(false);
@@ -71,7 +204,8 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
71
204
  if (!mounted.current)
72
205
  return;
73
206
  setRefreshNeeded(false);
74
- setMessage("Permissions saved.");
207
+ setSaved(true);
208
+ setMessage("Permissions saved");
75
209
  }
76
210
  catch (cause) {
77
211
  console.error("App permissions surrounding-screen refresh failed", cause);
@@ -97,6 +231,7 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
97
231
  return;
98
232
  setSaving(true);
99
233
  setRefreshNeeded(false);
234
+ setSaved(false);
100
235
  setMessage(null);
101
236
  try {
102
237
  if (!uncertain) {
@@ -166,7 +301,7 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
166
301
  setReload((value) => value + 1);
167
302
  }, children: "Retry permissions" })] }));
168
303
  if (view === null)
169
- return _jsx("p", { role: "status", children: retired ? message : "Loading permissions…" });
304
+ return retired ? (_jsx("p", { role: "status", children: message })) : (_jsx(LoadingState, { message: "Loading permissions\u2026" }));
170
305
  const authority = {
171
306
  ...view.permissions.authority,
172
307
  approvedGrants: [...draft].flatMap(([capability, grant]) => grant === null
@@ -194,12 +329,25 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
194
329
  const shellChanged = changes.some((change) => change.capability === "team.workbenchHost");
195
330
  const groups = screen.groups.filter((group) => group.id !== "shell");
196
331
  const pending = baseline.pending.filter((capability) => capability !== "team.workbenchHost");
197
- return (_jsxs(Form, { style: { maxWidth: "46rem" }, onSubmit: (event) => {
332
+ const context = {
333
+ disabled,
334
+ describedBy: (capability) => `${describeId}-${capability}`,
335
+ edit,
336
+ };
337
+ const rowsOf = (items) => items.flatMap((group) => groupRows(context, group));
338
+ const needsOk = (group) => [...group.columns, ...group.switches].some((column) => pending.some((item) => item === column.capability));
339
+ // The split is fixed on first sight, so rows never move under the cursor.
340
+ asked.current ??= new Set(groups.filter(needsOk).map((group) => group.id));
341
+ const askedIds = asked.current;
342
+ const first = rowsOf(askedIds.size > 0 ? groups.filter((g) => askedIds.has(g.id)) : groups);
343
+ const later = askedIds.size > 0 ? rowsOf(groups.filter((g) => !askedIds.has(g.id))) : [];
344
+ const showFooter = (groups.length > 0 && !shellChanged) || uncertain || refreshNeeded;
345
+ return (_jsxs(Form, { style: { maxWidth: "var(--container-2xl)", marginInline: "auto" }, onSubmit: (event) => {
198
346
  event.preventDefault();
199
347
  void save();
200
- }, children: [!screen.canEdit && _jsx("p", { children: "Only an owner can change these permissions." }), pending.length > 0 && (_jsxs("p", { children: ["Needs approval:", " ", pending
201
- .map((capability) => permissionCatalog[capability].label)
202
- .join(", ")] })), shell !== undefined && (_jsx(ShellPermissionRow, { column: shell, changed: shellChanged, otherChanges: changes.length > 1, disabled: disabled, onToggle: () => {
348
+ }, children: [!screen.canEdit && _jsx("p", { children: "Only an owner can change these permissions." }), pending.length > 0 && (_jsx(Alert, { tone: "warning", title: `${String(pending.length)} ${pending.length === 1 ? "permission needs" : "permissions need"} your OK`, action: _jsx(Button, { type: "button", size: "xs", variant: "outline", onClick: () => {
349
+ document.querySelector('[id^="grant-"]')?.focus();
350
+ }, children: "Review" }), children: "Turn on what you want this App to do, then save." })), shell !== undefined && (_jsx(ShellPermissionRow, { column: shell, changed: shellChanged, otherChanges: changes.length > 1, disabled: disabled, onToggle: () => {
203
351
  edit({
204
352
  kind: "switch",
205
353
  capability: "team.workbenchHost",
@@ -208,49 +356,16 @@ function PermissionEditor({ port, appKey, onAccepted, onStateChange, }) {
208
356
  }, onCancel: () => {
209
357
  setDraft((current) => new Map(current).set("team.workbenchHost", draftOf(view.permissions.authority).get("team.workbenchHost") ??
210
358
  null));
211
- } })), groups.length > 0 && (_jsx(Display, { size: "sm", "aria-label": `${view.appName} permissions`, children: _jsx(ScreenCells, { appearance: "compact", children: groups.map((group) => (_jsxs("section", { "aria-label": `${group.id} permissions`, children: [_jsx("h3", { children: group.id }), group.rows.map((row) => (_jsx(ScreenCell, { appearance: "compact", icon: _jsx(Sigil, { name: group.scope === "computer" ? "computer" : "listAgents" }), legend: row.label, children: row.cells.map((cell) => {
212
- const column = group.columns.find((item) => item.capability === cell.capability);
213
- if (column === undefined)
214
- return null;
215
- return (_jsx(IconCheckbox, { appearance: "compact", icon: _jsx(Sigil, { name: column.sigil }), caption: column.caption, label: `${row.label}: ${column.label}`, "aria-describedby": `${legendId}-${cell.capability}`, checked: cell.state !== "off", disabled: disabled || cell.state === "via-all", onChange: (event) => {
216
- edit(row.resource === "all"
217
- ? {
218
- kind: "all",
219
- capability: cell.capability,
220
- on: event.target.checked,
221
- }
222
- : {
223
- kind: "resource",
224
- capability: cell.capability,
225
- resource: row.resource,
226
- on: event.target.checked,
227
- });
228
- } }, cell.capability));
229
- }) }, row.resource === "all"
230
- ? "all"
231
- : `${row.resource.kind}:${row.resource.id}`))), group.switches.map((column) => (_jsx(ScreenCell, { appearance: "compact", legend: column.label, children: _jsx(IconCheckbox, { appearance: "compact", icon: _jsx(Sigil, { name: column.sigil }), caption: column.caption, label: column.label, "aria-describedby": `${legendId}-${column.capability}`, checked: column.on, disabled: disabled ||
232
- permissionCatalog[column.capability].consent ===
233
- "affordance", onChange: (event) => {
234
- edit({
235
- kind: "switch",
236
- capability: column.capability,
237
- on: event.target.checked,
238
- });
239
- } }) }, column.capability))), group.operations.map((operation) => (_jsxs("label", { children: [_jsx(Checkbox, { checked: operation.on, disabled: disabled, onChange: (event) => {
240
- edit({
241
- kind: "operation",
242
- capability: operation.capability,
243
- operation: operation.operation,
244
- on: event.target.checked,
245
- });
246
- } }), operation.operation] }, operation.operation))), _jsx("dl", { children: [...group.columns, ...group.switches].map((column) => (_jsxs("div", { id: `${legendId}-${column.capability}`, children: [_jsx("dt", { children: column.label }), _jsxs("dd", { children: [column.description, column.warning !== null && _jsx("p", { children: column.warning })] })] }, column.capability))) })] }, group.id))) }) })), screen.groups.length === 0 && _jsx("p", { children: "This App requests no permissions." }), message !== null && (_jsx("p", { role: "status", "aria-live": "polite", children: message })), ((groups.length > 0 && !shellChanged) || uncertain || refreshNeeded) && (_jsxs(FormFooter, { divider: false, children: [refreshNeeded && (_jsx(Button, { type: "button", size: "xs", variant: "outline", disabled: saving, onClick: () => void retryRefresh(), children: "Retry screen refresh" })), _jsx(Button, { type: "submit", size: "xs", variant: "outline", disabled: saving ||
247
- retired ||
248
- !screen.canEdit ||
249
- (!uncertain && changes.length === 0), children: saving
359
+ } })), first.length > 0 && (_jsx(PermissionGrid, { caption: later.length > 0
360
+ ? `${view.appName} asks for`
361
+ : `${view.appName} permissions`, headings: gridHeadings, rows: first })), later.length > 0 && (_jsx(Disclosure, { open: otherOpen, onOpenChange: setOtherOpen, summary: `Other permissions · ${String(later.length)}`, children: _jsx(PermissionGrid, { caption: "Already decided", headings: gridHeadings, rows: later }) })), _jsx("div", { className: "sr-only", children: groups
362
+ .flatMap((group) => [...group.columns, ...group.switches])
363
+ .map((column) => (_jsxs("p", { id: `${describeId}-${column.capability}`, children: [column.description, column.warning === null ? "" : ` ${column.warning}`] }, column.capability))) }), screen.groups.length === 0 && _jsx("p", { children: "This App requests no permissions." }), message !== null && !showFooter && (_jsx("p", { role: "status", "aria-live": "polite", children: message })), showFooter && (_jsxs(FormFooter, { divider: false, children: [message !== null &&
364
+ (saved && !uncertain && !refreshNeeded ? (_jsx(Status, { role: "status", size: "compact", glyph: "dot", tone: "success", label: message })) : (_jsx("span", { role: "status", children: message }))), refreshNeeded && (_jsx(Button, { type: "button", size: "xs", variant: "outline", disabled: saving, onClick: () => void retryRefresh(), children: "Retry screen refresh" })), (changes.length > 0 || saving || uncertain) && (_jsx(Button, { type: "submit", variant: "primary", disabled: saving || retired || !screen.canEdit, children: saving
250
365
  ? "Saving…"
251
366
  : uncertain
252
367
  ? "Review current permissions"
253
- : "Save permissions" })] }))] }));
368
+ : "Save permissions" }))] }))] }));
254
369
  }
255
370
  /** Shell is a single approval, not a resource/verb matrix. Confirmation still
256
371
  * submits the editor's whole draft through its one atomic save. */