import type { NavigationContainerRef } from '@react-navigation/core' ; import { nanoid } from 'nanoid/non-secure' ; import * as React from 'react' ; import type { Flipper } from 'react-native-flipper' ; import useDevToolsBase from './useDevToolsBase' ; let FlipperModule ; try { FlipperModule = require ( 'react-native-flipper' ) ; } catch ( e ) { } export default function useFlipper ( ref ) { if ( FlipperModule == null ) { throw new Error ( "Please install the 'react-native-flipper' package in your project to use Flipper integration for React Navigation." ) ; } const { addPlugin } = FlipperModule ; const connectionRef = React . useRef < Flipper . FlipperConnection > ( ) ; const { resetRoot } = useDevToolsBase ( ref , ( result ) => { const connection = connectionRef . current ; if ( ! connection ) { return ; } switch ( result . type ) { case 'init' : connection . send ( 'init' , { id : nanoid ( ) , state : result . state , } ) ; break ; case 'action' : connection . send ( 'action' , { id : nanoid ( ) , action : result . action , state : result . state , stack : result . stack , } ) ; break ; } } ) ; React . useEffect ( ( ) => { addPlugin ( { getId ( ) { return 'react-navigation' ; } , async onConnect ( connection ) { connectionRef . current = connection ; const on = ( event , listener ) => { connection . receive ( event , ( params , responder ) => { try { const result = listener ( params ) ; responder . success ( result ?? null ) ; } catch ( e ) { responder . error ( e ) ; } } ) ; } ; on ( 'navigation.invoke' , ( { method , args = [ ] } ) => { switch ( method ) { case 'resetRoot' : return resetRoot ( args [ 0 ] ) ; default : return ref . current ?. [ method ] ( ... args ) ; } } ) ; on ( 'linking.invoke' , ( { method , args = [ ] } ) => { const linking = ref . current ? global . REACT_NAVIGATION_DEVTOOLS ?. get ( ref . current ) ?. linking : null ; switch ( method ) { case 'getStateFromPath' : case 'getPathFromState' : case 'getActionFromState' : return linking ?. [ method ] ( args [ 0 ] , args [ 1 ] ?. trim ( ) ? eval ( `(function() { return ${ args [ 1 ] }; }())` ) : linking . config ) ; default : return linking ?. [ method ] ( ... args ) ; } } ) ; } , onDisconnect ( ) { connectionRef . current = undefined ; } , runInBackground ( ) { return false ; } , } ) ; } , [ addPlugin , ref , resetRoot ] ) ; }