2024-09-09 19:59:36 +03:00
|
|
|
import { createActorContext } 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-09-09 19:59:36 +03:00
|
|
|
import { useEffect } from 'react'
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-09-09 19:59:36 +03:00
|
|
|
export const CommandsContext = createActorContext(
|
|
|
|
commandBarMachine.provide({
|
2024-03-14 15:56:45 -04:00
|
|
|
guards: {
|
2024-09-09 19:59:36 +03:00
|
|
|
'Command has no arguments': ({ context }) => {
|
2024-03-14 15:56:45 -04:00
|
|
|
return (
|
|
|
|
!context.selectedCommand?.args ||
|
|
|
|
Object.keys(context.selectedCommand?.args).length === 0
|
|
|
|
)
|
|
|
|
},
|
2024-09-09 19:59:36 +03:00
|
|
|
'All arguments are skippable': ({ context }) => {
|
2024-07-12 16:16:26 -04:00
|
|
|
return Object.values(context.selectedCommand!.args!).every(
|
|
|
|
(argConfig) => argConfig.skip
|
|
|
|
)
|
|
|
|
},
|
2024-03-14 15:56:45 -04:00
|
|
|
},
|
|
|
|
})
|
2024-09-09 19:59:36 +03:00
|
|
|
)
|
2024-03-14 15:56:45 -04:00
|
|
|
|
2024-09-09 19:59:36 +03:00
|
|
|
export const CommandBarProvider = ({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
2024-03-14 15:56:45 -04:00
|
|
|
return (
|
2024-09-09 19:59:36 +03:00
|
|
|
<CommandsContext.Provider>
|
|
|
|
<CommandBarProviderInner>{children}</CommandBarProviderInner>
|
2024-03-14 15:56:45 -04:00
|
|
|
</CommandsContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
2024-09-09 19:59:36 +03:00
|
|
|
function CommandBarProviderInner({ children }: { children: React.ReactNode }) {
|
|
|
|
const commandBarActor = CommandsContext.useActorRef()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
editorManager.setCommandBarSend(commandBarActor.send)
|
|
|
|
})
|
|
|
|
|
|
|
|
return children
|
|
|
|
}
|