* chore: saving off skeleton * fix: saving skeleton * chore: skeleton for loading projects from project directory path * chore: cleaning up useless state transition to be an on event direct to action state * fix: new structure for web vs desktop vs react machine provider code * chore: saving off skeleton * fix: skeleton logic for react? going to move it from a string to obj.string * fix: trying to prevent error element unmount on global react components. This is bricking JS state * fix: we are so back * chore: implemented navigating to specfic KCL file * chore: implementing renaming project * chore: deleting project * fix: auto fixes * fix: old debug/testing file oops * chore: generic create new file * chore: skeleton for web create file provide * chore: basic machine vitest... need to figure out how to get window.electron implemented in vitest? * chore: save off progress before deleting other project implementation, a few missing features still * chore: trying a different init skeleton? most likely will migrate * chore: first attempt of purging projects context provider * chore: enabling toast for some machine state * chore: enabling more toast success and error * chore: writing read write state to the system io based on the project path * fix: tsc fixes * fix: use file system watcher, navigate to project after creation via the requestProjectName * chore: open project command, hooks vs snapshot context helpers * chore: implemented open and create project for e2e testing. They are hard coded in poor spot for now. * fix: codespell fixes * chore: implementing more project commands * chore: PR improvements for root.tsx * chore: leaving comment about new Router.tsx layout * fix: removing debugging code * fix: rewriting component for readability * fix: improving web initialization * chore: implementing import file from url which is not actually that? * fix: clearing search params on import file from url * fix: fixed two e2e tests, forgot needsReview when making new command * fix: fixing some import from url business logic to pass e2e tests * chore: script for diffing circular deps +/- * fix: formatting * fix: massive fix for circular depsga! * fix: trying to fix some errors and auto fmt * fix: updating deps * fix: removing debugging code * fix: big clean up * fix: more deletion * fix: tsc cleanup * fix: TSC TSC TSC TSC! * fix: typo fix * fix: clear query params on web only, desktop not required * fix: removing unused code * fmt * Bring back `trap` removed in merge * Use explicit types instead of `any`s on arg configs * Add project commands directly to command palette * fix: deleting debugging code, from PR review * fix: this got added back(?) * fix: using referred type * fix: more PR clean up * fix: big block comment for xstate architecture decision * fix: more pr comment fixes * fix: saving off logic, need a big cleanup because I hacked it together to get a POC * fix: extra business? * fix: merge conflict just added them back why dude * fix: more PR comments * fix: big ciruclar deps fix, commandBarActor in appActor * chore: writing e2e test, still need to fix 3 bugs * chore: adding more scenarios * fix: formatting * fix: fixing tsc errors * chore: deleting the old text to cad and using the new application level one, almost there * fix: prompt to edit works * fix: large push to get 1 text to cad command... the usage is a little buggy with delete and navigate within /file * fix: settings for highlight edges now works * chore: adding another e2e test * fix: cleaning up e2e tests and writing more of them * fix: tsc type * chore: more e2e improvements, unique project name in text to cad * chore: e2e tests should be good to go * fix: gotcha comment * fix: enabled web t2c, codespell fixes * fix: fixing merge conflcits?? * feat: implemented load external for kcl samples * feat: load external model from disk * fix: trying to delete old stuff * fix: all command trigger locations now have defaults for current project * fix: gotcha comment for the future * chore: hiding import file from url command, two separate commands for 3d and kcl file adding * chore: commands are now add file to project, 3rd iteration * fix: t2c in file menu fixed * chore: updating file menu for new global actions * fix: auto fixes * fix: the command bar arg flow for web add kcl file seems backwards? * chore: updated home layout, added create from kcl sample button * chore: remapping some menu actions * fix: fixing open dialog copy * fix: an e2e test * fix: fixed e2e tests * fix: fixed e2e tests * fix: auto fixes * fix: pr clean up * fix: removing console log * fix: PR updates * fix: the reviewed stage boolean required the expected state to change. Also I progressed the command bar too soon * fix: no idea how this passed locally yesterday? I removed the {dir} unused but I need the function's logic but not the return value... * fix: should be good to go? --------- Co-authored-by: Frank Noirot <frankjohnson1993@gmail.com>
201 lines
7.3 KiB
TypeScript
201 lines
7.3 KiB
TypeScript
import { Dialog, Popover, Transition } from '@headlessui/react'
|
|
import { Fragment, useEffect } from 'react'
|
|
import { useLocation } from 'react-router-dom'
|
|
|
|
import CommandBarArgument from '@src/components/CommandBar/CommandBarArgument'
|
|
import CommandBarReview from '@src/components/CommandBar/CommandBarReview'
|
|
import CommandComboBox from '@src/components/CommandComboBox'
|
|
import { CustomIcon } from '@src/components/CustomIcon'
|
|
import Tooltip from '@src/components/Tooltip'
|
|
import { useNetworkContext } from '@src/hooks/useNetworkContext'
|
|
import { EngineConnectionStateType } from '@src/lang/std/engineConnection'
|
|
import useHotkeyWrapper from '@src/lib/hotkeyWrapper'
|
|
import { engineCommandManager } from '@src/lib/singletons'
|
|
import { commandBarActor, useCommandBarState } from '@src/lib/singletons'
|
|
import toast from 'react-hot-toast'
|
|
|
|
export const COMMAND_PALETTE_HOTKEY = 'mod+k'
|
|
|
|
export const CommandBar = () => {
|
|
const { pathname } = useLocation()
|
|
const commandBarState = useCommandBarState()
|
|
const { immediateState } = useNetworkContext()
|
|
const {
|
|
context: { selectedCommand, currentArgument, commands },
|
|
} = commandBarState
|
|
const isSelectionArgument =
|
|
currentArgument?.inputType === 'selection' ||
|
|
currentArgument?.inputType === 'selectionMixed'
|
|
const WrapperComponent = isSelectionArgument ? Popover : Dialog
|
|
|
|
// Close the command bar when navigating
|
|
useEffect(() => {
|
|
if (commandBarState.matches('Closed')) return
|
|
commandBarActor.send({ type: 'Close' })
|
|
}, [pathname])
|
|
|
|
/**
|
|
* if the engine connection is about to end, we don't want users
|
|
* to be able to perform commands that might require that connection,
|
|
* so we just close the command palette.
|
|
* TODO: instead, let each command control whether it is disabled, and
|
|
* don't just bail out
|
|
*/
|
|
useEffect(() => {
|
|
if (
|
|
!commandBarActor.getSnapshot().matches('Closed') &&
|
|
engineCommandManager.engineConnection &&
|
|
(immediateState.type === EngineConnectionStateType.Disconnecting ||
|
|
immediateState.type === EngineConnectionStateType.Disconnected)
|
|
) {
|
|
commandBarActor.send({ type: 'Close' })
|
|
toast.error('Exiting command flow because engine disconnected')
|
|
}
|
|
}, [immediateState, commandBarActor])
|
|
|
|
// Hook up keyboard shortcuts
|
|
useHotkeyWrapper([COMMAND_PALETTE_HOTKEY], () => {
|
|
if (commandBarState.context.commands.length === 0) return
|
|
if (commandBarState.matches('Closed')) {
|
|
commandBarActor.send({ type: 'Open' })
|
|
} else {
|
|
commandBarActor.send({ type: 'Close' })
|
|
}
|
|
})
|
|
|
|
function stepBack() {
|
|
if (!currentArgument) {
|
|
if (commandBarState.matches('Review')) {
|
|
const entries = Object.entries(selectedCommand?.args || {}).filter(
|
|
([_, argConfig]) =>
|
|
!argConfig.hidden &&
|
|
(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],
|
|
}
|
|
|
|
commandBarActor.send({
|
|
type: 'Edit argument',
|
|
data: {
|
|
arg: currentArg,
|
|
},
|
|
})
|
|
} else {
|
|
commandBarActor.send({ type: 'Deselect command' })
|
|
}
|
|
} else {
|
|
const entries = Object.entries(selectedCommand?.args || {}).filter(
|
|
(a) => !a[1].hidden
|
|
)
|
|
const index = entries.findIndex(
|
|
([key, _]) => key === currentArgument.name
|
|
)
|
|
|
|
if (index === 0) {
|
|
commandBarActor.send({ type: 'Deselect command' })
|
|
} else {
|
|
commandBarActor.send({
|
|
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()
|
|
commandBarActor.send({ type: 'Clear' })
|
|
}}
|
|
as={Fragment}
|
|
>
|
|
<WrapperComponent
|
|
open={!commandBarState.matches('Closed') || isSelectionArgument}
|
|
onClose={() => {
|
|
commandBarActor.send({ type: 'Close' })
|
|
}}
|
|
className={
|
|
'fixed inset-0 z-50 overflow-y-auto pb-4 pt-1 ' +
|
|
(isSelectionArgument ? 'pointer-events-none' : '')
|
|
}
|
|
data-testid="command-bar-wrapper"
|
|
>
|
|
<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 pt-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.filter((command) => {
|
|
return (
|
|
// By default everything is undefined
|
|
// If marked explicitly as false hide
|
|
command.hideFromSearch === undefined ||
|
|
command.hideFromSearch === false
|
|
)
|
|
})}
|
|
/>
|
|
) : commandBarState.matches('Gathering arguments') ? (
|
|
<CommandBarArgument stepBack={stepBack} />
|
|
) : (
|
|
commandBarState.matches('Review') && (
|
|
<CommandBarReview stepBack={stepBack} />
|
|
)
|
|
)}
|
|
<div className="flex flex-col gap-2 !absolute left-auto right-full top-[-3px] m-2.5 p-0 border-none bg-transparent hover:bg-transparent">
|
|
<button
|
|
onClick={() => commandBarActor.send({ type: 'Close' })}
|
|
className="group m-0 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">
|
|
Cancel{' '}
|
|
<kbd className="hotkey ml-4 dark:!bg-chalkboard-80">esc</kbd>
|
|
</Tooltip>
|
|
</button>
|
|
{!commandBarState.matches('Selecting command') && (
|
|
<button onClick={stepBack} className="m-0 p-0 border-none">
|
|
<CustomIcon name="arrowLeft" className="w-5 h-5 rounded-sm" />
|
|
<Tooltip position="bottom">
|
|
Step back{' '}
|
|
<kbd className="hotkey ml-4 dark:!bg-chalkboard-80">
|
|
Shift
|
|
</kbd>
|
|
<kbd className="hotkey ml-4 dark:!bg-chalkboard-80">
|
|
Bksp
|
|
</kbd>
|
|
</Tooltip>
|
|
</button>
|
|
)}
|
|
</div>
|
|
</WrapperComponent.Panel>
|
|
</Transition.Child>
|
|
</WrapperComponent>
|
|
</Transition.Root>
|
|
)
|
|
}
|
|
|
|
export default CommandBar
|