Files
Mathias Jakobsen 4e088c54fd Merge pull request #36092 from overleaf/mj-command-palette-pre-release
[web] Pre-labs-release changes for command palette

GitOrigin-RevId: 578cf56f1ef51996ce928466edf50e360fa73b42
2026-09-17 11:46:54 +00:00

56 lines
1.3 KiB
TypeScript

import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useState,
} from 'react'
import useCommandPaletteTriggers from '../hooks/use-command-palette-triggers'
import CommandPaletteBody from './command-palette-body'
import { useLayoutContext } from '@/shared/context/layout-context'
import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
import { useTranslation } from 'react-i18next'
const CommandPaletteRoot = () => {
const [show, _setShow] = useState(false)
const { view } = useLayoutContext()
const { t } = useTranslation()
const setShow: Dispatch<SetStateAction<boolean>> = useCallback(
value => {
_setShow(view === 'history' ? false : value)
},
[view]
)
useEffect(() => {
if (view === 'history') {
_setShow(false)
}
}, [view])
useCommandPaletteTriggers(setShow)
const onHide = useCallback(() => setShow(false), [setShow])
useCommandProvider(() => {
if (view === 'history') {
return
}
return [
{
id: 'command-palette',
label: t('command_palette'),
handler: () => setShow(true),
},
]
}, [view, t, setShow])
if (!show) {
return null
}
return <CommandPaletteBody show={show} onHide={onHide} />
}
export default CommandPaletteRoot