@bridgebrew/plugin-manager 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/browser/browser.tsx +2173 -0
- package/browser/screen/harness-profiles.tsx +1348 -0
- package/browser/screen/marketplace-model.ts +356 -0
- package/browser/screen/marketplace.tsx +1173 -0
- package/browser/screen/plugin-dependencies.tsx +139 -0
- package/browser/screen/plugin-docs.tsx +142 -0
- package/browser/screen/plugin-logs.tsx +549 -0
- package/browser/screen/plugin-parts.tsx +257 -0
- package/browser/screen/plugin-readme.tsx +152 -0
- package/browser/screen/plugin-settings.tsx +169 -0
- package/browser/screen/plugins-model.ts +427 -0
- package/browser/screen/plugins.tsx +2593 -0
- package/browser/screen/settings-action.ts +42 -0
- package/browser/screen/settings-fields.ts +414 -0
- package/browser/screen/settings-form.tsx +1669 -0
- package/browser/screen/settings-kinds.tsx +918 -0
- package/browser/screen/settings-page.tsx +72 -0
- package/browser/screen/settings-row.tsx +558 -0
- package/browser/screen/update-all.tsx +175 -0
- package/browser/tsconfig.json +15 -0
- package/package.json +61 -4
- package/tsconfig.json +5 -0
- package/README.md +0 -3
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// The pieces the Installed view (plugins.tsx) and the Marketplace view (marketplace.tsx) share: a plugin's icon in a
|
|
2
|
+
// tile, a labelled fact, the header of a details page and a row of the list.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { Icon, ListItem, Tooltip, TooltipContent, TooltipTrigger, cn } from '@bridge/ui';
|
|
5
|
+
|
|
6
|
+
export const kindIcon: Record<string, string> = {
|
|
7
|
+
application: 'layout-dashboard',
|
|
8
|
+
extension: 'puzzle',
|
|
9
|
+
service: 'plug',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** The icon the caller checked, else its kind's, in a tile; `className` sets the tile's tone and size. */
|
|
13
|
+
export function PluginGlyph({
|
|
14
|
+
icon,
|
|
15
|
+
kind,
|
|
16
|
+
size = 'md',
|
|
17
|
+
iconSize,
|
|
18
|
+
className,
|
|
19
|
+
}: {
|
|
20
|
+
icon?: string | undefined;
|
|
21
|
+
kind?: string | undefined;
|
|
22
|
+
size?: 'md' | 'lg';
|
|
23
|
+
iconSize?: number;
|
|
24
|
+
className?: string;
|
|
25
|
+
}) {
|
|
26
|
+
return (
|
|
27
|
+
<span
|
|
28
|
+
aria-hidden="true"
|
|
29
|
+
className={cn(
|
|
30
|
+
'flex shrink-0 items-center justify-center border border-border-subtle bg-surface text-secondary',
|
|
31
|
+
size === 'lg' ? 'size-14 rounded-[10px]' : 'size-9 rounded-default',
|
|
32
|
+
className,
|
|
33
|
+
)}
|
|
34
|
+
>
|
|
35
|
+
<Icon
|
|
36
|
+
name={icon ?? kindIcon[kind ?? ''] ?? 'package'}
|
|
37
|
+
size={iconSize ?? (size === 'lg' ? 26 : 17)}
|
|
38
|
+
/>
|
|
39
|
+
</span>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A labelled line of the details: label beside its value, or above it when `stacked`. */
|
|
44
|
+
export function Fact({
|
|
45
|
+
label,
|
|
46
|
+
stacked,
|
|
47
|
+
children,
|
|
48
|
+
}: {
|
|
49
|
+
label: string;
|
|
50
|
+
stacked?: boolean;
|
|
51
|
+
children: React.ReactNode;
|
|
52
|
+
}) {
|
|
53
|
+
return stacked ? (
|
|
54
|
+
<div className="flex flex-col gap-0.5 py-1.5 text-[13px] leading-5">
|
|
55
|
+
<dt className="text-xs text-muted">{label}</dt>
|
|
56
|
+
<dd className="m-0 min-w-0 break-words text-foreground">{children}</dd>
|
|
57
|
+
</div>
|
|
58
|
+
) : (
|
|
59
|
+
<div className="grid grid-cols-[112px_minmax(0,1fr)] items-start gap-3 py-1.5 text-[13px] leading-5">
|
|
60
|
+
<dt className="text-muted">{label}</dt>
|
|
61
|
+
<dd className="m-0 min-w-0 text-foreground">{children}</dd>
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The top of a details page: icon, title (and its mark), name · version with the badges, `actions` on the right, `children` below. */
|
|
67
|
+
export function PluginHeader({
|
|
68
|
+
icon,
|
|
69
|
+
title,
|
|
70
|
+
titleMark,
|
|
71
|
+
name,
|
|
72
|
+
version,
|
|
73
|
+
badges,
|
|
74
|
+
actions,
|
|
75
|
+
children,
|
|
76
|
+
}: {
|
|
77
|
+
icon: React.ReactNode;
|
|
78
|
+
title: string;
|
|
79
|
+
titleMark?: React.ReactNode;
|
|
80
|
+
name: string;
|
|
81
|
+
version: string;
|
|
82
|
+
badges?: React.ReactNode;
|
|
83
|
+
actions?: React.ReactNode;
|
|
84
|
+
children?: React.ReactNode;
|
|
85
|
+
}) {
|
|
86
|
+
return (
|
|
87
|
+
<header className="flex items-start gap-4">
|
|
88
|
+
{icon}
|
|
89
|
+
<div className="flex min-w-0 flex-1 flex-col gap-1">
|
|
90
|
+
<h2 className="flex min-w-0 items-center gap-2 text-xl font-semibold leading-7 text-foreground">
|
|
91
|
+
<span className="truncate">{title}</span>
|
|
92
|
+
{titleMark}
|
|
93
|
+
</h2>
|
|
94
|
+
<div className="flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-muted">
|
|
95
|
+
<span className="font-mono">{name}</span>
|
|
96
|
+
<span aria-hidden="true">·</span>
|
|
97
|
+
<span className="tabular-nums">v{version}</span>
|
|
98
|
+
{badges}
|
|
99
|
+
</div>
|
|
100
|
+
{children}
|
|
101
|
+
</div>
|
|
102
|
+
{actions && <div className="flex shrink-0 items-center">{actions}</div>}
|
|
103
|
+
</header>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A row of the list: icon, `title` line, `second` lines, `trailing` on the right; `children` come first (a mark). */
|
|
108
|
+
export function PluginRow({
|
|
109
|
+
icon,
|
|
110
|
+
title,
|
|
111
|
+
second,
|
|
112
|
+
trailing,
|
|
113
|
+
className,
|
|
114
|
+
children,
|
|
115
|
+
...item
|
|
116
|
+
}: Omit<React.ComponentProps<typeof ListItem>, 'title'> & {
|
|
117
|
+
icon: React.ReactNode;
|
|
118
|
+
title: React.ReactNode;
|
|
119
|
+
second: React.ReactNode;
|
|
120
|
+
trailing?: React.ReactNode;
|
|
121
|
+
}) {
|
|
122
|
+
return (
|
|
123
|
+
<ListItem
|
|
124
|
+
size="auto"
|
|
125
|
+
className={cn(
|
|
126
|
+
'group/row gap-3 border-b-0 px-3 py-2.5 aria-selected:bg-active focus-visible:bg-hover focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-accent',
|
|
127
|
+
className,
|
|
128
|
+
)}
|
|
129
|
+
{...item}
|
|
130
|
+
>
|
|
131
|
+
{children}
|
|
132
|
+
{icon}
|
|
133
|
+
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
|
|
134
|
+
{title}
|
|
135
|
+
{second}
|
|
136
|
+
</span>
|
|
137
|
+
{trailing}
|
|
138
|
+
</ListItem>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The list's width beside the details, the same for Installed and Marketplace, kept in this browser (resized with the
|
|
143
|
+
* kit's handle). */
|
|
144
|
+
export function useListWidth(): [number, (width: number) => void] {
|
|
145
|
+
const key = 'bridge.plugins.listWidth';
|
|
146
|
+
const [width, setState] = React.useState(() => {
|
|
147
|
+
try {
|
|
148
|
+
const kept = Number(localStorage.getItem(key));
|
|
149
|
+
return kept > 0 ? kept : 340;
|
|
150
|
+
} catch {
|
|
151
|
+
return 340;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
const set = (next: number) => {
|
|
155
|
+
setState(next);
|
|
156
|
+
try {
|
|
157
|
+
localStorage.setItem(key, String(next));
|
|
158
|
+
} catch {
|
|
159
|
+
/* this page only */
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
return [width, set];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The mark of a plugin published by Bridge (research-066): Built-in, or `@bridgebrew/*` from npmjs or this machine's
|
|
166
|
+
* registry. */
|
|
167
|
+
export function OfficialBadge() {
|
|
168
|
+
return (
|
|
169
|
+
<Tooltip>
|
|
170
|
+
<TooltipTrigger
|
|
171
|
+
render={
|
|
172
|
+
<span
|
|
173
|
+
data-official
|
|
174
|
+
className="inline-flex shrink-0 items-center text-accent"
|
|
175
|
+
aria-label="Official: published by Bridge"
|
|
176
|
+
/>
|
|
177
|
+
}
|
|
178
|
+
>
|
|
179
|
+
<Icon name="shield-check" size={13} />
|
|
180
|
+
</TooltipTrigger>
|
|
181
|
+
<TooltipContent>Official: published by Bridge</TooltipContent>
|
|
182
|
+
</Tooltip>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A plugin's line in either list (Installed, Marketplace), the same but for `action`: icon (blue when official, red
|
|
187
|
+
* when failing), title with the Official mark, version on the title's line, description on two lines with `action` beside it,
|
|
188
|
+
* the author below. `children` come first (a mark). */
|
|
189
|
+
export function PluginListRow({
|
|
190
|
+
icon,
|
|
191
|
+
kind,
|
|
192
|
+
title,
|
|
193
|
+
official,
|
|
194
|
+
tone,
|
|
195
|
+
description,
|
|
196
|
+
version,
|
|
197
|
+
author,
|
|
198
|
+
action,
|
|
199
|
+
children,
|
|
200
|
+
...item
|
|
201
|
+
}: Omit<React.ComponentProps<typeof ListItem>, 'title'> & {
|
|
202
|
+
icon?: string | undefined;
|
|
203
|
+
kind?: string | undefined;
|
|
204
|
+
title: string;
|
|
205
|
+
official?: boolean | undefined;
|
|
206
|
+
tone?: 'failing' | 'disabled' | undefined;
|
|
207
|
+
/** The description, or what stands for it (the package name). */
|
|
208
|
+
description: React.ReactNode;
|
|
209
|
+
version: string;
|
|
210
|
+
author?: string | undefined;
|
|
211
|
+
action: React.ReactNode;
|
|
212
|
+
}) {
|
|
213
|
+
return (
|
|
214
|
+
<PluginRow
|
|
215
|
+
{...item}
|
|
216
|
+
className="relative items-start"
|
|
217
|
+
icon={
|
|
218
|
+
<PluginGlyph
|
|
219
|
+
icon={icon}
|
|
220
|
+
kind={kind}
|
|
221
|
+
iconSize={19}
|
|
222
|
+
className={cn(
|
|
223
|
+
'size-10 rounded-default',
|
|
224
|
+
official && 'border-accent/25 bg-accent-subtle text-accent',
|
|
225
|
+
tone === 'failing' && 'border-destructive/30 bg-destructive-subtle text-destructive',
|
|
226
|
+
tone === 'disabled' && 'opacity-60',
|
|
227
|
+
)}
|
|
228
|
+
/>
|
|
229
|
+
}
|
|
230
|
+
title={
|
|
231
|
+
<span className="flex min-w-0 items-center gap-1.5">
|
|
232
|
+
<span
|
|
233
|
+
className={cn(
|
|
234
|
+
'truncate text-sm font-semibold',
|
|
235
|
+
tone === 'disabled' ? 'text-secondary' : 'text-foreground',
|
|
236
|
+
)}
|
|
237
|
+
>
|
|
238
|
+
{title}
|
|
239
|
+
</span>
|
|
240
|
+
{official && <OfficialBadge />}
|
|
241
|
+
<span className="ml-auto shrink-0 text-xs tabular-nums text-muted">{version}</span>
|
|
242
|
+
</span>
|
|
243
|
+
}
|
|
244
|
+
second={
|
|
245
|
+
<>
|
|
246
|
+
<span className="flex min-w-0 items-center gap-3">
|
|
247
|
+
<span className="flex min-w-0 flex-1 flex-col">{description}</span>
|
|
248
|
+
<span className="flex shrink-0 items-center">{action}</span>
|
|
249
|
+
</span>
|
|
250
|
+
{author && <span className="mt-1 min-w-0 truncate text-[11px] text-muted">{author}</span>}
|
|
251
|
+
</>
|
|
252
|
+
}
|
|
253
|
+
>
|
|
254
|
+
{children}
|
|
255
|
+
</PluginRow>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// A plugin's README and versions, which its page shows the same from the Installed and the Marketplace tab (research-043
|
|
2
|
+
// U2): read from `GET /_bridge/workspaces/<slug>/marketplace/detail?source=&name=`, once per page.
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import Markdown, { defaultUrlTransform, type Components } from 'react-markdown';
|
|
5
|
+
import remarkGfm from 'remark-gfm';
|
|
6
|
+
import { Badge, Skeleton, readJson } from '@bridge/ui';
|
|
7
|
+
import type { Context } from 'cordis';
|
|
8
|
+
import type { CatalogEntry } from './marketplace-model.ts';
|
|
9
|
+
|
|
10
|
+
export interface EntryDetail {
|
|
11
|
+
readme?: string;
|
|
12
|
+
versions: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The detail of `entry` (none when it has none: an installed plugin outside the catalog), read again when the catalog was
|
|
16
|
+
* (a Refresh may bring a new version or README). */
|
|
17
|
+
export function useEntryDetail(
|
|
18
|
+
ctx: Context,
|
|
19
|
+
slug: string,
|
|
20
|
+
entry: Pick<CatalogEntry, 'source' | 'name'> | undefined,
|
|
21
|
+
read: string | undefined,
|
|
22
|
+
) {
|
|
23
|
+
const [detail, setDetail] = React.useState<EntryDetail>();
|
|
24
|
+
const [error, setError] = React.useState('');
|
|
25
|
+
const source = entry?.source;
|
|
26
|
+
const name = entry?.name;
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
if (source === undefined || name === undefined) return;
|
|
29
|
+
let live = true;
|
|
30
|
+
setDetail(undefined);
|
|
31
|
+
setError('');
|
|
32
|
+
void ctx.workspace
|
|
33
|
+
.fetch(
|
|
34
|
+
`/_bridge/workspaces/${encodeURIComponent(slug)}/marketplace/detail?source=${encodeURIComponent(source)}&name=${encodeURIComponent(name)}`,
|
|
35
|
+
{ cache: 'no-store' },
|
|
36
|
+
)
|
|
37
|
+
.then(readJson)
|
|
38
|
+
.then(
|
|
39
|
+
(body) => {
|
|
40
|
+
const answer = body as EntryDetail;
|
|
41
|
+
if (live) setDetail(answer);
|
|
42
|
+
},
|
|
43
|
+
(cause: Error) => {
|
|
44
|
+
if (live) setError(cause.message);
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
return () => {
|
|
48
|
+
live = false;
|
|
49
|
+
};
|
|
50
|
+
}, [slug, source, name, read]);
|
|
51
|
+
return { detail, error };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A README in the screen's type: no raw HTML, no remote image fetched (its words instead), links in a new tab. */
|
|
55
|
+
const markdown: Components = {
|
|
56
|
+
h1: ({ children }) => <h2 className="mb-2 mt-6 text-lg font-semibold first:mt-0">{children}</h2>,
|
|
57
|
+
h2: ({ children }) => (
|
|
58
|
+
<h3 className="mb-2 mt-6 border-b border-border-subtle pb-1 text-base font-semibold first:mt-0">
|
|
59
|
+
{children}
|
|
60
|
+
</h3>
|
|
61
|
+
),
|
|
62
|
+
h3: ({ children }) => <h4 className="mb-1.5 mt-4 text-sm font-semibold">{children}</h4>,
|
|
63
|
+
p: ({ children }) => <p className="my-2! text-[13px] leading-6 text-foreground">{children}</p>,
|
|
64
|
+
ul: ({ children }) => <ul className="my-2! list-disc pl-5 text-[13px] leading-6">{children}</ul>,
|
|
65
|
+
ol: ({ children }) => (
|
|
66
|
+
<ol className="my-2! list-decimal pl-5 text-[13px] leading-6">{children}</ol>
|
|
67
|
+
),
|
|
68
|
+
a: ({ children, href }) => (
|
|
69
|
+
<a href={href} target="_blank" rel="noreferrer" className="text-accent hover:underline">
|
|
70
|
+
{children}
|
|
71
|
+
</a>
|
|
72
|
+
),
|
|
73
|
+
img: ({ alt }) => (alt ? <span className="text-muted">[{alt}]</span> : null),
|
|
74
|
+
code: ({ children, className }) =>
|
|
75
|
+
className ? (
|
|
76
|
+
<code className={className}>{children}</code>
|
|
77
|
+
) : (
|
|
78
|
+
<code className="rounded-sm bg-hover px-1 py-px font-mono text-[12px]">{children}</code>
|
|
79
|
+
),
|
|
80
|
+
pre: ({ children }) => (
|
|
81
|
+
<pre className="my-3! overflow-x-auto rounded-default border border-border-subtle bg-surface p-3 font-mono text-[12px] leading-5">
|
|
82
|
+
{children}
|
|
83
|
+
</pre>
|
|
84
|
+
),
|
|
85
|
+
table: ({ children }) => (
|
|
86
|
+
<div className="my-3 overflow-x-auto">
|
|
87
|
+
<table className="w-full border-collapse text-[13px]">{children}</table>
|
|
88
|
+
</div>
|
|
89
|
+
),
|
|
90
|
+
th: ({ children }) => (
|
|
91
|
+
<th className="border border-border-subtle px-2 py-1 text-left font-semibold">{children}</th>
|
|
92
|
+
),
|
|
93
|
+
td: ({ children }) => <td className="border border-border-subtle px-2 py-1">{children}</td>,
|
|
94
|
+
blockquote: ({ children }) => (
|
|
95
|
+
<blockquote className="my-3 border-l-2 border-border pl-3 text-secondary">
|
|
96
|
+
{children}
|
|
97
|
+
</blockquote>
|
|
98
|
+
),
|
|
99
|
+
};
|
|
100
|
+
const safeUrl = (url: string) =>
|
|
101
|
+
/^(https?:|mailto:|#)/i.test(url) ? defaultUrlTransform(url) : '';
|
|
102
|
+
|
|
103
|
+
/** The Details tab: the README, or why there is none yet. */
|
|
104
|
+
export function Readme({ detail, error }: { detail: EntryDetail | undefined; error: string }) {
|
|
105
|
+
if (error)
|
|
106
|
+
return (
|
|
107
|
+
<p role="alert" className="text-sm text-destructive">
|
|
108
|
+
The details could not be read: {error}
|
|
109
|
+
</p>
|
|
110
|
+
);
|
|
111
|
+
if (!detail)
|
|
112
|
+
return (
|
|
113
|
+
<div className="flex flex-col gap-2" aria-label="Loading">
|
|
114
|
+
<Skeleton className="h-5 w-1/3" />
|
|
115
|
+
<Skeleton className="h-4 w-full" />
|
|
116
|
+
<Skeleton className="h-4 w-5/6" />
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
if (!detail.readme) return <p className="text-[13px] text-muted">This plugin has no README.</p>;
|
|
120
|
+
return (
|
|
121
|
+
<div data-readme className="min-w-0">
|
|
122
|
+
<Markdown remarkPlugins={[remarkGfm]} components={markdown} urlTransform={safeUrl}>
|
|
123
|
+
{detail.readme}
|
|
124
|
+
</Markdown>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The Versions tab: the versions the source offers, the latest and the installed one marked. */
|
|
130
|
+
export function Versions({
|
|
131
|
+
versions,
|
|
132
|
+
latest,
|
|
133
|
+
installed,
|
|
134
|
+
}: {
|
|
135
|
+
versions: string[];
|
|
136
|
+
latest: string;
|
|
137
|
+
installed: string | undefined;
|
|
138
|
+
}) {
|
|
139
|
+
return (
|
|
140
|
+
<ul className="flex flex-col divide-y divide-border-subtle text-[13px]">
|
|
141
|
+
{versions.map((version) => (
|
|
142
|
+
<li key={version} className="flex items-center gap-2 py-1.5">
|
|
143
|
+
<span className="font-mono tabular-nums">{version}</span>
|
|
144
|
+
{version === latest && <Badge className="h-4 px-1.5 text-[11px]">latest</Badge>}
|
|
145
|
+
{version === installed && (
|
|
146
|
+
<Badge className="h-4 bg-accent-subtle px-1.5 text-[11px] text-accent">installed</Badge>
|
|
147
|
+
)}
|
|
148
|
+
</li>
|
|
149
|
+
))}
|
|
150
|
+
</ul>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// A plugin's Settings tab (research-027 P4): its fields at the workspace level, each a row as the Settings screen draws
|
|
2
|
+
// it (./settings-row.tsx), from `GET /_bridge/workspaces/<slug>/nodes` (values resolved, schemas), fetched again on the
|
|
3
|
+
// host's `nodes` events for that workspace; a change is written through `PATCH …/nodes/root` `{settings}`, as Settings
|
|
4
|
+
// and `bridge settings set|unset` do. With `slug="_host"` the values are Bridge's own, for every workspace (P10). Node
|
|
5
|
+
// levels stay in Settings (P13): `openSettings` leads there.
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import { Button, Icon, ensureOk, isRecord, readJson } from '@bridge/ui';
|
|
8
|
+
import type { Context } from 'cordis';
|
|
9
|
+
import { mineKind, originLabel, setOn, type Origin, type Schema } from './settings-fields.ts';
|
|
10
|
+
import { SettingRow } from './settings-row.tsx';
|
|
11
|
+
|
|
12
|
+
type Resolved = NonNullable<ReturnType<Context['nodes']['resolved']>>;
|
|
13
|
+
interface Tree {
|
|
14
|
+
resolved: Record<string, Resolved>;
|
|
15
|
+
schemas: Record<string, { properties?: Record<string, unknown> }>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function PluginSettings({
|
|
19
|
+
ctx,
|
|
20
|
+
slug,
|
|
21
|
+
plugin,
|
|
22
|
+
title,
|
|
23
|
+
openSettings,
|
|
24
|
+
}: {
|
|
25
|
+
ctx: Context;
|
|
26
|
+
slug: string;
|
|
27
|
+
plugin: string;
|
|
28
|
+
title: string;
|
|
29
|
+
/** The Settings screen on this plugin (for a workspace's, its node levels too). */
|
|
30
|
+
openSettings?: (() => void) | undefined;
|
|
31
|
+
}) {
|
|
32
|
+
const { workspace } = ctx;
|
|
33
|
+
const host = slug === '_host';
|
|
34
|
+
const [tree, setTree] = React.useState<Tree>();
|
|
35
|
+
const [failure, setFailure] = React.useState('');
|
|
36
|
+
const scope = React.useId();
|
|
37
|
+
const base = `/_bridge/workspaces/${encodeURIComponent(slug)}/nodes`;
|
|
38
|
+
/** Loads in the order asked: one answering after a later one was drawn draws nothing (audit L10-01); when the later
|
|
39
|
+
* one fails, the earlier one still draws. */
|
|
40
|
+
const asked = React.useRef(0);
|
|
41
|
+
const drawn = React.useRef(0);
|
|
42
|
+
const load = React.useCallback(() => {
|
|
43
|
+
const run = ++asked.current;
|
|
44
|
+
return workspace
|
|
45
|
+
.fetch(base, { cache: 'no-store' })
|
|
46
|
+
.then(readJson)
|
|
47
|
+
.then((body) => body as Tree)
|
|
48
|
+
.then(
|
|
49
|
+
(body) => {
|
|
50
|
+
if (run < drawn.current) return;
|
|
51
|
+
drawn.current = run;
|
|
52
|
+
setTree(body);
|
|
53
|
+
setFailure('');
|
|
54
|
+
},
|
|
55
|
+
(cause: Error) => {
|
|
56
|
+
if (run > drawn.current && cause.name !== 'AbortError') setFailure(cause.message);
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
}, [base]);
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
void load();
|
|
62
|
+
}, [load]);
|
|
63
|
+
React.useEffect(
|
|
64
|
+
() =>
|
|
65
|
+
ctx.hostStatus.subscribe((event) => {
|
|
66
|
+
if (event.event === 'nodes' && event.data.workspace === slug) void load();
|
|
67
|
+
}),
|
|
68
|
+
[load],
|
|
69
|
+
);
|
|
70
|
+
const save = (field: string) => async (value: unknown) => {
|
|
71
|
+
try {
|
|
72
|
+
const res = await workspace.fetch(`${base}/root`, {
|
|
73
|
+
method: 'PATCH',
|
|
74
|
+
headers: { 'content-type': 'application/json' },
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
settings: { plugin, field, ...(value !== undefined && { value }) },
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
await ensureOk(res);
|
|
80
|
+
await load();
|
|
81
|
+
return undefined;
|
|
82
|
+
} catch (cause) {
|
|
83
|
+
return (cause as Error).message;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const resolved = tree?.resolved.root;
|
|
87
|
+
const properties = tree?.schemas[plugin]?.properties;
|
|
88
|
+
// A field each user keeps for themselves (`x-bridge-mine: "client"`, the mode) is no workspace's: Settings › Mine
|
|
89
|
+
// has it, as the Settings screen's other levels leave it out (the host refuses writing it on a node).
|
|
90
|
+
const fields = isRecord(properties)
|
|
91
|
+
? Object.entries(properties).flatMap(([field, raw]) => {
|
|
92
|
+
const property = (isRecord(raw) ? raw : {}) as Schema;
|
|
93
|
+
return mineKind(property) === 'client'
|
|
94
|
+
? []
|
|
95
|
+
: [{ field, property, entry: resolved?.settings[plugin]?.values[field] }];
|
|
96
|
+
})
|
|
97
|
+
: [];
|
|
98
|
+
const invalid = resolved?.settings[plugin]?.invalid ?? [];
|
|
99
|
+
const labelOf = (origin: Origin) =>
|
|
100
|
+
host
|
|
101
|
+
? origin === 'default'
|
|
102
|
+
? 'Default'
|
|
103
|
+
: 'Set for all of Bridge'
|
|
104
|
+
: originLabel(origin, 'root', () => 'Workspace');
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<div data-plugin-settings={plugin} className="flex flex-col gap-4">
|
|
108
|
+
<p className="flex flex-wrap items-center gap-x-3 gap-y-2 text-[13px] leading-5 text-secondary">
|
|
109
|
+
<span className="min-w-0 flex-1">
|
|
110
|
+
{host
|
|
111
|
+
? 'The same in every workspace and every browser.'
|
|
112
|
+
: 'For the whole workspace. A node can set its own in Settings.'}
|
|
113
|
+
</span>
|
|
114
|
+
{openSettings && (
|
|
115
|
+
<Button size="sm" variant="outline" onClick={openSettings}>
|
|
116
|
+
<Icon name="arrow-up-right" size={13} />
|
|
117
|
+
Open in Settings
|
|
118
|
+
</Button>
|
|
119
|
+
)}
|
|
120
|
+
</p>
|
|
121
|
+
{failure && (
|
|
122
|
+
<p role="alert" className="text-sm text-destructive">
|
|
123
|
+
Settings could not be loaded: {failure}
|
|
124
|
+
</p>
|
|
125
|
+
)}
|
|
126
|
+
{tree && fields.length === 0 && (
|
|
127
|
+
<p role="note" className="text-sm text-secondary">
|
|
128
|
+
{title} has no settings{resolved?.settings[plugin] ? '' : ' while it is disabled'}.
|
|
129
|
+
</p>
|
|
130
|
+
)}
|
|
131
|
+
{resolved && fields.length > 0 && (
|
|
132
|
+
<div className="-mx-5 flex flex-col gap-1">
|
|
133
|
+
{fields.map(({ field, property, entry }) => (
|
|
134
|
+
<SettingRow
|
|
135
|
+
key={field}
|
|
136
|
+
scope={scope}
|
|
137
|
+
id={`${plugin}.${field}`}
|
|
138
|
+
field={field}
|
|
139
|
+
property={property}
|
|
140
|
+
value={entry?.value}
|
|
141
|
+
origin={entry?.origin}
|
|
142
|
+
label={entry ? labelOf(entry.origin) : ''}
|
|
143
|
+
modified={setOn(entry?.origin, 'root')}
|
|
144
|
+
readOnly={false}
|
|
145
|
+
needle=""
|
|
146
|
+
options={() => []}
|
|
147
|
+
save={save(field)}
|
|
148
|
+
/>
|
|
149
|
+
))}
|
|
150
|
+
</div>
|
|
151
|
+
)}
|
|
152
|
+
{invalid.length > 0 && (
|
|
153
|
+
<ul
|
|
154
|
+
aria-label={`Skipped ${title} values`}
|
|
155
|
+
className="flex list-none flex-col gap-1 rounded-default border border-destructive/30 bg-destructive-subtle px-3 py-2 text-xs text-destructive"
|
|
156
|
+
>
|
|
157
|
+
{invalid.map((each, index) => (
|
|
158
|
+
<li key={index} data-invalid={each.field}>
|
|
159
|
+
<span className="font-mono">
|
|
160
|
+
{each.field} = {JSON.stringify(each.value)}
|
|
161
|
+
</span>{' '}
|
|
162
|
+
skipped: it no longer fits the schema
|
|
163
|
+
</li>
|
|
164
|
+
))}
|
|
165
|
+
</ul>
|
|
166
|
+
)}
|
|
167
|
+
</div>
|
|
168
|
+
);
|
|
169
|
+
}
|