* Fix Commands button to show correct shortcut
* Fix onboarding to use the same shortcut reference
* Rename test file to be more general
* Add test for commands button text
* Remove outdated reference to Ctrl+/
* Change shortcut separator to be + and no spaces
* Add JSDocs and improve comments
* Add unit tests
* Change control modifier to regular ASCII caret
* Add browser test and fix platform detection
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest)
* Add useful debug info to the error message
* Fix to display metaKey as Super on Linux
* Revert "A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)"
This reverts commit f8da90d5d2
.
* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)
* Approve snapshots
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jess Frazelle <jessfraz@users.noreply.github.com>
144 lines
4.8 KiB
TypeScript
144 lines
4.8 KiB
TypeScript
import { Dialog, Popover, Transition } from '@headlessui/react'
|
|
import { Fragment, useEffect } from 'react'
|
|
import { useCommandsContext } from 'hooks/useCommandsContext'
|
|
import CommandBarArgument from './CommandBarArgument'
|
|
import CommandComboBox from '../CommandComboBox'
|
|
import CommandBarReview from './CommandBarReview'
|
|
import { useLocation } from 'react-router-dom'
|
|
import useHotkeyWrapper from 'lib/hotkeyWrapper'
|
|
import { CustomIcon } from 'components/CustomIcon'
|
|
import Tooltip from 'components/Tooltip'
|
|
|
|
export const COMMAND_PALETTE_HOTKEY = 'mod+k'
|
|
|
|
export const CommandBar = () => {
|
|
const { pathname } = useLocation()
|
|
const { commandBarState, commandBarSend } = useCommandsContext()
|
|
const {
|
|
context: { selectedCommand, currentArgument, commands },
|
|
} = commandBarState
|
|
const isSelectionArgument = currentArgument?.inputType === 'selection'
|
|
const WrapperComponent = isSelectionArgument ? Popover : Dialog
|
|
|
|
// Close the command bar when navigating
|
|
useEffect(() => {
|
|
commandBarSend({ type: 'Close' })
|
|
}, [pathname])
|
|
|
|
// Hook up keyboard shortcuts
|
|
useHotkeyWrapper([COMMAND_PALETTE_HOTKEY], () => {
|
|
if (commandBarState.context.commands.length === 0) return
|
|
if (commandBarState.matches('Closed')) {
|
|
commandBarSend({ type: 'Open' })
|
|
} else {
|
|
commandBarSend({ type: 'Close' })
|
|
}
|
|
})
|
|
|
|
function stepBack() {
|
|
if (!currentArgument) {
|
|
if (commandBarState.matches('Review')) {
|
|
const entries = Object.entries(selectedCommand?.args || {}).filter(
|
|
([_, argConfig]) =>
|
|
typeof argConfig.required === 'function'
|
|
? argConfig.required(commandBarState.context)
|
|
: argConfig.required
|
|
)
|
|
|
|
const currentArgName = entries[entries.length - 1][0]
|
|
const currentArg = {
|
|
name: currentArgName,
|
|
...entries[entries.length - 1][1],
|
|
}
|
|
|
|
commandBarSend({
|
|
type: 'Edit argument',
|
|
data: {
|
|
arg: currentArg,
|
|
},
|
|
})
|
|
} else {
|
|
commandBarSend({ type: 'Deselect command' })
|
|
}
|
|
} else {
|
|
const entries = Object.entries(selectedCommand?.args || {})
|
|
const index = entries.findIndex(
|
|
([key, _]) => key === currentArgument.name
|
|
)
|
|
|
|
if (index === 0) {
|
|
commandBarSend({ type: 'Deselect command' })
|
|
} else {
|
|
commandBarSend({
|
|
type: 'Change current argument',
|
|
data: {
|
|
arg: { name: entries[index - 1][0], ...entries[index - 1][1] },
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Transition.Root
|
|
show={!commandBarState.matches('Closed') || false}
|
|
afterLeave={() => {
|
|
if (selectedCommand?.onCancel) selectedCommand.onCancel()
|
|
commandBarSend({ type: 'Clear' })
|
|
}}
|
|
as={Fragment}
|
|
>
|
|
<WrapperComponent
|
|
open={!commandBarState.matches('Closed') || isSelectionArgument}
|
|
onClose={() => {
|
|
commandBarSend({ type: 'Close' })
|
|
}}
|
|
className={
|
|
'fixed inset-0 z-50 overflow-y-auto pb-4 pt-1 ' +
|
|
(isSelectionArgument ? 'pointer-events-none' : '')
|
|
}
|
|
>
|
|
<Transition.Child
|
|
enter="duration-100 ease-out"
|
|
enterFrom="opacity-0 scale-95"
|
|
enterTo="opacity-100 scale-100"
|
|
leave="duration-75 ease-in"
|
|
leaveFrom="opacity-100 scale-100"
|
|
leaveTo="opacity-0 scale-95"
|
|
>
|
|
<WrapperComponent.Panel
|
|
className="relative z-50 pointer-events-auto w-full max-w-xl py-2 mx-auto border rounded rounded-tl-none shadow-lg bg-chalkboard-10 dark:bg-chalkboard-100 dark:border-chalkboard-70"
|
|
as="div"
|
|
data-testid="command-bar"
|
|
>
|
|
{commandBarState.matches('Selecting command') ? (
|
|
<CommandComboBox options={commands} />
|
|
) : commandBarState.matches('Gathering arguments') ? (
|
|
<CommandBarArgument stepBack={stepBack} />
|
|
) : (
|
|
commandBarState.matches('Review') && (
|
|
<CommandBarReview stepBack={stepBack} />
|
|
)
|
|
)}
|
|
<button
|
|
onClick={() => commandBarSend({ type: 'Close' })}
|
|
className="group block !absolute left-auto right-full top-[-3px] m-2.5 p-0 border-none bg-transparent hover:bg-transparent"
|
|
>
|
|
<CustomIcon
|
|
name="close"
|
|
className="w-5 h-5 rounded-sm bg-destroy-10 text-destroy-80 dark:bg-destroy-80 dark:text-destroy-10 group-hover:brightness-110"
|
|
/>
|
|
<Tooltip position="bottom" delay={500}>
|
|
Cancel{' '}
|
|
<kbd className="hotkey ml-4 dark:!bg-chalkboard-80">esc</kbd>
|
|
</Tooltip>
|
|
</button>
|
|
</WrapperComponent.Panel>
|
|
</Transition.Child>
|
|
</WrapperComponent>
|
|
</Transition.Root>
|
|
)
|
|
}
|
|
|
|
export default CommandBar
|