@bevel-software/platform-core-frontend 0.4.0 → 0.4.1
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/__tests__/ShellRoutes.test.tsx +95 -95
- package/src/lib/utils.ts +138 -138
- package/src/modules/admin/__tests__/UserAccountsPage.test.tsx +156 -156
- package/src/modules/admin/components/UserAccountsPage.tsx +353 -353
- package/src/modules/layout/components/MaintenanceOverlay.tsx +104 -104
- package/src/modules/library/__tests__/DetailDialog.test.tsx +200 -200
- package/src/modules/library/__tests__/loadout.test.tsx +72 -72
- package/src/modules/library/__tests__/status.test.ts +117 -117
- package/src/modules/library/components/ChangeRequestDock.tsx +47 -47
- package/src/modules/library/components/CompareView.tsx +357 -357
- package/src/modules/library/components/DetailDialog.tsx +566 -566
- package/src/modules/library/components/GearIcon.tsx +12 -12
- package/src/modules/library/components/LibraryCard.tsx +132 -132
- package/src/modules/library/components/LibraryPage.tsx +254 -254
- package/src/modules/library/components/LoadoutSidebar.tsx +156 -156
- package/src/modules/library/components/StatusGem.tsx +21 -21
- package/src/modules/library/components/SuggestChange.tsx +171 -171
- package/src/modules/library/components/particles.ts +50 -50
- package/src/modules/library/library.css +320 -320
- package/src/modules/library/state/loadout.tsx +129 -129
- package/src/modules/library/utils/status.ts +116 -116
- package/src/modules/secrets-vault/components/SecretsPage.tsx +276 -276
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +51 -51
- package/src/modules/toolbar/components/AppSwitcher.tsx +108 -108
- package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +548 -548
- package/src/modules/workspace/components/FileExplorer.tsx +1068 -1068
- package/src/modules/workspace/components/FileViewer.tsx +906 -906
- package/src/modules/workspace/utils/fileTree.ts +179 -179
- package/src/shared/components/PageShell.tsx +51 -51
|
@@ -1,357 +1,357 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { useNavigate } from 'react-router-dom';
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_BRANCH,
|
|
5
|
-
protectedBranchDisplayName,
|
|
6
|
-
type PullRequestDetail,
|
|
7
|
-
type PullRequestSummary,
|
|
8
|
-
} from '@bevel-software/platform-shared';
|
|
9
|
-
import { useModalLayer } from '../../../shared/components/useModalLayer';
|
|
10
|
-
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
11
|
-
import { kbFileUrl } from '../../workspace/routing/kb-routes';
|
|
12
|
-
import { fetchPrDetail } from '../../pr/services/pr-detail.api';
|
|
13
|
-
import { mergePullRequest } from '../../pr/services/pr-merge.api';
|
|
14
|
-
import { cancelPullRequest } from '../../pr/services/pr-cancel.api';
|
|
15
|
-
import { postPrComment } from '../../pr/services/pr-comments.api';
|
|
16
|
-
import { readFileOnBranch, type LibrarySkill } from '../services/library.api';
|
|
17
|
-
import { diffLines, type DiffLine } from '../utils/diff';
|
|
18
|
-
|
|
19
|
-
interface CompareViewProps {
|
|
20
|
-
skill: LibrarySkill;
|
|
21
|
-
/** Content of a main-side file (SKILL.md body included), null while loading. */
|
|
22
|
-
mainContent(relFile: string): string | null;
|
|
23
|
-
loadMainFile(relFile: string): void;
|
|
24
|
-
cr: PullRequestSummary;
|
|
25
|
-
onClose(): void;
|
|
26
|
-
onResolved(kind: 'applied' | 'sent-back'): void;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Owner review = compare, not overlay: two panels side by side — left is the
|
|
31
|
-
* skill on the default branch, right is the change-request branch. Removed
|
|
32
|
-
* text tints red on the left, added text green on the right (via the module's
|
|
33
|
-
* line differ over real per-branch file contents); changed files get an amber
|
|
34
|
-
* dot in both lists, files new on the branch get a NEW row. Apply / Send back
|
|
35
|
-
* act through the existing change-request services. Esc exits the compare
|
|
36
|
-
* (registered as its own modal layer so the detail dialog beneath survives).
|
|
37
|
-
*/
|
|
38
|
-
export function CompareView({
|
|
39
|
-
skill,
|
|
40
|
-
mainContent,
|
|
41
|
-
loadMainFile,
|
|
42
|
-
cr,
|
|
43
|
-
onClose,
|
|
44
|
-
onResolved,
|
|
45
|
-
}: CompareViewProps) {
|
|
46
|
-
const navigate = useNavigate();
|
|
47
|
-
const { kbDirName } = useWorkspace();
|
|
48
|
-
const [detail, setDetail] = useState<PullRequestDetail | null>(null);
|
|
49
|
-
const [error, setError] = useState<string | null>(null);
|
|
50
|
-
const [branchContents, setBranchContents] = useState<Record<string, string | null>>({});
|
|
51
|
-
const [busy, setBusy] = useState(false);
|
|
52
|
-
const [sendBackOpen, setSendBackOpen] = useState(false);
|
|
53
|
-
const [sendBackNote, setSendBackNote] = useState('');
|
|
54
|
-
|
|
55
|
-
const isTop = useModalLayer(true);
|
|
56
|
-
useEffect(() => {
|
|
57
|
-
function onKeyDown(e: KeyboardEvent) {
|
|
58
|
-
if (e.key === 'Escape' && isTop()) {
|
|
59
|
-
e.stopPropagation();
|
|
60
|
-
onClose();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
document.addEventListener('keydown', onKeyDown);
|
|
64
|
-
return () => document.removeEventListener('keydown', onKeyDown);
|
|
65
|
-
}, [isTop, onClose]);
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
let cancelled = false;
|
|
69
|
-
fetchPrDetail(cr.number)
|
|
70
|
-
.then((d) => {
|
|
71
|
-
if (!cancelled) setDetail(d);
|
|
72
|
-
})
|
|
73
|
-
.catch((err) => {
|
|
74
|
-
if (!cancelled) setError(err instanceof Error ? err.message : "Couldn't load this change request.");
|
|
75
|
-
});
|
|
76
|
-
return () => {
|
|
77
|
-
cancelled = true;
|
|
78
|
-
};
|
|
79
|
-
}, [cr.number]);
|
|
80
|
-
|
|
81
|
-
const prefix = `${skill.path}/`;
|
|
82
|
-
const mainFiles = useMemo(
|
|
83
|
-
() => ['SKILL.md', ...skill.files.map((f) => f.slice(prefix.length))],
|
|
84
|
-
[skill.files, prefix],
|
|
85
|
-
);
|
|
86
|
-
const changedFiles = useMemo(() => {
|
|
87
|
-
const set = new Set<string>();
|
|
88
|
-
for (const f of detail?.files ?? []) {
|
|
89
|
-
if (f.path.startsWith(prefix)) set.add(f.path.slice(prefix.length));
|
|
90
|
-
}
|
|
91
|
-
return set;
|
|
92
|
-
}, [detail, prefix]);
|
|
93
|
-
const addedFiles = useMemo(
|
|
94
|
-
() =>
|
|
95
|
-
(detail?.files ?? [])
|
|
96
|
-
.filter((f) => f.status === 'added' && f.path.startsWith(prefix))
|
|
97
|
-
.map((f) => f.path.slice(prefix.length)),
|
|
98
|
-
[detail, prefix],
|
|
99
|
-
);
|
|
100
|
-
const outsideCount = useMemo(
|
|
101
|
-
() => (detail?.files ?? []).filter((f) => !f.path.startsWith(prefix)).length,
|
|
102
|
-
[detail, prefix],
|
|
103
|
-
);
|
|
104
|
-
const branchFiles = useMemo(
|
|
105
|
-
() => [...mainFiles, ...addedFiles.filter((f) => !mainFiles.includes(f))],
|
|
106
|
-
[mainFiles, addedFiles],
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
// Land on the first changed (or added) file once the detail is in.
|
|
110
|
-
const [selected, setSelected] = useState<string>('SKILL.md');
|
|
111
|
-
const landed = useRef(false);
|
|
112
|
-
useEffect(() => {
|
|
113
|
-
if (!detail || landed.current) return;
|
|
114
|
-
landed.current = true;
|
|
115
|
-
const first = branchFiles.find((f) => changedFiles.has(f));
|
|
116
|
-
if (first) setSelected(first);
|
|
117
|
-
}, [detail, branchFiles, changedFiles]);
|
|
118
|
-
|
|
119
|
-
const loadBranchFile = useCallback(
|
|
120
|
-
(rel: string) => {
|
|
121
|
-
if (branchContents[rel] !== undefined) return;
|
|
122
|
-
setBranchContents((c) => ({ ...c, [rel]: null }));
|
|
123
|
-
readFileOnBranch(cr.branch, `${skill.path}/${rel}`)
|
|
124
|
-
.then((content) => setBranchContents((c) => ({ ...c, [rel]: content })))
|
|
125
|
-
.catch(() => setBranchContents((c) => ({ ...c, [rel]: '' })));
|
|
126
|
-
},
|
|
127
|
-
[branchContents, cr.branch, skill.path],
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
useEffect(() => {
|
|
131
|
-
loadBranchFile(selected);
|
|
132
|
-
if (!addedFiles.includes(selected)) loadMainFile(selected);
|
|
133
|
-
}, [selected, loadBranchFile, loadMainFile, addedFiles]);
|
|
134
|
-
|
|
135
|
-
const isAdded = addedFiles.includes(selected);
|
|
136
|
-
const mainRaw = isAdded ? null : mainContent(selected);
|
|
137
|
-
const branchRaw = branchContents[selected] ?? null;
|
|
138
|
-
const diff: DiffLine[] | null =
|
|
139
|
-
!isAdded && mainRaw !== null && branchRaw !== null ? diffLines(mainRaw, branchRaw) : null;
|
|
140
|
-
|
|
141
|
-
async function applyChanges() {
|
|
142
|
-
setBusy(true);
|
|
143
|
-
setError(null);
|
|
144
|
-
try {
|
|
145
|
-
await mergePullRequest(cr.number);
|
|
146
|
-
onResolved('applied');
|
|
147
|
-
} catch (err) {
|
|
148
|
-
setError(err instanceof Error ? err.message : "Couldn't apply this change request.");
|
|
149
|
-
setBusy(false);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async function sendBack() {
|
|
154
|
-
setBusy(true);
|
|
155
|
-
setError(null);
|
|
156
|
-
try {
|
|
157
|
-
const note = sendBackNote.trim();
|
|
158
|
-
if (note) await postPrComment(cr.number, { body: note });
|
|
159
|
-
await cancelPullRequest(cr.number);
|
|
160
|
-
onResolved('sent-back');
|
|
161
|
-
} catch (err) {
|
|
162
|
-
setError(err instanceof Error ? err.message : "Couldn't send this change request back.");
|
|
163
|
-
setBusy(false);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const fileRow = (rel: string, side: 'main' | 'branch') => {
|
|
168
|
-
const added = addedFiles.includes(rel);
|
|
169
|
-
const active = selected === rel;
|
|
170
|
-
return (
|
|
171
|
-
<button
|
|
172
|
-
key={rel}
|
|
173
|
-
type="button"
|
|
174
|
-
className={`flex w-full items-center gap-2 rounded-[9px] border px-2.5 py-1.5 text-left transition-colors ${
|
|
175
|
-
active
|
|
176
|
-
? 'border-[#0d9488] bg-[#e6f7f4]'
|
|
177
|
-
: added
|
|
178
|
-
? 'border-[#a9e8cd] bg-[#f2fcf8] hover:border-[#7fd0c4]'
|
|
179
|
-
: 'border-slate-200 bg-[#fafbfd] hover:border-[#7fd0c4]'
|
|
180
|
-
}`}
|
|
181
|
-
onClick={() => setSelected(rel)}
|
|
182
|
-
>
|
|
183
|
-
<span className={`font-mono text-[11.5px] font-semibold ${active ? 'text-[#0f766e]' : 'text-slate-600'}`}>
|
|
184
|
-
{rel}
|
|
185
|
-
</span>
|
|
186
|
-
{rel === 'SKILL.md' && (
|
|
187
|
-
<span className="rounded-full bg-white px-1.5 text-[9.5px] font-bold tracking-[.04em] text-[#0f766e] shadow-[inset_0_0_0_1px_#d2e9e4]">
|
|
188
|
-
MAIN
|
|
189
|
-
</span>
|
|
190
|
-
)}
|
|
191
|
-
{added && side === 'branch' && (
|
|
192
|
-
<span className="rounded-full bg-[#e0f8ee] px-1.5 text-[9.5px] font-bold tracking-[.04em] text-[#0a8f60]">
|
|
193
|
-
NEW
|
|
194
|
-
</span>
|
|
195
|
-
)}
|
|
196
|
-
{!added && changedFiles.has(rel) && (
|
|
197
|
-
<span
|
|
198
|
-
className="h-[7px] w-[7px] shrink-0 rounded-full bg-[#f59e0b]"
|
|
199
|
-
title="Changed in this request"
|
|
200
|
-
/>
|
|
201
|
-
)}
|
|
202
|
-
</button>
|
|
203
|
-
);
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const lineView = (lines: DiffLine[] | null, raw: string | null, hide: 'added' | 'removed') => {
|
|
207
|
-
if (raw === null && lines === null) {
|
|
208
|
-
return <div className="p-3 text-xs text-slate-400">Loading…</div>;
|
|
209
|
-
}
|
|
210
|
-
const rendered = lines
|
|
211
|
-
? lines.filter((l) => l.kind !== hide)
|
|
212
|
-
: (raw ?? '').split('\n').map((text) => ({ kind: 'same' as const, text }));
|
|
213
|
-
return (
|
|
214
|
-
<pre className="whitespace-pre-wrap break-words rounded-[13px] border border-slate-200 bg-[#f7f9fb] p-3 font-mono text-[11.5px] leading-relaxed text-slate-600">
|
|
215
|
-
{rendered.map((l, i) => (
|
|
216
|
-
<div
|
|
217
|
-
key={i}
|
|
218
|
-
className={
|
|
219
|
-
l.kind === 'removed'
|
|
220
|
-
? 'lib-line-removed rounded px-1'
|
|
221
|
-
: l.kind === 'added'
|
|
222
|
-
? 'lib-line-added rounded px-1'
|
|
223
|
-
: 'px-1'
|
|
224
|
-
}
|
|
225
|
-
>
|
|
226
|
-
{l.text || ' '}
|
|
227
|
-
</div>
|
|
228
|
-
))}
|
|
229
|
-
</pre>
|
|
230
|
-
);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
return (
|
|
234
|
-
<div className="fixed inset-0 z-[60]" role="dialog" aria-modal="true" aria-label={`Compare change request: ${cr.title}`}>
|
|
235
|
-
<div className="absolute inset-0 bg-slate-950/40" onClick={onClose} />
|
|
236
|
-
<div className="absolute inset-x-4 bottom-[5vh] top-[5vh] flex gap-3.5">
|
|
237
|
-
{/* left: the default branch */}
|
|
238
|
-
<section className="relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-[20px] border border-slate-200 bg-white shadow-[0_30px_80px_rgba(22,35,58,0.30)]">
|
|
239
|
-
<div className="absolute left-6 right-6 top-0 h-[3px] rounded-full bg-gradient-to-r from-transparent via-[#10b981] to-transparent" />
|
|
240
|
-
<div className="flex items-center gap-3 px-5 pb-3 pt-5">
|
|
241
|
-
<div className="min-w-0">
|
|
242
|
-
<div className="truncate text-[17px] font-bold text-slate-800">{skill.name}</div>
|
|
243
|
-
<div className="text-[11px] uppercase tracking-[.08em] text-slate-400">
|
|
244
|
-
Skill · {protectedBranchDisplayName(DEFAULT_BRANCH)}
|
|
245
|
-
</div>
|
|
246
|
-
</div>
|
|
247
|
-
</div>
|
|
248
|
-
<div className="flex-1 overflow-y-auto px-5 pb-5">
|
|
249
|
-
<h4 className="mb-2 text-[10.5px] font-bold uppercase tracking-[.09em] text-slate-400">
|
|
250
|
-
Files on {protectedBranchDisplayName(DEFAULT_BRANCH)}
|
|
251
|
-
</h4>
|
|
252
|
-
<div className="mb-4 flex flex-col gap-1.5">{mainFiles.map((f) => fileRow(f, 'main'))}</div>
|
|
253
|
-
<h4 className="mb-2 font-mono text-[11px] text-slate-500">{selected}</h4>
|
|
254
|
-
{isAdded ? (
|
|
255
|
-
<div className="rounded-[13px] border border-dashed border-slate-200 p-4 text-center text-xs text-slate-400">
|
|
256
|
-
This file doesn't exist here yet
|
|
257
|
-
</div>
|
|
258
|
-
) : (
|
|
259
|
-
lineView(diff, mainRaw, 'added')
|
|
260
|
-
)}
|
|
261
|
-
</div>
|
|
262
|
-
</section>
|
|
263
|
-
|
|
264
|
-
{/* right: the change-request branch */}
|
|
265
|
-
<section className="relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-[20px] border border-slate-200 bg-white shadow-[0_30px_80px_rgba(22,35,58,0.30)]">
|
|
266
|
-
<div className="absolute left-6 right-6 top-0 h-[3px] rounded-full bg-gradient-to-r from-transparent via-[#0d9488] to-transparent" />
|
|
267
|
-
<div className="flex items-center gap-3 px-5 pb-3 pt-5">
|
|
268
|
-
<div className="min-w-0">
|
|
269
|
-
<div className="truncate text-[17px] font-bold text-slate-800">{cr.title}</div>
|
|
270
|
-
<div className="truncate text-[11px] text-slate-400">
|
|
271
|
-
<span className="font-mono">{cr.branch}</span>
|
|
272
|
-
{' · by '}
|
|
273
|
-
{cr.appAuthor?.name ?? cr.author.name ?? cr.author.login}
|
|
274
|
-
</div>
|
|
275
|
-
</div>
|
|
276
|
-
<div className="relative ml-auto flex shrink-0 items-center gap-2">
|
|
277
|
-
<button
|
|
278
|
-
type="button"
|
|
279
|
-
disabled={busy}
|
|
280
|
-
className="rounded-[10px] border border-[#f5c2c2] bg-[#fdecec] px-3.5 py-2 text-[12.5px] font-bold text-[#c53030] transition-transform hover:-translate-y-px disabled:opacity-60"
|
|
281
|
-
onClick={() => setSendBackOpen((o) => !o)}
|
|
282
|
-
>
|
|
283
|
-
Send back
|
|
284
|
-
</button>
|
|
285
|
-
<button
|
|
286
|
-
type="button"
|
|
287
|
-
disabled={busy}
|
|
288
|
-
className="rounded-[10px] bg-gradient-to-br from-[#0d9488] to-[#0f766e] px-3.5 py-2 text-[12.5px] font-bold text-white shadow-[0_4px_14px_rgba(13,148,136,0.22)] transition-transform hover:-translate-y-px disabled:opacity-60"
|
|
289
|
-
onClick={() => void applyChanges()}
|
|
290
|
-
>
|
|
291
|
-
{busy ? 'Working…' : 'Apply changes'}
|
|
292
|
-
</button>
|
|
293
|
-
<button
|
|
294
|
-
type="button"
|
|
295
|
-
aria-label="Close compare"
|
|
296
|
-
className="flex h-[30px] w-[30px] items-center justify-center rounded-full border border-slate-200 bg-white text-[13px] text-slate-500 hover:border-[#7fd0c4] hover:text-slate-800"
|
|
297
|
-
onClick={onClose}
|
|
298
|
-
>
|
|
299
|
-
✕
|
|
300
|
-
</button>
|
|
301
|
-
{sendBackOpen && (
|
|
302
|
-
<div className="absolute right-0 top-full z-10 mt-2 w-72 rounded-[14px] border border-slate-200 bg-white p-3.5 shadow-[0_16px_44px_rgba(22,35,58,0.24)]">
|
|
303
|
-
<label className="block text-[10px] font-bold uppercase tracking-[.07em] text-slate-400">
|
|
304
|
-
Note for the author (optional)
|
|
305
|
-
<textarea
|
|
306
|
-
rows={3}
|
|
307
|
-
className="mt-1 w-full resize-y rounded-lg border border-slate-200 px-2 py-1.5 text-xs font-normal normal-case tracking-normal text-slate-800 outline-none focus:border-[#0d9488]"
|
|
308
|
-
value={sendBackNote}
|
|
309
|
-
onChange={(e) => setSendBackNote(e.target.value)}
|
|
310
|
-
/>
|
|
311
|
-
</label>
|
|
312
|
-
<div className="mt-2 flex justify-end">
|
|
313
|
-
<button
|
|
314
|
-
type="button"
|
|
315
|
-
disabled={busy}
|
|
316
|
-
className="rounded-[10px] bg-[#fdecec] px-3.5 py-2 text-[12.5px] font-bold text-[#c53030] shadow-[inset_0_0_0_1px_#f3c4c4] disabled:opacity-60"
|
|
317
|
-
onClick={() => void sendBack()}
|
|
318
|
-
>
|
|
319
|
-
Send back
|
|
320
|
-
</button>
|
|
321
|
-
</div>
|
|
322
|
-
</div>
|
|
323
|
-
)}
|
|
324
|
-
</div>
|
|
325
|
-
</div>
|
|
326
|
-
{error && <div className="mx-5 mb-2 rounded-lg bg-[#fdecec] px-3 py-2 text-xs text-[#c53030]">{error}</div>}
|
|
327
|
-
<div className="flex-1 overflow-y-auto px-5 pb-5">
|
|
328
|
-
<h4 className="mb-2 text-[10.5px] font-bold uppercase tracking-[.09em] text-slate-400">
|
|
329
|
-
Files on this draft
|
|
330
|
-
{outsideCount > 0 && (
|
|
331
|
-
<span className="ml-2 normal-case tracking-normal text-[#6d28d9]">
|
|
332
|
-
+{outsideCount} file{outsideCount === 1 ? '' : 's'} outside this skill
|
|
333
|
-
</span>
|
|
334
|
-
)}
|
|
335
|
-
</h4>
|
|
336
|
-
<div className="mb-4 flex flex-col gap-1.5">{branchFiles.map((f) => fileRow(f, 'branch'))}</div>
|
|
337
|
-
<div className="mb-2 flex items-center justify-between">
|
|
338
|
-
<h4 className="font-mono text-[11px] text-slate-500">{selected}</h4>
|
|
339
|
-
<button
|
|
340
|
-
type="button"
|
|
341
|
-
className="rounded-lg bg-[#e0f8ee] px-3.5 py-1 text-[11.5px] font-bold text-[#0a8f60] shadow-[inset_0_0_0_1px_#a9e8cd] transition-transform hover:-translate-y-px"
|
|
342
|
-
onClick={() => {
|
|
343
|
-
if (kbDirName) {
|
|
344
|
-
navigate(kbFileUrl(cr.branch, `${kbDirName}/${skill.path}/${selected}`));
|
|
345
|
-
}
|
|
346
|
-
}}
|
|
347
|
-
>
|
|
348
|
-
Edit
|
|
349
|
-
</button>
|
|
350
|
-
</div>
|
|
351
|
-
{lineView(diff, branchRaw, 'removed')}
|
|
352
|
-
</div>
|
|
353
|
-
</section>
|
|
354
|
-
</div>
|
|
355
|
-
</div>
|
|
356
|
-
);
|
|
357
|
-
}
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { useNavigate } from 'react-router-dom';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_BRANCH,
|
|
5
|
+
protectedBranchDisplayName,
|
|
6
|
+
type PullRequestDetail,
|
|
7
|
+
type PullRequestSummary,
|
|
8
|
+
} from '@bevel-software/platform-shared';
|
|
9
|
+
import { useModalLayer } from '../../../shared/components/useModalLayer';
|
|
10
|
+
import { useWorkspace } from '../../workspace/state/workspace.context';
|
|
11
|
+
import { kbFileUrl } from '../../workspace/routing/kb-routes';
|
|
12
|
+
import { fetchPrDetail } from '../../pr/services/pr-detail.api';
|
|
13
|
+
import { mergePullRequest } from '../../pr/services/pr-merge.api';
|
|
14
|
+
import { cancelPullRequest } from '../../pr/services/pr-cancel.api';
|
|
15
|
+
import { postPrComment } from '../../pr/services/pr-comments.api';
|
|
16
|
+
import { readFileOnBranch, type LibrarySkill } from '../services/library.api';
|
|
17
|
+
import { diffLines, type DiffLine } from '../utils/diff';
|
|
18
|
+
|
|
19
|
+
interface CompareViewProps {
|
|
20
|
+
skill: LibrarySkill;
|
|
21
|
+
/** Content of a main-side file (SKILL.md body included), null while loading. */
|
|
22
|
+
mainContent(relFile: string): string | null;
|
|
23
|
+
loadMainFile(relFile: string): void;
|
|
24
|
+
cr: PullRequestSummary;
|
|
25
|
+
onClose(): void;
|
|
26
|
+
onResolved(kind: 'applied' | 'sent-back'): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Owner review = compare, not overlay: two panels side by side — left is the
|
|
31
|
+
* skill on the default branch, right is the change-request branch. Removed
|
|
32
|
+
* text tints red on the left, added text green on the right (via the module's
|
|
33
|
+
* line differ over real per-branch file contents); changed files get an amber
|
|
34
|
+
* dot in both lists, files new on the branch get a NEW row. Apply / Send back
|
|
35
|
+
* act through the existing change-request services. Esc exits the compare
|
|
36
|
+
* (registered as its own modal layer so the detail dialog beneath survives).
|
|
37
|
+
*/
|
|
38
|
+
export function CompareView({
|
|
39
|
+
skill,
|
|
40
|
+
mainContent,
|
|
41
|
+
loadMainFile,
|
|
42
|
+
cr,
|
|
43
|
+
onClose,
|
|
44
|
+
onResolved,
|
|
45
|
+
}: CompareViewProps) {
|
|
46
|
+
const navigate = useNavigate();
|
|
47
|
+
const { kbDirName } = useWorkspace();
|
|
48
|
+
const [detail, setDetail] = useState<PullRequestDetail | null>(null);
|
|
49
|
+
const [error, setError] = useState<string | null>(null);
|
|
50
|
+
const [branchContents, setBranchContents] = useState<Record<string, string | null>>({});
|
|
51
|
+
const [busy, setBusy] = useState(false);
|
|
52
|
+
const [sendBackOpen, setSendBackOpen] = useState(false);
|
|
53
|
+
const [sendBackNote, setSendBackNote] = useState('');
|
|
54
|
+
|
|
55
|
+
const isTop = useModalLayer(true);
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
function onKeyDown(e: KeyboardEvent) {
|
|
58
|
+
if (e.key === 'Escape' && isTop()) {
|
|
59
|
+
e.stopPropagation();
|
|
60
|
+
onClose();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
document.addEventListener('keydown', onKeyDown);
|
|
64
|
+
return () => document.removeEventListener('keydown', onKeyDown);
|
|
65
|
+
}, [isTop, onClose]);
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
let cancelled = false;
|
|
69
|
+
fetchPrDetail(cr.number)
|
|
70
|
+
.then((d) => {
|
|
71
|
+
if (!cancelled) setDetail(d);
|
|
72
|
+
})
|
|
73
|
+
.catch((err) => {
|
|
74
|
+
if (!cancelled) setError(err instanceof Error ? err.message : "Couldn't load this change request.");
|
|
75
|
+
});
|
|
76
|
+
return () => {
|
|
77
|
+
cancelled = true;
|
|
78
|
+
};
|
|
79
|
+
}, [cr.number]);
|
|
80
|
+
|
|
81
|
+
const prefix = `${skill.path}/`;
|
|
82
|
+
const mainFiles = useMemo(
|
|
83
|
+
() => ['SKILL.md', ...skill.files.map((f) => f.slice(prefix.length))],
|
|
84
|
+
[skill.files, prefix],
|
|
85
|
+
);
|
|
86
|
+
const changedFiles = useMemo(() => {
|
|
87
|
+
const set = new Set<string>();
|
|
88
|
+
for (const f of detail?.files ?? []) {
|
|
89
|
+
if (f.path.startsWith(prefix)) set.add(f.path.slice(prefix.length));
|
|
90
|
+
}
|
|
91
|
+
return set;
|
|
92
|
+
}, [detail, prefix]);
|
|
93
|
+
const addedFiles = useMemo(
|
|
94
|
+
() =>
|
|
95
|
+
(detail?.files ?? [])
|
|
96
|
+
.filter((f) => f.status === 'added' && f.path.startsWith(prefix))
|
|
97
|
+
.map((f) => f.path.slice(prefix.length)),
|
|
98
|
+
[detail, prefix],
|
|
99
|
+
);
|
|
100
|
+
const outsideCount = useMemo(
|
|
101
|
+
() => (detail?.files ?? []).filter((f) => !f.path.startsWith(prefix)).length,
|
|
102
|
+
[detail, prefix],
|
|
103
|
+
);
|
|
104
|
+
const branchFiles = useMemo(
|
|
105
|
+
() => [...mainFiles, ...addedFiles.filter((f) => !mainFiles.includes(f))],
|
|
106
|
+
[mainFiles, addedFiles],
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// Land on the first changed (or added) file once the detail is in.
|
|
110
|
+
const [selected, setSelected] = useState<string>('SKILL.md');
|
|
111
|
+
const landed = useRef(false);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (!detail || landed.current) return;
|
|
114
|
+
landed.current = true;
|
|
115
|
+
const first = branchFiles.find((f) => changedFiles.has(f));
|
|
116
|
+
if (first) setSelected(first);
|
|
117
|
+
}, [detail, branchFiles, changedFiles]);
|
|
118
|
+
|
|
119
|
+
const loadBranchFile = useCallback(
|
|
120
|
+
(rel: string) => {
|
|
121
|
+
if (branchContents[rel] !== undefined) return;
|
|
122
|
+
setBranchContents((c) => ({ ...c, [rel]: null }));
|
|
123
|
+
readFileOnBranch(cr.branch, `${skill.path}/${rel}`)
|
|
124
|
+
.then((content) => setBranchContents((c) => ({ ...c, [rel]: content })))
|
|
125
|
+
.catch(() => setBranchContents((c) => ({ ...c, [rel]: '' })));
|
|
126
|
+
},
|
|
127
|
+
[branchContents, cr.branch, skill.path],
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
loadBranchFile(selected);
|
|
132
|
+
if (!addedFiles.includes(selected)) loadMainFile(selected);
|
|
133
|
+
}, [selected, loadBranchFile, loadMainFile, addedFiles]);
|
|
134
|
+
|
|
135
|
+
const isAdded = addedFiles.includes(selected);
|
|
136
|
+
const mainRaw = isAdded ? null : mainContent(selected);
|
|
137
|
+
const branchRaw = branchContents[selected] ?? null;
|
|
138
|
+
const diff: DiffLine[] | null =
|
|
139
|
+
!isAdded && mainRaw !== null && branchRaw !== null ? diffLines(mainRaw, branchRaw) : null;
|
|
140
|
+
|
|
141
|
+
async function applyChanges() {
|
|
142
|
+
setBusy(true);
|
|
143
|
+
setError(null);
|
|
144
|
+
try {
|
|
145
|
+
await mergePullRequest(cr.number);
|
|
146
|
+
onResolved('applied');
|
|
147
|
+
} catch (err) {
|
|
148
|
+
setError(err instanceof Error ? err.message : "Couldn't apply this change request.");
|
|
149
|
+
setBusy(false);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function sendBack() {
|
|
154
|
+
setBusy(true);
|
|
155
|
+
setError(null);
|
|
156
|
+
try {
|
|
157
|
+
const note = sendBackNote.trim();
|
|
158
|
+
if (note) await postPrComment(cr.number, { body: note });
|
|
159
|
+
await cancelPullRequest(cr.number);
|
|
160
|
+
onResolved('sent-back');
|
|
161
|
+
} catch (err) {
|
|
162
|
+
setError(err instanceof Error ? err.message : "Couldn't send this change request back.");
|
|
163
|
+
setBusy(false);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const fileRow = (rel: string, side: 'main' | 'branch') => {
|
|
168
|
+
const added = addedFiles.includes(rel);
|
|
169
|
+
const active = selected === rel;
|
|
170
|
+
return (
|
|
171
|
+
<button
|
|
172
|
+
key={rel}
|
|
173
|
+
type="button"
|
|
174
|
+
className={`flex w-full items-center gap-2 rounded-[9px] border px-2.5 py-1.5 text-left transition-colors ${
|
|
175
|
+
active
|
|
176
|
+
? 'border-[#0d9488] bg-[#e6f7f4]'
|
|
177
|
+
: added
|
|
178
|
+
? 'border-[#a9e8cd] bg-[#f2fcf8] hover:border-[#7fd0c4]'
|
|
179
|
+
: 'border-slate-200 bg-[#fafbfd] hover:border-[#7fd0c4]'
|
|
180
|
+
}`}
|
|
181
|
+
onClick={() => setSelected(rel)}
|
|
182
|
+
>
|
|
183
|
+
<span className={`font-mono text-[11.5px] font-semibold ${active ? 'text-[#0f766e]' : 'text-slate-600'}`}>
|
|
184
|
+
{rel}
|
|
185
|
+
</span>
|
|
186
|
+
{rel === 'SKILL.md' && (
|
|
187
|
+
<span className="rounded-full bg-white px-1.5 text-[9.5px] font-bold tracking-[.04em] text-[#0f766e] shadow-[inset_0_0_0_1px_#d2e9e4]">
|
|
188
|
+
MAIN
|
|
189
|
+
</span>
|
|
190
|
+
)}
|
|
191
|
+
{added && side === 'branch' && (
|
|
192
|
+
<span className="rounded-full bg-[#e0f8ee] px-1.5 text-[9.5px] font-bold tracking-[.04em] text-[#0a8f60]">
|
|
193
|
+
NEW
|
|
194
|
+
</span>
|
|
195
|
+
)}
|
|
196
|
+
{!added && changedFiles.has(rel) && (
|
|
197
|
+
<span
|
|
198
|
+
className="h-[7px] w-[7px] shrink-0 rounded-full bg-[#f59e0b]"
|
|
199
|
+
title="Changed in this request"
|
|
200
|
+
/>
|
|
201
|
+
)}
|
|
202
|
+
</button>
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const lineView = (lines: DiffLine[] | null, raw: string | null, hide: 'added' | 'removed') => {
|
|
207
|
+
if (raw === null && lines === null) {
|
|
208
|
+
return <div className="p-3 text-xs text-slate-400">Loading…</div>;
|
|
209
|
+
}
|
|
210
|
+
const rendered = lines
|
|
211
|
+
? lines.filter((l) => l.kind !== hide)
|
|
212
|
+
: (raw ?? '').split('\n').map((text) => ({ kind: 'same' as const, text }));
|
|
213
|
+
return (
|
|
214
|
+
<pre className="whitespace-pre-wrap break-words rounded-[13px] border border-slate-200 bg-[#f7f9fb] p-3 font-mono text-[11.5px] leading-relaxed text-slate-600">
|
|
215
|
+
{rendered.map((l, i) => (
|
|
216
|
+
<div
|
|
217
|
+
key={i}
|
|
218
|
+
className={
|
|
219
|
+
l.kind === 'removed'
|
|
220
|
+
? 'lib-line-removed rounded px-1'
|
|
221
|
+
: l.kind === 'added'
|
|
222
|
+
? 'lib-line-added rounded px-1'
|
|
223
|
+
: 'px-1'
|
|
224
|
+
}
|
|
225
|
+
>
|
|
226
|
+
{l.text || ' '}
|
|
227
|
+
</div>
|
|
228
|
+
))}
|
|
229
|
+
</pre>
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
return (
|
|
234
|
+
<div className="fixed inset-0 z-[60]" role="dialog" aria-modal="true" aria-label={`Compare change request: ${cr.title}`}>
|
|
235
|
+
<div className="absolute inset-0 bg-slate-950/40" onClick={onClose} />
|
|
236
|
+
<div className="absolute inset-x-4 bottom-[5vh] top-[5vh] flex gap-3.5">
|
|
237
|
+
{/* left: the default branch */}
|
|
238
|
+
<section className="relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-[20px] border border-slate-200 bg-white shadow-[0_30px_80px_rgba(22,35,58,0.30)]">
|
|
239
|
+
<div className="absolute left-6 right-6 top-0 h-[3px] rounded-full bg-gradient-to-r from-transparent via-[#10b981] to-transparent" />
|
|
240
|
+
<div className="flex items-center gap-3 px-5 pb-3 pt-5">
|
|
241
|
+
<div className="min-w-0">
|
|
242
|
+
<div className="truncate text-[17px] font-bold text-slate-800">{skill.name}</div>
|
|
243
|
+
<div className="text-[11px] uppercase tracking-[.08em] text-slate-400">
|
|
244
|
+
Skill · {protectedBranchDisplayName(DEFAULT_BRANCH)}
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
</div>
|
|
248
|
+
<div className="flex-1 overflow-y-auto px-5 pb-5">
|
|
249
|
+
<h4 className="mb-2 text-[10.5px] font-bold uppercase tracking-[.09em] text-slate-400">
|
|
250
|
+
Files on {protectedBranchDisplayName(DEFAULT_BRANCH)}
|
|
251
|
+
</h4>
|
|
252
|
+
<div className="mb-4 flex flex-col gap-1.5">{mainFiles.map((f) => fileRow(f, 'main'))}</div>
|
|
253
|
+
<h4 className="mb-2 font-mono text-[11px] text-slate-500">{selected}</h4>
|
|
254
|
+
{isAdded ? (
|
|
255
|
+
<div className="rounded-[13px] border border-dashed border-slate-200 p-4 text-center text-xs text-slate-400">
|
|
256
|
+
This file doesn't exist here yet
|
|
257
|
+
</div>
|
|
258
|
+
) : (
|
|
259
|
+
lineView(diff, mainRaw, 'added')
|
|
260
|
+
)}
|
|
261
|
+
</div>
|
|
262
|
+
</section>
|
|
263
|
+
|
|
264
|
+
{/* right: the change-request branch */}
|
|
265
|
+
<section className="relative flex min-w-0 flex-1 flex-col overflow-hidden rounded-[20px] border border-slate-200 bg-white shadow-[0_30px_80px_rgba(22,35,58,0.30)]">
|
|
266
|
+
<div className="absolute left-6 right-6 top-0 h-[3px] rounded-full bg-gradient-to-r from-transparent via-[#0d9488] to-transparent" />
|
|
267
|
+
<div className="flex items-center gap-3 px-5 pb-3 pt-5">
|
|
268
|
+
<div className="min-w-0">
|
|
269
|
+
<div className="truncate text-[17px] font-bold text-slate-800">{cr.title}</div>
|
|
270
|
+
<div className="truncate text-[11px] text-slate-400">
|
|
271
|
+
<span className="font-mono">{cr.branch}</span>
|
|
272
|
+
{' · by '}
|
|
273
|
+
{cr.appAuthor?.name ?? cr.author.name ?? cr.author.login}
|
|
274
|
+
</div>
|
|
275
|
+
</div>
|
|
276
|
+
<div className="relative ml-auto flex shrink-0 items-center gap-2">
|
|
277
|
+
<button
|
|
278
|
+
type="button"
|
|
279
|
+
disabled={busy}
|
|
280
|
+
className="rounded-[10px] border border-[#f5c2c2] bg-[#fdecec] px-3.5 py-2 text-[12.5px] font-bold text-[#c53030] transition-transform hover:-translate-y-px disabled:opacity-60"
|
|
281
|
+
onClick={() => setSendBackOpen((o) => !o)}
|
|
282
|
+
>
|
|
283
|
+
Send back
|
|
284
|
+
</button>
|
|
285
|
+
<button
|
|
286
|
+
type="button"
|
|
287
|
+
disabled={busy}
|
|
288
|
+
className="rounded-[10px] bg-gradient-to-br from-[#0d9488] to-[#0f766e] px-3.5 py-2 text-[12.5px] font-bold text-white shadow-[0_4px_14px_rgba(13,148,136,0.22)] transition-transform hover:-translate-y-px disabled:opacity-60"
|
|
289
|
+
onClick={() => void applyChanges()}
|
|
290
|
+
>
|
|
291
|
+
{busy ? 'Working…' : 'Apply changes'}
|
|
292
|
+
</button>
|
|
293
|
+
<button
|
|
294
|
+
type="button"
|
|
295
|
+
aria-label="Close compare"
|
|
296
|
+
className="flex h-[30px] w-[30px] items-center justify-center rounded-full border border-slate-200 bg-white text-[13px] text-slate-500 hover:border-[#7fd0c4] hover:text-slate-800"
|
|
297
|
+
onClick={onClose}
|
|
298
|
+
>
|
|
299
|
+
✕
|
|
300
|
+
</button>
|
|
301
|
+
{sendBackOpen && (
|
|
302
|
+
<div className="absolute right-0 top-full z-10 mt-2 w-72 rounded-[14px] border border-slate-200 bg-white p-3.5 shadow-[0_16px_44px_rgba(22,35,58,0.24)]">
|
|
303
|
+
<label className="block text-[10px] font-bold uppercase tracking-[.07em] text-slate-400">
|
|
304
|
+
Note for the author (optional)
|
|
305
|
+
<textarea
|
|
306
|
+
rows={3}
|
|
307
|
+
className="mt-1 w-full resize-y rounded-lg border border-slate-200 px-2 py-1.5 text-xs font-normal normal-case tracking-normal text-slate-800 outline-none focus:border-[#0d9488]"
|
|
308
|
+
value={sendBackNote}
|
|
309
|
+
onChange={(e) => setSendBackNote(e.target.value)}
|
|
310
|
+
/>
|
|
311
|
+
</label>
|
|
312
|
+
<div className="mt-2 flex justify-end">
|
|
313
|
+
<button
|
|
314
|
+
type="button"
|
|
315
|
+
disabled={busy}
|
|
316
|
+
className="rounded-[10px] bg-[#fdecec] px-3.5 py-2 text-[12.5px] font-bold text-[#c53030] shadow-[inset_0_0_0_1px_#f3c4c4] disabled:opacity-60"
|
|
317
|
+
onClick={() => void sendBack()}
|
|
318
|
+
>
|
|
319
|
+
Send back
|
|
320
|
+
</button>
|
|
321
|
+
</div>
|
|
322
|
+
</div>
|
|
323
|
+
)}
|
|
324
|
+
</div>
|
|
325
|
+
</div>
|
|
326
|
+
{error && <div className="mx-5 mb-2 rounded-lg bg-[#fdecec] px-3 py-2 text-xs text-[#c53030]">{error}</div>}
|
|
327
|
+
<div className="flex-1 overflow-y-auto px-5 pb-5">
|
|
328
|
+
<h4 className="mb-2 text-[10.5px] font-bold uppercase tracking-[.09em] text-slate-400">
|
|
329
|
+
Files on this draft
|
|
330
|
+
{outsideCount > 0 && (
|
|
331
|
+
<span className="ml-2 normal-case tracking-normal text-[#6d28d9]">
|
|
332
|
+
+{outsideCount} file{outsideCount === 1 ? '' : 's'} outside this skill
|
|
333
|
+
</span>
|
|
334
|
+
)}
|
|
335
|
+
</h4>
|
|
336
|
+
<div className="mb-4 flex flex-col gap-1.5">{branchFiles.map((f) => fileRow(f, 'branch'))}</div>
|
|
337
|
+
<div className="mb-2 flex items-center justify-between">
|
|
338
|
+
<h4 className="font-mono text-[11px] text-slate-500">{selected}</h4>
|
|
339
|
+
<button
|
|
340
|
+
type="button"
|
|
341
|
+
className="rounded-lg bg-[#e0f8ee] px-3.5 py-1 text-[11.5px] font-bold text-[#0a8f60] shadow-[inset_0_0_0_1px_#a9e8cd] transition-transform hover:-translate-y-px"
|
|
342
|
+
onClick={() => {
|
|
343
|
+
if (kbDirName) {
|
|
344
|
+
navigate(kbFileUrl(cr.branch, `${kbDirName}/${skill.path}/${selected}`));
|
|
345
|
+
}
|
|
346
|
+
}}
|
|
347
|
+
>
|
|
348
|
+
Edit
|
|
349
|
+
</button>
|
|
350
|
+
</div>
|
|
351
|
+
{lineView(diff, branchRaw, 'removed')}
|
|
352
|
+
</div>
|
|
353
|
+
</section>
|
|
354
|
+
</div>
|
|
355
|
+
</div>
|
|
356
|
+
);
|
|
357
|
+
}
|