@bevel-software/platform-core-frontend 0.21.0 → 0.22.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.
Files changed (34) hide show
  1. package/package.json +2 -2
  2. package/src/core/registry.ts +57 -0
  3. package/src/modules/access/__tests__/ManageAccessDialog.request.test.tsx +376 -0
  4. package/src/modules/access/components/AccessRequestControl.tsx +155 -0
  5. package/src/modules/{library → access}/components/AccessRequestsBanner.tsx +62 -24
  6. package/src/modules/access/components/ManageAccessDialog.tsx +81 -4
  7. package/src/modules/access/hooks/useAccessRequests.ts +183 -0
  8. package/src/modules/access/requests.api.ts +121 -0
  9. package/src/modules/auth/components/LoginScreen.tsx +5 -0
  10. package/src/modules/change-requests/__tests__/ChangeRequestDialog.brings-itself-up-to-date.test.tsx +171 -38
  11. package/src/modules/change-requests/__tests__/ChangeRequestDialog.fork-point.test.tsx +34 -19
  12. package/src/modules/change-requests/__tests__/ChangeRequestDialog.test.tsx +173 -6
  13. package/src/modules/change-requests/components/ChangeRequestDialog.tsx +155 -98
  14. package/src/modules/library/__tests__/SkillPage.test.tsx +15 -0
  15. package/src/modules/library/__tests__/ToolPage.test.tsx +15 -1
  16. package/src/modules/library/__tests__/title-row-actions.ts +53 -1
  17. package/src/modules/library/components/PluginJoinRequests.tsx +2 -2
  18. package/src/modules/library/components/skill-page/SkillPage.tsx +2 -2
  19. package/src/modules/library/components/tool-page/ToolPage.tsx +11 -1
  20. package/src/modules/library/hooks/useJoinRequests.ts +26 -101
  21. package/src/modules/pr/services/pr-merge.api.ts +10 -2
  22. package/src/modules/settings/components/DeploymentPage.tsx +22 -0
  23. package/src/modules/settings/components/__tests__/DeploymentPage.test.tsx +72 -4
  24. package/src/modules/setup/__tests__/setup.first-run.test.tsx +276 -0
  25. package/src/modules/setup/__tests__/setup.github.test.tsx +416 -0
  26. package/src/modules/setup/__tests__/setup.kept-draft.test.tsx +275 -0
  27. package/src/modules/setup/__tests__/setup.repository.test.tsx +373 -0
  28. package/src/modules/setup/__tests__/setup.test.tsx +8 -14
  29. package/src/modules/setup/components/GitHubRepositoryPanel.tsx +369 -0
  30. package/src/modules/setup/components/SetupGate.tsx +1 -0
  31. package/src/modules/setup/components/SetupScreen.tsx +464 -38
  32. package/src/modules/setup/services/setup.api.ts +93 -0
  33. package/src/modules/setup/utils/kept-draft.ts +86 -0
  34. package/src/shared/components/SlotBoundary.tsx +51 -0
@@ -1,13 +1,17 @@
1
- import { useEffect, useRef, useState, type FormEvent } from 'react';
1
+ import { useEffect, useRef, useState, type FormEvent, type KeyboardEvent, type ReactNode } from 'react';
2
2
  import {
3
3
  DEFAULT_KB_LAYOUT,
4
4
  agentsFilePointerSentence,
5
5
  type KbLayout,
6
6
  } from '@bevel-software/platform-shared';
7
7
  import { Banner, Button, Surface, TextField } from '../../../shared/components';
8
+ import { SlotBoundary } from '../../../shared/components/SlotBoundary';
8
9
  import { tokenUsernameForHost } from '../utils/git-host';
9
10
  import { isRootFolderSuggestion, rootFolderState, type RootFolderState } from '../utils/root-folders';
10
11
  import { copyToClipboard } from '../../../lib/clipboard';
12
+ import { GitHubRepositoryPanel } from './GitHubRepositoryPanel';
13
+ import { forgetDraft, keepDraft, keptDraft } from '../utils/kept-draft';
14
+ import { useAppRegistry } from '../../../core/registry';
11
15
  import { MarketplaceSection } from '../../settings/components/MarketplaceSection';
12
16
  import {
13
17
  saveSettings,
@@ -22,6 +26,8 @@ import {
22
26
  type LastSync,
23
27
  type OidcTest,
24
28
  type OidcVerification,
29
+ type GitMode,
30
+ type RepositoryStatus,
25
31
  type SettingStatus,
26
32
  type SyncNowResult,
27
33
  type SyncStatus,
@@ -175,7 +181,7 @@ const FIELDS: Record<
175
181
  },
176
182
  allowedEmailDomains: {
177
183
  label: 'Allowed email domains',
178
- help: 'Only people with an address at these domains can sign in this way. Separate several with commas. Leave blank to allow any address: safe with a provider that only serves your organisation, risky with one that does not.',
184
+ help: 'Only people with an address at these domains can sign in through this provider. Separate several with commas. Leave blank to allow any address: safe with a provider that only serves your organisation, risky with one that does not.',
179
185
  placeholder: 'example.com',
180
186
  },
181
187
  auditRetentionDays: {
@@ -193,6 +199,16 @@ const FIELDS: Record<
193
199
  */
194
200
  const REQUIRED_KEYS = ['kbRepoUrl', 'gitToken', 'defaultBranch', 'protectedBranches'];
195
201
 
202
+ /** The half of those every deployment owes, however it has its repository. */
203
+ const BRANCH_MODEL_KEYS = ['defaultBranch', 'protectedBranches'];
204
+
205
+ /** What each way of having a repository is called on its tab. */
206
+ const GIT_MODE_LABEL: Record<GitMode, string> = {
207
+ managed: 'Managed for you',
208
+ 'github-app': 'GitHub',
209
+ token: 'Address and token',
210
+ };
211
+
196
212
  /**
197
213
  * The answers a connection test actually proves — the address, the credential
198
214
  * and the name that goes beside it.
@@ -204,6 +220,17 @@ const REQUIRED_KEYS = ['kbRepoUrl', 'gitToken', 'defaultBranch', 'protectedBranc
204
220
  */
205
221
  const CONNECTION_KEYS = ['kbRepoUrl', 'gitToken', 'gitUsername'];
206
222
 
223
+ /**
224
+ * The settings each way of having a repository is answered by, on its tab.
225
+ * What belongs to a tab is sent only while that tab is open, and a refusal
226
+ * about it opens that tab.
227
+ */
228
+ const TAB_KEYS: Record<GitMode, readonly string[]> = {
229
+ managed: [],
230
+ 'github-app': ['githubRepository'],
231
+ token: CONNECTION_KEYS,
232
+ };
233
+
207
234
  /**
208
235
  * The answers the sign-in check proves. Editing one invalidates its result on
209
236
  * screen; the scopes, the button text and the allowed domains are not among
@@ -270,6 +297,56 @@ const SECTIONS: { id: SettingStatus['section']; title: string; blurb: string }[]
270
297
  },
271
298
  ];
272
299
 
300
+ /**
301
+ * What the first run asks: where the knowledge lives, which the gate waits
302
+ * on, and how people sign in, which decides who can follow the admin in.
303
+ * Everything else is a preference with a working default. It is set on the
304
+ * Deployment page, by someone who has seen the product it configures; on the
305
+ * first screen it only made two questions look like five.
306
+ */
307
+ const FIRST_RUN_SECTIONS: readonly SettingStatus['section'][] = ['knowledge-base', 'sign-in'];
308
+
309
+ /** The two ways of signing in the section can show, when the distribution runs one. */
310
+ type SignInTab = 'managed' | 'own';
311
+
312
+ /**
313
+ * A section's fields. Alone in their section they are its content as it has
314
+ * always been; beside a distribution's tab they are the panel of theirs, so
315
+ * the tab that names them has something to name.
316
+ */
317
+ function SectionFields({
318
+ panel,
319
+ children,
320
+ }: {
321
+ /** The tab these fields are the panel of: which set of tabs, and which one. Null: no tabs. */
322
+ panel: { group: 'sign-in' | 'repository'; id: string } | null;
323
+ children: ReactNode;
324
+ }) {
325
+ if (panel === null) return <>{children}</>;
326
+ return (
327
+ <div
328
+ role="tabpanel"
329
+ id={`${panel.group}-panel-${panel.id}`}
330
+ aria-labelledby={`${panel.group}-tab-${panel.id}`}
331
+ className="space-y-6"
332
+ >
333
+ {children}
334
+ </div>
335
+ );
336
+ }
337
+
338
+ /**
339
+ * The distribution's panel sits inside the settings form, and a browser
340
+ * submits a form when Enter is pressed in any text input in it. On first
341
+ * run that submit finishes setup and leaves the screen, from a field that
342
+ * had nothing to do with it. Held here once, so no panel has to remember:
343
+ * Enter in one of its inputs is the panel's own key. A text area keeps its
344
+ * new line and a button its press, neither of which submits anything.
345
+ */
346
+ function keepEnterFromTheForm(event: KeyboardEvent<HTMLElement>) {
347
+ if (event.key === 'Enter' && event.target instanceof HTMLInputElement) event.preventDefault();
348
+ }
349
+
273
350
  interface Props {
274
351
  settings: SettingStatus[];
275
352
  /**
@@ -305,6 +382,12 @@ interface Props {
305
382
  kbInit?: KbInitFailure;
306
383
  /** Whether the single sign-on configuration in effect is proven. Absent from an older server. */
307
384
  oidcVerification?: OidcVerification;
385
+ /**
386
+ * The ways this deployment can have its repository, and the one it is on.
387
+ * Absent from a server that knows one way only: the address and the token,
388
+ * drawn as they always were, with no tabs.
389
+ */
390
+ repository?: RepositoryStatus;
308
391
  }
309
392
 
310
393
  /**
@@ -328,8 +411,39 @@ function sameFailure(a: KbInitFailure, b: KbInitFailure): boolean {
328
411
  return a.kind === b.kind && a.cause === b.cause;
329
412
  }
330
413
 
331
- export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit, oidcVerification }: Props) {
332
- const [draft, setDraft] = useState<Record<string, string>>({});
414
+ export function SetupScreen({
415
+ settings,
416
+ onSaved,
417
+ variant = 'setup',
418
+ sync,
419
+ kbInit,
420
+ oidcVerification,
421
+ repository,
422
+ }: Props) {
423
+ /** Whether a setting is a secret: what is never written to the browser's storage. */
424
+ const isSecret = (key: string) => settings.find((s) => s.key === key)?.secret !== false;
425
+ /**
426
+ * What was typed before the browser left for GitHub, put back now that it
427
+ * has returned (see `kept-draft.ts`). Only on a return from GitHub, which
428
+ * the address says: what was kept for a trip that was abandoned is not
429
+ * sprung on someone who opens the screen later. And only for settings
430
+ * that are still this form's to edit.
431
+ */
432
+ const [restored] = useState(() => {
433
+ const back = new URLSearchParams(window.location.search ?? '').has('github');
434
+ const kept = back ? keptDraft(isSecret) : { draft: {}, dropped: [] };
435
+ const editableNow = (key: string) => settings.some((s) => s.key === key && s.source !== 'env');
436
+ return {
437
+ draft: Object.fromEntries(Object.entries(kept.draft).filter(([key]) => editableNow(key))),
438
+ dropped: kept.dropped.filter(editableNow),
439
+ };
440
+ });
441
+ // Read once, then gone: in an effect, since the page may be built twice
442
+ // before it is shown and must find the same thing both times.
443
+ useEffect(() => forgetDraft(), []);
444
+ const [draft, setDraft] = useState<Record<string, string>>(restored.draft);
445
+ /** The secrets that were typed before the trip and not kept, until each is entered again. */
446
+ const toEnterAgain = restored.dropped.filter((key) => !draft[key]?.trim());
333
447
  /**
334
448
  * The initialization failure on screen: the status endpoint's, until a save
335
449
  * or a retry from this screen answers more recently. A fresh status read
@@ -416,6 +530,126 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
416
530
 
417
531
  const editable = settings.filter((s) => s.source !== 'env');
418
532
  const fromEnv = settings.filter((s) => s.source === 'env');
533
+ const sections = variant === 'setup' ? SECTIONS.filter((s) => FIRST_RUN_SECTIONS.includes(s.id)) : SECTIONS;
534
+
535
+ /**
536
+ * The distribution's own way of signing in, when it runs one, and which of
537
+ * the two tabs is open. The section opens on the way that is in effect: the
538
+ * deployment's own provider once it has one, the distribution's until then.
539
+ * Chosen once, from what was stored when the screen opened, so typing an
540
+ * issuer does not move the reader to another tab.
541
+ */
542
+ const { signInOption } = useAppRegistry();
543
+ const ownProviderConfigured = OIDC_KEYS.every((key) => settings.find((s) => s.key === key)?.configured === true);
544
+ const [signInTab, setSignInTab] = useState<SignInTab>(
545
+ // What was being typed about the deployment's own provider is shown, not put back out of sight.
546
+ ownProviderConfigured || [...Object.keys(restored.draft), ...restored.dropped].some((key) => OIDC_KEYS.includes(key))
547
+ ? 'own'
548
+ : 'managed',
549
+ );
550
+
551
+ /**
552
+ * The way the deployment has its repository, and which one's tab is open.
553
+ * THE TAB IS THE CHOICE: what is open when Save is pressed is what the
554
+ * deployment is on afterwards. It opens on the way CHOSEN, so a
555
+ * deployment that is configured stays as it is unless its admin opens
556
+ * another tab, and on the first way offered for one that has none.
557
+ * Absent from a server that knows one way only, which is then the only
558
+ * thing drawn.
559
+ *
560
+ * Two ways are told apart: the one IN EFFECT, which the running
561
+ * deployment is on, and the one chosen, which a restart puts in effect.
562
+ * They differ between a move and the restart the move owes.
563
+ */
564
+ const inEffect = repository?.mode ?? null;
565
+ const chosen = repository ? (repository.chosen ?? repository.mode) : null;
566
+ const [gitTab, setGitTab] = useState<GitMode>(() => {
567
+ // Back from a round trip to GitHub: that tab is where it started, and
568
+ // where what came of it is said.
569
+ const backFromGitHub =
570
+ !repository?.pinned &&
571
+ repository?.modes.includes('github-app') &&
572
+ new URLSearchParams(window.location.search ?? '').has('github');
573
+ return backFromGitHub ? 'github-app' : (chosen ?? repository?.modes[0] ?? 'token');
574
+ });
575
+ /** The tab a setting is answered on, for the ones that belong to one way of having a repository. */
576
+ const tabOf = (key: string): GitMode | null =>
577
+ repository ? ((Object.keys(TAB_KEYS) as GitMode[]).find((mode) => TAB_KEYS[mode].includes(key)) ?? null) : null;
578
+ /** Whether the repository is reached by an address and a token: the fields, the test, the proof. */
579
+ const byAddress = !repository || gitTab === 'token';
580
+ /** A move was saved and the restart it owes has not happened: the deployment is still on the way it was. */
581
+ const movePending = inEffect !== null && chosen !== inEffect;
582
+ /**
583
+ * Saving NOW would choose another way than the one chosen, on a deployment
584
+ * that has a repository: a move, or a move taken back. This is what is
585
+ * asked about at the button, where the decision is made; an open tab is
586
+ * not a decision.
587
+ */
588
+ const savingMoves = inEffect !== null && gitTab !== chosen;
589
+ const movingTo = GIT_MODE_LABEL[gitTab];
590
+ const movingFrom = inEffect ? GIT_MODE_LABEL[inEffect] : '';
591
+ /** A save that moves the deployment away from the way in effect. Taking a move back is not one. */
592
+ const mustConfirmMove = savingMoves && gitTab !== inEffect;
593
+ /**
594
+ * The way the admin said yes to moving TO. Held as the way and not as a
595
+ * yes, so a yes given to one move is not a yes to another: opening a
596
+ * third tab asks again.
597
+ */
598
+ const [moveConfirmed, setMoveConfirmed] = useState<GitMode | null>(null);
599
+ const moveIsConfirmed = moveConfirmed === gitTab;
600
+
601
+ /**
602
+ * What a save sends about the repository, given what was typed: the way
603
+ * chosen, when it is not the one in effect, and only the answers of the
604
+ * tab that is open. Something entered on a tab that was then left is not
605
+ * an answer, and must not be stored beside the choice of another.
606
+ */
607
+ function chosenRepository(typed: Record<string, string>): Record<string, string> {
608
+ if (!repository) return typed;
609
+ const kept = Object.fromEntries(
610
+ Object.entries(typed).filter(([key]) => {
611
+ const tab = tabOf(key);
612
+ return tab === null || tab === gitTab;
613
+ }),
614
+ );
615
+ // Against the way CHOSEN: a move that is pending was already sent, and
616
+ // opening the tab of the way in effect is how it is taken back.
617
+ return gitTab === chosen ? kept : { ...kept, gitMode: gitTab };
618
+ }
619
+
620
+ /**
621
+ * Whether the form has a place for this field at all: it is the admin's to
622
+ * edit, and its section is one this variant shows. A field on a tab that
623
+ * is not open still counts — the tab is opened for it (see
624
+ * `showProblems`), where a field the form never draws has nowhere to be
625
+ * shown.
626
+ */
627
+ const hasPlace = (key: string) =>
628
+ // A setting with no field of its own (the way the repository is had,
629
+ // which is chosen by its tab) has nowhere to hold a message. The
630
+ // repository on GitHub has one, drawn by that tab's panel.
631
+ (FIELDS[key] !== undefined || (key === 'githubRepository' && !!repository?.modes.includes('github-app'))) &&
632
+ editable.some((s) => s.key === key && sections.some((section) => section.id === s.section));
633
+
634
+ /**
635
+ * Put a refused save's problems where the reader will see them. EVERY
636
+ * problem ends up on screen, which is the one property this function is
637
+ * for: one about a field the form draws goes beside that field, with the
638
+ * tab it lives on opened; one about a field the form does not draw goes
639
+ * to the message line. A save that failed must never look like a save
640
+ * that did nothing.
641
+ */
642
+ function showProblems(found: Record<string, string>) {
643
+ setProblems(found);
644
+ const placed = Object.keys(found).filter(hasPlace);
645
+ if (signInOption && placed.some((key) => settings.find((s) => s.key === key)?.section === 'sign-in')) {
646
+ setSignInTab('own');
647
+ }
648
+ const owner = placed.map(tabOf).find((tab) => tab !== null);
649
+ if (owner) setGitTab(owner);
650
+ const unplaced = Object.entries(found).filter(([key]) => !hasPlace(key));
651
+ if (unplaced.length > 0) setError(unplaced.map(([, message]) => message).join(' '));
652
+ }
419
653
 
420
654
  /**
421
655
  * Whether saving now would actually change this connection field: something
@@ -467,6 +701,8 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
467
701
  * blank form is the server's to complain about, field by field.
468
702
  */
469
703
  const mustProveConnection =
704
+ // A repository the deployment keeps has no host to prove anything to.
705
+ byAddress &&
470
706
  !!resolved('kbRepoUrl') &&
471
707
  (variant === 'setup' || CONNECTION_KEYS.some((key) => connectionKeyChanged(key)));
472
708
 
@@ -826,7 +1062,7 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
826
1062
  // this deployment still needs…" directly above this attempt's "Not saved."
827
1063
  setStillMissing([]);
828
1064
  try {
829
- let payload = draft;
1065
+ let payload = chosenRepository(draft);
830
1066
  /** What the host said this time, or null when it could not be asked. */
831
1067
  let proven: ConnectionTest | null = test;
832
1068
  let probed = false;
@@ -875,6 +1111,8 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
875
1111
  // above fills the same fields from the same answer.
876
1112
  if (
877
1113
  !probed &&
1114
+ // Only a repository reached by its address is asked this way.
1115
+ byAddress &&
878
1116
  // No address, nothing to look a branch up in.
879
1117
  !!resolvedIn(payload, 'kbRepoUrl') &&
880
1118
  (!resolvedIn(payload, 'defaultBranch') || !resolvedIn(payload, 'protectedBranches'))
@@ -893,8 +1131,12 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
893
1131
  // accepts a batch that answers only some of what it needs. Saying so is
894
1132
  // the difference between a form that looks broken and one that tells you
895
1133
  // what is left.
1134
+ // An address and a token are owed only by a deployment that reaches
1135
+ // its repository by them; the branch model by every one.
1136
+ const way = result.repository ? (result.repository.chosen ?? result.repository.mode) : 'token';
1137
+ const owed = (way ?? 'token') === 'token' ? REQUIRED_KEYS : BRANCH_MODEL_KEYS;
896
1138
  const missing = result.settings
897
- .filter((setting) => REQUIRED_KEYS.includes(setting.key) && !setting.configured)
1139
+ .filter((setting) => owed.includes(setting.key) && !setting.configured)
898
1140
  .map((setting) => FIELDS[setting.key]?.label ?? setting.key);
899
1141
  setStillMissing(result.complete || result.awaitingRestart ? [] : missing);
900
1142
  if (result.awaitingRestart) {
@@ -922,14 +1164,7 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
922
1164
  onSaved();
923
1165
  } catch (err) {
924
1166
  if (err instanceof SettingsProblems) {
925
- setProblems(err.problems);
926
- // A problem about a field this form does not render — the server's
927
- // connection check blaming a token the environment supplies, say —
928
- // would otherwise vanish, leaving a save that failed silently.
929
- const unshown = Object.entries(err.problems).filter(
930
- ([key]) => !editable.some((s) => s.key === key),
931
- );
932
- if (unshown.length > 0) setError(unshown.map(([, message]) => message).join(' '));
1167
+ showProblems(err.problems);
933
1168
  } else if (err instanceof KbInitFailed) {
934
1169
  // The values ARE stored — only the initialization failed. The form
935
1170
  // shows what was saved, and the banner says what to fix and retries
@@ -1107,14 +1342,25 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1107
1342
  <>
1108
1343
  <h1 className="text-display font-semibold text-ink">Set up this deployment</h1>
1109
1344
  <p className="mt-2 max-w-[62ch] text-lede text-ink-muted">
1110
- One thing is needed before anyone can use it: somewhere to keep your knowledge,
1111
- skills and tools. Connect a repository below, test it, and the rest fills itself in.
1112
- Single sign-on is optional and can wait.
1345
+ {repository
1346
+ ? 'One thing is needed before anyone can use it: somewhere to keep your knowledge, skills and tools. Choose where below; this deployment can keep it for you. Single sign-on is optional and can wait.'
1347
+ : 'One thing is needed before anyone can use it: somewhere to keep your knowledge, skills and tools. Connect a repository below, test it, and the rest fills itself in. Single sign-on is optional and can wait.'}
1113
1348
  </p>
1114
1349
  </>
1115
1350
  )}
1116
1351
 
1117
1352
  <div ref={noticeRef}>
1353
+ {/* Back from GitHub, with a secret that was typed before the trip
1354
+ and not kept. Said, because an empty field that was full a
1355
+ minute ago otherwise reads as something that went wrong. It
1356
+ goes as each is entered again. */}
1357
+ {toEnterAgain.length > 0 && (
1358
+ <Banner tone="wait" role="status" className="mt-6" data-testid="enter-again">
1359
+ What you had entered is back, except{' '}
1360
+ {toEnterAgain.map((key) => FIELDS[key]?.label ?? key).join(', ')}. For safety, a secret is not kept while
1361
+ the browser is away: enter {toEnterAgain.length === 1 ? 'it' : 'them'} again.
1362
+ </Banner>
1363
+ )}
1118
1364
  {/* Saved, but the knowledge base behind the gate was never set up.
1119
1365
  The cause is the server's classified sentence — what to fix, not
1120
1366
  what git said — and the retry needs nothing re-entered. */}
@@ -1180,12 +1426,32 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1180
1426
  Marketplace section: the button is the last thing on the page, but
1181
1427
  Marketplace is deliberately not part of this form (see below). */}
1182
1428
  <form id="setup-settings-form" onSubmit={submit} className="mt-8 space-y-10">
1183
- {SECTIONS.map((section) => {
1184
- const fields = editable.filter((s) => s.section === section.id);
1429
+ {sections.map((section) => {
1430
+ const fields = editable.filter(
1431
+ (s) =>
1432
+ s.section === section.id &&
1433
+ // Chosen by its tab, not typed into a field.
1434
+ s.key !== 'gitMode' &&
1435
+ // Chosen from a list, by the panel of its tab.
1436
+ s.key !== 'githubRepository' &&
1437
+ // The address, the token and the name beside it belong to the
1438
+ // one way that reaches a repository by them.
1439
+ (byAddress || !CONNECTION_KEYS.includes(s.key)),
1440
+ );
1441
+ // The ways of having a repository, each on a tab of its own.
1442
+ const repositoryTabs = section.id === 'knowledge-base' ? repository : undefined;
1185
1443
  // A section whose every field comes from the environment has
1186
1444
  // nothing to offer — the locked list at the bottom already names
1187
1445
  // them, and an empty heading would read as something missing.
1188
1446
  if (fields.length === 0) return null;
1447
+ // Two ways of signing in, each on a tab of its own.
1448
+ const tabbed = section.id === 'sign-in' && signInOption !== undefined;
1449
+ const redirectUri = (
1450
+ <p className="mt-1.5 text-meta text-ink-faint">
1451
+ Redirect URI:{' '}
1452
+ <code className="font-mono">{`${window.location.origin}/api/auth/oidc/callback`}</code>
1453
+ </p>
1454
+ );
1189
1455
  return (
1190
1456
  <Surface
1191
1457
  key={section.id}
@@ -1206,14 +1472,147 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1206
1472
  <span className="text-meta text-ink-faint">Optional</span>
1207
1473
  )}
1208
1474
  </div>
1209
- <p className="mt-1 max-w-[60ch] text-detail text-ink-muted">{section.blurb}</p>
1210
- {section.id === 'sign-in' && (
1211
- <p className="mt-1.5 text-meta text-ink-faint">
1212
- Redirect URI:{' '}
1213
- <code className="font-mono">{`${window.location.origin}/api/auth/oidc/callback`}</code>
1214
- </p>
1215
- )}
1475
+ <p className="mt-1 max-w-[60ch] text-detail text-ink-muted">
1476
+ {repositoryTabs
1477
+ ? 'Where everything lives, together in one git repository: knowledge, skills and tools. Choose where that repository is.'
1478
+ : section.blurb}
1479
+ </p>
1480
+ {/* About the deployment's own provider: under the heading
1481
+ when that is all the section holds, inside its tab when
1482
+ the section has two. */}
1483
+ {section.id === 'sign-in' && !tabbed && redirectUri}
1216
1484
  </div>
1485
+ {section.id === 'sign-in' && signInOption && (
1486
+ <>
1487
+ <div role="tablist" aria-label="How people sign in" className="flex gap-1 border-b border-line">
1488
+ {(
1489
+ [
1490
+ ['managed', signInOption.label],
1491
+ ['own', signInOption.ownProviderLabel ?? 'Your own provider'],
1492
+ ] as const
1493
+ ).map(([id, label]) => (
1494
+ <button
1495
+ key={id}
1496
+ type="button"
1497
+ role="tab"
1498
+ id={`sign-in-tab-${id}`}
1499
+ aria-selected={signInTab === id}
1500
+ aria-controls={`sign-in-panel-${id}`}
1501
+ onClick={() => setSignInTab(id)}
1502
+ className={`-mb-px border-b-2 px-3 py-2 text-detail font-medium ${
1503
+ signInTab === id
1504
+ ? 'border-accent text-ink'
1505
+ : 'border-transparent text-ink-muted hover:text-ink'
1506
+ }`}
1507
+ >
1508
+ {label}
1509
+ </button>
1510
+ ))}
1511
+ </div>
1512
+ {signInTab === 'managed' && (
1513
+ <div
1514
+ role="tabpanel"
1515
+ id="sign-in-panel-managed"
1516
+ aria-labelledby="sign-in-tab-managed"
1517
+ onKeyDown={keepEnterFromTheForm}
1518
+ >
1519
+ {/* The distribution's code: a throw in it costs this
1520
+ tab, not the form the repository is entered on. */}
1521
+ <SlotBoundary label="sign-in panel">
1522
+ <signInOption.Panel variant={variant} ownProviderConfigured={ownProviderConfigured} />
1523
+ </SlotBoundary>
1524
+ </div>
1525
+ )}
1526
+ </>
1527
+ )}
1528
+
1529
+ {/* The section's own fields: every field it has, in ONE
1530
+ place. With a distribution's tab beside them they are
1531
+ the second tab's panel, there while that tab is open;
1532
+ what was typed stays in the draft either way, so
1533
+ switching tabs loses nothing and saves what was entered. */}
1534
+ {(!tabbed || signInTab === 'own') && (
1535
+ <SectionFields panel={tabbed ? { group: 'sign-in', id: 'own' } : null}>
1536
+ {tabbed && redirectUri}
1537
+ {repositoryTabs && (
1538
+ <div role="tablist" aria-label="Where the repository is" className="flex gap-1 border-b border-line">
1539
+ {repositoryTabs.modes.map((mode) => (
1540
+ <button
1541
+ key={mode}
1542
+ type="button"
1543
+ role="tab"
1544
+ id={`repository-tab-${mode}`}
1545
+ aria-selected={gitTab === mode}
1546
+ aria-controls={`repository-panel-${mode}`}
1547
+ // Chosen by the environment, the choice is not the
1548
+ // screen's: the other ways are shown, as what they
1549
+ // are, and cannot be opened.
1550
+ disabled={repositoryTabs.pinned !== undefined && mode !== gitTab}
1551
+ onClick={() => setGitTab(mode)}
1552
+ className={`-mb-px border-b-2 px-3 py-2 text-detail font-medium disabled:cursor-not-allowed disabled:opacity-50 ${
1553
+ gitTab === mode
1554
+ ? 'border-accent text-ink'
1555
+ : 'border-transparent text-ink-muted hover:text-ink'
1556
+ }`}
1557
+ >
1558
+ {GIT_MODE_LABEL[mode]}
1559
+ </button>
1560
+ ))}
1561
+ </div>
1562
+ )}
1563
+ {repositoryTabs?.pinned && (
1564
+ <p className="text-meta text-ink-faint" data-testid="repository-pinned">
1565
+ Set by the <span className="font-mono">{repositoryTabs.pinned}</span> environment variable. Change it
1566
+ there.
1567
+ </p>
1568
+ )}
1569
+ {/* What the open tab asks: nothing, for a repository the
1570
+ deployment keeps; the address and the token, and the test
1571
+ that proves them, for one reached by them. */}
1572
+ <SectionFields panel={repositoryTabs ? { group: 'repository', id: gitTab } : null}>
1573
+ {repositoryTabs && movePending && gitTab === chosen && (
1574
+ <Banner tone="wait" role="status" data-testid="move-pending">
1575
+ A restart is pending. This deployment is still working on &ldquo;{movingFrom}&rdquo; and moves
1576
+ here when it is restarted. To stay where it is, open &ldquo;{movingFrom}&rdquo; and save.
1577
+ </Banner>
1578
+ )}
1579
+ {repositoryTabs && savingMoves && gitTab !== inEffect && (
1580
+ <Banner tone="wait" role="status" data-testid="moves-repository">
1581
+ Saving moves this deployment to another repository, which starts without what the
1582
+ current one holds. Nothing is deleted: the current repository is left as it is, and
1583
+ this deployment&rsquo;s working copies of it are set aside at the next restart.
1584
+ </Banner>
1585
+ )}
1586
+ {repositoryTabs && savingMoves && gitTab === inEffect && (
1587
+ <Banner tone="wait" role="status" data-testid="move-taken-back">
1588
+ Saving takes the move back: this deployment stays on the repository it is working on.
1589
+ </Banner>
1590
+ )}
1591
+ {repositoryTabs && gitTab === 'github-app' && (
1592
+ <GitHubRepositoryPanel
1593
+ repository={resolved('githubRepository')}
1594
+ onChoose={(name) => set('githubRepository', name)}
1595
+ problem={problems.githubRepository}
1596
+ disabled={saving}
1597
+ // The browser is about to leave for GitHub, and the page
1598
+ // that comes back is a new one.
1599
+ onLeaving={() => keepDraft(draft, isSecret)}
1600
+ />
1601
+ )}
1602
+ {repositoryTabs && gitTab === 'managed' && (
1603
+ <div className="space-y-2" data-testid="managed-repository">
1604
+ <p className="max-w-[60ch] text-detail text-ink">
1605
+ This deployment keeps the repository itself. There is nothing to connect and
1606
+ nothing to enter.
1607
+ </p>
1608
+ <p className="max-w-[60ch] text-meta text-ink-muted">
1609
+ Everything is versioned as it is with any repository: every change is a commit,
1610
+ and changes are reviewed as change requests. The repository is stored with this
1611
+ deployment&rsquo;s backups, so backing those up backs it up. You can move to a
1612
+ repository of your own later.
1613
+ </p>
1614
+ </div>
1615
+ )}
1217
1616
  {fields
1218
1617
  .filter(
1219
1618
  (f) =>
@@ -1236,7 +1635,7 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1236
1635
  section, so the answer to "did I type the token right?"
1237
1636
  was below the identity-provider questions and, once the
1238
1637
  page grew, below the fold entirely. */}
1239
- {section.id === 'knowledge-base' && (
1638
+ {section.id === 'knowledge-base' && byAddress && (
1240
1639
  <Surface tone="sunken" radius="md" className="p-4">
1241
1640
  <div className="flex flex-wrap items-center gap-3">
1242
1641
  <Button
@@ -1272,6 +1671,7 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1272
1671
  )}
1273
1672
  </Surface>
1274
1673
  )}
1674
+ </SectionFields>
1275
1675
 
1276
1676
  {/* The layout: the three root folders and the agent guide's
1277
1677
  file name, in the main section, directly under the test
@@ -1320,6 +1720,8 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1320
1720
  sync &&
1321
1721
  !fields.some((f) => f.key === 'kbSyncSecret') &&
1322
1722
  renderSyncPanel()}
1723
+ </SectionFields>
1724
+ )}
1323
1725
  </Surface>
1324
1726
  );
1325
1727
  })}
@@ -1354,25 +1756,49 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1354
1756
  )}
1355
1757
  </form>
1356
1758
 
1357
- {/* Outside the form: nothing in it is saved by "Save and continue",
1358
- and on first run it is optional — the gate never waits on it. The
1359
- same section in both variants, so first run and Deployment
1360
- settings cannot drift. */}
1361
- <MarketplaceSection variant={variant} />
1759
+ {/* Outside the form: nothing in it is saved by "Save and continue".
1760
+ On the Deployment page only: the first run asks for the
1761
+ repository and for sign-in (see FIRST_RUN_SECTIONS). */}
1762
+ {variant === 'settings' && <MarketplaceSection variant={variant} />}
1362
1763
 
1363
1764
  {/* The submit button lives HERE, after Marketplace, though it belongs
1364
1765
  to the form above — `form=` is what lets those two facts hold at
1365
1766
  once. It is the last thing on the page because a reader should
1366
- meet every section, Marketplace included, before the control that
1367
- leaves the screen; when it sat above Marketplace, the page looked
1368
- finished while a section was still below it.
1767
+ meet every section, Marketplace included where it is shown, before
1768
+ the control that leaves the screen; when it sat above Marketplace,
1769
+ the page looked finished while a section was still below it.
1369
1770
 
1370
1771
  A rejected connection stops here rather than at the far side of
1371
1772
  it. Saving these answers would finish setup — the server checks
1372
1773
  that they are present, not that they work — and open the app onto
1373
1774
  a repository it cannot reach, which reads as a broken product
1374
1775
  rather than a wrong token. */}
1375
- <div className="mt-10 flex flex-wrap items-center justify-end gap-3">
1776
+ {/* ASKED WHERE THE DECISION IS MADE. The tab is the choice, and the
1777
+ tab is a screen above this button: an admin who opened another
1778
+ way to read about it, then changed something else and saved,
1779
+ would have moved the deployment to an empty repository. So a
1780
+ save that moves is a save the admin has said yes to, here,
1781
+ naming what is left and what is moved to. */}
1782
+ {mustConfirmMove && (
1783
+ <Surface tone="sunken" radius="md" className="mt-10 p-4" data-testid="confirm-move">
1784
+ <label className="flex items-start gap-2.5 text-detail text-ink">
1785
+ <input
1786
+ type="checkbox"
1787
+ className="mt-0.5"
1788
+ checked={moveConfirmed === gitTab}
1789
+ onChange={(e) => setMoveConfirmed(e.target.checked ? gitTab : null)}
1790
+ />
1791
+ <span>
1792
+ Move this deployment from &ldquo;{movingFrom}&rdquo; to &ldquo;{movingTo}&rdquo;.
1793
+ <span className="mt-1 block text-meta text-ink-muted">
1794
+ It starts on a repository without what the current one holds. Nothing is deleted, and the move takes
1795
+ effect when the deployment is restarted.
1796
+ </span>
1797
+ </span>
1798
+ </label>
1799
+ </Surface>
1800
+ )}
1801
+ <div className={`${mustConfirmMove ? 'mt-4' : 'mt-10'} flex flex-wrap items-center justify-end gap-3`}>
1376
1802
  {connectionRejected && (
1377
1803
  // Before the button in the DOM so the reason is read first, and
1378
1804
  // so `justify-end` leaves the button itself at the right edge.
@@ -1389,12 +1815,12 @@ export function SetupScreen({ settings, onSaved, variant = 'setup', sync, kbInit
1389
1815
  type="submit"
1390
1816
  form="setup-settings-form"
1391
1817
  variant="primary"
1392
- disabled={saving || testing || retrying || oidcTesting || connectionRejected}
1818
+ disabled={saving || testing || retrying || oidcTesting || connectionRejected || (mustConfirmMove && !moveIsConfirmed)}
1393
1819
  // Described by the refusal, so a reader who lands on a button
1394
1820
  // that will not move is told why rather than left guessing.
1395
1821
  aria-describedby={connectionRejected ? 'connection-refusal' : undefined}
1396
1822
  >
1397
- {saving ? 'Saving…' : 'Save and continue'}
1823
+ {saving ? 'Saving…' : mustConfirmMove ? 'Save and move' : 'Save and continue'}
1398
1824
  </Button>
1399
1825
  </div>
1400
1826