Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak(ui): show hotkey menu only not focus debug input #73

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ui/src/modules/app_builder/Canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface BuilderCanvasProps {
onClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
interaction?: InteractionInfo
errorInteraction?: ErrorInteraction
toobarFocusedRef: React.RefObject<boolean>
shhdgit marked this conversation as resolved.
Show resolved Hide resolved
onNodeDragStop: NodeDragHandler
onNodesDelete: OnNodesDelete
onAddNode: (n: Node<BlockNodeProps>[]) => void
Expand All @@ -62,6 +63,7 @@ export const BuilderCanvas: React.FC<BuilderCanvasProps> = ({
metadata,
interaction,
errorInteraction,
toobarFocusedRef,
onClick,
onNodeDragStop,
onNodesDelete,
Expand Down Expand Up @@ -154,7 +156,13 @@ export const BuilderCanvas: React.FC<BuilderCanvasProps> = ({
)

// hot keys
const { events: paneEvents, hotKeyMenuOpened, setHotKeyMenuOpened, menuPosition, menuStatus } = useHotKeyMenu()
const {
events: paneEvents,
hotKeyMenuOpened,
setHotKeyMenuOpened,
menuPosition,
menuStatus
} = useHotKeyMenu(toobarFocusedRef)

useHotkeys([
[
Expand Down
5 changes: 3 additions & 2 deletions ui/src/modules/app_builder/Canvas/useHotKeyMenu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getHotkeyHandler } from '@mantine/hooks'
import { useEffect, useRef, useState } from 'react'

export const useHotKeyMenu = () => {
export const useHotKeyMenu = (toobarFocusedRef: React.RefObject<boolean>) => {
const [menuPosition, setMenuPosition] = useState([0, 0])
const [hotKeyMenuOpened, setHotKeyMenuOpened] = useState(false)
const menuStatus = useRef({
Expand Down Expand Up @@ -29,7 +29,7 @@ export const useHotKeyMenu = () => {
}

const showHotKeyMenu = () => {
if (!menuStatus.current.inPane) {
if (!menuStatus.current.inPane || toobarFocusedRef.current) {
return
}
setMenuPosition([menuStatus.current.mouseX, menuStatus.current.mouseY])
Expand All @@ -50,6 +50,7 @@ export const useHotKeyMenu = () => {
const showHotKeyMenuHandler = getHotkeyHandler([['Space', showHotKeyMenu]])
document.body.addEventListener('keyup', showHotKeyMenuHandler)
return () => document.body.removeEventListener('keyup', showHotKeyMenuHandler)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return {
Expand Down
29 changes: 19 additions & 10 deletions ui/src/modules/app_builder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { useNavigate, useParams, useSearchParams } from 'react-router-dom'
import download from 'downloadjs'
import yaml from 'js-yaml'
import { useDebouncedValue, useDisclosure, useHotkeys } from '@mantine/hooks'
import { useDebouncedValue, useDisclosure, useFocusWithin, useHotkeys } from '@mantine/hooks'
import { ReactFlowProvider, useNodesInitialized, useReactFlow } from 'reactflow'
import { FormProvider, useForm, useFormContext } from 'react-hook-form'
import { ApplicationInfo, ApplicationVersionInfo, InteractionInfo, VersionMetadata } from '@api/linguflow.schemas'
Expand Down Expand Up @@ -96,6 +96,12 @@ const AppBuilder: React.FC = () => {
const containerElem = useRef<HTMLDivElement>(null)
const [menuOpened, setMenuOpened] = useState(false)
const [toolbarPaneOpened, setToolbarPaneOpened] = useState(false)
const { ref: focusRef, focused: toolbarFocused } = useFocusWithin()
const toobarFocusedRef = useRef(toolbarFocused)

useEffect(() => {
toobarFocusedRef.current = toolbarFocused
}, [toolbarFocused])

const [currentInteraction, setCurrentInteraction] = useState<InteractionInfo>()
const [errorInteraction, setErrorInteraction] = useState<ErrorInteraction>()
Expand Down Expand Up @@ -223,6 +229,7 @@ const AppBuilder: React.FC = () => {
metadata={verMetadata}
interaction={currentInteraction}
errorInteraction={errorInteraction}
toobarFocusedRef={toobarFocusedRef}
onClick={() => setMenuOpened(false)}
onNodeDragStop={() => setCanUpdate(true)}
onRelayout={() => setCanUpdate(true)}
Expand All @@ -239,15 +246,17 @@ const AppBuilder: React.FC = () => {
onCanSave={() => setCanSave(true)}
/>
</Box>
<Toolbar
app={appData?.application}
ver={verData?.version}
toolbarPaneOpened={toolbarPaneOpened}
setToolbarPaneOpened={setToolbarPaneOpened}
isCreatingVersion={isCreatingVersion}
onUpdateCurrentInteraction={setCurrentInteraction}
onInteractionError={setErrorInteraction}
/>
<div ref={focusRef}>
<Toolbar
app={appData?.application}
ver={verData?.version}
toolbarPaneOpened={toolbarPaneOpened}
setToolbarPaneOpened={setToolbarPaneOpened}
isCreatingVersion={isCreatingVersion}
onUpdateCurrentInteraction={setCurrentInteraction}
onInteractionError={setErrorInteraction}
/>
</div>
</Box>
</ContainerElemProvider>
)
Expand Down