Add inline review comment selection to mobile diffs

- render comment rows and line selection state in the native diff view
- wire long-press/tap gestures to start and complete review comments
- tighten shared diff utilities and add coverage
This commit is contained in:
Julius Marminge committed 2026-05-20 10:55:55 +02:00
1 parent a49c2d1301
commit eba28035d2
15 files changed
+640 -37

No files matched your search

@@ -171,6 +171,7 @@ async function mountTerminalViewport(props: {
threadRef: ReturnType<typeof scopeThreadRef>;
drawerBackgroundColor?: string;
drawerTextColor?: string;
runtimeEnv?: Record<string, string>;
}) {
const drawer = document.createElement("div");
drawer.className = "thread-terminal-drawer";
@@ -194,6 +195,7 @@ async function mountTerminalViewport(props: {
terminalId="term-1"
terminalLabel="Terminal"
cwd="/repo/project"
{...(props.runtimeEnv ? { runtimeEnv: props.runtimeEnv } : {})}
onSessionExited={() => undefined}
onAddTerminalContext={() => undefined}
focusRequestId={0}
@@ -206,7 +208,10 @@ async function mountTerminalViewport(props: {
);
return {
rerender: async (nextProps: { threadRef: ReturnType<typeof scopeThreadRef> }) => {
rerender: async (nextProps: {
threadRef: ReturnType<typeof scopeThreadRef>;
runtimeEnv?: Record<string, string>;
}) => {
await screen.rerender(
<TerminalViewport
threadRef={nextProps.threadRef}
@@ -214,6 +219,7 @@ async function mountTerminalViewport(props: {
terminalId="term-1"
terminalLabel="Terminal"
cwd="/repo/project"
{...(nextProps.runtimeEnv ? { runtimeEnv: nextProps.runtimeEnv } : {})}
onSessionExited={() => undefined}
onAddTerminalContext={() => undefined}
focusRequestId={0}
@@ -259,6 +265,47 @@ describe("TerminalViewport", () => {
}
});
it("renders and attaches the terminal without the desktop local API", async () => {
const environment = createEnvironmentApi();
environmentApiById.set("environment-a", environment);
readLocalApiMock.mockReturnValueOnce(undefined);
const mounted = await mountTerminalViewport({
threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
});
try {
await vi.waitFor(() => {
expect(environment.terminal.attach).toHaveBeenCalledTimes(1);
});
expect(terminalConstructorSpy).toHaveBeenCalledTimes(1);
} finally {
await mounted.cleanup();
}
});
it("keeps the terminal mounted when xterm fit runs before dimensions are ready", async () => {
const environment = createEnvironmentApi();
environmentApiById.set("environment-a", environment);
fitAddonFitSpy.mockImplementationOnce(() => {
throw new TypeError("Cannot read properties of undefined (reading 'dimensions')");
});
const mounted = await mountTerminalViewport({
threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
});
try {
await vi.waitFor(() => {
expect(environment.terminal.attach).toHaveBeenCalledTimes(1);
});
expect(terminalConstructorSpy).toHaveBeenCalledTimes(1);
expect(fitAddonFitSpy).toHaveBeenCalled();
} finally {
await mounted.cleanup();
}
});
it("reattaches the terminal when the scoped thread reference changes", async () => {
const environmentA = createEnvironmentApi();
const environmentB = createEnvironmentApi();
@@ -313,6 +360,34 @@ describe("TerminalViewport", () => {
}
});
it("does not reattach when runtime env contents are unchanged but object identity changes", async () => {
const environment = createEnvironmentApi();
environmentApiById.set("environment-a", environment);
const mounted = await mountTerminalViewport({
threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
runtimeEnv: { PATH: "/usr/bin", T3: "1" },
});
try {
await vi.waitFor(() => {
expect(environment.terminal.attach).toHaveBeenCalledTimes(1);
});
await mounted.rerender({
threadRef: scopeThreadRef("environment-a" as never, THREAD_ID),
runtimeEnv: { T3: "1", PATH: "/usr/bin" },
});
await vi.waitFor(() => {
expect(environment.terminal.attach).toHaveBeenCalledTimes(1);
});
expect(terminalDisposeSpy).not.toHaveBeenCalled();
} finally {
await mounted.cleanup();
}
});
it("uses the drawer surface colors for the terminal theme", async () => {
const environment = createEnvironmentApi();
environmentApiById.set("environment-a", environment);
@@ -75,6 +75,24 @@ function writeTerminalSnapshot(terminal: Terminal, snapshot: TerminalSessionSnap
}
}
function fitTerminalSafely(fitAddon: FitAddon): boolean {
try {
fitAddon.fit();
return true;
} catch {
return false;
}
}
function runtimeEnvSignature(runtimeEnv: Record<string, string> | undefined): string {
if (!runtimeEnv) return "";
return JSON.stringify(
Object.entries(runtimeEnv)
.filter(([key, value]) => key.length > 0 && typeof value === "string")
.sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey)),
);
}
function normalizeComputedColor(value: string | null | undefined, fallback: string): string {
const normalizedValue = value?.trim().toLowerCase();
if (
@@ -283,6 +301,7 @@ export function TerminalViewport({
const selectionActionOpenRef = useRef(false);
const selectionActionTimerRef = useRef<number | null>(null);
const keybindingsRef = useRef(keybindings);
const runtimeEnvKey = useMemo(() => runtimeEnvSignature(runtimeEnv), [runtimeEnv]);
const handleSessionExited = useEffectEvent(() => {
onSessionExited();
});
@@ -302,7 +321,7 @@ export function TerminalViewport({
let disposed = false;
const api = readEnvironmentApi(environmentId);
const localApi = readLocalApi();
if (!api || !localApi) return;
if (!api) return;
const fitAddon = new FitAddon();
const terminal = new Terminal({
@@ -315,7 +334,7 @@ export function TerminalViewport({
});
terminal.loadAddon(fitAddon);
terminal.open(mount);
fitAddon.fit();
fitTerminalSafely(fitAddon);
terminalRef.current = terminal;
fitAddonRef.current = fitAddon;
@@ -369,6 +388,10 @@ export function TerminalViewport({
};
const showSelectionAction = async () => {
if (!localApi) {
clearSelectionAction();
return;
}
if (selectionActionOpenRef.current) {
return;
}
@@ -479,6 +502,10 @@ export function TerminalViewport({
const latestTerminal = terminalRef.current;
if (!latestTerminal) return;
if (!localApi) {
writeSystemMessage(latestTerminal, "Opening links is unavailable in this browser.");
return;
}
if (match.kind === "url") {
void localApi.shell.openExternal(match.text).catch((error: unknown) => {
@@ -630,7 +657,7 @@ export function TerminalViewport({
const activeTerminal = terminalRef.current;
const activeFitAddon = fitAddonRef.current;
if (!activeTerminal || !activeFitAddon) return;
activeFitAddon.fit();
fitTerminalSafely(activeFitAddon);
unsubscribeAttach = attachTerminalSession({
environmentId,
client: api,
@@ -664,7 +691,7 @@ export function TerminalViewport({
if (!activeTerminal || !activeFitAddon) return;
const wasAtBottom =
activeTerminal.buffer.active.viewportY >= activeTerminal.buffer.active.baseY;
activeFitAddon.fit();
fitTerminalSafely(activeFitAddon);
if (wasAtBottom) {
activeTerminal.scrollToBottom();
}
@@ -700,7 +727,7 @@ export function TerminalViewport({
// autoFocus is intentionally omitted;
// it is only read at mount time and must not trigger terminal teardown/recreation.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [cwd, environmentId, runtimeEnv, terminalId, threadId, worktreePath]);
}, [cwd, environmentId, runtimeEnvKey, terminalId, threadId, worktreePath]);
useEffect(() => {
if (!autoFocus) return;
@@ -721,7 +748,7 @@ export function TerminalViewport({
if (!api || !terminal || !fitAddon) return;
const wasAtBottom = terminal.buffer.active.viewportY >= terminal.buffer.active.baseY;
const frame = window.requestAnimationFrame(() => {
fitAddon.fit();
fitTerminalSafely(fitAddon);
if (wasAtBottom) {
terminal.scrollToBottom();
}