'use client' import { useConstant } from 'common' import { type PropsWithChildren, useEffect, useMemo, useCallback } from 'react' import { CommandContext } from '../internal/Context' import { initCommandsState } from '../internal/state/commandsState' import { initPagesState } from '../internal/state/pagesState' import { initQueryState } from '../internal/state/queryState' import { initViewState } from '../internal/state/viewState' import { useCrossCompatRouter } from './hooks/useCrossCompatRouter' import { useSetCommandMenuOpen, useToggleCommandMenu } from './hooks/viewHooks' const CommandProviderInternal = ({ children }: PropsWithChildren) => { const combinedState = useConstant(() => ({ commandsState: initCommandsState(), pagesState: initPagesState(), queryState: initQueryState(), viewState: initViewState(), })) return {children} } // This is a component not a hook so it can access the wrapping context. const CommandShortcut = () => { const toggleOpen = useToggleCommandMenu() useEffect(() => { const handleKeydown = (evt: KeyboardEvent) => { if (evt.key === 'k' && evt.metaKey) { evt.preventDefault() toggleOpen() } } document.addEventListener('keydown', handleKeydown) return () => document.removeEventListener('keydown', handleKeydown) }, [toggleOpen]) return null } // This is a component not a hook so it can access the wrapping context. function CloseOnNavigation() { const setIsOpen = useSetCommandMenuOpen() const router = useCrossCompatRouter() const completeNavigation = useCallback(() => { setIsOpen(false) }, [setIsOpen]) useEffect(() => { router.events.onRouteChangeComplete(completeNavigation) return () => router.events.offRouteChangeComplete(completeNavigation) }, [router]) return null } const CommandProvider = ({ children }: PropsWithChildren) => ( {children} ) export { CommandProvider }