@deepseek-ai/dsh-experimental-client-ui-forge 0.2.2-alpha.3 → 0.2.2-rc.2
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/README.i18n.yaml +8 -8
- package/README.md +41 -14
- package/README.zh.md +40 -13
- package/lib/client.js +13 -11800
- package/lib/client.js.map +1 -0
- package/lib/index.js +1340 -115
- package/lib/typert.host.js +1715 -212
- package/lib/typert.remote-client.d.ts +42 -5
- package/lib/typert.remote-client.d.ts.map +1 -0
- package/lib/typert.remote-client.js +1523 -244
- package/lib/types/client/ActionToast.d.ts.map +1 -0
- package/lib/types/client/ActionToast.js.map +1 -0
- package/lib/types/client/BaseMenu.d.ts +30 -0
- package/lib/types/client/BaseMenu.d.ts.map +1 -0
- package/lib/types/client/BaseMenu.js.map +1 -0
- package/lib/types/client/ChangeRequestCard.d.ts +9 -5
- package/lib/types/client/ChangeRequestCard.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestCard.js.map +1 -0
- package/lib/types/client/ChangeRequestOverview.d.ts +124 -0
- package/lib/types/client/ChangeRequestOverview.d.ts.map +1 -0
- package/lib/types/client/ChangeRequestOverview.js.map +1 -0
- package/lib/types/client/ChangedFiles.d.ts +2 -0
- package/lib/types/client/ChangedFiles.d.ts.map +1 -0
- package/lib/types/client/ChangedFiles.js.map +1 -0
- package/lib/types/client/ChangesTab.d.ts +30 -5
- package/lib/types/client/ChangesTab.d.ts.map +1 -0
- package/lib/types/client/ChangesTab.js.map +1 -0
- package/lib/types/client/ChangesTabTitle.d.ts.map +1 -0
- package/lib/types/client/ChangesTabTitle.js.map +1 -0
- package/lib/types/client/CommitMenu.d.ts +41 -0
- package/lib/types/client/CommitMenu.d.ts.map +1 -0
- package/lib/types/client/CommitMenu.js.map +1 -0
- package/lib/types/client/DiffFile.d.ts +47 -15
- package/lib/types/client/DiffFile.d.ts.map +1 -0
- package/lib/types/client/DiffFile.js.map +1 -0
- package/lib/types/client/FileLabel.d.ts.map +1 -0
- package/lib/types/client/FileLabel.js.map +1 -0
- package/lib/types/client/FileTreePane.d.ts +4 -2
- package/lib/types/client/FileTreePane.d.ts.map +1 -0
- package/lib/types/client/FileTreePane.js.map +1 -0
- package/lib/types/client/ForgeTab.d.ts +9 -1
- package/lib/types/client/ForgeTab.d.ts.map +1 -0
- package/lib/types/client/ForgeTab.js.map +1 -0
- package/lib/types/client/ForgeTabTitle.d.ts.map +1 -0
- package/lib/types/client/ForgeTabTitle.js.map +1 -0
- package/lib/types/client/HeaderButton.d.ts.map +1 -0
- package/lib/types/client/HeaderButton.js.map +1 -0
- package/lib/types/client/LocalChangesCard.d.ts.map +1 -0
- package/lib/types/client/LocalChangesCard.js.map +1 -0
- package/lib/types/client/RefreshGlyph.d.ts.map +1 -0
- package/lib/types/client/RefreshGlyph.js.map +1 -0
- package/lib/types/client/ReviewComposer.d.ts +20 -6
- package/lib/types/client/ReviewComposer.d.ts.map +1 -0
- package/lib/types/client/ReviewComposer.js.map +1 -0
- package/lib/types/client/ReviewControls.d.ts +79 -0
- package/lib/types/client/ReviewControls.d.ts.map +1 -0
- package/lib/types/client/ReviewControls.js.map +1 -0
- package/lib/types/client/ReviewThread.d.ts +4 -4
- package/lib/types/client/ReviewThread.d.ts.map +1 -0
- package/lib/types/client/ReviewThread.js.map +1 -0
- package/lib/types/client/ReviewThreads.d.ts +108 -0
- package/lib/types/client/ReviewThreads.d.ts.map +1 -0
- package/lib/types/client/ReviewThreads.js.map +1 -0
- package/lib/types/client/SessionHoverForge.d.ts.map +1 -0
- package/lib/types/client/SessionHoverForge.js.map +1 -0
- package/lib/types/client/SessionRowForge.d.ts +1 -1
- package/lib/types/client/SessionRowForge.d.ts.map +1 -0
- package/lib/types/client/SessionRowForge.js.map +1 -0
- package/lib/types/client/SubmitReview.d.ts +12 -4
- package/lib/types/client/SubmitReview.d.ts.map +1 -0
- package/lib/types/client/SubmitReview.js.map +1 -0
- package/lib/types/client/ThreadList.d.ts +43 -0
- package/lib/types/client/ThreadList.d.ts.map +1 -0
- package/lib/types/client/ThreadList.js.map +1 -0
- package/lib/types/client/ViewOptionsMenu.d.ts +31 -0
- package/lib/types/client/ViewOptionsMenu.d.ts.map +1 -0
- package/lib/types/client/ViewOptionsMenu.js.map +1 -0
- package/lib/types/client/WorktreeProgress.d.ts.map +1 -0
- package/lib/types/client/WorktreeProgress.js.map +1 -0
- package/lib/types/client/WorktreeSettings.d.ts +12 -0
- package/lib/types/client/WorktreeSettings.d.ts.map +1 -0
- package/lib/types/client/WorktreeSettings.js.map +1 -0
- package/lib/types/client/WorktreeToggle.d.ts.map +1 -0
- package/lib/types/client/WorktreeToggle.js.map +1 -0
- package/lib/types/client/changes-address.d.ts.map +1 -0
- package/lib/types/client/changes-address.js.map +1 -0
- package/lib/types/client/changes-store.d.ts +20 -5
- package/lib/types/client/changes-store.d.ts.map +1 -0
- package/lib/types/client/changes-store.js.map +1 -0
- package/lib/types/client/checkout.d.ts.map +1 -0
- package/lib/types/client/checkout.js.map +1 -0
- package/lib/types/client/code-selection.d.ts.map +1 -0
- package/lib/types/client/code-selection.js.map +1 -0
- package/lib/types/client/diff-rows.d.ts.map +1 -0
- package/lib/types/client/diff-rows.js.map +1 -0
- package/lib/types/client/file-tree.d.ts.map +1 -0
- package/lib/types/client/file-tree.js.map +1 -0
- package/lib/types/client/format.d.ts.map +1 -0
- package/lib/types/client/format.js.map +1 -0
- package/lib/types/client/glyphs.d.ts +4 -2
- package/lib/types/client/glyphs.d.ts.map +1 -0
- package/lib/types/client/glyphs.js.map +1 -0
- package/lib/types/client/host-viewed.d.ts +84 -0
- package/lib/types/client/host-viewed.d.ts.map +1 -0
- package/lib/types/client/host-viewed.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/locales.d.ts +175 -3
- package/lib/types/client/locales.d.ts.map +1 -0
- package/lib/types/client/locales.js.map +1 -0
- package/lib/types/client/menu-search.d.ts +10 -0
- package/lib/types/client/menu-search.d.ts.map +1 -0
- package/lib/types/client/menu-search.js.map +1 -0
- package/lib/types/client/mount.d.ts +4 -0
- package/lib/types/client/mount.d.ts.map +1 -0
- package/lib/types/client/mount.js.map +1 -0
- package/lib/types/client/review-queue.d.ts +5 -8
- package/lib/types/client/review-queue.d.ts.map +1 -0
- package/lib/types/client/review-queue.js.map +1 -0
- package/lib/types/client/review-threads.d.ts +108 -0
- package/lib/types/client/review-threads.d.ts.map +1 -0
- package/lib/types/client/review-threads.js.map +1 -0
- package/lib/types/client/review.d.ts +31 -4
- package/lib/types/client/review.d.ts.map +1 -0
- package/lib/types/client/review.js.map +1 -0
- package/lib/types/client/section-reach.d.ts +26 -0
- package/lib/types/client/section-reach.d.ts.map +1 -0
- package/lib/types/client/section-reach.js.map +1 -0
- package/lib/types/client/session-list.d.ts.map +1 -0
- package/lib/types/client/session-list.js.map +1 -0
- package/lib/types/client/source.d.ts.map +1 -0
- package/lib/types/client/source.js.map +1 -0
- package/lib/types/client/status.d.ts.map +1 -0
- package/lib/types/client/status.js.map +1 -0
- package/lib/types/client/thread-state.d.ts +68 -0
- package/lib/types/client/thread-state.d.ts.map +1 -0
- package/lib/types/client/thread-state.js.map +1 -0
- package/lib/types/client/toolbar-fold.d.ts +19 -0
- package/lib/types/client/toolbar-fold.d.ts.map +1 -0
- package/lib/types/client/toolbar-fold.js.map +1 -0
- package/lib/types/client/viewed-files.d.ts +70 -0
- package/lib/types/client/viewed-files.d.ts.map +1 -0
- package/lib/types/client/viewed-files.js.map +1 -0
- package/lib/types/client/worktree-settings-form.d.ts +32 -0
- package/lib/types/client/worktree-settings-form.d.ts.map +1 -0
- package/lib/types/client/worktree-settings-form.js.map +1 -0
- package/lib/types/commit-range.d.ts +17 -0
- package/lib/types/commit-range.d.ts.map +1 -0
- package/lib/types/commit-range.js.map +1 -0
- package/lib/types/diff.d.ts +10 -2
- package/lib/types/diff.d.ts.map +1 -0
- package/lib/types/diff.js.map +1 -0
- package/lib/types/git.d.ts.map +1 -0
- package/lib/types/git.js.map +1 -0
- package/lib/types/index.d.ts +194 -14
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/layers.d.ts +66 -0
- package/lib/types/layers.d.ts.map +1 -0
- package/lib/types/layers.js.map +1 -0
- package/lib/types/local-diff.d.ts +54 -6
- package/lib/types/local-diff.d.ts.map +1 -0
- package/lib/types/local-diff.js.map +1 -0
- package/lib/types/panel.d.ts.map +1 -0
- package/lib/types/panel.js.map +1 -0
- package/lib/types/parse.d.ts +16 -0
- package/lib/types/parse.d.ts.map +1 -0
- package/lib/types/parse.js.map +1 -0
- package/lib/types/patch-cache.d.ts +12 -7
- package/lib/types/patch-cache.d.ts.map +1 -0
- package/lib/types/patch-cache.js.map +1 -0
- package/lib/types/review-cache.d.ts +54 -0
- package/lib/types/review-cache.d.ts.map +1 -0
- package/lib/types/review-cache.js.map +1 -0
- package/lib/types/states.d.ts.map +1 -0
- package/lib/types/states.js.map +1 -0
- package/lib/types/types.d.ts +113 -11
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/wildmatch.d.ts +14 -0
- package/lib/types/wildmatch.d.ts.map +1 -0
- package/lib/types/wildmatch.js.map +1 -0
- package/lib/types/worktree.d.ts +8 -6
- package/lib/types/worktree.d.ts.map +1 -0
- package/lib/types/worktree.js.map +1 -0
- package/package.json +59 -49
- package/src/client/ActionToast.tsx +38 -0
- package/src/client/BaseMenu.module.css +153 -0
- package/src/client/BaseMenu.tsx +180 -0
- package/src/client/ChangeRequestCard.tsx +439 -0
- package/src/client/ChangeRequestOverview.module.css +196 -0
- package/src/client/ChangeRequestOverview.tsx +620 -0
- package/src/client/ChangedFiles.module.css +26 -0
- package/src/client/ChangedFiles.tsx +144 -0
- package/src/client/ChangesTab.module.css +984 -0
- package/src/client/ChangesTab.tsx +1288 -0
- package/src/client/ChangesTabTitle.tsx +28 -0
- package/src/client/CommitMenu.module.css +139 -0
- package/src/client/CommitMenu.tsx +186 -0
- package/src/client/DiffFile.tsx +621 -0
- package/src/client/FileLabel.module.css +53 -0
- package/src/client/FileLabel.tsx +66 -0
- package/src/client/FileTreePane.module.css +10 -0
- package/src/client/FileTreePane.tsx +79 -0
- package/src/client/ForgeTab.module.css +627 -0
- package/src/client/ForgeTab.tsx +263 -0
- package/src/client/ForgeTabTitle.tsx +21 -0
- package/src/client/HeaderButton.module.css +29 -0
- package/src/client/HeaderButton.tsx +68 -0
- package/src/client/LocalChangesCard.tsx +83 -0
- package/src/client/RefreshGlyph.module.css +22 -0
- package/src/client/RefreshGlyph.tsx +35 -0
- package/src/client/ReviewComposer.module.css +37 -0
- package/src/client/ReviewComposer.tsx +128 -0
- package/src/client/ReviewControls.module.css +162 -0
- package/src/client/ReviewControls.tsx +191 -0
- package/src/client/ReviewThread.tsx +96 -0
- package/src/client/ReviewThreads.module.css +326 -0
- package/src/client/ReviewThreads.tsx +443 -0
- package/src/client/SessionHoverForge.tsx +72 -0
- package/src/client/SessionList.module.css +85 -0
- package/src/client/SessionRowForge.tsx +86 -0
- package/src/client/SubmitReview.module.css +26 -0
- package/src/client/SubmitReview.tsx +95 -0
- package/src/client/ThreadList.module.css +218 -0
- package/src/client/ThreadList.tsx +165 -0
- package/src/client/ViewOptionsMenu.tsx +79 -0
- package/src/client/WorktreeProgress.module.css +226 -0
- package/src/client/WorktreeProgress.tsx +280 -0
- package/src/client/WorktreeSettings.module.css +29 -0
- package/src/client/WorktreeSettings.tsx +42 -0
- package/src/client/WorktreeToggle.module.css +132 -0
- package/src/client/WorktreeToggle.tsx +165 -0
- package/src/client/changes-address.ts +110 -0
- package/src/client/changes-store.ts +155 -0
- package/src/client/checkout.ts +75 -0
- package/src/client/code-selection.ts +53 -0
- package/src/client/diff-rows.ts +199 -0
- package/src/client/file-tree.ts +86 -0
- package/src/client/format.ts +37 -0
- package/src/client/glyphs.tsx +151 -0
- package/src/client/host-viewed.ts +195 -0
- package/src/client/index.ts +31 -0
- package/src/client/locales.ts +3446 -0
- package/src/client/menu-search.ts +16 -0
- package/src/client/mount.ts +439 -0
- package/src/client/review-queue.ts +256 -0
- package/src/client/review-threads.ts +204 -0
- package/src/client/review.ts +120 -0
- package/src/client/section-reach.ts +61 -0
- package/src/client/session-list.ts +79 -0
- package/src/client/source.ts +274 -0
- package/src/client/status.ts +171 -0
- package/src/client/thread-state.ts +127 -0
- package/src/client/toolbar-fold.ts +70 -0
- package/src/client/viewed-files.ts +178 -0
- package/src/client/worktree-settings-form.ts +43 -0
- package/src/commit-range.ts +27 -0
- package/src/css-modules.d.ts +5 -0
- package/src/diff.ts +162 -0
- package/src/git.ts +293 -0
- package/src/index.ts +1162 -0
- package/src/layers.ts +163 -0
- package/src/local-diff.ts +438 -0
- package/src/panel.ts +422 -0
- package/src/parse.ts +137 -0
- package/src/patch-cache.ts +73 -0
- package/src/review-cache.ts +98 -0
- package/src/states.ts +350 -0
- package/src/types.ts +504 -0
- package/src/wildmatch.ts +121 -0
- package/src/worktree.ts +576 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawn rows of one file diff: numbered patch lines, their side-by-side
|
|
3
|
+
* pairing, and the unchanged lines between hunks, which stay collapsed until
|
|
4
|
+
* the file's new side supplies them.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ForgeDiffFile, ForgeDiffHunk } from '../types.ts'
|
|
8
|
+
|
|
9
|
+
/** One drawn line with its number on each side that has it. */
|
|
10
|
+
export type DiffRow =
|
|
11
|
+
| { readonly kind: 'add'; readonly old: null; readonly new: number; readonly text: string }
|
|
12
|
+
| { readonly kind: 'del'; readonly old: number; readonly new: null; readonly text: string }
|
|
13
|
+
| { readonly kind: 'context'; readonly old: number; readonly new: number; readonly text: string }
|
|
14
|
+
|
|
15
|
+
/** One side of a side-by-side row. */
|
|
16
|
+
export interface SplitCell {
|
|
17
|
+
readonly kind: DiffRow['kind']
|
|
18
|
+
readonly no: number
|
|
19
|
+
readonly text: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One side-by-side row; a side without a line is null. */
|
|
23
|
+
export interface SplitRow {
|
|
24
|
+
readonly left: SplitCell | null
|
|
25
|
+
readonly right: SplitCell | null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A run of drawn rows, or unchanged lines drawn as one row that expands them. */
|
|
29
|
+
export type DiffSegment =
|
|
30
|
+
| { readonly kind: 'rows'; readonly rows: readonly DiffRow[] }
|
|
31
|
+
/**
|
|
32
|
+
* `index` is the hunk the lines precede, or the hunk count for the lines after the last hunk;
|
|
33
|
+
* `count` is null while the length of the file is unknown.
|
|
34
|
+
*/
|
|
35
|
+
| { readonly kind: 'gap'; readonly index: number; readonly count: number | null }
|
|
36
|
+
|
|
37
|
+
/** A line as drawn: without the carriage return of a CRLF line ending. */
|
|
38
|
+
function drawn(text: string): string {
|
|
39
|
+
return text.endsWith('\r') ? text.slice(0, -1) : text
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Number the lines of a hunk. Context lines count on both sides, deletions on
|
|
44
|
+
* the old side, and additions on the new side; a ``
|
|
45
|
+
* marker is not a line.
|
|
46
|
+
* @param hunk - one hunk.
|
|
47
|
+
* @returns the rows in patch order.
|
|
48
|
+
*/
|
|
49
|
+
export function hunkRows(hunk: ForgeDiffHunk): DiffRow[] {
|
|
50
|
+
let oldNo = hunk.oldStart
|
|
51
|
+
let newNo = hunk.newStart
|
|
52
|
+
const rows: DiffRow[] = []
|
|
53
|
+
for (const line of hunk.lines) {
|
|
54
|
+
const sign = line.charAt(0)
|
|
55
|
+
const text = drawn(line.slice(1))
|
|
56
|
+
if (sign === '+') rows.push({ kind: 'add', old: null, new: newNo++, text })
|
|
57
|
+
else if (sign === '-') rows.push({ kind: 'del', old: oldNo++, new: null, text })
|
|
58
|
+
else if (sign !== '\\') rows.push({ kind: 'context', old: oldNo++, new: newNo++, text })
|
|
59
|
+
}
|
|
60
|
+
return rows
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Pair rows for the side-by-side view: each run of deletions sits beside the
|
|
65
|
+
* run of additions that follows it, row by row, and context rows fill both sides.
|
|
66
|
+
* @param rows - rows in patch order.
|
|
67
|
+
* @returns the paired rows.
|
|
68
|
+
*/
|
|
69
|
+
export function splitRows(rows: readonly DiffRow[]): SplitRow[] {
|
|
70
|
+
const paired: SplitRow[] = []
|
|
71
|
+
let deleted: SplitCell[] = []
|
|
72
|
+
let added: SplitCell[] = []
|
|
73
|
+
const flush = (): void => {
|
|
74
|
+
for (let at = 0; at < Math.max(deleted.length, added.length); at++) {
|
|
75
|
+
paired.push({ left: deleted[at] ?? null, right: added[at] ?? null })
|
|
76
|
+
}
|
|
77
|
+
deleted = []
|
|
78
|
+
added = []
|
|
79
|
+
}
|
|
80
|
+
for (const row of rows) {
|
|
81
|
+
switch (row.kind) {
|
|
82
|
+
case 'del':
|
|
83
|
+
if (added.length > 0) flush()
|
|
84
|
+
deleted.push({ kind: 'del', no: row.old, text: row.text })
|
|
85
|
+
break
|
|
86
|
+
case 'add':
|
|
87
|
+
added.push({ kind: 'add', no: row.new, text: row.text })
|
|
88
|
+
break
|
|
89
|
+
case 'context':
|
|
90
|
+
flush()
|
|
91
|
+
paired.push({ left: { kind: 'context', no: row.old, text: row.text }, right: { kind: 'context', no: row.new, text: row.text } })
|
|
92
|
+
break
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
flush()
|
|
96
|
+
return paired
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Split file text into lines as a patch numbers them; a final line ending ends the last line.
|
|
101
|
+
* @param text - the file text.
|
|
102
|
+
* @returns the lines with any carriage returns kept.
|
|
103
|
+
*/
|
|
104
|
+
export function textLines(text: string): string[] {
|
|
105
|
+
const lines = text.split('\n')
|
|
106
|
+
if (lines.at(-1) === '') lines.pop()
|
|
107
|
+
return lines
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Whether file text is the new side the hunks describe: every context and
|
|
112
|
+
* added line sits at its number. A file changed after its diff was read fails.
|
|
113
|
+
* @param lines - the file's lines.
|
|
114
|
+
* @param hunks - hunks of the file's diff.
|
|
115
|
+
* @returns whether the lines match.
|
|
116
|
+
*/
|
|
117
|
+
export function matchesHunks(lines: readonly string[], hunks: readonly ForgeDiffHunk[]): boolean {
|
|
118
|
+
return hunks.every((hunk) => {
|
|
119
|
+
let at = hunk.newStart - 1
|
|
120
|
+
for (const line of hunk.lines) {
|
|
121
|
+
const sign = line.charAt(0)
|
|
122
|
+
if (sign === '-' || sign === '\\') continue
|
|
123
|
+
if (lines[at] !== line.slice(1)) return false
|
|
124
|
+
at++
|
|
125
|
+
}
|
|
126
|
+
return true
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Whether the hunk ends the new side: a missing-newline marker follows one of its new-side lines. */
|
|
131
|
+
function endsNewSide(hunk: ForgeDiffHunk): boolean {
|
|
132
|
+
let previous = '-'
|
|
133
|
+
for (const line of hunk.lines) {
|
|
134
|
+
if (line.startsWith('\\') && !previous.startsWith('-')) return true
|
|
135
|
+
previous = line
|
|
136
|
+
}
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Lay out one file as runs of rows separated by collapsed unchanged lines.
|
|
142
|
+
* Unchanged lines before a hunk, between hunks, and after the last hunk
|
|
143
|
+
* collapse into one gap each; an open gap draws its lines from `lines` once
|
|
144
|
+
* they are known. The lines after the last hunk are unknown until `lines`
|
|
145
|
+
* arrives, unless the file is new, deleted, or its last hunk reaches its end.
|
|
146
|
+
* @param file - one file of a diff.
|
|
147
|
+
* @param lines - the file's new side, which {@link matchesHunks} accepted, or undefined while it has not been read.
|
|
148
|
+
* @param open - indexes of the gaps the user expanded.
|
|
149
|
+
* @returns the segments in file order.
|
|
150
|
+
*/
|
|
151
|
+
export function segmentsOf(file: ForgeDiffFile, lines: readonly string[] | undefined, open: ReadonlySet<number>): DiffSegment[] {
|
|
152
|
+
const segments: DiffSegment[] = []
|
|
153
|
+
let rows: DiffRow[] = []
|
|
154
|
+
const flush = (): void => {
|
|
155
|
+
if (rows.length > 0) segments.push({ kind: 'rows', rows })
|
|
156
|
+
rows = []
|
|
157
|
+
}
|
|
158
|
+
/** Unchanged new-side lines `from` to `to`, null while unknown, whose old numbers are `offset` higher. */
|
|
159
|
+
const gap = (index: number, from: number, to: number | null, offset: number): void => {
|
|
160
|
+
if (lines !== undefined && to !== null && open.has(index)) {
|
|
161
|
+
for (const [at, text] of lines.slice(from - 1, to).entries()) {
|
|
162
|
+
rows.push({ kind: 'context', old: from + at + offset, new: from + at, text: drawn(text) })
|
|
163
|
+
}
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
flush()
|
|
167
|
+
segments.push({ kind: 'gap', index, count: to === null ? null : to - from + 1 })
|
|
168
|
+
}
|
|
169
|
+
let newEnd = 0
|
|
170
|
+
let oldEnd = 0
|
|
171
|
+
for (const [index, hunk] of file.hunks.entries()) {
|
|
172
|
+
if (hunk.newStart - 1 > newEnd) gap(index, newEnd + 1, hunk.newStart - 1, hunk.oldStart - hunk.newStart)
|
|
173
|
+
rows.push(...hunkRows(hunk))
|
|
174
|
+
newEnd = hunk.newStart + hunk.newLines - 1
|
|
175
|
+
oldEnd = hunk.oldStart + hunk.oldLines - 1
|
|
176
|
+
}
|
|
177
|
+
const last = file.hunks.at(-1)
|
|
178
|
+
if (last !== undefined && file.status !== 'added' && file.status !== 'deleted' && !endsNewSide(last)) {
|
|
179
|
+
if (lines === undefined) gap(file.hunks.length, newEnd + 1, null, oldEnd - newEnd)
|
|
180
|
+
else if (lines.length > newEnd) gap(file.hunks.length, newEnd + 1, lines.length, oldEnd - newEnd)
|
|
181
|
+
}
|
|
182
|
+
flush()
|
|
183
|
+
return segments
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Count the rows a file's hunks draw.
|
|
188
|
+
* @param file - one file of a diff.
|
|
189
|
+
* @returns the number of context, added, and deleted lines in its hunks.
|
|
190
|
+
*/
|
|
191
|
+
export function drawnLines(file: ForgeDiffFile): number {
|
|
192
|
+
let count = 0
|
|
193
|
+
for (const hunk of file.hunks) {
|
|
194
|
+
for (const line of hunk.lines) {
|
|
195
|
+
if (!line.startsWith('\\')) count++
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return count
|
|
199
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** Directory navigation derived only from the files in one loaded diff. */
|
|
2
|
+
import { assertNever } from '@deepseek-ai/dsh-util-values'
|
|
3
|
+
import type { ForgeDiffFile } from '../types.ts'
|
|
4
|
+
|
|
5
|
+
/** A changed file or a compacted chain of directories containing changed files. */
|
|
6
|
+
export type ChangedFileNode =
|
|
7
|
+
| { readonly kind: 'file'; readonly path: string; readonly name: string; readonly file: ForgeDiffFile }
|
|
8
|
+
| { readonly kind: 'directory'; readonly path: string; readonly name: string; readonly children: readonly ChangedFileNode[] }
|
|
9
|
+
|
|
10
|
+
interface Directory {
|
|
11
|
+
path: string
|
|
12
|
+
name: string
|
|
13
|
+
directories: Map<string, Directory>
|
|
14
|
+
files: ForgeDiffFile[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function childrenOf(directory: Directory): ChangedFileNode[] {
|
|
18
|
+
const directories = [...directory.directories.values()].map((child): ChangedFileNode => {
|
|
19
|
+
let leaf = child
|
|
20
|
+
let name = child.name
|
|
21
|
+
while (leaf.files.length === 0 && leaf.directories.size === 1) {
|
|
22
|
+
for (const next of leaf.directories.values()) {
|
|
23
|
+
name += `/${next.name}`
|
|
24
|
+
leaf = next
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return { kind: 'directory', path: leaf.path, name, children: childrenOf(leaf) }
|
|
28
|
+
})
|
|
29
|
+
const files = directory.files.map((file): ChangedFileNode => ({ kind: 'file', path: file.path, name: file.path.slice(file.path.lastIndexOf('/') + 1), file }))
|
|
30
|
+
const ordered = (nodes: ChangedFileNode[]): ChangedFileNode[] => nodes.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
|
|
31
|
+
return [...ordered(directories), ...ordered(files)]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Build a directory-first tree, compacting directories with a single directory child.
|
|
36
|
+
* @param files - changed files; Git paths use forward slashes, including on Windows.
|
|
37
|
+
* @returns the root's children in stable name order.
|
|
38
|
+
*/
|
|
39
|
+
export function changedFileTree(files: readonly ForgeDiffFile[]): readonly ChangedFileNode[] {
|
|
40
|
+
const root: Directory = { path: '', name: '', directories: new Map(), files: [] }
|
|
41
|
+
for (const file of files) {
|
|
42
|
+
let directory = root
|
|
43
|
+
for (const name of file.path.split('/').slice(0, -1)) {
|
|
44
|
+
let child = directory.directories.get(name)
|
|
45
|
+
if (child === undefined) {
|
|
46
|
+
child = { path: directory.path === '' ? name : `${directory.path}/${name}`, name, directories: new Map(), files: [] }
|
|
47
|
+
directory.directories.set(name, child)
|
|
48
|
+
}
|
|
49
|
+
directory = child
|
|
50
|
+
}
|
|
51
|
+
directory.files.push(file)
|
|
52
|
+
}
|
|
53
|
+
return childrenOf(root)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Read every file in the tree's depth-first display order, independent of navigation folds and filtering.
|
|
58
|
+
* @param nodes - the complete directory-first tree.
|
|
59
|
+
* @returns the original file objects in display order.
|
|
60
|
+
*/
|
|
61
|
+
export function filesInTreeOrder(nodes: readonly ChangedFileNode[]): readonly ForgeDiffFile[] {
|
|
62
|
+
return nodes.flatMap((node): readonly ForgeDiffFile[] => {
|
|
63
|
+
switch (node.kind) {
|
|
64
|
+
case 'file': return [node.file]
|
|
65
|
+
case 'directory': return filesInTreeOrder(node.children)
|
|
66
|
+
/* v8 ignore next -- closed ChangedFileNode union exhaustiveness guard */
|
|
67
|
+
default: return assertNever(node, 'ChangedFileNode')
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Keep matching files and their ancestors without changing directory identities.
|
|
74
|
+
* @param nodes - the complete compacted tree.
|
|
75
|
+
* @param query - case-insensitive text matched against current and previous paths.
|
|
76
|
+
* @returns matching branches, or the original tree for an empty query.
|
|
77
|
+
*/
|
|
78
|
+
export function filterFileTree(nodes: readonly ChangedFileNode[], query: string): readonly ChangedFileNode[] {
|
|
79
|
+
const needle = query.trim().toLowerCase()
|
|
80
|
+
if (needle === '') return nodes
|
|
81
|
+
return nodes.flatMap((node): ChangedFileNode[] => {
|
|
82
|
+
if (node.kind === 'file') return [node.path, node.file.previousPath ?? ''].some(path => path.toLowerCase().includes(needle)) ? [node] : []
|
|
83
|
+
const children = filterFileTree(node.children, needle)
|
|
84
|
+
return children.length === 0 ? [] : [{ ...node, children }]
|
|
85
|
+
})
|
|
86
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Localized durations and relative times shown on change request cards. */
|
|
2
|
+
|
|
3
|
+
import { relativeTime } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
4
|
+
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
|
|
5
|
+
import type { NS } from './locales.ts'
|
|
6
|
+
|
|
7
|
+
type Translate = PropsLocale<typeof NS>['t']
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Format an elapsed time as hours and minutes, minutes and seconds, or seconds.
|
|
11
|
+
* @param ms - elapsed milliseconds.
|
|
12
|
+
* @param t - forge panel copy.
|
|
13
|
+
* @returns the localized duration.
|
|
14
|
+
*/
|
|
15
|
+
export function formatDuration(ms: number, t: Translate): string {
|
|
16
|
+
const total = Math.round(ms / 1000)
|
|
17
|
+
const hours = Math.floor(total / 3600)
|
|
18
|
+
const minutes = Math.floor(total % 3600 / 60)
|
|
19
|
+
const seconds = total % 60
|
|
20
|
+
if (hours > 0) return t('duration.hours', { hours, minutes })
|
|
21
|
+
if (minutes > 0) return t('duration.minutes', { minutes, seconds })
|
|
22
|
+
return t('duration.seconds', { seconds })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Format how long ago an ISO 8601 time was.
|
|
27
|
+
* @param iso - the time.
|
|
28
|
+
* @param now - current epoch milliseconds.
|
|
29
|
+
* @param t - forge panel copy.
|
|
30
|
+
* @returns the localized relative time, or undefined when `iso` does not parse.
|
|
31
|
+
*/
|
|
32
|
+
export function formatAgo(iso: string, now: number, t: Translate): string | undefined {
|
|
33
|
+
const at = Date.parse(iso)
|
|
34
|
+
if (!Number.isFinite(at)) return undefined
|
|
35
|
+
const { unit, n } = relativeTime(at, now)
|
|
36
|
+
return t(`time.${unit}`, { n })
|
|
37
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/** Lifecycle glyphs of change requests and outcome glyphs of their checks. */
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from 'react'
|
|
4
|
+
import clsx from 'clsx'
|
|
5
|
+
import { IconCheckCircleFillRegular, IconCloseCircleFillRegular } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
6
|
+
import type { CheckState, ChecksRollup } from '@deepseek-ai/dsh-experimental-forge/types'
|
|
7
|
+
import type { ChangeRequestTone } from './status.ts'
|
|
8
|
+
import css from './ForgeTab.module.css'
|
|
9
|
+
|
|
10
|
+
const BRANCH_ENDS = (
|
|
11
|
+
<>
|
|
12
|
+
<circle cx="4" cy="3.5" r="1.6" />
|
|
13
|
+
<circle cx="4" cy="12.5" r="1.6" />
|
|
14
|
+
<circle cx="12" cy="12.5" r="1.6" />
|
|
15
|
+
</>
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
/** The head branch line that bends back toward the base, shared by the open and blocked shapes. */
|
|
19
|
+
const TOWARD_BASE = (
|
|
20
|
+
<>
|
|
21
|
+
<path d="M12 10.9V6.6C12 5.5 11.1 4.6 10 4.6H8.2" />
|
|
22
|
+
<path d="M9.6 3L8 4.6L9.6 6.2" />
|
|
23
|
+
</>
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const OPEN = (
|
|
27
|
+
<>
|
|
28
|
+
{BRANCH_ENDS}
|
|
29
|
+
<path d="M4 5.1V10.9" />
|
|
30
|
+
{TOWARD_BASE}
|
|
31
|
+
</>
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
const SHAPES: Record<ChangeRequestTone, ReactNode> = {
|
|
35
|
+
open: OPEN,
|
|
36
|
+
unknown: OPEN,
|
|
37
|
+
blocked: (
|
|
38
|
+
<>
|
|
39
|
+
<path d="M2.6 2.1L5.4 4.9M5.4 2.1L2.6 4.9" />
|
|
40
|
+
<circle cx="4" cy="12.5" r="1.6" />
|
|
41
|
+
<circle cx="12" cy="12.5" r="1.6" />
|
|
42
|
+
<path d="M4 5.6V10.9" />
|
|
43
|
+
{TOWARD_BASE}
|
|
44
|
+
</>
|
|
45
|
+
),
|
|
46
|
+
draft: (
|
|
47
|
+
<>
|
|
48
|
+
{BRANCH_ENDS}
|
|
49
|
+
<path d="M4 5.1V10.9M12 10.4V9.6M12 7.9V7.1M12 5.4V4.6" />
|
|
50
|
+
</>
|
|
51
|
+
),
|
|
52
|
+
merged: (
|
|
53
|
+
<>
|
|
54
|
+
<circle cx="4.5" cy="3.5" r="1.6" />
|
|
55
|
+
<circle cx="4.5" cy="12.5" r="1.6" />
|
|
56
|
+
<circle cx="11.5" cy="8" r="1.6" />
|
|
57
|
+
<path d="M4.5 5.1V10.9M4.5 5.1C4.5 7 6.5 8 9.9 8" />
|
|
58
|
+
</>
|
|
59
|
+
),
|
|
60
|
+
closed: (
|
|
61
|
+
<>
|
|
62
|
+
{BRANCH_ENDS}
|
|
63
|
+
<path d="M4 5.1V10.9M12 10.9V8.6M10.4 3.2L13.6 6.4M13.6 3.2L10.4 6.4" />
|
|
64
|
+
</>
|
|
65
|
+
),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Center of each shape's head branch end, which a checks mark covers. */
|
|
69
|
+
const HEAD: Record<ChangeRequestTone, readonly [number, number]> = {
|
|
70
|
+
open: [12, 12.5],
|
|
71
|
+
unknown: [12, 12.5],
|
|
72
|
+
blocked: [12, 12.5],
|
|
73
|
+
draft: [12, 12.5],
|
|
74
|
+
merged: [11.5, 8],
|
|
75
|
+
closed: [12, 12.5],
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Draw a change request in its lifecycle shape: open, open with a cross for
|
|
80
|
+
* one that needs its author, draft, merged, closed, or the open shape for an
|
|
81
|
+
* unknown state.
|
|
82
|
+
* @param props.tone - lifecycle state that picks the shape and, when tinted, the color.
|
|
83
|
+
* @param props.tinted - whether the state picks the color; otherwise the glyph uses the current text color.
|
|
84
|
+
* @param props.size - width and height in pixels, 15 by default.
|
|
85
|
+
* @param props.label - accessible name; omit it for a decorative glyph.
|
|
86
|
+
* @param props.checks - checks outcome drawn as a filled head branch end in its state color; omit it for the plain end.
|
|
87
|
+
* @returns the SVG glyph.
|
|
88
|
+
*/
|
|
89
|
+
export function ChangeRequestGlyph({ tone, tinted = true, size = 15, label, checks }: {
|
|
90
|
+
readonly tone: ChangeRequestTone
|
|
91
|
+
readonly tinted?: boolean | undefined
|
|
92
|
+
readonly size?: number | undefined
|
|
93
|
+
readonly label?: string | undefined
|
|
94
|
+
readonly checks?: Exclude<ChecksRollup, 'none'> | undefined
|
|
95
|
+
}): ReactNode {
|
|
96
|
+
return (
|
|
97
|
+
<svg
|
|
98
|
+
width={size}
|
|
99
|
+
height={size}
|
|
100
|
+
viewBox="0 0 16 16"
|
|
101
|
+
fill="none"
|
|
102
|
+
stroke="currentColor"
|
|
103
|
+
strokeWidth={1.3}
|
|
104
|
+
strokeLinecap="round"
|
|
105
|
+
strokeLinejoin="round"
|
|
106
|
+
className={clsx(css.glyph, tinted && css.tinted)}
|
|
107
|
+
data-tone={tone}
|
|
108
|
+
data-forge-glyph={tone}
|
|
109
|
+
{...label === undefined ? { 'aria-hidden': true } : { role: 'img', 'aria-label': label }}
|
|
110
|
+
>
|
|
111
|
+
{SHAPES[tone]}
|
|
112
|
+
{/* Concentric with the stroked end and larger than its outer edge, so it covers that end completely. */}
|
|
113
|
+
{checks !== undefined && <circle cx={HEAD[tone][0]} cy={HEAD[tone][1]} r="2.3" stroke="none" className={css.checksMark} data-checks={checks} />}
|
|
114
|
+
</svg>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const CHECK_TONE: Record<CheckState, string | undefined> = {
|
|
119
|
+
success: css.pass,
|
|
120
|
+
failure: css.fail,
|
|
121
|
+
pending: css.running,
|
|
122
|
+
neutral: undefined,
|
|
123
|
+
skipped: undefined,
|
|
124
|
+
cancelled: undefined,
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Draw the decorative outcome of one check beside its text: a filled circle
|
|
129
|
+
* for success or failure, a half-filled ring while it runs, and an empty ring
|
|
130
|
+
* for neutral, skipped, and cancelled checks.
|
|
131
|
+
* @param props.state - check outcome.
|
|
132
|
+
* @param props.size - width and height in pixels, 16 by default.
|
|
133
|
+
* @returns the glyph.
|
|
134
|
+
*/
|
|
135
|
+
export function CheckGlyph({ state, size = 16 }: { readonly state: CheckState; readonly size?: number | undefined }): ReactNode {
|
|
136
|
+
const running = state === 'pending'
|
|
137
|
+
return (
|
|
138
|
+
<span className={clsx(css.checkGlyph, CHECK_TONE[state])} data-forge-check={state} aria-hidden="true">
|
|
139
|
+
{state === 'success' && <IconCheckCircleFillRegular size={size} />}
|
|
140
|
+
{state === 'failure' && <IconCloseCircleFillRegular size={size} />}
|
|
141
|
+
{state !== 'success' && state !== 'failure' && (
|
|
142
|
+
<svg width={size} height={size} viewBox="0 0 16 16" fill="none">
|
|
143
|
+
<circle cx="8" cy="8" r="6.25" stroke="currentColor" strokeWidth={1.5} />
|
|
144
|
+
{running
|
|
145
|
+
? <path d="M8 3.5A4.5 4.5 0 0 1 8 12.5Z" fill="currentColor" />
|
|
146
|
+
: <path d="M5.5 8H10.5" stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" />}
|
|
147
|
+
</svg>
|
|
148
|
+
)}
|
|
149
|
+
</span>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Viewed marks a code host keeps for a whole change request. The changes view reads them with the diff and shows each
|
|
3
|
+
* mark the reader sets at once, while the host saves it; a mark the host refuses returns to what the host keeps, and a
|
|
4
|
+
* mark the host may have saved stays.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useMemo, useRef, useState } from 'react'
|
|
8
|
+
import type { ForgeViewedFiles, ForgeViewedRequest, ForgeViewedState } from '@deepseek-ai/dsh-experimental-forge/types'
|
|
9
|
+
import type { RemoteResult } from '@deepseek-ai/dsh-typert-protocol'
|
|
10
|
+
import type { ForgeDiffFile } from '../types.ts'
|
|
11
|
+
import type { ViewedMarks } from './viewed-files.ts'
|
|
12
|
+
|
|
13
|
+
/** The host's marks on the files of one diff, or why the view could not read them. */
|
|
14
|
+
export type HostViewed =
|
|
15
|
+
| { readonly kind: 'read'; readonly files: Readonly<Record<string, ForgeViewedState>> }
|
|
16
|
+
| { readonly kind: 'failed'; readonly message: string }
|
|
17
|
+
|
|
18
|
+
/** A loaded change request diff whose marks the host keeps. */
|
|
19
|
+
export interface HostViewedOwner {
|
|
20
|
+
/** Binding URL of the change request. */
|
|
21
|
+
readonly url: string
|
|
22
|
+
/** Head commit the diff and its marks were read at. */
|
|
23
|
+
readonly headSha: string
|
|
24
|
+
/** Files of the diff with the digest of each change. */
|
|
25
|
+
readonly files: readonly ForgeDiffFile[]
|
|
26
|
+
/** The host's marks. */
|
|
27
|
+
readonly viewed: HostViewed
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Why a mark the host did not save returned to its previous state, or why the view cannot tell whether the host saved
|
|
32
|
+
* a mark it keeps showing.
|
|
33
|
+
*/
|
|
34
|
+
export type HostViewedFailure =
|
|
35
|
+
| { readonly kind: 'stale' }
|
|
36
|
+
| { readonly kind: 'failed'; readonly message: string }
|
|
37
|
+
| { readonly kind: 'unknown'; readonly message: string }
|
|
38
|
+
|
|
39
|
+
/** The host's marks of the shown diff with the reader's writes. */
|
|
40
|
+
export interface HostViewedMarks {
|
|
41
|
+
/** Marks as digests, so a mark of the current change matches the file's digest and a dismissed mark matches none. */
|
|
42
|
+
readonly marks: ViewedMarks
|
|
43
|
+
/** Whether the host's marks could not be read, so no file can be marked. */
|
|
44
|
+
readonly unavailable: boolean
|
|
45
|
+
/** Whether a write of the file's mark is in flight. */
|
|
46
|
+
readonly saving: (path: string) => boolean
|
|
47
|
+
/**
|
|
48
|
+
* Show a mark at once and save it on the host.
|
|
49
|
+
* @param path - the file's path.
|
|
50
|
+
* @param viewed - whether the file is viewed.
|
|
51
|
+
* @returns false, changing nothing, while the marks are unavailable or the file's previous write is in flight.
|
|
52
|
+
*/
|
|
53
|
+
readonly mark: (path: string, viewed: boolean) => boolean
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The reader's mark of one file over the host's. It applies to every read of its change request and head while its
|
|
58
|
+
* write is in flight, and once settled only to `read`, the read shown when it settled, which may not hold it yet.
|
|
59
|
+
*/
|
|
60
|
+
interface Choice {
|
|
61
|
+
readonly viewed: boolean
|
|
62
|
+
readonly read: HostViewedOwner
|
|
63
|
+
readonly saving: boolean
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The reader's marks of one change request at one head, by path. */
|
|
67
|
+
interface Choices {
|
|
68
|
+
readonly key: string | null
|
|
69
|
+
readonly files: ReadonlyMap<string, Choice>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const NO_CHOICES: ReadonlyMap<string, boolean> = new Map()
|
|
73
|
+
const NO_MARKS: ViewedMarks = new Map()
|
|
74
|
+
|
|
75
|
+
/** Digest a dismissed mark records: no file change digests to it, so the file reads as changed since it was viewed. */
|
|
76
|
+
const DISMISSED = ''
|
|
77
|
+
|
|
78
|
+
/** Identity of the marks a read holds: one change request at one head commit. */
|
|
79
|
+
function keyOf(owner: HostViewedOwner): string {
|
|
80
|
+
return `${owner.url}\n${owner.headSha}`
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert the host's marks of a diff to digests, with the reader's choices taking the place of the host's.
|
|
85
|
+
* @param files - files of the diff with the digest of each change.
|
|
86
|
+
* @param host - the host's state of each file the host listed.
|
|
87
|
+
* @param chosen - the reader's marks that apply to the diff, by path.
|
|
88
|
+
* @returns the digest of each file marked viewed or dismissed.
|
|
89
|
+
*/
|
|
90
|
+
export function hostMarksOf(
|
|
91
|
+
files: readonly ForgeDiffFile[],
|
|
92
|
+
host: Readonly<Record<string, ForgeViewedState>>,
|
|
93
|
+
chosen: ReadonlyMap<string, boolean>,
|
|
94
|
+
): ViewedMarks {
|
|
95
|
+
const marks = new Map<string, string>()
|
|
96
|
+
for (const file of files) {
|
|
97
|
+
const choice = chosen.get(file.path)
|
|
98
|
+
// Only the host's own entries count, so a path such as `constructor` never reads an inherited property.
|
|
99
|
+
const state = choice === undefined ? Object.hasOwn(host, file.path) ? host[file.path] : undefined
|
|
100
|
+
: choice ? 'viewed' : 'unviewed'
|
|
101
|
+
if (state === 'viewed') marks.set(file.path, file.digest)
|
|
102
|
+
else if (state === 'dismissed') marks.set(file.path, DISMISSED)
|
|
103
|
+
}
|
|
104
|
+
return marks
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convert a read of the host's marks to the state the view keeps for a diff read at `headSha`.
|
|
109
|
+
* @param result - the read, or null when the diff shows chosen commits, which the host keeps no marks for.
|
|
110
|
+
* @param headSha - head commit the diff was read at.
|
|
111
|
+
* @returns the marks, why they could not be read, or null when the browser keeps the marks instead.
|
|
112
|
+
*/
|
|
113
|
+
export function hostViewedOf(result: RemoteResult<ForgeViewedFiles | null> | null, headSha: string): HostViewed | null {
|
|
114
|
+
if (result === null) return null
|
|
115
|
+
if (!result.ok) return { kind: 'failed', message: result.error.message }
|
|
116
|
+
if (result.value === null) return null
|
|
117
|
+
return result.value.headSha === headSha ? { kind: 'read', files: result.value.files }
|
|
118
|
+
// The diff and the marks were read at two heads, so neither matches the other.
|
|
119
|
+
: { kind: 'failed', message: `viewed marks were read at ${result.value.headSha}, the diff at ${headSha}` }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Keep the reader's marks of a change request at one head over the host's, saving each on the host. A read at the same
|
|
124
|
+
* head keeps the marks whose writes are in flight and their locks; a mark whose write settled stays until the next read,
|
|
125
|
+
* which then holds what the host saved. A read at another head or of another change request starts without choices.
|
|
126
|
+
* @param owner - the shown diff when the host keeps its marks, else null.
|
|
127
|
+
* @param save - saves one mark on the host.
|
|
128
|
+
* @param failed - reports a mark the host did not save after it returned to its previous state, or a mark the host may
|
|
129
|
+
* not have saved.
|
|
130
|
+
* @returns the marks and the write action, or null when the browser keeps the marks.
|
|
131
|
+
*/
|
|
132
|
+
export function useHostViewed(
|
|
133
|
+
owner: HostViewedOwner | null,
|
|
134
|
+
save: (url: string, request: ForgeViewedRequest) => Promise<RemoteResult<void>>,
|
|
135
|
+
failed: (failure: HostViewedFailure) => void,
|
|
136
|
+
): HostViewedMarks | null {
|
|
137
|
+
const [choices, setChoices] = useState<Choices>({ key: null, files: new Map() })
|
|
138
|
+
// The read a write that settles later finds shown; assigned in render so a settlement after this render sees it.
|
|
139
|
+
const shown = useRef(owner)
|
|
140
|
+
shown.current = owner
|
|
141
|
+
const key = owner === null ? null : keyOf(owner)
|
|
142
|
+
const applying = useMemo(() => {
|
|
143
|
+
if (owner === null || choices.key !== key) return new Map<string, Choice>()
|
|
144
|
+
return new Map([...choices.files].filter(([, choice]) => choice.saving || choice.read === owner))
|
|
145
|
+
}, [owner, key, choices])
|
|
146
|
+
const marks = useMemo(() => {
|
|
147
|
+
if (owner?.viewed.kind !== 'read') return NO_MARKS
|
|
148
|
+
const chosen = applying.size === 0 ? NO_CHOICES : new Map([...applying].map(([path, choice]) => [path, choice.viewed]))
|
|
149
|
+
return hostMarksOf(owner.files, owner.viewed.files, chosen)
|
|
150
|
+
}, [owner, applying])
|
|
151
|
+
if (owner === null || key === null) return null
|
|
152
|
+
const { viewed } = owner
|
|
153
|
+
const saving = (path: string): boolean => applying.get(path)?.saving === true
|
|
154
|
+
/** Settle the write of `path` at this key: keep `next` for the read shown now, or drop the choice. */
|
|
155
|
+
const settle = (path: string, next: boolean | undefined): void => {
|
|
156
|
+
setChoices((current) => {
|
|
157
|
+
if (current.key !== key) return current
|
|
158
|
+
const files = new Map(current.files)
|
|
159
|
+
// A read at another key never takes these choices, so only a view without the host's marks keeps the original read.
|
|
160
|
+
const read = shown.current ?? owner
|
|
161
|
+
if (next === undefined) files.delete(path)
|
|
162
|
+
else files.set(path, { viewed: next, read, saving: false })
|
|
163
|
+
return { key, files }
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
marks,
|
|
168
|
+
unavailable: viewed.kind === 'failed',
|
|
169
|
+
saving,
|
|
170
|
+
mark: (path, value) => {
|
|
171
|
+
if (viewed.kind === 'failed' || saving(path)) return false
|
|
172
|
+
const previous = applying.get(path)?.viewed
|
|
173
|
+
setChoices((current) => {
|
|
174
|
+
const files = new Map(current.key === key ? current.files : [])
|
|
175
|
+
return { key, files: files.set(path, { viewed: value, read: owner, saving: true }) }
|
|
176
|
+
})
|
|
177
|
+
void save(owner.url, { path, viewed: value, headSha: owner.headSha }).then((result) => {
|
|
178
|
+
if (result.ok) {
|
|
179
|
+
settle(path, value)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
const { code, message } = result.error
|
|
183
|
+
// The host may have saved a mark it did not confirm, so the mark stays and the reader is told to check.
|
|
184
|
+
if (code === 'forgePanel/action-unknown') {
|
|
185
|
+
settle(path, value)
|
|
186
|
+
failed({ kind: 'unknown', message })
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
settle(path, previous)
|
|
190
|
+
failed(code === 'forgePanel/stale-head' ? { kind: 'stale' } : { kind: 'failed', message })
|
|
191
|
+
})
|
|
192
|
+
return true
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
}
|