| 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 { useEditModeStore } from '@quick-edit/state/edit-mode'; |
| 6 |
import { useQuickEditStore } from '@quick-edit/state/store'; |
| 7 |
import { useEffect } from '@wordpress/element'; |
| 8 |
import { SidebarLayout } from './components/layouts/SidebarLayout'; |
| 9 |
|
| 10 |
export const Chat = ({ busy, working, children }) => { |
| 11 |
const { setIsMobile, isMobile, mode } = useGlobalStore(); |
| 12 |
const editModeOn = useEditModeStore((s) => s.on); |
| 13 |
const block = useQuickEditStore((s) => s.agentBlock); |
| 14 |
const setBlock = useQuickEditStore((s) => s.setAgentBlock); |
| 15 |
|
| 16 |
useEffect(() => { |
| 17 |
if (!isMobile || !block) return; |
| 18 |
// Remove the block if we switch to mobile |
| 19 |
setBlock(null); |
| 20 |
}, [isMobile, setIsMobile, block, setBlock]); |
| 21 |
|
| 22 |
useEffect(() => { |
| 23 |
let timeout; |
| 24 |
const onResize = () => { |
| 25 |
clearTimeout(timeout); |
| 26 |
timeout = window.setTimeout(() => { |
| 27 |
setIsMobile(window.innerWidth < 783); |
| 28 |
}, 10); |
| 29 |
}; |
| 30 |
window.addEventListener('resize', onResize); |
| 31 |
return () => { |
| 32 |
clearTimeout(timeout); |
| 33 |
window.removeEventListener('resize', onResize); |
| 34 |
}; |
| 35 |
}, [setIsMobile]); |
| 36 |
|
| 37 |
if (isMobile) { |
| 38 |
return ( |
| 39 |
<MobileLayout> |
| 40 |
<div |
| 41 |
id="extendify-agent-chat" |
| 42 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 43 |
> |
| 44 |
{children} |
| 45 |
</div> |
| 46 |
</MobileLayout> |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
if (mode === 'docked-left') { |
| 51 |
return ( |
| 52 |
<SidebarLayout> |
| 53 |
<div |
| 54 |
id="extendify-agent-chat" |
| 55 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 56 |
> |
| 57 |
{children} |
| 58 |
</div> |
| 59 |
{editModeOn && <DOMHighlighter busy={busy} working={working} />} |
| 60 |
</SidebarLayout> |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
return ( |
| 65 |
<DragResizeLayout> |
| 66 |
<div |
| 67 |
id="extendify-agent-chat" |
| 68 |
className="flex min-h-0 flex-1 grow flex-col font-sans" |
| 69 |
> |
| 70 |
{children} |
| 71 |
</div> |
| 72 |
{editModeOn && <DOMHighlighter busy={busy} working={working} />} |
| 73 |
</DragResizeLayout> |
| 74 |
); |
| 75 |
}; |
| 76 |
|