Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions apps/mobile/src/features/threads/NewTaskDraftScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ import {
} from "../voice-input/ComposerDictationControl";
import { useVoiceInputController } from "../voice-input/useVoiceInputController";
import { resolveVoiceComposerPresentation } from "../voice-input/voiceInputPresentation";
import {
settingsSheetRouteDidDismiss,
stackContainsRouteName,
} from "./thread-settings-sheet-presentation-state";
import {
useThreadSettingsSheetPresentation,
type NavigationWithFinishTransitioning,
Expand Down Expand Up @@ -270,6 +274,29 @@ export function NewTaskDraftScreen(props: {
};
}, [navigation]);
const settingsRoutePresentedRef = useRef(false);
const syncSettingsSheetRoute = useCallback(() => {
const sheetRouteVisible = stackContainsRouteName(
navigation.getState()?.routes,
"ThreadSettings",
);
if (
!settingsSheetRouteDidDismiss({
presented: settingsRoutePresentedRef.current,
sheetRouteVisible,
})
) {
return;
}
settingsRoutePresentedRef.current = false;
settingsSheetPresentation.onDismissed();
}, [navigation, settingsSheetPresentation.onDismissed]);
const openSettings = useCallback(() => {
// A swipe-dismissed iOS form sheet often never blurs this screen, so
// JS can still think the picker is open. Reconcile from the stack
// before the isActive latch can swallow this tap.
syncSettingsSheetRoute();
settingsSheetPresentation.open();
}, [settingsSheetPresentation.open, syncSettingsSheetRoute]);
useEffect(() => {
if (!settingsSheetPresentation.isVisible || settingsRoutePresentedRef.current) {
return;
Expand All @@ -288,15 +315,24 @@ export function NewTaskDraftScreen(props: {
settingsSheetPresentation.onDismissed();
}, [settingsSheetPresentation.onDismissed]),
);
useEffect(
() => navigation.addListener("state", syncSettingsSheetRoute),
[navigation, syncSettingsSheetRoute],
);
useEffect(
() =>
// UIKit's completion callback for the sheet dismissal, surfaced by the
// native-stack patch. This is when the queued keyboard restore runs.
// Form-sheet swipe dismiss often never blurs this screen, so also
// reconcile from the stack here.
(navigation as unknown as NavigationWithFinishTransitioning).addListener(
"finishTransitioning",
settingsSheetPresentation.onStackTransitionsFinished,
() => {
settingsSheetPresentation.onStackTransitionsFinished();
syncSettingsSheetRoute();
},
),
[navigation, settingsSheetPresentation.onStackTransitionsFinished],
[navigation, settingsSheetPresentation.onStackTransitionsFinished, syncSettingsSheetRoute],
);
const [importingShareKey, setImportingShareKey] = useState<string | null>(null);
const [isCancellingShareImport, setIsCancellingShareImport] = useState(false);
Expand Down Expand Up @@ -1484,7 +1520,7 @@ export function NewTaskDraftScreen(props: {
accessibilityRole="button"
className="px-3 py-2"
disabled={isComposerInteractionLocked}
onPress={settingsSheetPresentation.open}
onPress={openSettings}
>
<Text className="text-xs text-foreground">Model unavailable. Open model settings.</Text>
</Pressable>
Expand Down Expand Up @@ -1579,7 +1615,7 @@ export function NewTaskDraftScreen(props: {
}
label={flow.selectedModelOption?.label ?? "Choose model"}
maxWidth="100%"
onPress={settingsSheetPresentation.open}
onPress={openSettings}
/>
</View>
{flow.planModeEnabled ? (
Expand Down
52 changes: 49 additions & 3 deletions apps/mobile/src/features/threads/ThreadComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ import {
type ExistingThreadSettingsRouteSession,
useExistingThreadSettingsRoutePresentation,
} from "./ThreadSettingsSheet";
import {
settingsSheetRouteDidDismiss,
stackContainsRouteName,
} from "./thread-settings-sheet-presentation-state";
import {
useThreadSettingsSheetPresentation,
type NavigationWithFinishTransitioning,
Expand Down Expand Up @@ -578,10 +582,42 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
threadProviderGroups,
],
);
const syncSettingsSheetRoute = useCallback(() => {
const sheetRouteVisible = stackContainsRouteName(
navigation.getState()?.routes,
"ThreadSettingsSheet",
);
if (
!settingsSheetRouteDidDismiss({
presented: settingsRoutePresentedRef.current,
sheetRouteVisible,
})
) {
return;
}
settingsRoutePresentedRef.current = false;
settingsSheetPresentation.onDismissed();
settingsRoutePresentation.clear(settingsOwnerId);
}, [
navigation,
settingsOwnerId,
settingsRoutePresentation.clear,
settingsSheetPresentation.onDismissed,
]);

const openSettings = useCallback(() => {
// A swipe-dismissed iOS form sheet often never blurs this screen, so
// JS can still think the picker is open. Reconcile from the stack
// before the isActive latch can swallow this tap.
syncSettingsSheetRoute();
settingsRoutePresentation.present(settingsRouteSession);
settingsSheetPresentation.open();
}, [settingsRoutePresentation.present, settingsRouteSession, settingsSheetPresentation.open]);
}, [
settingsRoutePresentation.present,
settingsRouteSession,
settingsSheetPresentation.open,
syncSettingsSheetRoute,
]);

useEffect(() => {
if (settingsSheetPresentation.isActive) {
Expand Down Expand Up @@ -610,15 +646,25 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
}, [settingsOwnerId, settingsRoutePresentation.clear, settingsSheetPresentation.onDismissed]),
);

useEffect(
() => navigation.addListener("state", syncSettingsSheetRoute),
[navigation, syncSettingsSheetRoute],
);

useEffect(
() =>
// UIKit's completion callback for the sheet dismissal, surfaced by the
// native-stack patch. This is when the queued keyboard restore runs.
// Form-sheet swipe dismiss often never blurs this screen, so also
// reconcile from the stack here.
(navigation as unknown as NavigationWithFinishTransitioning).addListener(
"finishTransitioning",
settingsSheetPresentation.onStackTransitionsFinished,
() => {
settingsSheetPresentation.onStackTransitionsFinished();
syncSettingsSheetRoute();
},
),
[navigation, settingsSheetPresentation.onStackTransitionsFinished],
[navigation, settingsSheetPresentation.onStackTransitionsFinished, syncSettingsSheetRoute],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vite-plus/test";

import {
settingsSheetRouteDidDismiss,
stackContainsRouteName,
} from "./thread-settings-sheet-presentation-state";

describe("settings sheet route presence", () => {
it("finds the picker route on the presenting stack", () => {
expect(
stackContainsRouteName(
[{ name: "Thread" }, { name: "ThreadSettingsSheet" }],
"ThreadSettingsSheet",
),
).toBe(true);
expect(stackContainsRouteName([{ name: "Thread" }], "ThreadSettingsSheet")).toBe(false);
expect(stackContainsRouteName(undefined, "ThreadSettingsSheet")).toBe(false);
});

it("treats a swipe-dismissed form sheet as closed once the route is gone", () => {
expect(settingsSheetRouteDidDismiss({ presented: true, sheetRouteVisible: false })).toBe(true);
expect(settingsSheetRouteDidDismiss({ presented: true, sheetRouteVisible: true })).toBe(false);
expect(settingsSheetRouteDidDismiss({ presented: false, sheetRouteVisible: false })).toBe(
false,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* iOS form sheets often keep the presenting screen focused, so a swipe
* dismiss never re-runs useFocusEffect. JS then thinks the picker is still
* open and later model/effort taps no-op. The stack is the source of truth.
*/
export function stackContainsRouteName(
routes: ReadonlyArray<{ readonly name: string }> | undefined,
name: string,
): boolean {
return routes?.some((route) => route.name === name) === true;
}

export function settingsSheetRouteDidDismiss(input: {
readonly presented: boolean;
readonly sheetRouteVisible: boolean;
}): boolean {
return input.presented && !input.sheetRouteVisible;
}
Loading