@bevel-software/platform-core-frontend 0.1.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 (203) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +70 -0
  3. package/src/core/CoreAppShell.tsx +444 -0
  4. package/src/core/__tests__/ShellRoutes.test.tsx +95 -0
  5. package/src/core/events.ts +29 -0
  6. package/src/core/registry.ts +336 -0
  7. package/src/css-side-effect-imports.d.ts +7 -0
  8. package/src/html-extensions.ts +12 -0
  9. package/src/index.css +49 -0
  10. package/src/index.ts +23 -0
  11. package/src/lib/api.ts +57 -0
  12. package/src/lib/utils.ts +138 -0
  13. package/src/modules/access/__tests__/ManageAccessDialog.test.tsx +130 -0
  14. package/src/modules/access/__tests__/useFileAccess.test.tsx +253 -0
  15. package/src/modules/access/api.ts +205 -0
  16. package/src/modules/access/components/AccessRestrictedBanner.tsx +31 -0
  17. package/src/modules/access/components/ManageAccessDialog.tsx +1232 -0
  18. package/src/modules/access/components/NodeOwnersBanner.tsx +30 -0
  19. package/src/modules/access/hooks/useFileAccess.ts +151 -0
  20. package/src/modules/admin/components/AdminRolesPage.tsx +943 -0
  21. package/src/modules/admin/components/RolesCorruptedBanner.tsx +74 -0
  22. package/src/modules/admin/services/admin.api.ts +92 -0
  23. package/src/modules/admin/services/roles.api.ts +136 -0
  24. package/src/modules/admin/state/admin.context.tsx +223 -0
  25. package/src/modules/auth/components/LoginScreen.tsx +135 -0
  26. package/src/modules/auth/hooks/useAuthState.ts +59 -0
  27. package/src/modules/auth/services/auth.api.ts +24 -0
  28. package/src/modules/auth/services/microsoft-oauth.ts +57 -0
  29. package/src/modules/auth/state/auth.context.ts +18 -0
  30. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +347 -0
  31. package/src/modules/git/__tests__/FileHistoryPanel.test.tsx +201 -0
  32. package/src/modules/git/__tests__/ProtectedBranchBanner.test.tsx +82 -0
  33. package/src/modules/git/__tests__/PullNeededBanner.test.tsx +441 -0
  34. package/src/modules/git/__tests__/git.api.test.ts +113 -0
  35. package/src/modules/git/__tests__/pr.api.test.ts +44 -0
  36. package/src/modules/git/__tests__/useAutoPullUpdates.test.tsx +212 -0
  37. package/src/modules/git/components/BranchSwitcher.tsx +527 -0
  38. package/src/modules/git/components/DiffView.tsx +30 -0
  39. package/src/modules/git/components/FileComparisonPanel.tsx +377 -0
  40. package/src/modules/git/components/FileHistoryPanel.tsx +320 -0
  41. package/src/modules/git/components/ProtectedBranchBanner.tsx +42 -0
  42. package/src/modules/git/components/PullNeededBanner.tsx +204 -0
  43. package/src/modules/git/components/PullRequestsForMe.tsx +244 -0
  44. package/src/modules/git/components/UnifiedDiffView.tsx +37 -0
  45. package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +99 -0
  46. package/src/modules/git/hooks/useAutoPullUpdates.ts +121 -0
  47. package/src/modules/git/hooks/useGitState.ts +335 -0
  48. package/src/modules/git/services/__tests__/error-messages.test.ts +134 -0
  49. package/src/modules/git/services/error-messages.ts +257 -0
  50. package/src/modules/git/services/git.api.ts +163 -0
  51. package/src/modules/git/services/pr-prompts.ts +159 -0
  52. package/src/modules/git/services/pr.api.ts +39 -0
  53. package/src/modules/git/state/auto-update.context.ts +21 -0
  54. package/src/modules/git/state/git.context.ts +82 -0
  55. package/src/modules/layout/components/AppLayout.tsx +40 -0
  56. package/src/modules/layout/components/DemoBanner.tsx +57 -0
  57. package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -0
  58. package/src/modules/layout/components/MobileChatLayout.tsx +153 -0
  59. package/src/modules/layout/components/ResizableThreePaneLayout.tsx +203 -0
  60. package/src/modules/layout/components/SlideOverlay.tsx +224 -0
  61. package/src/modules/layout/components/__tests__/MobileChatLayout.test.tsx +77 -0
  62. package/src/modules/layout/components/__tests__/ResizableThreePaneLayout.test.tsx +57 -0
  63. package/src/modules/layout/hooks/useMediaQuery.ts +23 -0
  64. package/src/modules/layout/state/__tests__/layout.context.test.tsx +39 -0
  65. package/src/modules/layout/state/layout.context.ts +46 -0
  66. package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -0
  67. package/src/modules/library/__tests__/diff.test.ts +62 -0
  68. package/src/modules/library/__tests__/loadout.test.tsx +72 -0
  69. package/src/modules/library/__tests__/status.test.ts +117 -0
  70. package/src/modules/library/components/ChangeRequestDock.tsx +47 -0
  71. package/src/modules/library/components/CompareView.tsx +357 -0
  72. package/src/modules/library/components/DetailDialog.tsx +566 -0
  73. package/src/modules/library/components/GearIcon.tsx +12 -0
  74. package/src/modules/library/components/LibraryCard.tsx +132 -0
  75. package/src/modules/library/components/LibraryPage.tsx +254 -0
  76. package/src/modules/library/components/LoadoutSidebar.tsx +156 -0
  77. package/src/modules/library/components/StatusGem.tsx +21 -0
  78. package/src/modules/library/components/SuggestChange.tsx +171 -0
  79. package/src/modules/library/components/particles.ts +50 -0
  80. package/src/modules/library/hooks/useLibraryData.ts +119 -0
  81. package/src/modules/library/hooks/useSkillDetail.ts +74 -0
  82. package/src/modules/library/library.css +320 -0
  83. package/src/modules/library/services/library.api.ts +164 -0
  84. package/src/modules/library/state/loadout.tsx +129 -0
  85. package/src/modules/library/state/toast.tsx +42 -0
  86. package/src/modules/library/utils/diff.ts +102 -0
  87. package/src/modules/library/utils/status.ts +116 -0
  88. package/src/modules/pr/__tests__/PrApprovalBadge.test.tsx +93 -0
  89. package/src/modules/pr/__tests__/PrCancelButton.test.tsx +158 -0
  90. package/src/modules/pr/__tests__/PrCancelConfirmDialog.test.tsx +44 -0
  91. package/src/modules/pr/__tests__/PrMergeButton.test.tsx +360 -0
  92. package/src/modules/pr/__tests__/bucketize-comments.test.ts +85 -0
  93. package/src/modules/pr/__tests__/parsePatch.test.ts +104 -0
  94. package/src/modules/pr/__tests__/usePrViewerState.test.ts +72 -0
  95. package/src/modules/pr/components/OpenChangeRequestDialog.tsx +193 -0
  96. package/src/modules/pr/components/PrApprovalBadge.tsx +80 -0
  97. package/src/modules/pr/components/PrCancelButton.tsx +99 -0
  98. package/src/modules/pr/components/PrCancelConfirmDialog.tsx +119 -0
  99. package/src/modules/pr/components/PrCommentComposer.tsx +92 -0
  100. package/src/modules/pr/components/PrCommentThread.tsx +168 -0
  101. package/src/modules/pr/components/PrCommentsPanel.tsx +143 -0
  102. package/src/modules/pr/components/PrDiffPane.tsx +425 -0
  103. package/src/modules/pr/components/PrFileRow.tsx +164 -0
  104. package/src/modules/pr/components/PrFilesList.tsx +53 -0
  105. package/src/modules/pr/components/PrHeader.tsx +45 -0
  106. package/src/modules/pr/components/PrHeaderOverflowMenu.tsx +58 -0
  107. package/src/modules/pr/components/PrMergeButton.tsx +268 -0
  108. package/src/modules/pr/components/PrMergeConfirmDialog.tsx +112 -0
  109. package/src/modules/pr/components/PrRefreshFromTargetButton.tsx +110 -0
  110. package/src/modules/pr/components/PrViewer.tsx +364 -0
  111. package/src/modules/pr/hooks/usePrViewerState.ts +150 -0
  112. package/src/modules/pr/services/pr-approvals.api.ts +31 -0
  113. package/src/modules/pr/services/pr-cancel.api.ts +19 -0
  114. package/src/modules/pr/services/pr-comments.api.ts +45 -0
  115. package/src/modules/pr/services/pr-detail.api.ts +17 -0
  116. package/src/modules/pr/services/pr-merge.api.ts +53 -0
  117. package/src/modules/pr/services/pr-open.api.ts +30 -0
  118. package/src/modules/pr/state/pr-viewer.context.ts +34 -0
  119. package/src/modules/pr/utils/bucketize-comments.ts +55 -0
  120. package/src/modules/pr/utils/parsePatch.ts +57 -0
  121. package/src/modules/review/__tests__/review.api.test.ts +121 -0
  122. package/src/modules/review/components/BinaryChangePlaceholder.tsx +15 -0
  123. package/src/modules/review/components/DiffViewer.tsx +78 -0
  124. package/src/modules/review/components/MarkdownDiffViewer.tsx +263 -0
  125. package/src/modules/review/components/ReviewFileRow.tsx +114 -0
  126. package/src/modules/review/components/ReviewPanel.tsx +260 -0
  127. package/src/modules/review/components/ReviewPanelSurface.tsx +52 -0
  128. package/src/modules/review/components/__tests__/ReviewPanel.test.tsx +106 -0
  129. package/src/modules/review/hooks/__tests__/useReviewState.test.ts +88 -0
  130. package/src/modules/review/hooks/useReviewState.ts +256 -0
  131. package/src/modules/review/services/review.api.ts +72 -0
  132. package/src/modules/review/state/ReviewProvider.tsx +28 -0
  133. package/src/modules/review/state/review.context.ts +29 -0
  134. package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +488 -0
  135. package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -0
  136. package/src/modules/secrets-vault/components/ToolSecretsPanel.tsx +362 -0
  137. package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -0
  138. package/src/modules/secrets-vault/services/connect.api.ts +113 -0
  139. package/src/modules/secrets-vault/services/secrets.api.ts +82 -0
  140. package/src/modules/secrets-vault/services/tool-secrets.api.ts +116 -0
  141. package/src/modules/toolbar/components/AdminMenu.tsx +250 -0
  142. package/src/modules/toolbar/components/AppSwitcher.tsx +108 -0
  143. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -0
  144. package/src/modules/toolbar/components/Toolbar.tsx +123 -0
  145. package/src/modules/toolbar/components/__tests__/AppSwitcher.test.tsx +122 -0
  146. package/src/modules/toolbar/components/__tests__/Toolbar.test.tsx +353 -0
  147. package/src/modules/toolbar/services/external-api-keys.api.ts +65 -0
  148. package/src/modules/tools/ToolsExplorerPage.tsx +380 -0
  149. package/src/modules/workflow/hooks/useEventBusSubscription.ts +24 -0
  150. package/src/modules/workflow/hooks/useFileLock.ts +430 -0
  151. package/src/modules/workflow/services/lock.api.ts +139 -0
  152. package/src/modules/workflow/state/EventBusFocusBinder.tsx +25 -0
  153. package/src/modules/workflow/state/EventBusProvider.tsx +232 -0
  154. package/src/modules/workflow/state/event-bus.context.ts +75 -0
  155. package/src/modules/workspace/__tests__/testFixtures.ts +54 -0
  156. package/src/modules/workspace/__tests__/useWorkspaceState.test.ts +91 -0
  157. package/src/modules/workspace/components/EditorTabs.tsx +384 -0
  158. package/src/modules/workspace/components/FileExplorer.tsx +1068 -0
  159. package/src/modules/workspace/components/FileRoute.tsx +392 -0
  160. package/src/modules/workspace/components/FileViewer.tsx +906 -0
  161. package/src/modules/workspace/components/__tests__/EditorTabs.test.tsx +241 -0
  162. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +666 -0
  163. package/src/modules/workspace/components/__tests__/FileRoute.test.tsx +330 -0
  164. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +492 -0
  165. package/src/modules/workspace/components/renderers/CsvRenderer.tsx +174 -0
  166. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +87 -0
  167. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +231 -0
  168. package/src/modules/workspace/components/renderers/ImageRenderer.tsx +50 -0
  169. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +287 -0
  170. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +252 -0
  171. package/src/modules/workspace/components/renderers/MermaidDiagram.tsx +268 -0
  172. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +81 -0
  173. package/src/modules/workspace/components/renderers/TextRenderer.tsx +86 -0
  174. package/src/modules/workspace/components/renderers/ToolForm.tsx +404 -0
  175. package/src/modules/workspace/components/renderers/ToolRenderer.tsx +417 -0
  176. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +157 -0
  177. package/src/modules/workspace/components/renderers/__tests__/CsvRenderer.test.ts +57 -0
  178. package/src/modules/workspace/components/renderers/__tests__/KbMarkdownView.test.tsx +95 -0
  179. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +328 -0
  180. package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +167 -0
  181. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +318 -0
  182. package/src/modules/workspace/components/renderers/csvUtils.ts +76 -0
  183. package/src/modules/workspace/components/renderers/htmlSandbox.ts +332 -0
  184. package/src/modules/workspace/components/renderers/index.ts +39 -0
  185. package/src/modules/workspace/components/renderers/types.ts +27 -0
  186. package/src/modules/workspace/hooks/__tests__/useWorkspaceState.test.tsx +298 -0
  187. package/src/modules/workspace/hooks/useWorkspaceState.ts +1315 -0
  188. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +99 -0
  189. package/src/modules/workspace/routing/kb-routes.ts +266 -0
  190. package/src/modules/workspace/services/__tests__/workspace.api.test.ts +72 -0
  191. package/src/modules/workspace/services/workspace.api.ts +185 -0
  192. package/src/modules/workspace/state/workspace.context.ts +235 -0
  193. package/src/modules/workspace/utils/__tests__/frontmatter.test.ts +107 -0
  194. package/src/modules/workspace/utils/diff.ts +53 -0
  195. package/src/modules/workspace/utils/fileTree.ts +179 -0
  196. package/src/modules/workspace/utils/frontmatter.ts +80 -0
  197. package/src/modules/workspace/utils/readDroppedEntries.ts +119 -0
  198. package/src/modules/workspace/utils/tab-persistence.ts +23 -0
  199. package/src/shared/components/Dialog.tsx +198 -0
  200. package/src/shared/components/PageShell.tsx +51 -0
  201. package/src/shared/components/useModalLayer.ts +35 -0
  202. package/src/shared/markdown/Markdown.tsx +62 -0
  203. package/src/test-setup.ts +59 -0
@@ -0,0 +1,193 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import {
3
+ DEFAULT_BRANCH,
4
+ protectedBranchDisplayName,
5
+ } from '@bevel-software/platform-shared';
6
+ import { Dialog } from '../../../shared/components/Dialog';
7
+ import { useGit } from '../../git/state/git.context';
8
+ import { openChangeRequest } from '../services/pr-open.api';
9
+ import { PR_STALE_EVENT } from '../../../core/events';
10
+
11
+ interface Props {
12
+ open: boolean;
13
+ onClose(): void;
14
+ /** The shared draft the change request is opened from. */
15
+ sourceBranch: string;
16
+ /** Preselected destination, when the caller already picked one. */
17
+ initialTargetBranch?: string;
18
+ }
19
+
20
+ /**
21
+ * Direct change-request creation — the core default behind the
22
+ * change-request port. Collects a title / optional description / destination
23
+ * and posts to the existing `POST /api/workflow/change-requests` endpoint.
24
+ * Deployments with the chat module registered never see this dialog (their
25
+ * registry routes "Propose changes" through the agent instead); it exists so
26
+ * the core shell has a working proposal path on its own.
27
+ *
28
+ * Copy follows docs/glossary.md: "shared draft", "change request",
29
+ * "apply to", protected branches shown via `protectedBranchDisplayName`.
30
+ */
31
+ export function OpenChangeRequestDialog({
32
+ open,
33
+ onClose,
34
+ sourceBranch,
35
+ initialTargetBranch,
36
+ }: Props) {
37
+ const git = useGit();
38
+ const [title, setTitle] = useState('');
39
+ const [description, setDescription] = useState('');
40
+ const [targetBranch, setTargetBranch] = useState(
41
+ initialTargetBranch ?? DEFAULT_BRANCH,
42
+ );
43
+ const [busy, setBusy] = useState(false);
44
+ const [error, setError] = useState<string | null>(null);
45
+
46
+ // Fresh form per open — a dismissed proposal shouldn't leak into the next.
47
+ useEffect(() => {
48
+ if (!open) return;
49
+ setTitle('');
50
+ setDescription('');
51
+ setTargetBranch(initialTargetBranch ?? DEFAULT_BRANCH);
52
+ setError(null);
53
+ }, [open, initialTargetBranch]);
54
+
55
+ // Destinations: anything but the draft itself. Protected branches first
56
+ // (default branch leading) — same ordering as the BranchSwitcher's target
57
+ // picker so both surfaces read identically.
58
+ const targets = useMemo(
59
+ () =>
60
+ git.branches
61
+ .filter((b) => b.name !== sourceBranch)
62
+ .sort((a, b) => {
63
+ if (a.isProtected !== b.isProtected) return a.isProtected ? -1 : 1;
64
+ if (a.isProtected && b.isProtected) {
65
+ if (a.name === DEFAULT_BRANCH) return -1;
66
+ if (b.name === DEFAULT_BRANCH) return 1;
67
+ }
68
+ return a.name.localeCompare(b.name);
69
+ }),
70
+ [git.branches, sourceBranch],
71
+ );
72
+
73
+ async function submit() {
74
+ if (busy || !title.trim()) return;
75
+ setBusy(true);
76
+ setError(null);
77
+ try {
78
+ await openChangeRequest({
79
+ sourceBranch,
80
+ targetBranch,
81
+ title: title.trim(),
82
+ description: description.trim() || undefined,
83
+ });
84
+ // Same refresh signal every other CR-mutating surface fires — the
85
+ // "Change requests for you" sidebar refetches immediately.
86
+ window.dispatchEvent(new CustomEvent(PR_STALE_EVENT));
87
+ onClose();
88
+ } catch (err) {
89
+ setError(
90
+ err instanceof Error ? err.message : "Couldn't open the change request.",
91
+ );
92
+ } finally {
93
+ setBusy(false);
94
+ }
95
+ }
96
+
97
+ return (
98
+ <Dialog
99
+ open={open}
100
+ onClose={onClose}
101
+ title="Propose changes"
102
+ size="md"
103
+ busy={busy}
104
+ footer={
105
+ <>
106
+ <button
107
+ type="button"
108
+ onClick={onClose}
109
+ disabled={busy}
110
+ className="px-3 py-1.5 text-xs rounded hover:bg-slate-100 text-slate-700 disabled:opacity-50"
111
+ >
112
+ Cancel
113
+ </button>
114
+ <button
115
+ type="button"
116
+ onClick={() => void submit()}
117
+ disabled={busy || !title.trim()}
118
+ className="px-3 py-1.5 text-xs rounded bg-emerald-700 hover:bg-emerald-600 text-white font-medium disabled:opacity-50 disabled:cursor-not-allowed"
119
+ >
120
+ {busy ? 'Opening…' : 'Open change request'}
121
+ </button>
122
+ </>
123
+ }
124
+ >
125
+ <div className="flex flex-col gap-3 text-sm">
126
+ <p className="text-xs text-slate-600">
127
+ Propose your shared draft{' '}
128
+ <span className="font-mono font-medium text-slate-700">
129
+ {sourceBranch}
130
+ </span>{' '}
131
+ as a change request. Approvers can review it before it's applied.
132
+ </p>
133
+
134
+ <label className="flex flex-col gap-1">
135
+ <span className="text-xs font-medium text-slate-700">
136
+ What did you change?
137
+ </span>
138
+ <input
139
+ autoFocus
140
+ type="text"
141
+ value={title}
142
+ onChange={(e) => setTitle(e.target.value)}
143
+ onKeyDown={(e) => {
144
+ if (e.key === 'Enter') void submit();
145
+ }}
146
+ placeholder="e.g. Add owner to Checkout process"
147
+ className="w-full bg-white border border-slate-300 rounded px-2 py-1.5 text-sm focus:outline-none focus:border-slate-400"
148
+ />
149
+ </label>
150
+
151
+ <label className="flex flex-col gap-1">
152
+ <span className="text-xs font-medium text-slate-700">
153
+ Anything approvers should know?{' '}
154
+ <span className="font-normal text-slate-500">(optional)</span>
155
+ </span>
156
+ <textarea
157
+ value={description}
158
+ onChange={(e) => setDescription(e.target.value)}
159
+ rows={3}
160
+ className="w-full bg-white border border-slate-300 rounded px-2 py-1.5 text-sm focus:outline-none focus:border-slate-400 resize-y"
161
+ />
162
+ </label>
163
+
164
+ <label className="flex flex-col gap-1">
165
+ <span className="text-xs font-medium text-slate-700">Apply to</span>
166
+ <select
167
+ value={targetBranch}
168
+ onChange={(e) => setTargetBranch(e.target.value)}
169
+ className="w-full bg-white border border-slate-300 rounded px-2 py-1.5 text-sm focus:outline-none focus:border-slate-400"
170
+ >
171
+ {targets.length === 0 && (
172
+ <option value={targetBranch}>
173
+ {protectedBranchDisplayName(targetBranch) ?? targetBranch}
174
+ </option>
175
+ )}
176
+ {targets.map((b) => (
177
+ <option key={b.name} value={b.name}>
178
+ {protectedBranchDisplayName(b.name) ?? b.name}
179
+ {b.name === DEFAULT_BRANCH ? ' (default)' : ''}
180
+ </option>
181
+ ))}
182
+ </select>
183
+ </label>
184
+
185
+ {error && (
186
+ <div role="alert" className="text-xs text-red-600">
187
+ {error}
188
+ </div>
189
+ )}
190
+ </div>
191
+ </Dialog>
192
+ );
193
+ }
@@ -0,0 +1,80 @@
1
+ import { Check, Clock } from 'lucide-react';
2
+ import type { FileApprovalState } from '@bevel-software/platform-shared';
3
+
4
+ interface Props {
5
+ state: FileApprovalState | undefined;
6
+ size?: number;
7
+ }
8
+
9
+ function eligibleSummary(state: FileApprovalState): string {
10
+ const parts: string[] = [];
11
+ if (state.eligibleApprovers.roles.length) parts.push(state.eligibleApprovers.roles.join(', '));
12
+ if (state.eligibleApprovers.users.length) {
13
+ parts.push(
14
+ state.eligibleApprovers.users
15
+ .map((u) => (u.name ? u.name : u.email))
16
+ .join(', '),
17
+ );
18
+ }
19
+ return parts.join('; ') || 'someone with write access';
20
+ }
21
+
22
+ /**
23
+ * Small inline badge for the file row. Only renders for files the merge gate
24
+ * cares about — md files with at least one eligible approver. Non-md files
25
+ * and md files outside the access-controlled surface (no role/user grants
26
+ * write) are outside the gate, so we show no badge at all to keep the file
27
+ * list quiet.
28
+ *
29
+ * ✓ green — at least one eligible approver has a non-stale approval.
30
+ * ⏳ amber — only stale approvals exist; needs re-review after the latest push.
31
+ * · grey — no approval yet; default waiting state.
32
+ */
33
+ export function PrApprovalBadge({ state, size = 12 }: Props) {
34
+ if (!state) return null;
35
+ const isMd = state.path.toLowerCase().endsWith('.md');
36
+ const hasEligible =
37
+ state.eligibleApprovers.roles.length > 0 || state.eligibleApprovers.users.length > 0;
38
+ if (!isMd || !hasEligible) return null;
39
+
40
+ if (state.isApproved) {
41
+ const label = `Confirmed by ${eligibleSummary(state)}`;
42
+ return (
43
+ <span
44
+ role="img"
45
+ aria-label={label}
46
+ className="flex items-center gap-0.5 text-emerald-600 shrink-0"
47
+ title={label}
48
+ >
49
+ <Check size={size} />
50
+ </span>
51
+ );
52
+ }
53
+
54
+ const staleCount = state.approvedBy.filter((a) => a.isStale).length;
55
+ if (staleCount > 0) {
56
+ const label = 'Confirmation outdated — please re-confirm after the latest edits';
57
+ return (
58
+ <span
59
+ role="img"
60
+ aria-label={label}
61
+ className="flex items-center gap-0.5 text-amber-600 shrink-0"
62
+ title={label}
63
+ >
64
+ <Clock size={size} />
65
+ </span>
66
+ );
67
+ }
68
+
69
+ const label = `Waiting on ${eligibleSummary(state)}`;
70
+ return (
71
+ <span
72
+ role="img"
73
+ aria-label={label}
74
+ className="flex items-center gap-0.5 text-slate-600 shrink-0"
75
+ title={label}
76
+ >
77
+ <Clock size={size} />
78
+ </span>
79
+ );
80
+ }
@@ -0,0 +1,99 @@
1
+ import { useRef, useState } from 'react';
2
+ import { XCircle } from 'lucide-react';
3
+ import type { PullRequestDetail } from '@bevel-software/platform-shared';
4
+ import { cancelPullRequest } from '../services/pr-cancel.api';
5
+ import { friendlyGitError } from '../../git/services/error-messages';
6
+ import { PrCancelConfirmDialog } from './PrCancelConfirmDialog';
7
+ import { PR_STALE_EVENT } from '../../../core/events';
8
+
9
+ interface Props {
10
+ detail: PullRequestDetail;
11
+ /** Invoked after a successful cancel. The viewer uses this to close itself + refresh state. */
12
+ onCancelled(): void;
13
+ }
14
+
15
+ /**
16
+ * Secondary destructive action — `Cancel change request`. Mirrors PrMergeButton's
17
+ * shape (hidden on terminal state, disabled when authz lacks, inline error span,
18
+ * double-click guard, dispatches the `bevel:pr-stale` refresh event), but with
19
+ * red text-button styling so the primary purple Apply action stays the focus.
20
+ *
21
+ * Authority is server-computed via `detail.viewerCanCancel` (author OR admin on
22
+ * the base). The button disables — not hides — when the viewer can't cancel,
23
+ * so the affordance is discoverable but its absence-of-permission is explicit.
24
+ */
25
+ export function PrCancelButton({ detail, onCancelled }: Props) {
26
+ const [busy, setBusy] = useState(false);
27
+ const [error, setError] = useState<string | null>(null);
28
+ const [showDialog, setShowDialog] = useState(false);
29
+ // Cancel is irreversible — guard against rapid double-clicks / re-entrant
30
+ // dialog confirms, same as PrMergeButton's mergingRef pattern.
31
+ const cancellingRef = useRef(false);
32
+
33
+ if (detail.state !== 'open') return null;
34
+
35
+ const allowed = detail.viewerCanCancel;
36
+ const tooltip = allowed
37
+ ? 'Cancel this change request without applying it'
38
+ : 'Only the author or an admin can cancel this change request';
39
+
40
+ async function runCancel() {
41
+ if (cancellingRef.current) return;
42
+ cancellingRef.current = true;
43
+ setBusy(true);
44
+ setError(null);
45
+ try {
46
+ await cancelPullRequest(detail.number);
47
+ // Same cross-component refresh signal the merge button uses — the PR
48
+ // list panel and any other open viewers refetch on this event.
49
+ window.dispatchEvent(new CustomEvent(PR_STALE_EVENT));
50
+ setShowDialog(false);
51
+ onCancelled();
52
+ } catch (err) {
53
+ setError(friendlyGitError(err));
54
+ } finally {
55
+ setBusy(false);
56
+ cancellingRef.current = false;
57
+ }
58
+ }
59
+
60
+ return (
61
+ <div className="flex items-center gap-2">
62
+ {error && (
63
+ <span
64
+ role="alert"
65
+ aria-live="assertive"
66
+ className="text-[11px] text-red-700 max-w-xs truncate"
67
+ title={error}
68
+ >
69
+ {error}
70
+ </span>
71
+ )}
72
+ <button
73
+ type="button"
74
+ onClick={() => {
75
+ if (!allowed || busy) return;
76
+ setShowDialog(true);
77
+ }}
78
+ disabled={busy}
79
+ aria-disabled={!allowed || undefined}
80
+ title={tooltip}
81
+ aria-label="Cancel change request"
82
+ className="flex items-center gap-1.5 px-2 py-1 text-xs rounded whitespace-nowrap shrink-0 text-red-700 hover:bg-red-50 disabled:text-slate-400 disabled:hover:bg-transparent disabled:cursor-not-allowed aria-disabled:text-slate-400 aria-disabled:hover:bg-transparent aria-disabled:cursor-not-allowed"
83
+ >
84
+ <XCircle size={12} aria-hidden="true" />
85
+ {busy ? 'Cancelling…' : 'Cancel change request'}
86
+ </button>
87
+ {showDialog && (
88
+ <PrCancelConfirmDialog
89
+ busy={busy}
90
+ onConfirm={() => void runCancel()}
91
+ onCancel={() => {
92
+ if (busy) return;
93
+ setShowDialog(false);
94
+ }}
95
+ />
96
+ )}
97
+ </div>
98
+ );
99
+ }
@@ -0,0 +1,119 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { AlertTriangle, X, XCircle } from 'lucide-react';
3
+
4
+ interface Props {
5
+ busy: boolean;
6
+ onConfirm(): void;
7
+ onCancel(): void;
8
+ }
9
+
10
+ /**
11
+ * Plain are-you-sure confirm for the destructive Cancel action. No checkbox —
12
+ * unlike the merge bypass dialog, there's no rights-elevation happening here.
13
+ * Server already gates the action behind author-or-admin authority; the dialog
14
+ * is a behavioral gate (intent confirmation), not an authorization gate.
15
+ */
16
+ export function PrCancelConfirmDialog({ busy, onConfirm, onCancel }: Props) {
17
+ const panelRef = useRef<HTMLDivElement>(null);
18
+ const confirmRef = useRef<HTMLButtonElement>(null);
19
+
20
+ // Move initial focus into the dialog, trap Tab inside it, and restore focus
21
+ // to the previously-focused element on close — same pattern as DirtySwitchDialog.
22
+ useEffect(() => {
23
+ const previouslyFocused = document.activeElement as HTMLElement | null;
24
+ confirmRef.current?.focus();
25
+
26
+ function onKey(e: KeyboardEvent) {
27
+ if (e.key === 'Escape') {
28
+ if (!busy) onCancel();
29
+ return;
30
+ }
31
+ if (e.key !== 'Tab') return;
32
+ const panel = panelRef.current;
33
+ if (!panel) return;
34
+ const focusable = panel.querySelectorAll<HTMLElement>(
35
+ 'button:not([disabled]), [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
36
+ );
37
+ if (focusable.length === 0) return;
38
+ const first = focusable[0];
39
+ const last = focusable[focusable.length - 1];
40
+ const active = document.activeElement as HTMLElement | null;
41
+ if (e.shiftKey && active === first) {
42
+ e.preventDefault();
43
+ last.focus();
44
+ } else if (!e.shiftKey && active === last) {
45
+ e.preventDefault();
46
+ first.focus();
47
+ }
48
+ }
49
+ window.addEventListener('keydown', onKey);
50
+ return () => {
51
+ window.removeEventListener('keydown', onKey);
52
+ previouslyFocused?.focus?.();
53
+ };
54
+ }, [busy, onCancel]);
55
+
56
+ return (
57
+ <div
58
+ className="fixed inset-0 z-50 flex items-center justify-center bg-white/60"
59
+ role="dialog"
60
+ aria-modal="true"
61
+ aria-labelledby="pr-cancel-confirm-title"
62
+ >
63
+ <div
64
+ ref={panelRef}
65
+ className="w-[28rem] max-w-[90vw] bg-white border border-slate-200 rounded-md shadow-xl"
66
+ >
67
+ <div className="flex items-center gap-2 px-4 py-3 border-b border-slate-200">
68
+ <AlertTriangle size={14} className="text-red-600 shrink-0" aria-hidden="true" />
69
+ <h2
70
+ id="pr-cancel-confirm-title"
71
+ className="text-sm font-medium text-slate-900 flex-1"
72
+ >
73
+ Cancel this change request?
74
+ </h2>
75
+ <button
76
+ type="button"
77
+ onClick={onCancel}
78
+ disabled={busy}
79
+ aria-label="Keep change request open"
80
+ className="p-1 rounded text-slate-600 hover:text-slate-900 hover:bg-slate-100 disabled:opacity-40"
81
+ >
82
+ <X size={14} aria-hidden="true" />
83
+ </button>
84
+ </div>
85
+
86
+ <div className="px-4 py-3 space-y-2">
87
+ <p className="text-xs text-slate-700">
88
+ This won't delete your draft — you can still open a new change request
89
+ from the same draft later.
90
+ </p>
91
+ <p className="text-xs text-slate-600">
92
+ The cancellation is recorded in the change request history.
93
+ </p>
94
+ </div>
95
+
96
+ <div className="flex items-center justify-end gap-2 px-4 py-3 border-t border-slate-200">
97
+ <button
98
+ type="button"
99
+ onClick={onCancel}
100
+ disabled={busy}
101
+ className="px-3 py-1 text-xs rounded text-slate-700 hover:bg-slate-100 disabled:opacity-40"
102
+ >
103
+ Keep open
104
+ </button>
105
+ <button
106
+ ref={confirmRef}
107
+ type="button"
108
+ onClick={onConfirm}
109
+ disabled={busy}
110
+ className="flex items-center gap-1.5 px-3 py-1 text-xs rounded bg-red-600 hover:bg-red-500 text-white disabled:bg-slate-100 disabled:text-slate-600 disabled:cursor-not-allowed"
111
+ >
112
+ <XCircle size={12} aria-hidden="true" />
113
+ {busy ? 'Cancelling…' : 'Cancel change request'}
114
+ </button>
115
+ </div>
116
+ </div>
117
+ </div>
118
+ );
119
+ }
@@ -0,0 +1,92 @@
1
+ import { useState } from 'react';
2
+ import { Send } from 'lucide-react';
3
+
4
+ interface Props {
5
+ placeholder?: string;
6
+ submitLabel?: string;
7
+ autoFocus?: boolean;
8
+ disabled?: boolean;
9
+ /** Prefill for edit mode. Uncontrolled — changes after mount are ignored. */
10
+ initialValue?: string;
11
+ onSubmit(body: string): Promise<void>;
12
+ onCancel?(): void;
13
+ }
14
+
15
+ /**
16
+ * Plain textarea + submit button. Keeps its own draft state — parents just
17
+ * observe `onSubmit` and replace/refresh whatever they need. Enter submits,
18
+ * shift+Enter inserts a newline, Escape cancels when `onCancel` is provided.
19
+ */
20
+ export function PrCommentComposer({
21
+ placeholder = 'Leave a comment…',
22
+ submitLabel = 'Comment',
23
+ autoFocus,
24
+ disabled,
25
+ initialValue,
26
+ onSubmit,
27
+ onCancel,
28
+ }: Props) {
29
+ const [body, setBody] = useState(initialValue ?? '');
30
+ const [busy, setBusy] = useState(false);
31
+ const [error, setError] = useState<string | null>(null);
32
+
33
+ async function handleSubmit() {
34
+ const trimmed = body.trim();
35
+ if (!trimmed || busy || disabled) return;
36
+ setBusy(true);
37
+ setError(null);
38
+ try {
39
+ await onSubmit(trimmed);
40
+ setBody('');
41
+ } catch (err) {
42
+ setError(err instanceof Error ? err.message : 'Failed to post comment');
43
+ } finally {
44
+ setBusy(false);
45
+ }
46
+ }
47
+
48
+ return (
49
+ <div className="flex flex-col gap-1.5">
50
+ <textarea
51
+ autoFocus={autoFocus}
52
+ value={body}
53
+ onChange={(e) => setBody(e.target.value)}
54
+ onKeyDown={(e) => {
55
+ if (e.key === 'Enter' && !e.shiftKey) {
56
+ e.preventDefault();
57
+ handleSubmit();
58
+ } else if (e.key === 'Escape' && onCancel) {
59
+ e.preventDefault();
60
+ onCancel();
61
+ }
62
+ }}
63
+ disabled={disabled || busy}
64
+ placeholder={placeholder}
65
+ rows={3}
66
+ className="w-full bg-white border border-slate-200 rounded px-2 py-1.5 text-xs text-slate-900 placeholder-slate-500 resize-none focus:outline-none focus:border-slate-300 disabled:opacity-60"
67
+ />
68
+ {error && <div className="text-[10px] text-red-600">{error}</div>}
69
+ <div className="flex items-center gap-1.5 justify-end">
70
+ {onCancel && (
71
+ <button
72
+ type="button"
73
+ onClick={onCancel}
74
+ disabled={busy}
75
+ className="px-2 py-1 text-xs rounded text-slate-600 hover:text-slate-900 hover:bg-slate-100 disabled:opacity-50"
76
+ >
77
+ Cancel
78
+ </button>
79
+ )}
80
+ <button
81
+ type="button"
82
+ onClick={handleSubmit}
83
+ disabled={!body.trim() || busy || disabled}
84
+ className="flex items-center gap-1.5 px-2.5 py-1 text-xs rounded bg-emerald-700 hover:bg-emerald-600 text-white disabled:bg-slate-100 disabled:text-slate-600 disabled:cursor-not-allowed"
85
+ >
86
+ <Send size={11} />
87
+ {busy ? 'Posting…' : submitLabel}
88
+ </button>
89
+ </div>
90
+ </div>
91
+ );
92
+ }