import { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react' import type { RpcClient } from '../transport/rpc-client' import { NewWorktreeModal } from './NewWorktreeModal' export type NewWorktreeModalControllerHandle = { open: () => void } type Props = { routeVisible: boolean client: RpcClient | null hostId?: string existingWorktreePaths?: readonly string[] existingWorktrees?: readonly { repoId: string; branch: string }[] openExternalUrl: (url: string) => Promise onVisibleChange?: (visible: boolean) => void onRouteVisibleChange: (visible: boolean) => void onCreated: (worktreeId: string, name: string, warning?: string) => void } export const NewWorktreeModalController = forwardRef( function NewWorktreeModalController( { routeVisible, client, hostId, existingWorktreePaths, existingWorktrees, openExternalUrl, onVisibleChange, onRouteVisibleChange, onCreated }, ref ) { const [manualVisible, setManualVisible] = useState(false) const visible = routeVisible || manualVisible useImperativeHandle( ref, () => ({ open: () => setManualVisible(true) }), [] ) const close = useCallback(() => { setManualVisible(false) if (routeVisible) { onRouteVisibleChange(false) } }, [onRouteVisibleChange, routeVisible]) useEffect(() => { onVisibleChange?.(visible) }, [onVisibleChange, visible]) return ( ) } )