xtralab 0.4.0 → 0.5.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.
@@ -1,575 +0,0 @@
1
- import * as React from 'react';
2
-
3
- import { Poll } from '@lumino/polling';
4
- import { ISignal } from '@lumino/signaling';
5
-
6
- import {
7
- add,
8
- addAll,
9
- checkout,
10
- porcelainToFileChanges,
11
- reset,
12
- resetAll,
13
- showTopLevel,
14
- status
15
- } from './api';
16
- import { FileChangeStatus, IFileChange, IGitStatusResult } from './tokens';
17
-
18
- /**
19
- * Callbacks the panel needs to delegate to (so the React component stays
20
- * free of any JupyterLab application coupling). The plugin wires these up
21
- * with the actual `JupyterFrontEnd` reference.
22
- */
23
- export interface IGitPanelHandlers {
24
- /** Open a diff viewer for the given file in the main area. */
25
- openDiff(
26
- change: IFileChange,
27
- options?: {
28
- pin?: boolean;
29
- }
30
- ): void;
31
- /** Show a confirmation dialog. Returns true when the user accepted. */
32
- confirm(title: string, body: string, accept: string): Promise<boolean>;
33
- /** Show an error dialog. */
34
- showError(title: string, error: Error): Promise<void>;
35
- }
36
-
37
- interface IPanelState {
38
- /** True while the initial status request is in flight. */
39
- loading: boolean;
40
- /** The raw result of the last status call, or `null` if it hasn't completed yet. */
41
- result: IGitStatusResult | null;
42
- /** Set when the status request errored or the path is not in a repo. */
43
- error: string | null;
44
- }
45
-
46
- /**
47
- * The polling interval for the panel. We refresh the status periodically so
48
- * out-of-band changes (e.g. the user runs `git add` from a terminal) become
49
- * visible without requiring a manual refresh.
50
- */
51
- const STATUS_POLL_INTERVAL_MS = 5000;
52
-
53
- /**
54
- * Upper bound on the poll's exponential backoff when status requests fail
55
- * repeatedly. Matches the default JupyterLab file browser's `max`: long
56
- * enough that a server outage stops hammering the API, short enough that
57
- * a transient failure heals on its own.
58
- */
59
- const STATUS_POLL_MAX_MS = 300_000;
60
-
61
- /**
62
- * Top-level React component rendered inside the {@link GitPanel} widget.
63
- * Owns the polling + refresh wiring around the `/git/status` endpoint and
64
- * the small bit of UI state that controls section collapse and the
65
- * in-flight per-file action spinners.
66
- */
67
- export function GitPanelComponent(props: {
68
- handlers: IGitPanelHandlers;
69
- refreshSignal: ISignal<unknown, void>;
70
- }): React.ReactElement {
71
- const { handlers, refreshSignal } = props;
72
-
73
- const [state, setState] = React.useState<IPanelState>({
74
- loading: true,
75
- result: null,
76
- error: null
77
- });
78
- // The absolute filesystem path of the resolved git top-level. Used purely
79
- // for display in the panel header.
80
- const [topLevel, setTopLevel] = React.useState<string | null>(null);
81
- // Per-action spinner gating: keyed by `${group}:${path}`, value is the
82
- // action currently in flight. Lets the row render a disabled state on
83
- // exactly the file that is being mutated, without locking the whole panel.
84
- const [pendingActions, setPendingActions] = React.useState<
85
- Record<string, string>
86
- >({});
87
- const [stagedCollapsed, setStagedCollapsed] = React.useState(false);
88
- const [changesCollapsed, setChangesCollapsed] = React.useState(false);
89
-
90
- // The repo path passed to every API call. We use the JupyterLab server's
91
- // root (the empty string) and let `git` resolve the enclosing repo on the
92
- // server side. This works as long as the server was started inside a git
93
- // repo — the common case.
94
- const repoPath = '';
95
-
96
- const refresh = React.useCallback(async (): Promise<void> => {
97
- try {
98
- const [topLevelResult, statusResult] = await Promise.all([
99
- showTopLevel(repoPath),
100
- status(repoPath)
101
- ]);
102
- setTopLevel(topLevelResult);
103
- if (statusResult.code !== 0) {
104
- setState({
105
- loading: false,
106
- result: null,
107
- error: statusResult.message ?? 'Failed to load git status'
108
- });
109
- return;
110
- }
111
- setState({ loading: false, result: statusResult, error: null });
112
- } catch (err) {
113
- setState({
114
- loading: false,
115
- result: null,
116
- error: err instanceof Error ? err.message : String(err)
117
- });
118
- }
119
- }, [repoPath]);
120
-
121
- // Initial load + polling. Driven by a Lumino `Poll` so the panel stops
122
- // hitting the git endpoint while the JupyterLab tab is hidden, and so
123
- // repeated failures back off exponentially instead of spinning at 5s.
124
- // External refresh triggers (toolbar button, plugin-level events) call
125
- // `poll.refresh()` to schedule an immediate tick instead of running
126
- // `refresh()` directly — that keeps the panel from issuing two parallel
127
- // status requests when an automatic tick happens to land at the same
128
- // time as an external trigger.
129
- React.useEffect(() => {
130
- const poll = new Poll({
131
- name: '@xtralab/git:panelStatus',
132
- factory: () => refresh(),
133
- frequency: {
134
- interval: STATUS_POLL_INTERVAL_MS,
135
- backoff: true,
136
- max: STATUS_POLL_MAX_MS
137
- },
138
- standby: 'when-hidden'
139
- });
140
- const handler = (): void => {
141
- void poll.refresh();
142
- };
143
- refreshSignal.connect(handler);
144
- return () => {
145
- refreshSignal.disconnect(handler);
146
- poll.dispose();
147
- };
148
- }, [refresh, refreshSignal]);
149
-
150
- const runAction = React.useCallback(
151
- async (
152
- change: IFileChange,
153
- action: string,
154
- run: () => Promise<void>
155
- ): Promise<void> => {
156
- const key = `${change.group}:${change.path}`;
157
- setPendingActions(prev => ({ ...prev, [key]: action }));
158
- try {
159
- await run();
160
- await refresh();
161
- } catch (err) {
162
- await handlers.showError(
163
- 'Git operation failed',
164
- err instanceof Error ? err : new Error(String(err))
165
- );
166
- } finally {
167
- setPendingActions(prev => {
168
- const next = { ...prev };
169
- delete next[key];
170
- return next;
171
- });
172
- }
173
- },
174
- [handlers, refresh]
175
- );
176
-
177
- const onStage = React.useCallback(
178
- (change: IFileChange) =>
179
- runAction(change, 'stage', () => add(repoPath, change.path)),
180
- [repoPath, runAction]
181
- );
182
-
183
- const onUnstage = React.useCallback(
184
- (change: IFileChange) =>
185
- runAction(change, 'unstage', () => reset(repoPath, change.path)),
186
- [repoPath, runAction]
187
- );
188
-
189
- const onDiscard = React.useCallback(
190
- async (change: IFileChange): Promise<void> => {
191
- const verb = change.status === 'untracked' ? 'remove' : 'restore';
192
- const accepted = await handlers.confirm(
193
- verb === 'remove' ? 'Remove file' : 'Discard changes',
194
- verb === 'remove'
195
- ? `Permanently remove ${change.path}? This cannot be undone.`
196
- : `Discard all changes to ${change.path}? This will revert the file to its last staged version and cannot be undone.`,
197
- verb === 'remove' ? 'Remove' : 'Discard'
198
- );
199
- if (!accepted) {
200
- return;
201
- }
202
- // The git extension's `checkout` endpoint without a branch reverts the
203
- // working tree copy of the file to the index. For untracked files we
204
- // can't checkout, so the user is told the action is unsupported and we
205
- // bail out — they can delete the file via the file browser instead.
206
- if (change.status === 'untracked') {
207
- await handlers.showError(
208
- 'Cannot discard untracked file',
209
- new Error(
210
- 'Untracked files cannot be discarded from the git panel — delete them from the file browser instead.'
211
- )
212
- );
213
- return;
214
- }
215
- await runAction(change, 'discard', () => checkout(repoPath, change.path));
216
- },
217
- [handlers, repoPath, runAction]
218
- );
219
-
220
- const onStageAll = React.useCallback(async (): Promise<void> => {
221
- try {
222
- await addAll(repoPath);
223
- await refresh();
224
- } catch (err) {
225
- await handlers.showError(
226
- 'Stage all failed',
227
- err instanceof Error ? err : new Error(String(err))
228
- );
229
- }
230
- }, [handlers, refresh, repoPath]);
231
-
232
- const onUnstageAll = React.useCallback(async (): Promise<void> => {
233
- try {
234
- await resetAll(repoPath);
235
- await refresh();
236
- } catch (err) {
237
- await handlers.showError(
238
- 'Unstage all failed',
239
- err instanceof Error ? err : new Error(String(err))
240
- );
241
- }
242
- }, [handlers, refresh, repoPath]);
243
-
244
- // Categorize the porcelain entries into the two visible groups. Because
245
- // `porcelainToFileChanges` may emit two changes per file (one staged, one
246
- // unstaged), the same file can show up in both lists when it has been
247
- // partially staged.
248
- const { staged, unstaged } = React.useMemo(() => {
249
- const stagedAcc: IFileChange[] = [];
250
- const unstagedAcc: IFileChange[] = [];
251
- if (state.result === null) {
252
- return { staged: stagedAcc, unstaged: unstagedAcc };
253
- }
254
- for (const file of state.result.files) {
255
- for (const change of porcelainToFileChanges(file)) {
256
- if (change.group === 'staged') {
257
- stagedAcc.push(change);
258
- } else {
259
- unstagedAcc.push(change);
260
- }
261
- }
262
- }
263
- stagedAcc.sort((a, b) => a.path.localeCompare(b.path));
264
- unstagedAcc.sort((a, b) => a.path.localeCompare(b.path));
265
- return { staged: stagedAcc, unstaged: unstagedAcc };
266
- }, [state.result]);
267
-
268
- return (
269
- <div className="jp-xtralab-GitPanel-content">
270
- <PanelHeader
271
- topLevel={topLevel}
272
- result={state.result}
273
- loading={state.loading}
274
- error={state.error}
275
- onRefresh={() => void refresh()}
276
- />
277
- {state.error !== null ? (
278
- <div className="jp-xtralab-GitPanel-error">{state.error}</div>
279
- ) : (
280
- <>
281
- <ChangeSection
282
- label="Staged Changes"
283
- count={staged.length}
284
- collapsed={stagedCollapsed}
285
- onToggleCollapsed={() => setStagedCollapsed(c => !c)}
286
- actions={
287
- staged.length > 0
288
- ? [
289
- {
290
- label: 'Unstage all changes',
291
- symbol: '−',
292
- onClick: onUnstageAll
293
- }
294
- ]
295
- : undefined
296
- }
297
- >
298
- {staged.map(change => (
299
- <ChangeRow
300
- key={`${change.group}:${change.path}`}
301
- change={change}
302
- pendingAction={pendingActions[`${change.group}:${change.path}`]}
303
- onClick={event =>
304
- handlers.openDiff(change, {
305
- pin: event.ctrlKey || event.metaKey
306
- })
307
- }
308
- actions={[
309
- {
310
- label: 'Unstage changes',
311
- symbol: '−',
312
- onClick: () => void onUnstage(change)
313
- }
314
- ]}
315
- />
316
- ))}
317
- </ChangeSection>
318
- <ChangeSection
319
- label="Changes"
320
- count={unstaged.length}
321
- collapsed={changesCollapsed}
322
- onToggleCollapsed={() => setChangesCollapsed(c => !c)}
323
- actions={
324
- unstaged.length > 0
325
- ? [
326
- {
327
- label: 'Stage all changes',
328
- symbol: '+',
329
- onClick: onStageAll
330
- }
331
- ]
332
- : undefined
333
- }
334
- >
335
- {unstaged.map(change => (
336
- <ChangeRow
337
- key={`${change.group}:${change.path}`}
338
- change={change}
339
- pendingAction={pendingActions[`${change.group}:${change.path}`]}
340
- onClick={event =>
341
- handlers.openDiff(change, {
342
- pin: event.ctrlKey || event.metaKey
343
- })
344
- }
345
- actions={[
346
- {
347
- label: 'Discard changes',
348
- symbol: '⟲',
349
- onClick: () => void onDiscard(change)
350
- },
351
- {
352
- label: 'Stage changes',
353
- symbol: '+',
354
- onClick: () => void onStage(change)
355
- }
356
- ]}
357
- />
358
- ))}
359
- </ChangeSection>
360
- {staged.length === 0 && unstaged.length === 0 && !state.loading ? (
361
- <div className="jp-xtralab-GitPanel-empty">
362
- No changes detected.
363
- </div>
364
- ) : null}
365
- </>
366
- )}
367
- </div>
368
- );
369
- }
370
-
371
- interface IRowAction {
372
- label: string;
373
- symbol: string;
374
- onClick: () => void;
375
- }
376
-
377
- function PanelHeader(props: {
378
- topLevel: string | null;
379
- result: IGitStatusResult | null;
380
- loading: boolean;
381
- error: string | null;
382
- onRefresh: () => void;
383
- }): React.ReactElement {
384
- const { topLevel, result, loading, error, onRefresh } = props;
385
- const repoName =
386
- topLevel !== null
387
- ? (topLevel
388
- .split('/')
389
- .filter(s => s.length > 0)
390
- .pop() ?? topLevel)
391
- : null;
392
- return (
393
- <div className="jp-xtralab-GitPanel-header">
394
- <div className="jp-xtralab-GitPanel-headerInfo">
395
- {repoName !== null ? (
396
- <div className="jp-xtralab-GitPanel-repo" title={topLevel ?? ''}>
397
- {repoName}
398
- </div>
399
- ) : null}
400
- {result?.branch !== undefined && result?.branch !== null ? (
401
- <div className="jp-xtralab-GitPanel-branch">
402
- {result.branch}
403
- {result.ahead > 0 ? <span> ↑{result.ahead}</span> : null}
404
- {result.behind > 0 ? <span> ↓{result.behind}</span> : null}
405
- </div>
406
- ) : null}
407
- </div>
408
- <button
409
- type="button"
410
- className="jp-xtralab-GitPanel-iconButton"
411
- title="Refresh"
412
- disabled={loading && error === null}
413
- onClick={onRefresh}
414
- >
415
- ⟳
416
- </button>
417
- </div>
418
- );
419
- }
420
-
421
- function ChangeSection(props: {
422
- label: string;
423
- count: number;
424
- collapsed: boolean;
425
- onToggleCollapsed: () => void;
426
- actions?: IRowAction[];
427
- children: React.ReactNode;
428
- }): React.ReactElement {
429
- const { label, count, collapsed, onToggleCollapsed, actions, children } =
430
- props;
431
- return (
432
- <div className="jp-xtralab-GitPanel-section">
433
- <div
434
- className="jp-xtralab-GitPanel-sectionHeader"
435
- onClick={onToggleCollapsed}
436
- >
437
- <span className="jp-xtralab-GitPanel-sectionChevron">
438
- {collapsed ? '▸' : '▾'}
439
- </span>
440
- <span className="jp-xtralab-GitPanel-sectionLabel">{label}</span>
441
- {actions !== undefined ? (
442
- <span className="jp-xtralab-GitPanel-sectionActions">
443
- {actions.map(action => (
444
- <button
445
- key={action.label}
446
- type="button"
447
- className="jp-xtralab-GitPanel-iconButton"
448
- title={action.label}
449
- onClick={event => {
450
- event.stopPropagation();
451
- action.onClick();
452
- }}
453
- >
454
- {action.symbol}
455
- </button>
456
- ))}
457
- </span>
458
- ) : null}
459
- <span className="jp-xtralab-GitPanel-sectionCount">{count}</span>
460
- </div>
461
- {!collapsed ? (
462
- <div className="jp-xtralab-GitPanel-sectionBody">{children}</div>
463
- ) : null}
464
- </div>
465
- );
466
- }
467
-
468
- function ChangeRow(props: {
469
- change: IFileChange;
470
- pendingAction: string | undefined;
471
- onClick: (event: React.MouseEvent<HTMLDivElement>) => void;
472
- actions: IRowAction[];
473
- }): React.ReactElement {
474
- const { change, pendingAction, onClick, actions } = props;
475
- const filename = basename(change.path);
476
- const dirname = dirnameOrEmpty(change.path);
477
- return (
478
- <div
479
- className="jp-xtralab-GitPanel-row"
480
- data-status={change.status}
481
- title={
482
- change.from !== undefined
483
- ? `${change.from} → ${change.path}`
484
- : change.path
485
- }
486
- onClick={onClick}
487
- >
488
- <span className="jp-xtralab-GitPanel-rowLabel">
489
- <span className="jp-xtralab-GitPanel-rowName">{filename}</span>
490
- {dirname !== '' ? (
491
- <span className="jp-xtralab-GitPanel-rowPath">{dirname}</span>
492
- ) : null}
493
- </span>
494
- <span
495
- className="jp-xtralab-GitPanel-rowActions"
496
- data-pending={pendingAction !== undefined}
497
- >
498
- {actions.map(action => (
499
- <button
500
- key={action.label}
501
- type="button"
502
- className="jp-xtralab-GitPanel-iconButton"
503
- title={action.label}
504
- disabled={pendingAction !== undefined}
505
- onClick={event => {
506
- event.stopPropagation();
507
- action.onClick();
508
- }}
509
- >
510
- {action.symbol}
511
- </button>
512
- ))}
513
- </span>
514
- <span
515
- className="jp-xtralab-GitPanel-rowBadge"
516
- data-status={change.status}
517
- title={statusTooltip(change.status)}
518
- >
519
- {statusBadge(change.status)}
520
- </span>
521
- </div>
522
- );
523
- }
524
-
525
- function statusBadge(status: FileChangeStatus): string {
526
- switch (status) {
527
- case 'modified':
528
- return 'M';
529
- case 'added':
530
- return 'A';
531
- case 'deleted':
532
- return 'D';
533
- case 'renamed':
534
- return 'R';
535
- case 'untracked':
536
- return 'U';
537
- case 'unmerged':
538
- return '!';
539
- case 'typechange':
540
- return 'T';
541
- default:
542
- return '?';
543
- }
544
- }
545
-
546
- function statusTooltip(status: FileChangeStatus): string {
547
- switch (status) {
548
- case 'modified':
549
- return 'Modified';
550
- case 'added':
551
- return 'Added';
552
- case 'deleted':
553
- return 'Deleted';
554
- case 'renamed':
555
- return 'Renamed';
556
- case 'untracked':
557
- return 'Untracked';
558
- case 'unmerged':
559
- return 'Unmerged (conflict)';
560
- case 'typechange':
561
- return 'Type changed';
562
- default:
563
- return 'Unknown';
564
- }
565
- }
566
-
567
- function basename(path: string): string {
568
- const idx = path.lastIndexOf('/');
569
- return idx < 0 ? path : path.slice(idx + 1);
570
- }
571
-
572
- function dirnameOrEmpty(path: string): string {
573
- const idx = path.lastIndexOf('/');
574
- return idx < 0 ? '' : path.slice(0, idx);
575
- }