2024-03-14 15:56:45 -04:00
|
|
|
import { useMachine } from '@xstate/react'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { editorManager } from 'lib/singletons'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { commandBarMachine } from 'machines/commandBarMachine'
|
2024-04-19 14:24:40 -07:00
|
|
|
import { createContext, useEffect } from 'react'
|
2024-03-14 15:56:45 -04:00
|
|
|
import { EventFrom, StateFrom } from 'xstate'
|
|
|
|
|
|
|
|
type CommandsContextType = {
|
|
|
|
commandBarState: StateFrom<typeof commandBarMachine>
|
|
|
|
commandBarSend: (event: EventFrom<typeof commandBarMachine>) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const CommandsContext = createContext<CommandsContextType>({
|
|
|
|
commandBarState: commandBarMachine.initialState,
|
|
|
|
commandBarSend: () => {},
|
|
|
|
})
|
|
|
|
|
|
|
|
export const CommandBarProvider = ({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
|
|
|
const [commandBarState, commandBarSend] = useMachine(commandBarMachine, {
|
|
|
|
devTools: true,
|
|
|
|
guards: {
|
|
|
|
'Command has no arguments': (context, _event) => {
|
|
|
|
return (
|
|
|
|
!context.selectedCommand?.args ||
|
|
|
|
Object.keys(context.selectedCommand?.args).length === 0
|
|
|
|
)
|
|
|
|
},
|
2024-07-12 16:16:26 -04:00
|
|
|
'All arguments are skippable': (context, _event) => {
|
|
|
|
return Object.values(context.selectedCommand!.args!).every(
|
|
|
|
(argConfig) => argConfig.skip
|
|
|
|
)
|
|
|
|
},
|
2024-03-14 15:56:45 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-04-19 14:24:40 -07:00
|
|
|
useEffect(() => {
|
|
|
|
editorManager.setCommandBarSend(commandBarSend)
|
|
|
|
})
|
|
|
|
|
2024-03-14 15:56:45 -04:00
|
|
|
return (
|
|
|
|
<CommandsContext.Provider
|
|
|
|
value={{
|
|
|
|
commandBarState,
|
|
|
|
commandBarSend,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</CommandsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|