@bevel-software/platform-core-frontend 0.37.0 → 0.39.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/package.json +2 -2
- package/src/core/CoreAppShell.tsx +22 -10
- package/src/core/__tests__/AppChrome.test.tsx +27 -10
- package/src/core/__tests__/ShellRoutes.test.tsx +43 -2
- package/src/core/registry.ts +43 -2
- package/src/core/shell-paths.ts +30 -0
- package/src/lib/api.ts +7 -1
- package/src/modules/auth/__tests__/LoginScreen.test.tsx +9 -0
- package/src/modules/auth/__tests__/useAuthState.test.ts +92 -0
- package/src/modules/auth/components/LoginScreen.tsx +2 -0
- package/src/modules/auth/hooks/useAuthState.ts +4 -1
- package/src/modules/auth/index.ts +2 -0
- package/src/modules/auth/state/accountStorage.ts +32 -0
- package/src/modules/auth/state/tokenAccount.ts +53 -0
- package/src/modules/layout/index.ts +1 -1
- package/src/modules/settings/components/SettingsLayout.tsx +52 -9
- package/src/modules/settings/components/SettingsNav.tsx +24 -3
- package/src/modules/settings/components/__tests__/SettingsLayout.test.tsx +266 -5
- package/src/modules/settings/index.ts +1 -1
- package/src/modules/settings/settings-nav-items.tsx +130 -10
- package/src/modules/toolbar/commands/__tests__/actions.test.tsx +20 -5
- package/src/modules/toolbar/commands/actions.tsx +1 -0
- package/src/modules/toolbar/components/ProfileMenu.tsx +13 -1
- package/src/modules/toolbar/components/Toolbar.tsx +3 -2
- package/src/modules/toolbar/components/__tests__/ProfileMenu.app-sections.test.tsx +87 -0
- package/src/modules/toolbar/components/__tests__/ProfileMenu.test.tsx +1 -0
- package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +2 -1
- package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.tools.test.tsx +80 -32
- package/src/modules/workspace/components/renderers/__tests__/htmlToolBridge.test.ts +78 -8
- package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +10 -3
- package/src/modules/workspace/components/renderers/__tests__/pageConsentMemory.test.ts +177 -0
- package/src/modules/workspace/components/renderers/__tests__/useHtmlToolBridge.test.tsx +249 -23
- package/src/modules/workspace/components/renderers/htmlToolBridge.ts +52 -16
- package/src/modules/workspace/components/renderers/pageConsentMemory.ts +75 -0
- package/src/modules/workspace/components/renderers/useHtmlToolBridge.tsx +46 -11
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'react';
|
|
2
2
|
import { Outlet, useLocation } from 'react-router-dom';
|
|
3
3
|
import { AdminContext } from '../../admin';
|
|
4
|
-
import type
|
|
4
|
+
import { useAppRegistry, type AdminMenuItem } from '../../../core/registry';
|
|
5
5
|
import { SidebarFrame } from '../../layout/components/SidebarFrame';
|
|
6
6
|
import { useMediaQuery } from '../../layout';
|
|
7
|
+
import { AuthContext } from '../../auth';
|
|
8
|
+
import type { AuthUser } from '@bevel-software/platform-shared';
|
|
7
9
|
import {
|
|
8
10
|
isSettingsNavPath,
|
|
9
11
|
normalizeSettingsPath,
|
|
12
|
+
registrySettingsPages,
|
|
10
13
|
useMenuSections,
|
|
11
14
|
} from '../settings-nav-items';
|
|
12
15
|
import { SettingsNav, type LinkableItem } from './SettingsNav';
|
|
@@ -51,7 +54,8 @@ import { SettingsNav, type LinkableItem } from './SettingsNav';
|
|
|
51
54
|
*/
|
|
52
55
|
/**
|
|
53
56
|
* A row can be shown only if it declares where it goes AND that destination is
|
|
54
|
-
* one of the routes this layout wraps
|
|
57
|
+
* one of the routes this layout wraps: one of core's pages, or the row's own
|
|
58
|
+
* page mounted here.
|
|
55
59
|
*
|
|
56
60
|
* The first half excludes dialog rows — a dialog has no URL, so it can be
|
|
57
61
|
* neither linked to nor marked current. The second excludes a registry row
|
|
@@ -69,12 +73,32 @@ import { SettingsNav, type LinkableItem } from './SettingsNav';
|
|
|
69
73
|
* in, and a registry row that happened to declare `/secrets/` would otherwise
|
|
70
74
|
* render a link that never matches the page it is on.
|
|
71
75
|
*/
|
|
72
|
-
function linkable(items: AdminMenuItem[]): LinkableItem[] {
|
|
76
|
+
function linkable(items: AdminMenuItem[], mountedPages: ReadonlySet<string>): LinkableItem[] {
|
|
73
77
|
return items
|
|
74
|
-
.filter((item): item is LinkableItem => !!item.path
|
|
78
|
+
.filter((item): item is LinkableItem => !!item.path)
|
|
79
|
+
// A row that does something when picked belongs to the dropdown: there its
|
|
80
|
+
// action wins over its `path`, and a link here would go around the action.
|
|
81
|
+
.filter((item) => !item.dialog && !item.onSelect)
|
|
82
|
+
// Which rows are links depends on whose address the path is. At one of
|
|
83
|
+
// core's addresses: a row that points there, and never a row that brought
|
|
84
|
+
// a page of its own (it lost the address to core's page). Anywhere else:
|
|
85
|
+
// only the row whose page is the one mounted there (`registrySettingsPages`
|
|
86
|
+
// decides), so a row that lost the address, or merely names it, is not a
|
|
87
|
+
// link to somebody else's page under its own name.
|
|
88
|
+
.filter((item) => (isSettingsNavPath(item.path) ? !item.page : mountedPages.has(item.id)))
|
|
75
89
|
.map((item) => ({ ...item, path: normalizeSettingsPath(item.path) }));
|
|
76
90
|
}
|
|
77
91
|
|
|
92
|
+
/**
|
|
93
|
+
* The rows offered to this person (`AdminMenuItem.isShown`): with nobody
|
|
94
|
+
* signed in, none of the rows that ask who is. Asked on every render, never
|
|
95
|
+
* memoized, as the profile menu asks: a row may come and go with state the
|
|
96
|
+
* layout does not own (a role learnt after sign-in, a preference in storage).
|
|
97
|
+
*/
|
|
98
|
+
function offered<T extends AdminMenuItem>(items: T[], user: AuthUser | null): T[] {
|
|
99
|
+
return items.filter((item) => !item.isShown || (user !== null && item.isShown(user)));
|
|
100
|
+
}
|
|
101
|
+
|
|
78
102
|
export function SettingsLayout() {
|
|
79
103
|
// Normalized, because the row paths are — see `normalizeSettingsPath`. The
|
|
80
104
|
// page renders at `/secrets/` either way; without this its row is the only
|
|
@@ -82,16 +106,33 @@ export function SettingsLayout() {
|
|
|
82
106
|
const currentPath = normalizeSettingsPath(useLocation().pathname);
|
|
83
107
|
// Not `useAdmin()` — see (2) above.
|
|
84
108
|
const isAdmin = useContext(AdminContext)?.isAdmin ?? false;
|
|
85
|
-
|
|
109
|
+
// The same reading for the person: the nav is wayfinding, and nobody signed in is nobody a role's rows are for.
|
|
110
|
+
const user = useContext(AuthContext)?.user ?? null;
|
|
111
|
+
const { defaultItems, adminItems, appSections } = useMenuSections();
|
|
86
112
|
/* Below `md` the sidebar frame is not mounted AT ALL and the rows become a
|
|
87
113
|
horizontal strip. A 212px rail on a 375px screen is most of the screen. */
|
|
88
114
|
const isCompact = useMediaQuery('(max-width: 767px)');
|
|
89
115
|
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
116
|
+
// The registry's rows whose page is mounted, by id: the rows that are links at an address that is not core's.
|
|
117
|
+
const registry = useAppRegistry();
|
|
118
|
+
const mountedPages = useMemo(() => new Set(registrySettingsPages(registry).map((row) => row.id)), [registry]);
|
|
119
|
+
|
|
120
|
+
const linkDefault = useMemo(() => linkable(defaultItems, mountedPages), [defaultItems, mountedPages]);
|
|
121
|
+
const linkAdmin = useMemo(
|
|
122
|
+
() => (isAdmin ? linkable(adminItems, mountedPages) : []),
|
|
123
|
+
[adminItems, isAdmin, mountedPages],
|
|
124
|
+
);
|
|
125
|
+
const linkSections = useMemo(
|
|
126
|
+
() => appSections.map((section) => ({ id: section.id, label: section.label, items: linkable(section.items, mountedPages) })),
|
|
127
|
+
[appSections, mountedPages],
|
|
94
128
|
);
|
|
129
|
+
// Who each row is offered to is asked now, on this render (see `offered`).
|
|
130
|
+
const navDefault = offered(linkDefault, user);
|
|
131
|
+
const navAdmin = offered(linkAdmin, user);
|
|
132
|
+
// The app's own sections ("Infrastructure only"), each with the pages offered to this person; one with none is left out.
|
|
133
|
+
const navSections = linkSections
|
|
134
|
+
.map((section) => ({ ...section, items: offered(section.items, user) }))
|
|
135
|
+
.filter((section) => section.items.length > 0);
|
|
95
136
|
|
|
96
137
|
if (isCompact) {
|
|
97
138
|
return (
|
|
@@ -106,6 +147,7 @@ export function SettingsLayout() {
|
|
|
106
147
|
>
|
|
107
148
|
<SettingsNav
|
|
108
149
|
defaultItems={navDefault}
|
|
150
|
+
sections={navSections}
|
|
109
151
|
adminItems={navAdmin}
|
|
110
152
|
currentPath={currentPath}
|
|
111
153
|
orientation="strip"
|
|
@@ -123,6 +165,7 @@ export function SettingsLayout() {
|
|
|
123
165
|
<SidebarFrame label="Settings">
|
|
124
166
|
<SettingsNav
|
|
125
167
|
defaultItems={navDefault}
|
|
168
|
+
sections={navSections}
|
|
126
169
|
adminItems={navAdmin}
|
|
127
170
|
currentPath={currentPath}
|
|
128
171
|
/>
|
|
@@ -2,6 +2,7 @@ import type { ReactNode } from 'react';
|
|
|
2
2
|
import { Link } from 'react-router-dom';
|
|
3
3
|
import type { AdminMenuItem } from '../../../core/registry';
|
|
4
4
|
import { cn } from '../../../lib/utils';
|
|
5
|
+
import { sameSettingsPath } from '../settings-nav-items';
|
|
5
6
|
|
|
6
7
|
/** A row the nav can render: one that declares where it goes. */
|
|
7
8
|
export type LinkableItem = AdminMenuItem & { path: string };
|
|
@@ -17,8 +18,8 @@ export type SettingsNavOrientation = 'rail' | 'strip';
|
|
|
17
18
|
/**
|
|
18
19
|
* The settings nav's CONTENTS — a pure view of the URL, in the same sense
|
|
19
20
|
* `PluginsSidebar` is. It holds no state, decides no membership and fetches
|
|
20
|
-
* nothing; the layout works out which rows exist
|
|
21
|
-
* and this renders them.
|
|
21
|
+
* nothing; the layout works out which rows exist, the current one is the
|
|
22
|
+
* URL's by the router's rule (`sameSettingsPath`), and this renders them.
|
|
22
23
|
*
|
|
23
24
|
* Row styling is `PluginsSidebar`'s declaration token for token, minus the
|
|
24
25
|
* count slot it has no use for, plus `FileExplorer`'s inset focus ring — the
|
|
@@ -44,11 +45,14 @@ export type SettingsNavOrientation = 'rail' | 'strip';
|
|
|
44
45
|
*/
|
|
45
46
|
export function SettingsNav({
|
|
46
47
|
defaultItems,
|
|
48
|
+
sections = [],
|
|
47
49
|
adminItems,
|
|
48
50
|
currentPath,
|
|
49
51
|
orientation = 'rail',
|
|
50
52
|
}: {
|
|
51
53
|
defaultItems: LinkableItem[];
|
|
54
|
+
/** The app's own sections (see `AdminMenuItem.section`), drawn between the default rows and "Admin only", as the profile menu draws them. */
|
|
55
|
+
sections?: Array<{ id: string; label: string; items: LinkableItem[] }>;
|
|
52
56
|
adminItems: LinkableItem[];
|
|
53
57
|
currentPath: string;
|
|
54
58
|
orientation?: SettingsNavOrientation;
|
|
@@ -56,7 +60,8 @@ export function SettingsNav({
|
|
|
56
60
|
const strip = orientation === 'strip';
|
|
57
61
|
|
|
58
62
|
const row = (item: LinkableItem) => {
|
|
59
|
-
|
|
63
|
+
// By the router's rule, so the row of the page on screen is the one marked, however the URL is spelt.
|
|
64
|
+
const current = sameSettingsPath(item.path, currentPath);
|
|
60
65
|
return (
|
|
61
66
|
<Link
|
|
62
67
|
key={item.id}
|
|
@@ -87,6 +92,22 @@ export function SettingsNav({
|
|
|
87
92
|
)}
|
|
88
93
|
>
|
|
89
94
|
{defaultItems.map(row)}
|
|
95
|
+
{sections.map((section) => (
|
|
96
|
+
// A named group, so a reader who moves from link to link hears which
|
|
97
|
+
// section a row is in. A box of its own that lays its rows out as the
|
|
98
|
+
// nav does (a column in the rail, a row in the strip), not
|
|
99
|
+
// `display: contents`: an element without a box can drop out of the
|
|
100
|
+
// accessibility tree, and the group's name with it.
|
|
101
|
+
<div
|
|
102
|
+
key={section.id}
|
|
103
|
+
role="group"
|
|
104
|
+
aria-label={section.label}
|
|
105
|
+
className={cn('flex shrink-0', strip ? 'flex-row items-center gap-1' : 'flex-col')}
|
|
106
|
+
>
|
|
107
|
+
{!strip && <SectionLabel spaced>{section.label}</SectionLabel>}
|
|
108
|
+
{section.items.map(row)}
|
|
109
|
+
</div>
|
|
110
|
+
))}
|
|
90
111
|
{adminItems.length > 0 && (
|
|
91
112
|
<>
|
|
92
113
|
{/* Inline, the rows simply continue — see the orientation docblock. */}
|
|
@@ -4,10 +4,12 @@ import { render, screen, within } from '@testing-library/react';
|
|
|
4
4
|
import userEvent from '@testing-library/user-event';
|
|
5
5
|
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
|
|
6
6
|
import { SettingsLayout } from '../SettingsLayout';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { setSidebarCollapsed } from '../../../layout
|
|
7
|
+
import { isSettingsNavPath, registrySettingsPages, useSettingsNavPaths } from '../../settings-nav-items';
|
|
8
|
+
import { AdminContext, type AdminContextValue } from '../../../admin';
|
|
9
|
+
import { setSidebarCollapsed } from '../../../layout';
|
|
10
10
|
import { AppRegistryContext, makeRegistry, type AdminMenuItem } from '../../../../core/registry';
|
|
11
|
+
import { AuthContext, type AuthContextValue } from '../../../auth';
|
|
12
|
+
import type { AuthUser } from '@bevel-software/platform-shared';
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* The settings nav's contract.
|
|
@@ -230,7 +232,8 @@ describe('SettingsLayout', () => {
|
|
|
230
232
|
it('fills the app sidebar frame on a wide window', () => {
|
|
231
233
|
stubMatchMedia(false);
|
|
232
234
|
renderAt('/secrets');
|
|
233
|
-
|
|
235
|
+
// The app's one sidebar frame: an `<aside>` named for the surface it holds.
|
|
236
|
+
const frame = screen.queryByRole('complementary', { name: 'Settings' });
|
|
234
237
|
expect(frame).not.toBeNull();
|
|
235
238
|
expect(frame).toContainElement(nav());
|
|
236
239
|
vi.unstubAllGlobals();
|
|
@@ -241,10 +244,268 @@ describe('SettingsLayout', () => {
|
|
|
241
244
|
it('becomes a strip with no sidebar frame on a narrow window', () => {
|
|
242
245
|
stubMatchMedia(true);
|
|
243
246
|
renderAt('/secrets');
|
|
244
|
-
expect(
|
|
247
|
+
expect(screen.queryByRole('complementary', { name: 'Settings' })).toBeNull();
|
|
245
248
|
expect(noNav()).not.toBeNull();
|
|
246
249
|
expect(within(nav()).getAllByRole('link')).toHaveLength(5);
|
|
247
250
|
vi.unstubAllGlobals();
|
|
248
251
|
});
|
|
249
252
|
});
|
|
250
253
|
});
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* An app's own settings pages (`AdminMenuItem.page`): rows of the registry
|
|
257
|
+
* that bring a page are listed in the nav like core's, under the section they
|
|
258
|
+
* name, to the people they are shown to.
|
|
259
|
+
*/
|
|
260
|
+
describe('SettingsLayout with pages the registry brings', () => {
|
|
261
|
+
const INFRASTRUCTURE = { id: 'infrastructure', label: 'Infrastructure only' };
|
|
262
|
+
const ada = { id: 'u1', email: 'ada@example.com', name: 'Ada' } as AuthUser;
|
|
263
|
+
const pageRows: AdminMenuItem[] = [
|
|
264
|
+
{ id: 'connected-apps', order: 60, label: 'Connected apps', path: '/connected-apps', page: <div>Connected apps page</div> },
|
|
265
|
+
{ id: 'executors', section: INFRASTRUCTURE, label: 'Executors', path: '/executors/', page: <div>Executors page</div>, isShown: (user) => user.email === 'ada@example.com' },
|
|
266
|
+
{ id: 'llm', section: 'admin', order: 30, label: 'LLM configuration', path: '/llm-configuration', page: <div>LLM page</div> },
|
|
267
|
+
// A path with no page is somewhere else in the app.
|
|
268
|
+
{ id: 'elsewhere', label: 'No page', path: '/agents/workflows' },
|
|
269
|
+
// Rows that must NOT be linked: a page at a built-in address, at the old Connection keys address, at a path an
|
|
270
|
+
// earlier row took, and a page row that also runs an action (the menu runs the action; a link would go around it).
|
|
271
|
+
{ id: 'takeover', label: 'Not the secrets page', path: '/secrets', page: <div>Never mounted</div> },
|
|
272
|
+
{ id: 'old-keys', label: 'Not the keys page', path: '/connection-keys', page: <div>Never mounted</div> },
|
|
273
|
+
{ id: 'second', label: 'Second at the same path', path: '/connected-apps', page: <div>Never mounted</div> },
|
|
274
|
+
{ id: 'acts', label: 'Runs an action', path: '/acts', page: <div>Acts page</div>, onSelect: () => {} },
|
|
275
|
+
// A page row that also opens a dialog: the menu opens the dialog, so the row stays in the menu only.
|
|
276
|
+
{ id: 'opens', label: 'Opens a dialog', path: '/opens', page: <div>Opens page</div>, dialog: () => <></> },
|
|
277
|
+
// Addresses the router reads as one already taken: the `/connect` redirect, core's page in other
|
|
278
|
+
// case, and an earlier row's path in other case and with a trailing slash.
|
|
279
|
+
{ id: 'connect', label: 'Not the connect redirect', path: '/connect', page: <div>Never mounted</div> },
|
|
280
|
+
{ id: 'shouting', label: 'Not the secrets page either', path: '/SECRETS', page: <div>Never mounted</div> },
|
|
281
|
+
{ id: 'recased', label: 'Connected apps again', path: '/Connected-Apps/', page: <div>Never mounted</div> },
|
|
282
|
+
// An address belongs to the first row that names it, page or not: a page for the address `elsewhere`
|
|
283
|
+
// points to is not mounted, and a later row that merely names a mounted page's address is not a second link to it.
|
|
284
|
+
{ id: 'late-claim', label: 'Page at a named address', path: '/agents/workflows', page: <div>Never mounted</div> },
|
|
285
|
+
{ id: 'alias', label: 'Another name for connected apps', path: '/connected-apps' },
|
|
286
|
+
];
|
|
287
|
+
|
|
288
|
+
function renderPages(route: string, { user = ada, isAdmin = false }: { user?: AuthUser | null; isAdmin?: boolean } = {}) {
|
|
289
|
+
const tree = (
|
|
290
|
+
<AppRegistryContext.Provider value={makeRegistry({ adminMenuItems: pageRows })}>
|
|
291
|
+
<MemoryRouter initialEntries={[route]}>
|
|
292
|
+
<Routes>
|
|
293
|
+
<Route element={<SettingsLayout />}>
|
|
294
|
+
<Route path="/secrets" element={<div>Secrets stub</div>} />
|
|
295
|
+
{/* The app's pages as the shell mounts them: from the rows, through the one function that decides which are. */}
|
|
296
|
+
{registrySettingsPages(makeRegistry({ adminMenuItems: pageRows })).map((row) => (
|
|
297
|
+
<Route key={row.path} path={row.path} element={row.page} />
|
|
298
|
+
))}
|
|
299
|
+
</Route>
|
|
300
|
+
</Routes>
|
|
301
|
+
</MemoryRouter>
|
|
302
|
+
</AppRegistryContext.Provider>
|
|
303
|
+
);
|
|
304
|
+
return render(
|
|
305
|
+
<AuthContext.Provider value={(user ? { user } : null) as AuthContextValue | null}>{withAdmin(isAdmin, tree)}</AuthContext.Provider>,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
it('lists them with core\'s rows, in their order, each under its own section', () => {
|
|
310
|
+
renderPages('/secrets', { isAdmin: true });
|
|
311
|
+
expect(within(nav()).getAllByRole('link').map((l) => l.textContent)).toEqual([
|
|
312
|
+
'External agent access',
|
|
313
|
+
'Audit log',
|
|
314
|
+
'Secrets',
|
|
315
|
+
'Connected apps',
|
|
316
|
+
'Browse available tools',
|
|
317
|
+
'Account',
|
|
318
|
+
'Executors',
|
|
319
|
+
'Deployment',
|
|
320
|
+
'App roles',
|
|
321
|
+
'Groups & Members',
|
|
322
|
+
'User accounts',
|
|
323
|
+
'LLM configuration',
|
|
324
|
+
]);
|
|
325
|
+
// The app's section is labelled, between the default rows and "Admin only".
|
|
326
|
+
expect(within(nav()).getByText('Infrastructure only')).toBeInTheDocument();
|
|
327
|
+
expect(within(nav()).getAllByRole('link', { name: 'Secrets' })).toHaveLength(1);
|
|
328
|
+
expect(within(nav()).queryByRole('link', { name: 'No page' })).toBeNull();
|
|
329
|
+
expect(within(nav()).queryByRole('link', { name: 'Page at a named address' })).toBeNull();
|
|
330
|
+
expect(within(nav()).queryByRole('link', { name: 'Another name for connected apps' })).toBeNull();
|
|
331
|
+
// One link per page: the row whose page is mounted there.
|
|
332
|
+
expect(within(nav()).getAllByRole('link').filter((l) => l.getAttribute('href') === '/connected-apps')).toHaveLength(1);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('mounts a page per row that brings one, at its own path: never at a built-in address, and one page per path', () => {
|
|
336
|
+
expect(registrySettingsPages(makeRegistry({ adminMenuItems: pageRows })).map((row) => `${row.id} ${row.path}`)).toEqual([
|
|
337
|
+
'connected-apps /connected-apps',
|
|
338
|
+
'executors /executors',
|
|
339
|
+
'llm /llm-configuration',
|
|
340
|
+
'acts /acts',
|
|
341
|
+
'opens /opens',
|
|
342
|
+
]);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('mounts no page at an address something else answers: a shell route, an app, a top-level route', () => {
|
|
346
|
+
const page = (id: string, path: string): AdminMenuItem => ({ id, label: id, path, page: <div>{id}</div> });
|
|
347
|
+
const registry = makeRegistry({
|
|
348
|
+
apps: [{ id: 'knowledge', label: 'Knowledge', path: '/workspace', element: <div /> }],
|
|
349
|
+
topLevelRoutes: [{ path: '/onboarding', element: <div /> }],
|
|
350
|
+
adminMenuItems: [
|
|
351
|
+
// One request's address, which the shell's `/change-requests/:number` answers.
|
|
352
|
+
page('one-request', '/change-requests/123'),
|
|
353
|
+
page('landing', '/'),
|
|
354
|
+
page('sign-in-return', '/auth/oidc/callback'),
|
|
355
|
+
page('embed', '/embed/link'),
|
|
356
|
+
// An app's surface is its path and everything under it.
|
|
357
|
+
page('app-root', '/workspace'),
|
|
358
|
+
page('inside-app', '/workspace/main/settings'),
|
|
359
|
+
page('top-level', '/onboarding'),
|
|
360
|
+
page('free', '/change-requests'),
|
|
361
|
+
],
|
|
362
|
+
});
|
|
363
|
+
expect(registrySettingsPages(registry).map((row) => row.id)).toEqual(['free']);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('mounts a page only at one absolute address, never at a route pattern, and such a row holds no address', () => {
|
|
367
|
+
const page = (id: string, path: string): AdminMenuItem => ({ id, label: id, path, page: <div>{id}</div> });
|
|
368
|
+
const rows = [
|
|
369
|
+
page('parameter', '/teams/:id'),
|
|
370
|
+
page('splat', '/teams/*'),
|
|
371
|
+
page('optional', '/teams/all?'),
|
|
372
|
+
page('query', '/teams?tab=all'),
|
|
373
|
+
page('fragment', '/teams#all'),
|
|
374
|
+
page('relative', 'teams'),
|
|
375
|
+
// Spellings the browser or the router resolves to another address.
|
|
376
|
+
page('parent-segment', '/teams/../secrets'),
|
|
377
|
+
page('same-segment', '/teams/./all'),
|
|
378
|
+
page('ends-in-parent', '/teams/..'),
|
|
379
|
+
page('doubled-slash', '/teams//all'),
|
|
380
|
+
page('backslash', '/teams\\all'),
|
|
381
|
+
page('tab', '/teams\tall'),
|
|
382
|
+
page('another-origin', '//elsewhere.example/teams'),
|
|
383
|
+
page('encoded-letter', '/%74eams'),
|
|
384
|
+
// After them all, and still free: none of the rows above took it.
|
|
385
|
+
page('one-address', '/teams/1'),
|
|
386
|
+
page('colon-inside', '/teams/a:b'),
|
|
387
|
+
page('not-ascii', '/übersicht'),
|
|
388
|
+
page('trailing-slash', '/groups/'),
|
|
389
|
+
];
|
|
390
|
+
expect(registrySettingsPages(makeRegistry({ adminMenuItems: rows })).map((row) => `${row.id} ${row.path}`)).toEqual([
|
|
391
|
+
'one-address /teams/1',
|
|
392
|
+
'colon-inside /teams/a:b',
|
|
393
|
+
'not-ascii /übersicht',
|
|
394
|
+
'trailing-slash /groups',
|
|
395
|
+
]);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('lists no row whose path is a route pattern, or resolves to another page', () => {
|
|
399
|
+
const rows: AdminMenuItem[] = [
|
|
400
|
+
{ id: 'pattern', label: 'A pattern', path: '/teams/:id', page: <div>Teams</div> },
|
|
401
|
+
// A link to this would land on the Secrets page under the row's own name.
|
|
402
|
+
{ id: 'elsewhere', label: 'Lands on secrets', path: '/teams/../secrets', page: <div>Teams</div> },
|
|
403
|
+
{ id: 'elsewhere-no-page', label: 'Points at secrets', path: '/teams/../secrets' },
|
|
404
|
+
];
|
|
405
|
+
render(
|
|
406
|
+
<AppRegistryContext.Provider value={makeRegistry({ adminMenuItems: rows })}>
|
|
407
|
+
<MemoryRouter initialEntries={['/secrets']}>
|
|
408
|
+
<Routes>
|
|
409
|
+
<Route element={<SettingsLayout />}>
|
|
410
|
+
<Route path="/secrets" element={<div>Secrets stub</div>} />
|
|
411
|
+
</Route>
|
|
412
|
+
</Routes>
|
|
413
|
+
</MemoryRouter>
|
|
414
|
+
</AppRegistryContext.Provider>,
|
|
415
|
+
);
|
|
416
|
+
expect(within(nav()).queryByRole('link', { name: 'A pattern' })).toBeNull();
|
|
417
|
+
expect(within(nav()).queryByRole('link', { name: 'Lands on secrets' })).toBeNull();
|
|
418
|
+
expect(within(nav()).queryByRole('link', { name: 'Points at secrets' })).toBeNull();
|
|
419
|
+
expect(within(nav()).getAllByRole('link').filter((l) => l.getAttribute('href') === '/secrets')).toHaveLength(1);
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('an app that claims the root takes no address from a page', () => {
|
|
423
|
+
const registry = makeRegistry({
|
|
424
|
+
apps: [{ id: 'everything', label: 'Everything', path: '/', element: <div /> }],
|
|
425
|
+
adminMenuItems: [{ id: 'free', label: 'Free', path: '/executors', page: <div /> }],
|
|
426
|
+
});
|
|
427
|
+
expect(registrySettingsPages(registry).map((row) => row.id)).toEqual(['free']);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('marks the row current and shows its page when the URL spells the path percent-encoded', () => {
|
|
431
|
+
renderPages('/%65xecutors');
|
|
432
|
+
expect(screen.getByText('Executors page')).toBeInTheDocument();
|
|
433
|
+
expect(within(nav()).getByRole('link', { name: 'Executors' })).toHaveAttribute('aria-current', 'page');
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('marks the row current and shows its page when the URL spells the path in another case', () => {
|
|
437
|
+
renderPages('/Executors/');
|
|
438
|
+
expect(screen.getByText('Executors page')).toBeInTheDocument();
|
|
439
|
+
expect(within(nav()).getByRole('link', { name: 'Executors' })).toHaveAttribute('aria-current', 'page');
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it("counts an app's pages as settings paths through useSettingsNavPaths, and only core's without it", () => {
|
|
443
|
+
function Probe() {
|
|
444
|
+
const paths = useSettingsNavPaths();
|
|
445
|
+
return (
|
|
446
|
+
<>
|
|
447
|
+
<output aria-label="with the app's pages">{String(isSettingsNavPath('/executors', paths))}</output>
|
|
448
|
+
<output aria-label="percent-encoded">{String(isSettingsNavPath('/%65xecutors/', paths))}</output>
|
|
449
|
+
<output aria-label="core only">{String(isSettingsNavPath('/executors'))}</output>
|
|
450
|
+
</>
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
render(
|
|
454
|
+
<AppRegistryContext.Provider value={makeRegistry({ adminMenuItems: pageRows })}>
|
|
455
|
+
<Probe />
|
|
456
|
+
</AppRegistryContext.Provider>,
|
|
457
|
+
);
|
|
458
|
+
expect(screen.getByRole('status', { name: "with the app's pages" })).toHaveTextContent('true');
|
|
459
|
+
// What the toolbar's sidebar toggle goes by, for a URL the router reads as that page.
|
|
460
|
+
expect(screen.getByRole('status', { name: 'percent-encoded' })).toHaveTextContent('true');
|
|
461
|
+
expect(screen.getByRole('status', { name: 'core only' })).toHaveTextContent('false');
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('shows the row\'s own page at its path and marks it current, whatever slashes the row was written with', () => {
|
|
465
|
+
renderPages('/executors');
|
|
466
|
+
expect(screen.getByText('Executors page')).toBeInTheDocument();
|
|
467
|
+
expect(within(nav()).getByRole('link', { name: 'Executors' })).toHaveAttribute('aria-current', 'page');
|
|
468
|
+
// Its section is a named group, so a link is heard with the section it is in.
|
|
469
|
+
expect(within(screen.getByRole('group', { name: 'Infrastructure only' })).getByRole('link', { name: 'Executors' })).toBeInTheDocument();
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it('asks who a row is shown to on every render, not once', () => {
|
|
473
|
+
let role = false;
|
|
474
|
+
const rows: AdminMenuItem[] = [{ id: 'late', label: 'Learnt later', path: '/late', page: <div>Late page</div>, isShown: () => role }];
|
|
475
|
+
const tree = () => (
|
|
476
|
+
<AuthContext.Provider value={{ user: ada } as AuthContextValue}>
|
|
477
|
+
<AppRegistryContext.Provider value={registry}>
|
|
478
|
+
<MemoryRouter initialEntries={['/secrets']}>
|
|
479
|
+
<Routes>
|
|
480
|
+
<Route element={<SettingsLayout />}>
|
|
481
|
+
<Route path="/secrets" element={<div>Secrets stub</div>} />
|
|
482
|
+
</Route>
|
|
483
|
+
</Routes>
|
|
484
|
+
</MemoryRouter>
|
|
485
|
+
</AppRegistryContext.Provider>
|
|
486
|
+
</AuthContext.Provider>
|
|
487
|
+
);
|
|
488
|
+
// The same registry object across both renders: nothing the layout memoizes on changes, only the answer does.
|
|
489
|
+
const registry = makeRegistry({ adminMenuItems: rows });
|
|
490
|
+
const { rerender } = render(tree());
|
|
491
|
+
expect(within(nav()).queryByRole('link', { name: 'Learnt later' })).toBeNull();
|
|
492
|
+
role = true;
|
|
493
|
+
rerender(tree());
|
|
494
|
+
expect(within(nav()).getByRole('link', { name: 'Learnt later' })).toBeInTheDocument();
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it('offers a row to the people it is shown to, and its section only while a row of it shows', () => {
|
|
498
|
+
renderPages('/secrets', { user: { ...ada, email: 'ben@example.com' } as AuthUser });
|
|
499
|
+
expect(within(nav()).queryByRole('link', { name: 'Executors' })).toBeNull();
|
|
500
|
+
expect(within(nav()).queryByText('Infrastructure only')).toBeNull();
|
|
501
|
+
expect(within(nav()).getByRole('link', { name: 'Connected apps' })).toBeInTheDocument();
|
|
502
|
+
// Not an admin either: a page the registry puts under "Admin only" is not advertised to them.
|
|
503
|
+
expect(within(nav()).queryByRole('link', { name: 'LLM configuration' })).toBeNull();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it('with nobody signed in, offers none of the rows that ask who is', () => {
|
|
507
|
+
renderPages('/secrets', { user: null });
|
|
508
|
+
expect(within(nav()).queryByRole('link', { name: 'Executors' })).toBeNull();
|
|
509
|
+
expect(within(nav()).getByRole('link', { name: 'Connected apps' })).toBeInTheDocument();
|
|
510
|
+
});
|
|
511
|
+
});
|
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* layout and free to move. Export what another module needs; nothing more.
|
|
5
5
|
*/
|
|
6
6
|
export { fetchMarketplaceRegistration } from './services/github-facade.api';
|
|
7
|
-
export { isSettingsNavPath, useMenuSections } from './settings-nav-items';
|
|
7
|
+
export { isSettingsNavPath, registrySettingsPages, useMenuSections, useSettingsNavPaths } from './settings-nav-items';
|
|
8
8
|
export type { MenuSections } from './settings-nav-items';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
|
+
import { matchRoutes } from 'react-router-dom';
|
|
2
3
|
import { Boxes, CircleUserRound, KeyRound, Lock, ScrollText, SlidersHorizontal, Users, UsersRound } from 'lucide-react';
|
|
3
|
-
import { useAppRegistry, type AdminMenuItem } from '../../core/registry';
|
|
4
|
+
import { useAppRegistry, type AdminMenuItem, type AppRegistry } from '../../core/registry';
|
|
5
|
+
import { SHELL_PATHS } from '../../core/shell-paths';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* The settings rows, and the one merge that orders them.
|
|
@@ -152,20 +154,127 @@ export function normalizeSettingsPath(pathname: string): string {
|
|
|
152
154
|
return pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
|
|
153
155
|
}
|
|
154
156
|
|
|
157
|
+
/** A registry row that is a settings page of the app's own: it says where it lives and what is there. */
|
|
158
|
+
export type SettingsPageRow = AdminMenuItem & { path: string; page: NonNullable<AdminMenuItem['page']> };
|
|
159
|
+
|
|
160
|
+
/** What of the registry decides which settings pages are mounted: the rows, and everything else that answers an address. */
|
|
161
|
+
export type SettingsPagesSource = Pick<AppRegistry, 'adminMenuItems' | 'apps' | 'topLevelRoutes'>;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Whether the route at `path` answers `pathname`, by the router itself: React
|
|
165
|
+
* Router's `matchRoutes` over that one route, so decoding (`/%65xecutors` is
|
|
166
|
+
* `/executors`), case and a trailing slash (`/Secrets/` is `/secrets`), and a
|
|
167
|
+
* pattern's parameters and splat count exactly as they count when the route
|
|
168
|
+
* is chosen. Every comparison of a settings path goes through here, so which
|
|
169
|
+
* page the router shows, which page is mounted, which row is listed and which
|
|
170
|
+
* row is marked current cannot disagree.
|
|
171
|
+
*/
|
|
172
|
+
export function sameSettingsPath(path: string, pathname: string): boolean {
|
|
173
|
+
return matchRoutes([{ path }], pathname) !== null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Whether a row's `path` is ONE absolute address, written the way everything
|
|
178
|
+
* that reads it reads it. A row's path goes to two places: the browser's
|
|
179
|
+
* address bar (through the row's link) and the router's route table (where
|
|
180
|
+
* its page is mounted). It is one address only if both read it back
|
|
181
|
+
* unchanged, each asked with its own parser:
|
|
182
|
+
*
|
|
183
|
+
* - the browser's URL parser keeps it on this origin with no query and no
|
|
184
|
+
* fragment, and leaves the path as written (it folds `..` and `.`, turns a
|
|
185
|
+
* backslash into a slash, drops a tab; an optional segment's `?` is a
|
|
186
|
+
* query to it, and a relative path gains a slash);
|
|
187
|
+
* - the path, mounted as a route, answers itself, as written, and nothing
|
|
188
|
+
* more: no parameter (`:name`) and no splat, the same pathname (the router
|
|
189
|
+
* folds a doubled slash), and a match at all (a percent-encoded letter
|
|
190
|
+
* never matches the address it spells).
|
|
191
|
+
*/
|
|
192
|
+
function isOneAddress(path: string): boolean {
|
|
193
|
+
const origin = 'http://settings.invalid';
|
|
194
|
+
let inBrowser: string;
|
|
195
|
+
try {
|
|
196
|
+
const url = new URL(path, origin);
|
|
197
|
+
if (url.origin !== origin || url.search || url.hash) return false;
|
|
198
|
+
// Decoded for the comparison: the browser percent-encodes a space or an `ü` it is given as written.
|
|
199
|
+
inBrowser = decodeURI(url.pathname);
|
|
200
|
+
} catch {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (inBrowser !== path) return false;
|
|
204
|
+
const itself = matchRoutes([{ path }], path);
|
|
205
|
+
return itself !== null && itself[0].pathname === path && Object.keys(itself[0].params).length === 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The registry's rows that bring a settings page (see `AdminMenuItem.page`),
|
|
210
|
+
* each with its path normalized. A row whose path is not one absolute address
|
|
211
|
+
* (see `isOneAddress`: a route pattern, a path with a query or a fragment, a
|
|
212
|
+
* relative path, or any spelling the browser or the router would resolve to
|
|
213
|
+
* something else, such as `/teams/../secrets`) is no destination: it brings
|
|
214
|
+
* no page and holds no address.
|
|
215
|
+
*
|
|
216
|
+
* An address belongs to whatever answers it first, and a row's page is
|
|
217
|
+
* mounted only at an address nothing else answers: not one of core's settings
|
|
218
|
+
* pages, not a route the shell answers itself, not an app's surface (its path
|
|
219
|
+
* and everything under it), not a top-level route of the registry, and not a
|
|
220
|
+
* path an earlier row named, whether or not that row brings a page. So a
|
|
221
|
+
* page never takes over a destination that exists, and a row that only points
|
|
222
|
+
* somewhere (a `path` with no page) keeps its destination against a later
|
|
223
|
+
* row's page. An app that claims the root owns no address in particular, like
|
|
224
|
+
* the shell's catch-all.
|
|
225
|
+
*/
|
|
226
|
+
export function registrySettingsPages(registry: SettingsPagesSource): SettingsPageRow[] {
|
|
227
|
+
const pages: SettingsPageRow[] = [];
|
|
228
|
+
const taken: string[] = [
|
|
229
|
+
...SETTINGS_NAV_PATHS,
|
|
230
|
+
// The very patterns the shell mounts its own routes at (`core/shell-paths.ts`).
|
|
231
|
+
...Object.values(SHELL_PATHS),
|
|
232
|
+
...registry.apps.filter((app) => app.path !== '/').map((app) => `${app.path}/*`),
|
|
233
|
+
...registry.topLevelRoutes.map((route) => route.path),
|
|
234
|
+
];
|
|
235
|
+
for (const item of registry.adminMenuItems) {
|
|
236
|
+
// A path that is not one address names no destination: it brings no page and holds no address.
|
|
237
|
+
if (!item.path || !isOneAddress(item.path)) continue;
|
|
238
|
+
const path = normalizeSettingsPath(item.path);
|
|
239
|
+
if (taken.some((other) => sameSettingsPath(other, path))) continue;
|
|
240
|
+
taken.push(path);
|
|
241
|
+
if (item.page) pages.push({ ...item, path, page: item.page });
|
|
242
|
+
}
|
|
243
|
+
return pages;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Every path the settings layout wraps: core's, and the pages the registry's
|
|
248
|
+
* rows bring. What the toolbar's sidebar toggle goes by.
|
|
249
|
+
*/
|
|
250
|
+
export function useSettingsNavPaths(): readonly string[] {
|
|
251
|
+
const registry = useAppRegistry();
|
|
252
|
+
return useMemo(() => [...SETTINGS_NAV_PATHS, ...registrySettingsPages(registry).map((row) => row.path)], [registry]);
|
|
253
|
+
}
|
|
254
|
+
|
|
155
255
|
/**
|
|
156
256
|
* Exact match, never a prefix. These are exact routes, and the nav marks the
|
|
157
257
|
* current row by comparing the whole pathname — a prefix test would light up
|
|
158
|
-
* `/account` for a hypothetical `/accounts-of-other-people`. Exact
|
|
159
|
-
*
|
|
160
|
-
* than a different one.
|
|
258
|
+
* `/account` for a hypothetical `/accounts-of-other-people`. Exact by the
|
|
259
|
+
* router's rule (see `sameSettingsPath`): a trailing slash or another case is
|
|
260
|
+
* a spelling of the same path rather than a different one.
|
|
161
261
|
*/
|
|
162
|
-
export function isSettingsNavPath(pathname: string): boolean {
|
|
163
|
-
return
|
|
262
|
+
export function isSettingsNavPath(pathname: string, paths: readonly string[] = SETTINGS_NAV_PATHS): boolean {
|
|
263
|
+
return paths.some((path) => sameSettingsPath(path, pathname));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** One of the app's own labelled sections, with its rows sorted. */
|
|
267
|
+
export interface AppMenuSection {
|
|
268
|
+
id: string;
|
|
269
|
+
label: string;
|
|
270
|
+
items: AdminMenuItem[];
|
|
164
271
|
}
|
|
165
272
|
|
|
166
273
|
export interface MenuSections {
|
|
167
274
|
defaultItems: AdminMenuItem[];
|
|
168
275
|
adminItems: AdminMenuItem[];
|
|
276
|
+
/** The app's own sections (see `AdminMenuItem.section`), in their order; drawn between the two above. */
|
|
277
|
+
appSections: AppMenuSection[];
|
|
169
278
|
}
|
|
170
279
|
|
|
171
280
|
/**
|
|
@@ -180,10 +289,21 @@ export function useMenuSections(): MenuSections {
|
|
|
180
289
|
const registry = useAppRegistry();
|
|
181
290
|
return useMemo(() => {
|
|
182
291
|
const all = [...CORE_MENU_ITEMS, ...registry.adminMenuItems];
|
|
292
|
+
const byOrder = (a: AdminMenuItem, b: AdminMenuItem) => (a.order ?? 100) - (b.order ?? 100);
|
|
183
293
|
const bySection = (section: 'default' | 'admin') =>
|
|
184
|
-
all
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
294
|
+
all.filter((item) => (item.section ?? 'default') === section).sort(byOrder);
|
|
295
|
+
// The app's sections: one per id, its label and order from the first row naming it.
|
|
296
|
+
const sections = new Map<string, AppMenuSection & { order: number }>();
|
|
297
|
+
for (const item of all) {
|
|
298
|
+
const section = item.section;
|
|
299
|
+
if (typeof section !== 'object') continue;
|
|
300
|
+
const entry = sections.get(section.id) ?? { id: section.id, label: section.label, order: section.order ?? 100, items: [] };
|
|
301
|
+
entry.items.push(item);
|
|
302
|
+
sections.set(section.id, entry);
|
|
303
|
+
}
|
|
304
|
+
const appSections = [...sections.values()]
|
|
305
|
+
.sort((a, b) => a.order - b.order)
|
|
306
|
+
.map(({ id, label, items }) => ({ id, label, items: items.sort(byOrder) }));
|
|
307
|
+
return { defaultItems: bySection('default'), adminItems: bySection('admin'), appSections };
|
|
188
308
|
}, [registry]);
|
|
189
309
|
}
|