The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | CodeViewScrollTarget, |
| 3 | SelectedLineRange, |
| 4 | FileDiffMetadata, |
| 5 | SelectionSide, |
| 6 | } from "@pierre/diffs"; |
| 7 | import { useState } from "react"; |
| 8 | |
| 9 | interface FileRevealHandle { |
| 10 | getInstance(): |
| 11 | | { |
| 12 | getRenderedItems?(): readonly { |
| 13 | id: string; |
| 14 | instance: { |
| 15 | getDiffForSearch?(): FileDiffMetadata | undefined; |
| 16 | revealLine?(line: number): boolean; |
| 17 | expandHunk?(hunkIndex: number, direction: "up" | "down", lineCount: number): void; |
| 18 | getLinePosition( |
| 19 | line: number, |
| 20 | side?: SelectionSide, |
| 21 | ): { top: number; height: number } | undefined; |
| 22 | }; |
| 23 | }[]; |
| 24 | } |
| 25 | | undefined; |
| 26 | getItem(id: string): { collapsed?: boolean } | undefined; |
| 27 | scrollTo(target: CodeViewScrollTarget): void; |
| 28 | } |
| 29 | |
| 30 | // Mirrors the viewer's `revealLine`, which reads distances from the edges of the collapsed gap. |
| 31 | function expandPartialDiffToLine( |
| 32 | diff: FileDiffMetadata, |
| 33 | line: number, |
| 34 | expandHunk: (hunkIndex: number, direction: "up" | "down", lineCount: number) => void, |
| 35 | ): boolean { |
| 36 | const { hunks } = diff; |
| 37 | if ( |
| 38 | hunks.some( |
| 39 | (hunk) => line >= hunk.additionStart && line < hunk.additionStart + hunk.additionCount, |
| 40 | ) |
| 41 | ) { |
| 42 | return false; |
| 43 | } |
| 44 | const next = hunks.findIndex((hunk) => line < hunk.additionStart); |
| 45 | const previous = next === -1 ? hunks.at(-1) : hunks[next - 1]; |
| 46 | const fromStart = line - (previous ? previous.additionStart + previous.additionCount : 1) + 1; |
| 47 | const after = next === -1 ? undefined : hunks[next]; |
| 48 | if (after === undefined) expandHunk(hunks.length, "up", fromStart + REVEAL_CONTEXT_LINES); |
| 49 | else if (fromStart <= after.additionStart - line) { |
| 50 | expandHunk(next, "up", fromStart + REVEAL_CONTEXT_LINES); |
| 51 | } else expandHunk(next, "down", after.additionStart - line + REVEAL_CONTEXT_LINES); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | const REVEAL_CONTEXT_LINES = 10; |
| 56 | |
| 57 | /** |
| 58 | * Scrolls a code view to a file. `attach` is the viewer's ref and `flush` its post-render |
| 59 | * handler, so a reveal asked before the viewer mounts, or for a file it does not hold yet, is |
| 60 | * positioned once it does. A reveal belongs to the diff it was asked of, so a viewer remounted for |
| 61 | * another diff scope, which no longer holds the file, drops it. |
| 62 | */ |
| 63 | export function createCodeViewFileReveal() { |
| 64 | let viewer: FileRevealHandle | null = null; |
| 65 | let detachedInstance: object | undefined; |
| 66 | let pending: { |
| 67 | id: string; |
| 68 | range?: SelectedLineRange; |
| 69 | positionedFile?: boolean; |
| 70 | loadingFile?: boolean; |
| 71 | settling?: boolean; |
| 72 | } | null = null; |
| 73 | const flush = () => { |
| 74 | if (pending === null || viewer === null) return; |
| 75 | const item = viewer.getItem(pending.id); |
| 76 | if (!viewer.getInstance() || item === undefined || item.collapsed === true) return; |
| 77 | const renderedItems = viewer.getInstance()?.getRenderedItems?.(); |
| 78 | if (pending.range && renderedItems) { |
| 79 | const rendered = renderedItems.find((entry) => entry.id === pending?.id); |
| 80 | if (!rendered) { |
| 81 | if (!pending.positionedFile) { |
| 82 | pending.positionedFile = true; |
| 83 | viewer.scrollTo({ type: "item", id: pending.id, align: "start" }); |
| 84 | } |
| 85 | return; |
| 86 | } |
| 87 | const diff = rendered.instance.getDiffForSearch?.(); |
| 88 | const additionLine = (line: number, side: SelectionSide | undefined) => { |
| 89 | if (side !== "deletions" || !diff) return line; |
| 90 | const next = diff.hunks.find((hunk) => line < hunk.deletionStart + hunk.deletionCount); |
| 91 | const last = diff.hunks.at(-1); |
| 92 | return next |
| 93 | ? line + next.additionStart - next.deletionStart |
| 94 | : last |
| 95 | ? line + |
| 96 | last.additionStart + |
| 97 | last.additionCount - |
| 98 | last.deletionStart - |
| 99 | last.deletionCount |
| 100 | : line; |
| 101 | }; |
| 102 | const { range } = pending; |
| 103 | const startLine = additionLine(range.start, range.side); |
| 104 | const endLine = additionLine(range.end, range.endSide ?? range.side); |
| 105 | // A patch-only diff holds no unchanged lines, and `revealLine` declines to expand it. The |
| 106 | // viewer's own expander loads the file first, so a line outside every hunk is opened with it. |
| 107 | if (diff?.isPartial && rendered.instance.expandHunk) { |
| 108 | // Wait for the file contents the expander asked for. |
| 109 | if (pending.loadingFile) return; |
| 110 | const { expandHunk } = rendered.instance; |
| 111 | // A removed line is always in a hunk, and a deleted file has no contents to load. |
| 112 | const removed = (line: number, side: SelectionSide | undefined) => |
| 113 | side === "deletions" && |
| 114 | diff.hunks.some( |
| 115 | (hunk) => line >= hunk.deletionStart && line < hunk.deletionStart + hunk.deletionCount, |
| 116 | ); |
| 117 | const outside = [ |
| 118 | ...new Set([ |
| 119 | ...(removed(range.start, range.side) ? [] : [startLine]), |
| 120 | ...(removed(range.end, range.endSide ?? range.side) ? [] : [endLine]), |
| 121 | ]), |
| 122 | ]; |
| 123 | const opened = outside.map((line) => expandPartialDiffToLine(diff, line, expandHunk)); |
| 124 | if (opened.some(Boolean)) { |
| 125 | pending.loadingFile = true; |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | const startExpanded = rendered.instance.revealLine?.(startLine); |
| 130 | const endExpanded = rendered.instance.revealLine?.(endLine); |
| 131 | if (startExpanded || endExpanded) return; |
| 132 | if ( |
| 133 | !rendered.instance.getLinePosition(range.start, range.side) || |
| 134 | !rendered.instance.getLinePosition(range.end, range.endSide ?? range.side) |
| 135 | ) |
| 136 | return; |
| 137 | } |
| 138 | const target = pending; |
| 139 | // The diff's first load can remount the viewer or re-render it right after this scroll, which |
| 140 | // loses it; the range is positioned once more on the next render that can hold it. |
| 141 | pending = target.range && !target.settling ? { ...target, settling: true } : null; |
| 142 | viewer.scrollTo( |
| 143 | target.range |
| 144 | ? { type: "range", id: target.id, range: target.range, align: "center" } |
| 145 | : { type: "item", id: target.id, align: "start" }, |
| 146 | ); |
| 147 | }; |
| 148 | return { |
| 149 | attach(next: FileRevealHandle | null) { |
| 150 | // The viewer rebuilds its handle on every render and can remount during the diff's first |
| 151 | // load. Item ids carry the diff's scope, so only a viewer without the item drops the reveal. |
| 152 | const previous = viewer?.getInstance() ?? detachedInstance; |
| 153 | if (next === null) detachedInstance = previous; |
| 154 | else { |
| 155 | const instance = next.getInstance(); |
| 156 | if (pending !== null && previous !== undefined && instance !== previous) { |
| 157 | if (instance === undefined || next.getItem(pending.id) === undefined) pending = null; |
| 158 | // A new instance starts from the patch again, so it must expand the line itself. |
| 159 | else pending.loadingFile = false; |
| 160 | } |
| 161 | detachedInstance = undefined; |
| 162 | } |
| 163 | viewer = next; |
| 164 | flush(); |
| 165 | }, |
| 166 | request(fileKey: string, range?: SelectedLineRange) { |
| 167 | pending = { id: fileKey, ...(range ? { range } : {}) }; |
| 168 | flush(); |
| 169 | }, |
| 170 | /** The user scrolled; a reveal still being positioned must not pull them back. */ |
| 171 | cancel() { |
| 172 | pending = null; |
| 173 | }, |
| 174 | flush, |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | export function useCodeViewFileReveal() { |
| 179 | const [reveal] = useState(createCodeViewFileReveal); |
| 180 | return reveal; |
| 181 | } |