11import { NativeStackScreenOptions } from "../../native/StackHeader" ;
22import { StackActions , useNavigation , usePreventRemove } from "@react-navigation/native" ;
33import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
4- import { Alert , InteractionManager , Keyboard , Platform , View , useColorScheme } from "react-native" ;
4+ import { Alert , InteractionManager , Platform , View , useColorScheme } from "react-native" ;
55import { KeyboardAvoidingView , useKeyboardState } from "react-native-keyboard-controller" ;
66import { useSafeAreaInsets } from "react-native-safe-area-context" ;
77import { useThemeColor } from "../../lib/useThemeColor" ;
@@ -26,6 +26,7 @@ import { ControlPill, ControlPillMenu } from "../../components/ControlPill";
2626import { ProviderIcon } from "../../components/ProviderIcon" ;
2727import { ComposerSurface } from "./ThreadComposer" ;
2828import { ThreadSettingsSheet , threadSettingsSummaryLabel } from "./ThreadSettingsSheet" ;
29+ import { useThreadSettingsSheetPresentation } from "./use-thread-settings-sheet-presentation" ;
2930
3031import { makeTurnCommandMetadata } from "../../lib/commandMetadata" ;
3132import { convertPastedImagesToAttachments , pickComposerImages } from "../../lib/composerImages" ;
@@ -99,9 +100,10 @@ export function NewTaskDraftScreen(props: {
99100 const promptInputRef = useRef < ComposerEditorHandle > ( null ) ;
100101 const loadedBranchesProjectKeyRef = useRef < string | null > ( null ) ;
101102 const [ isComposerFocused , setIsComposerFocused ] = useState ( false ) ;
102- const [ isSettingsSheetVisible , setIsSettingsSheetVisible ] = useState ( false ) ;
103- const isSettingsSheetVisibleRef = useRef ( false ) ;
104- isSettingsSheetVisibleRef . current = isSettingsSheetVisible ;
103+ const settingsSheetPresentation = useThreadSettingsSheetPresentation ( {
104+ editorRef : promptInputRef ,
105+ isEditorFocused : isComposerFocused ,
106+ } ) ;
105107 const [ importingShareKey , setImportingShareKey ] = useState < string | null > ( null ) ;
106108 const [ isCancellingShareImport , setIsCancellingShareImport ] = useState ( false ) ;
107109 const [ cancelledIncomingShareId , setCancelledIncomingShareId ] = useState < string | null > ( null ) ;
@@ -523,8 +525,10 @@ export function NewTaskDraftScreen(props: {
523525 focusFrame = requestAnimationFrame ( ( ) => {
524526 // The delayed focus can land after the settings sheet opened, which
525527 // would pop the keyboard underneath its modal.
526- if ( ! isSettingsSheetVisibleRef . current ) {
528+ if ( ! settingsSheetPresentation . isActiveRef . current ) {
527529 promptInputRef . current ?. focus ( ) ;
530+ } else {
531+ settingsSheetPresentation . restoreFocusAfterSave ( ) ;
528532 }
529533 } ) ;
530534 } ) ;
@@ -535,7 +539,11 @@ export function NewTaskDraftScreen(props: {
535539 cancelAnimationFrame ( focusFrame ) ;
536540 }
537541 } ;
538- } , [ selectedProject ] ) ;
542+ } , [
543+ selectedProject ,
544+ settingsSheetPresentation . isActiveRef ,
545+ settingsSheetPresentation . restoreFocusAfterSave ,
546+ ] ) ;
539547
540548 const environmentMenuActions = useMemo (
541549 ( ) =>
@@ -643,24 +651,6 @@ export function NewTaskDraftScreen(props: {
643651 } ) ,
644652 [ currentBranchName , flow . selectedBranchName , flow . workspaceMode ] ,
645653 ) ;
646- // Order matters: mark the sheet open before dismissing the keyboard so the
647- // Android draft layout (expanded only while focused) doesn't collapse.
648- // The keyboard only comes back on close if it was up when the sheet opened.
649- const wasFocusedBeforeSheetRef = useRef ( false ) ;
650- const openSettingsSheet = useCallback ( ( ) => {
651- wasFocusedBeforeSheetRef . current = isComposerFocused ;
652- setIsSettingsSheetVisible ( true ) ;
653- Keyboard . dismiss ( ) ;
654- } , [ isComposerFocused ] ) ;
655- const closeSettingsSheet = useCallback ( ( reason : "save" | "dismiss" ) => {
656- setIsSettingsSheetVisible ( false ) ;
657- // Only Save/Done restores the keyboard: a dismissal (backdrop or
658- // grabber, including stray taps near the sheet's edge) closes quietly.
659- if ( reason === "save" && wasFocusedBeforeSheetRef . current ) {
660- promptInputRef . current ?. focus ( ) ;
661- }
662- } , [ ] ) ;
663-
664654 function handleEnvironmentMenuAction ( event : string ) {
665655 if ( isIncomingShareTransferPending || ! event . startsWith ( "environment:" ) ) {
666656 return ;
@@ -876,7 +866,7 @@ export function NewTaskDraftScreen(props: {
876866 // the touch gesture that opens the keyboard.
877867 // The settings sheet dismisses the keyboard, so its flag keeps the Android
878868 // draft composer expanded through the blur (mirrors ThreadComposer).
879- const isExpanded = ! isAndroid || isComposerFocused || isSettingsSheetVisible ;
869+ const isExpanded = ! isAndroid || isComposerFocused || settingsSheetPresentation . isActive ;
880870 const canStart =
881871 Boolean ( flow . selectedProject ) &&
882872 Boolean ( flow . selectedModel ) &&
@@ -936,7 +926,7 @@ export function NewTaskDraftScreen(props: {
936926 iconNode = { < ProviderIcon provider = { flow . selectedModelOption ?. providerDriver } size = { 16 } /> }
937927 label = { settingsSummaryLabel }
938928 maxWidth = { 320 }
939- onPress = { openSettingsSheet }
929+ onPress = { settingsSheetPresentation . open }
940930 />
941931 < ControlPillMenu
942932 actions = { environmentMenuActions }
@@ -965,8 +955,9 @@ export function NewTaskDraftScreen(props: {
965955
966956 const settingsSheet = (
967957 < ThreadSettingsSheet
968- visible = { isSettingsSheetVisible }
969- onClose = { closeSettingsSheet }
958+ visible = { settingsSheetPresentation . isVisible }
959+ onClose = { settingsSheetPresentation . close }
960+ onDismissed = { settingsSheetPresentation . onDismissed }
970961 providerGroups = { flow . providerGroups }
971962 selectedModel = { flow . selectedModel }
972963 onSelectModel = { ( option ) => flow . setSelectedModelKey ( option . key , option . selection . options ) }
0 commit comments