Skip to content

Commit c4f3486

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-32669
2 parents 878f1a5 + 0080084 commit c4f3486

11 files changed

Lines changed: 248 additions & 47 deletions

File tree

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
286286
onInput(mapOf("data" to "\r"))
287287
},
288288
)
289+
val navigationKeys = TerminalNavigationKeyListener { onInput(mapOf("data" to it)) }
289290
inputView.setOnKeyListener { _, keyCode, event ->
290291
if (readOnly) return@setOnKeyListener true
292+
if (navigationKeys.onKey(inputView, keyCode, event)) return@setOnKeyListener true
291293
if (event.action != KeyEvent.ACTION_DOWN) return@setOnKeyListener false
292294
when {
293295
keyCode == KeyEvent.KEYCODE_DEL -> {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package expo.modules.t3terminal
2+
3+
import android.view.KeyEvent
4+
import android.view.View
5+
6+
internal class TerminalNavigationKeyListener(
7+
private val send: (String) -> Unit
8+
) : View.OnKeyListener {
9+
override fun onKey(view: View?, keyCode: Int, event: KeyEvent): Boolean {
10+
val sequence = sequenceFor(keyCode, event) ?: return false
11+
if (event.action == KeyEvent.ACTION_DOWN) send(sequence)
12+
// Unconsumed arrow and Tab key events move focus off the terminal input.
13+
return true
14+
}
15+
16+
// Matches iOS: these keys only without modifiers, plus Shift+Tab.
17+
private fun sequenceFor(keyCode: Int, event: KeyEvent): String? = when {
18+
keyCode == KeyEvent.KEYCODE_TAB && event.hasModifiers(KeyEvent.META_SHIFT_ON) -> "\u001b[Z"
19+
!event.hasNoModifiers() -> null
20+
keyCode == KeyEvent.KEYCODE_DPAD_UP -> "\u001b[A"
21+
keyCode == KeyEvent.KEYCODE_DPAD_DOWN -> "\u001b[B"
22+
keyCode == KeyEvent.KEYCODE_DPAD_RIGHT -> "\u001b[C"
23+
keyCode == KeyEvent.KEYCODE_DPAD_LEFT -> "\u001b[D"
24+
keyCode == KeyEvent.KEYCODE_TAB -> "\t"
25+
keyCode == KeyEvent.KEYCODE_ESCAPE -> "\u001b"
26+
else -> null
27+
}
28+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package expo.modules.t3terminal
2+
3+
import android.app.Activity
4+
import android.view.KeyEvent
5+
import android.view.View
6+
import android.widget.Button
7+
import android.widget.EditText
8+
import android.widget.LinearLayout
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Assert.assertTrue
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.robolectric.Robolectric
14+
import org.robolectric.RobolectricTestRunner
15+
import org.robolectric.annotation.Config
16+
17+
@RunWith(RobolectricTestRunner::class)
18+
@Config(sdk = [36], manifest = Config.NONE)
19+
class TerminalNavigationKeyListenerTest {
20+
@Test
21+
fun hardwareNavigationKeysReachThePtyAndKeepFocus() {
22+
val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
23+
val sent = StringBuilder()
24+
val input = EditText(activity).apply {
25+
setSingleLine(true)
26+
isFocusableInTouchMode = true
27+
setOnKeyListener(TerminalNavigationKeyListener { sent.append(it) })
28+
}
29+
val nextControl = Button(activity).apply { isFocusableInTouchMode = true }
30+
val root = LinearLayout(activity).apply {
31+
orientation = LinearLayout.VERTICAL
32+
addView(input)
33+
addView(nextControl)
34+
}
35+
activity.setContentView(root)
36+
// Focus search below the input only finds controls that have bounds.
37+
root.measure(
38+
View.MeasureSpec.makeMeasureSpec(1080, View.MeasureSpec.EXACTLY),
39+
View.MeasureSpec.makeMeasureSpec(1920, View.MeasureSpec.EXACTLY),
40+
)
41+
root.layout(0, 0, 1080, 1920)
42+
assertTrue(input.requestFocus())
43+
44+
val shift = KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON
45+
val keys = listOf(
46+
Triple(KeyEvent.KEYCODE_DPAD_UP, 0, "\u001b[A"),
47+
Triple(KeyEvent.KEYCODE_DPAD_DOWN, 0, "\u001b[B"),
48+
Triple(KeyEvent.KEYCODE_DPAD_RIGHT, 0, "\u001b[C"),
49+
Triple(KeyEvent.KEYCODE_DPAD_LEFT, 0, "\u001b[D"),
50+
Triple(KeyEvent.KEYCODE_TAB, 0, "\t"),
51+
Triple(KeyEvent.KEYCODE_TAB, shift, "\u001b[Z"),
52+
Triple(KeyEvent.KEYCODE_ESCAPE, 0, "\u001b"),
53+
)
54+
for ((keyCode, meta, expected) in keys) {
55+
sent.clear()
56+
input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, meta))
57+
input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, meta))
58+
assertEquals("bytes for key $keyCode meta $meta", expected, sent.toString())
59+
assertTrue("focus left the input after key $keyCode meta $meta", input.isFocused)
60+
}
61+
62+
sent.clear()
63+
val ctrl = KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON
64+
input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 0, ctrl))
65+
assertEquals("modified arrows are left to the app, as on iOS", "", sent.toString())
66+
}
67+
}

‎apps/mobile/src/features/pull-requests/PullRequestAgentRow.tsx‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ export function PullRequestAgentRow(props: {
1919
title={props.title ?? action.title}
2020
icon="sparkles"
2121
accessibilityLabel={props.title ?? action.title}
22-
menu={agent.threads.length > 1}
22+
menu={agent.chooseThread && !action.attributes.disabled}
2323
onPress={
24-
action.attributes.disabled || agent.threads.length > 1
25-
? undefined
26-
: () => agent.start(props.task)
24+
action.attributes.disabled || agent.chooseThread ? undefined : () => agent.start(props.task)
2725
}
2826
/>
2927
);
30-
if (agent.threads.length <= 1 || action.attributes.disabled) return row;
28+
if (!agent.chooseThread || action.attributes.disabled) return row;
3129
return (
3230
<Menu
3331
title={props.title ?? action.title}

‎apps/mobile/src/features/pull-requests/PullRequestControls.tsx‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Animated from "react-native-reanimated";
33
import { PR_ROW_LAYOUT } from "./pullRequestAnimations";
44
import { useNavigation } from "@react-navigation/native";
55
import type { PullRequestDetail } from "@t3tools/contracts";
6+
import type { MenuAction } from "@react-native-menu/menu";
67
import type { ReactNode } from "react";
78
import { Platform, Pressable, View } from "react-native";
89
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -128,7 +129,7 @@ export function PullRequestActionToolbar(props: {
128129
).changeRequest;
129130
const threadLabel = agent.threads.length ? "Continue thread" : `Work on ${shortLabel}`;
130131
const primaryLabel = pendingReview ? `Submit review · ${draft.comments.length}` : threadLabel;
131-
const chooseThread = agent.threads.length > 1;
132+
const chooseThread = agent.chooseThread;
132133
const agentActions = agent.actions.filter((action) => action.task !== "ask");
133134
const open = (mode: "review" | "comment") =>
134135
navigation.navigate("PullRequestReview", { ...props.params, mode });
@@ -193,17 +194,6 @@ export function PullRequestActionToolbar(props: {
193194
</View>
194195
);
195196
}
196-
const nativeThreadItems = threadMenuItems.map((item) => (
197-
<Toolbar.MenuAction
198-
key={item.id}
199-
icon={item.image}
200-
subtitle={"subtitle" in item ? item.subtitle : undefined}
201-
disabled={"attributes" in item && item.attributes.disabled}
202-
onPress={() => agent.select(item.id)}
203-
>
204-
<Toolbar.Label>{item.title}</Toolbar.Label>
205-
</Toolbar.MenuAction>
206-
));
207197
const primaryControl = showPrimary ? (
208198
!pendingReview && chooseThread ? (
209199
<Toolbar.Menu
@@ -222,7 +212,7 @@ export function PullRequestActionToolbar(props: {
222212
disabled={primaryDisabled}
223213
separateBackground
224214
>
225-
{nativeThreadItems}
215+
{renderThreadMenuItems(threadMenuItems, agent.select)}
226216
</Toolbar.Menu>
227217
) : (
228218
<Toolbar.Button
@@ -256,17 +246,7 @@ export function PullRequestActionToolbar(props: {
256246
{agentActions.map((action) =>
257247
chooseThread && !action.disabled ? (
258248
<Toolbar.Menu key={action.task} title={action.title} icon={action.icon}>
259-
{agent.threadActions(action.task).map((item) => (
260-
<Toolbar.MenuAction
261-
key={item.id}
262-
icon={item.image}
263-
subtitle={"subtitle" in item ? item.subtitle : undefined}
264-
disabled={"attributes" in item && item.attributes.disabled}
265-
onPress={() => agent.select(item.id)}
266-
>
267-
<Toolbar.Label>{item.title}</Toolbar.Label>
268-
</Toolbar.MenuAction>
269-
))}
249+
{renderThreadMenuItems(agent.threadActions(action.task), agent.select)}
270250
</Toolbar.Menu>
271251
) : (
272252
<Toolbar.MenuAction
@@ -308,3 +288,23 @@ export function PullRequestActionToolbar(props: {
308288
</Toolbar>
309289
);
310290
}
291+
292+
function renderThreadMenuItems(items: MenuAction[], select: (id: string) => void): ReactNode {
293+
return items.map((item) =>
294+
item.subactions ? (
295+
<Toolbar.Menu key={item.id} title={item.title} icon={item.image}>
296+
{renderThreadMenuItems(item.subactions, select)}
297+
</Toolbar.Menu>
298+
) : (
299+
<Toolbar.MenuAction
300+
key={item.id}
301+
icon={item.image}
302+
subtitle={item.subtitle}
303+
disabled={item.attributes?.disabled}
304+
onPress={() => item.id && select(item.id)}
305+
>
306+
<Toolbar.Label>{item.title}</Toolbar.Label>
307+
</Toolbar.MenuAction>
308+
),
309+
);
310+
}

‎apps/mobile/src/features/pull-requests/pullRequestAgentTasks.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
appendPullRequestAgentPrompt,
44
pullRequestAgentPrompt,
55
pullRequestAgentThread,
6+
pullRequestAgentThreadActions,
67
} from "./pullRequestAgentTasks";
78
import { IsoDateTime, ProjectId, ThreadId } from "@t3tools/contracts";
89
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
@@ -27,7 +28,30 @@ describe("PR agent tasks", () => {
2728
archivedAt: IsoDateTime.make("2026-10-09T10:00:00.000Z"),
2829
};
2930
expect(pullRequestAgentThread([thread, archived])).toBeUndefined();
31+
expect(pullRequestAgentThread([archived])).toBeUndefined();
32+
const settled = { ...thread, settled: true };
33+
expect(pullRequestAgentThread([settled])).toBeUndefined();
34+
expect(pullRequestAgentThread([settled], settled.id)).toBe(settled);
35+
expect(pullRequestAgentThread([thread, { ...settled, id: archived.id }])).toBeUndefined();
3036
expect(pullRequestAgentThread([thread, archived], archived.id)).toBe(archived);
37+
const actions = pullRequestAgentThreadActions([settled, archived], "ask", true);
38+
expect(actions.map((action) => action.title)).toEqual([
39+
"Start a new thread",
40+
"Existing work",
41+
"Settled threads",
42+
]);
43+
expect(actions[2]?.subactions).toMatchObject([
44+
{ id: `ask:${settled.id}`, subtitle: "Settled thread" },
45+
]);
46+
expect(pullRequestAgentThreadActions([thread], "ask", false)).toMatchObject([
47+
{ id: `ask:${thread.id}`, subtitle: "Continue thread" },
48+
]);
49+
expect(
50+
pullRequestAgentThreadActions([settled], "feedback", false)[0]?.subactions,
51+
).toMatchObject([{ id: `feedback:${settled.id}`, attributes: { disabled: false } }]);
52+
expect(pullRequestAgentThreadActions([archived], "feedback", true)[1]).toMatchObject({
53+
attributes: { disabled: true },
54+
});
3155
});
3256
it("replaces the helper's own text when switching tasks, keeping user edits", () => {
3357
const first = appendPullRequestAgentPrompt("My question", "explain", GITHUB);

‎apps/mobile/src/features/pull-requests/pullRequestAgentTasks.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
sourceControlClients,
33
type ChangeRequestTerminology,
44
} from "@t3tools/client-runtime/source-control-clients";
5+
import type { MenuAction } from "@react-native-menu/menu";
56
import type { PullRequestDetail, PullRequestLinkedThreadsResult } from "@t3tools/contracts";
67

78
export type PullRequestAgentTask = "ask" | "explain" | "feedback" | "conflicts" | "watch";
@@ -11,7 +12,39 @@ export function pullRequestAgentThread(
1112
threadId?: string,
1213
) {
1314
if (threadId) return threads.find((thread) => thread.id === threadId);
14-
return threads.length === 1 ? threads[0] : undefined;
15+
return threads.length === 1 && !threads[0]?.settled && !threads[0]?.archivedAt
16+
? threads[0]
17+
: undefined;
18+
}
19+
20+
export function pullRequestAgentThreadActions(
21+
threads: PullRequestLinkedThreadsResult["threads"],
22+
task: PullRequestAgentTask,
23+
canAsk: boolean,
24+
): MenuAction[] {
25+
const active: MenuAction[] = [];
26+
const settled: MenuAction[] = [];
27+
for (const thread of threads) {
28+
const action = {
29+
id: `${task}:${thread.id}`,
30+
title: thread.title,
31+
subtitle: thread.archivedAt
32+
? "Archived thread"
33+
: thread.settled
34+
? "Settled thread"
35+
: "Continue thread",
36+
image: "bubble.left.and.bubble.right",
37+
attributes: { disabled: task !== "ask" && Boolean(thread.archivedAt) },
38+
};
39+
(thread.settled ? settled : active).push(action);
40+
}
41+
return [
42+
...(canAsk ? [{ id: `${task}:new`, title: "Start a new thread", image: "plus" }] : []),
43+
...active,
44+
...(settled.length
45+
? [{ id: `${task}:settled`, title: "Settled threads", subactions: settled }]
46+
: []),
47+
];
1548
}
1649

1750
export function pullRequestAgentPrompt(

‎apps/mobile/src/features/pull-requests/usePullRequestAgentActions.ts‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Platform } from "react-native";
12
import type { PullRequestDetail } from "@t3tools/contracts";
23
import {
34
pullRequestAgentActions,
45
pullRequestAgentThread,
6+
pullRequestAgentThreadActions,
57
type PullRequestAgentTask,
68
} from "./pullRequestAgentTasks";
79
import type { PullRequestParams } from "./usePullRequestDiff";
@@ -28,24 +30,30 @@ export function usePullRequestAgentActions(
2830
if (!links.canAsk || actions.find((action) => action.task === task)?.disabled) return;
2931
void links.ask(thread?.archivedAt ? undefined : thread, task);
3032
};
31-
const threadActions = (task: PullRequestAgentTask) => [
32-
...links.threads.map((thread) => ({
33-
id: `${task}:${thread.id}`,
34-
title: thread.title,
35-
subtitle: thread.archivedAt ? "Archived thread" : "Continue thread",
36-
image: "bubble.left.and.bubble.right",
37-
attributes: { disabled: task !== "ask" && Boolean(thread.archivedAt) },
38-
})),
39-
...(links.canAsk ? [{ id: `${task}:new`, title: "Start a new thread", image: "plus" }] : []),
40-
];
33+
const chooseThread =
34+
links.threads.length > 1 || links.threads.some((thread) => thread.settled || thread.archivedAt);
35+
const threadActions = (task: PullRequestAgentTask) =>
36+
pullRequestAgentThreadActions(links.threads, task, links.canAsk);
4137
const menuAction = (task: PullRequestAgentTask) => {
4238
const action = actions.find((action) => action.task === task);
39+
const items = threadActions(task);
40+
// Android supports only one submenu level beneath each helper action.
41+
const subactions =
42+
Platform.OS === "android"
43+
? items.flatMap(
44+
(item) =>
45+
item.subactions?.map((child) => ({
46+
...child,
47+
title: `Settled: ${child.title}`,
48+
})) ?? [item],
49+
)
50+
: items;
4351
return {
4452
id: task,
4553
title: action?.title ?? "Open thread",
4654
image: action?.icon,
4755
attributes: { disabled: disabled || !links.canAsk || action?.disabled },
48-
...(links.threads.length > 1 ? { subactions: threadActions(task) } : {}),
56+
...(chooseThread ? { subactions } : {}),
4957
};
5058
};
5159
const select = (event: string) => {
@@ -55,5 +63,5 @@ export function usePullRequestAgentActions(
5563
if (action)
5664
start(action.task, event === action.task ? undefined : event.slice(action.task.length + 1));
5765
};
58-
return { ...links, actions, disabled, start, threadActions, menuAction, select };
66+
return { ...links, actions, disabled, chooseThread, start, threadActions, menuAction, select };
5967
}

0 commit comments

Comments
 (0)