Skip to content
TypeScript

fix(mobile): dragging a microphone or environment route now saves its new place (#26096)

Julius Marmingefe0e8d4ee12e
diff --git a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx
index adcf71595b1..b7ccdc0e006 100644
--- a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx
@@ -1,14 +1,13 @@
-import { Button } from "../../components/ui/Button";
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
import { useAtomSet, useAtomValue } from "@effect/atom-react";
import { AsyncResult } from "effect/reactivity";
import { getRecordingPermissionsAsync, requestRecordingPermissionsAsync } from "expo-audio";
import { useState } from "react";
-import { Linking, Pressable, View } from "react-native";
+import { Linking, useWindowDimensions, View } from "react-native";
import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";
-import { type AppSymbolName, SymbolView } from "../../components/ui/AppSymbol";
+import { type AppSymbolName } from "../../components/ui/AppSymbol";
import { AppText as Text } from "../../components/ui/AppText";
import { cn } from "../../lib/cn";
import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom";
@@ -21,12 +20,11 @@ import {
} from "../../lib/microphonePriority";
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
import { ListRowLayout } from "../../components/ui/ListRowLayout";
+import { ThreadSwipeable } from "../home/thread-swipe-actions";
import { SettingsActionRow } from "./components/SettingsActionRow";
import { SettingsDragHandle } from "./components/SettingsDragHandle";
import { SettingsSection } from "./components/SettingsSection";
-const REMOVE_SIZE = 20;
-
type MicrophonePermission = "checking" | "granted" | "ask" | "denied";
function toMicrophonePermission(response: {
@@ -98,9 +96,7 @@ export function SettingsMicrophoneRouteScreen() {
const entries = microphoneOrder(
AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined,
);
- const hasDevices = entries.some(isMicrophoneDevice);
const { permission, request: requestPermission } = useMicrophonePermission();
- const [editing, setEditing] = useState(false);
const [drag, setDrag] = useState<{ readonly key: string; readonly translation: number } | null>(
null,
);
@@ -166,18 +162,7 @@ export function SettingsMicrophoneRouteScreen() {
</Text>
</>
) : null}
- <SettingsSection
- title="Preferred order"
- trailing={
- hasDevices ? (
- <Button
- label={editing ? "Done" : "Edit"}
- tone="text"
- onPress={() => setEditing((value) => !value)}
- />
- ) : undefined
- }
- >
+ <SettingsSection title="Preferred order">
{entries.map((entry, index) => {
const key = microphoneEntryKey(entry);
const shift =
@@ -195,7 +180,6 @@ export function SettingsMicrophoneRouteScreen() {
position={index + 1}
count={entries.length}
reorderable={ready}
- editing={editing && hasDevices}
offset={index === dragFrom ? (drag?.translation ?? 0) : shift}
lifted={index === dragFrom}
onHeight={setRowHeight}
@@ -215,7 +199,7 @@ export function SettingsMicrophoneRouteScreen() {
<Text className="px-2 text-sm text-foreground-muted">
Voice input records from the first connected microphone in this list. Wired and Bluetooth
devices are added by name after you use voice input with them connected; until then, the
- Other row ranks them.
+ Other row ranks them. Swipe a device left to forget it.
</Text>
</ScrollView>
</View>
@@ -227,7 +211,6 @@ function MicrophoneRow(props: {
readonly position: number;
readonly count: number;
readonly reorderable: boolean;
- readonly editing: boolean;
readonly offset: number;
readonly lifted: boolean;
readonly onHeight: (height: number) => void;
@@ -237,9 +220,10 @@ function MicrophoneRow(props: {
readonly onStep: (direction: "up" | "down") => void;
readonly onForget: (() => void) | undefined;
}) {
- const { lifted, offset, entry } = props;
+ const { lifted, offset, entry, onForget } = props;
const kind = KINDS[entry.kind];
const title = isMicrophoneDevice(entry) ? entry.name : kind.row;
+ const { width: windowWidth } = useWindowDimensions();
const style = useAnimatedStyle(() => ({
transform: [
{
@@ -250,39 +234,16 @@ function MicrophoneRow(props: {
],
zIndex: lifted ? 1 : 0,
}));
- return (
- <Reanimated.View
- style={style}
- onLayout={(event) => props.onHeight(event.nativeEvent.layout.height)}
- className={cn(
- "flex-row items-center",
- props.position > 1 && !lifted && "border-t border-border-subtle",
- lifted && "bg-grouped-card shadow-md",
- )}
- >
- {props.editing && props.onForget ? (
- <Pressable
- accessibilityRole="button"
- accessibilityLabel={`Forget ${title}`}
- hitSlop={8}
- onPress={props.onForget}
- className="pl-4 active:opacity-70"
- >
- <SymbolView
- name="xmark.circle.fill"
- size={REMOVE_SIZE}
- tintColorClassName="accent-danger-foreground"
- type="monochrome"
- />
- </Pressable>
- ) : props.editing ? (
- <View className="pl-4">
- <View style={{ width: REMOVE_SIZE }} />
- </View>
- ) : null}
+ const row = (
+ // Opaque so the swipe actions stay hidden until the row slides over them.
+ <View className="flex-row items-center bg-grouped-card">
<View
accessible
accessibilityLabel={`${title}, ${kind.label}, ${props.position} of ${props.count}`}
+ accessibilityActions={onForget ? [{ name: "forget", label: "Forget" }] : undefined}
+ onAccessibilityAction={({ nativeEvent }) => {
+ if (nativeEvent.actionName === "forget") onForget?.();
+ }}
className="min-w-0 flex-1"
>
<ListRowLayout
@@ -302,6 +263,39 @@ function MicrophoneRow(props: {
onStep={props.onStep}
/>
) : null}
+ </View>
+ );
+ return (
+ <Reanimated.View
+ style={style}
+ onLayout={(event) => props.onHeight(event.nativeEvent.layout.height)}
+ className={cn(
+ props.position > 1 && !lifted && "border-t border-border-subtle",
+ lifted && "bg-grouped-card shadow-md",
+ )}
+ >
+ {onForget ? (
+ <ThreadSwipeable
+ backgroundColor="transparent"
+ fullSwipeAction="primary"
+ fullSwipeWidth={windowWidth - 40}
+ onDelete={onForget}
+ primaryAction={{
+ accessibilityLabel: `Forget ${title}`,
+ destructive: true,
+ icon: "trash",
+ label: "Forget",
+ onPress: onForget,
+ }}
+ secondaryAction={null}
+ threadKey={`microphone:${microphoneEntryKey(entry)}`}
+ threadTitle={title}
+ >
+ {() => row}
+ </ThreadSwipeable>
+ ) : (
+ row
+ )}
</Reanimated.View>
);
}
diff --git a/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx b/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx
new file mode 100644
index 00000000000..8c1f1384fa4
--- /dev/null
+++ b/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx
@@ -0,0 +1,75 @@
+import type { ReactNode } from "react";
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import { describe, expect, it, vi } from "vite-plus/test";
+
+type PanEvent = { readonly translationY: number };
+type PanHandlers = {
+ onStart?: () => void;
+ onUpdate?: (event: PanEvent) => void;
+ onFinalize?: (event: PanEvent, success: boolean) => void;
+};
+
+const pans = vi.hoisted(() => [] as PanHandlers[]);
+
+vi.mock("react-native", () => ({
+ View: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
+}));
+vi.mock("react-native-gesture-handler", () => ({
+ GestureDetector: ({ children }: { children?: ReactNode }) => children,
+ Gesture: {
+ Pan: () => {
+ const handlers: PanHandlers = {};
+ pans.push(handlers);
+ const pan = {
+ minDistance: () => pan,
+ shouldCancelWhenOutside: () => pan,
+ runOnJS: () => pan,
+ onStart: (callback: PanHandlers["onStart"]) => ((handlers.onStart = callback), pan),
+ onUpdate: (callback: PanHandlers["onUpdate"]) => ((handlers.onUpdate = callback), pan),
+ onFinalize: (callback: PanHandlers["onFinalize"]) => (
+ (handlers.onFinalize = callback),
+ pan
+ ),
+ };
+ return pan;
+ },
+ },
+}));
+vi.mock("../../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
+
+import { SettingsDragHandle } from "./SettingsDragHandle";
+
+describe("SettingsDragHandle", () => {
+ // The worklets Babel plugin copies values a gesture callback closes over when the gesture is
+ // built, so a drop must not read a translation tracked in a local variable.
+ it("reports the released translation when the pan ends", () => {
+ pans.length = 0;
+ const onEnd = vi.fn();
+ const root = createRoot(document.createElement("div"));
+ act(() =>
+ root.render(
+ <SettingsDragHandle
+ title="Built-in microphone"
+ canMoveUp
+ canMoveDown
+ onStart={() => {}}
+ onMove={() => {}}
+ onEnd={onEnd}
+ onStep={() => {}}
+ />,
+ ),
+ );
+ const pan = pans.at(-1)!;
+
+ act(() => {
+ pan.onStart?.();
+ pan.onUpdate?.({ translationY: 64 });
+ pan.onUpdate?.({ translationY: 130 });
+ pan.onFinalize?.({ translationY: 130 }, true);
+ });
+
+ expect(onEnd).toHaveBeenCalledWith(130, false);
+ act(() => root.unmount());
+ });
+});
diff --git a/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx b/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx
index c27db4ce28e..9a73983f9da 100644
--- a/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx
+++ b/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx
@@ -18,22 +18,17 @@ export function SettingsDragHandle(props: {
const onEnd = useEffectEvent((translation: number, cancelled: boolean) =>
props.onEnd(translation, cancelled),
);
- const [gesture] = useState(() => {
- let translation = 0;
- return Gesture.Pan()
+ // The worklets plugin copies the values these callbacks close over when the gesture is built,
+ // so the drop reads its translation from the final event rather than a tracked variable.
+ const [gesture] = useState(() =>
+ Gesture.Pan()
.minDistance(0)
.shouldCancelWhenOutside(false)
.runOnJS(true)
- .onStart(() => {
- translation = 0;
- onStart();
- })
- .onUpdate((event) => {
- translation = event.translationY;
- onMove(event.translationY);
- })
- .onFinalize((_, success) => onEnd(translation, !success));
- });
+ .onStart(() => onStart())
+ .onUpdate((event) => onMove(event.translationY))
+ .onFinalize((event, success) => onEnd(event.translationY, !success)),
+ );
return (
<GestureDetector gesture={gesture}>
<View
diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts
index 9ada83fe13b..eb24fa2a058 100644
--- a/apps/mobile/vite.config.ts
+++ b/apps/mobile/vite.config.ts
@@ -17,6 +17,7 @@ const reactTests = [
"src/features/pull-requests/guide/useReviewGuide.test.tsx",
"src/features/settings/SettingsServerControlsRouteScreen.test.tsx",
"src/features/settings/components/BranchNamingSettings.test.tsx",
+ "src/features/settings/components/SettingsDragHandle.test.tsx",
"src/features/threads/ThreadMarkdownImage.test.tsx",
"src/features/threads/UsageLimitRecoveryCard.test.tsx",
"src/features/threads/NewTaskDraftRouteScreen.test.tsx",
diff --git a/docs/user/composer.md b/docs/user/composer.md
index 82cc005d061..c3e1ab74ecf 100644
--- a/docs/user/composer.md
+++ b/docs/user/composer.md
@@ -190,7 +190,7 @@ The list mixes kinds (built-in, other wired, other Bluetooth, CarPlay) with wire
Bluetooth devices you have dictated with, listed by name. Drag to rank them together:
for example, put **Other Bluetooth** above your AirPods so a new headset is preferred
while the AirPods microphone stays low. By default, CarPlay comes after the built-in
-microphone.
+microphone. Swipe a device left to forget it.
Transcription runs on your device. T3 Code deletes the temporary audio after
transcription or cancellation; only the message text is sent when you submit.