| 1 |
import { DOMHighlighter } from '@agent/components/DOMHighlighter'; |
| 2 |
import { DragResizeLayout } from '@agent/components/layouts/DragResizeLayout'; |
| 3 |
import { MobileLayout } from '@agent/components/layouts/MobileLayout'; |
| 4 |
import { useGlobalStore } from '@agent/state/global'; |
| 5 |
import { useWorkflowStore } from '@agent/state/workflows'; |
| 6 |
import { useEffect } from '@wordpress/element'; |
| 7 |
import { SidebarLayout } from './components/layouts/SidebarLayout'; |
| 8 |
|
| 9 |
export const Chat = ({ busy, children }) => { |
| 10 |
const { setIsMobile, isMobile, mode } = useGlobalStore(); |
| 11 |
const { domToolEnabled, block, setBlock, setDomToolEnabled } = |
| 12 |
useWorkflowStore(); |
| 13 |
|
| 14 |
useEffect(() => { |
| 15 |
if (!isMobile || !block) return; |
| 16 |
// Remove the block if we switch to mobile |
| 17 |
setBlock(null); |
| 18 |
}, [isMobile, setIsMobile, block, setBlock]); |
| 19 |
|
| 20 |
useEffect(() => { |
| 21 |
let timeout; |
| 22 |
const onResize = () => { |
| 23 |
clearTimeout(timeout); |
| 24 |
timeout = window.setTimeout(() => { |
| 25 |
setIsMobile(window.innerWidth < 783); |
| 26 |
}, 10); |
| 27 |
}; |
| 28 |
window.addEventListener('resize', onResize); |
| 29 |
return () => { |
| 30 |
clearTimeout(timeout); |
| 31 |
window.removeEventListener('resize', onResize); |
| 32 |
}; |
| 33 |
}, [setIsMobile]); |
| 34 |
|
| 35 |
useEffect(() => { |
| 36 |
// Exit select mode when Escape is pressed |
| 37 |
const onKeyDown = (e) => { |
| 38 |
if (e.key !== 'Escape' || !domToolEnabled) return; |
| 39 |
|
| 40 |
// Cancel the workflow |
| 41 |
window.dispatchEvent(new CustomEvent('extendify-agent:cancel-workflow')); |
| 42 |
|
| 43 |
// If a block is selected, clear it |
| 44 |
if (block) return setBlock(null); |
| 45 |
|
| 46 |
// If no block is selected, exit select mode |
| 47 |
setDomToolEnabled(false); |
| 48 |
}; |
| 49 |
window.addEventListener('keydown', onKeyDown); |
| 50 |
return () => { |
| 51 |
window.removeEventListener('keydown', onKeyDown); |
| 52 |
}; |
| 53 |
}, [domToolEnabled, block, setBlock, setDomToolEnabled]); |
| 54 |
|
| 55 |
if (isMobile) { |
| 56 |
return ( |
| 57 |
<MobileLayout> |
| 58 |
<div |
| 59 |
id="extendify-agent-chat" |
| 60 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 61 |
> |
| 62 |
{children} |
| 63 |
</div> |
| 64 |
</MobileLayout> |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
if (mode === 'docked-left') { |
| 69 |
return ( |
| 70 |
<SidebarLayout> |
| 71 |
<div |
| 72 |
id="extendify-agent-chat" |
| 73 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 74 |
> |
| 75 |
{children} |
| 76 |
</div> |
| 77 |
{domToolEnabled && <DOMHighlighter busy={busy} />} |
| 78 |
</SidebarLayout> |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return ( |
| 83 |
<DragResizeLayout> |
| 84 |
<div |
| 85 |
id="extendify-agent-chat" |
| 86 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 87 |
> |
| 88 |
{children} |
| 89 |
</div> |
| 90 |
{domToolEnabled && <DOMHighlighter busy={busy} />} |
| 91 |
</DragResizeLayout> |
| 92 |
); |
| 93 |
}; |
| 94 |
|