2025-12-06 12:46:40 +09:00
|
|
|
"use client"
|
2025-12-17 20:24:53 +09:00
|
|
|
import { useCallback, useEffect, useRef, useState } from "react"
|
2025-12-06 12:46:40 +09:00
|
|
|
import { DrawIoEmbed } from "react-drawio"
|
|
|
|
|
import type { ImperativePanelHandle } from "react-resizable-panels"
|
|
|
|
|
import ChatPanel from "@/components/chat-panel"
|
2025-12-06 21:42:28 +09:00
|
|
|
import { STORAGE_CLOSE_PROTECTION_KEY } from "@/components/settings-dialog"
|
2025-12-05 22:42:39 +09:00
|
|
|
import {
|
|
|
|
|
ResizableHandle,
|
2025-12-06 12:46:40 +09:00
|
|
|
ResizablePanel,
|
|
|
|
|
ResizablePanelGroup,
|
|
|
|
|
} from "@/components/ui/resizable"
|
|
|
|
|
import { useDiagram } from "@/contexts/diagram-context"
|
2025-03-19 06:04:06 +00:00
|
|
|
|
2025-12-09 22:00:54 +09:00
|
|
|
const drawioBaseUrl =
|
|
|
|
|
process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || "https://embed.diagrams.net"
|
|
|
|
|
|
2025-03-27 08:17:54 +00:00
|
|
|
export default function Home() {
|
2025-12-15 19:10:21 +05:30
|
|
|
const {
|
|
|
|
|
drawioRef,
|
|
|
|
|
handleDiagramExport,
|
|
|
|
|
onDrawioLoad,
|
|
|
|
|
resetDrawioReady,
|
|
|
|
|
saveDiagramToStorage,
|
2025-12-17 20:24:53 +09:00
|
|
|
showSaveDialog,
|
|
|
|
|
setShowSaveDialog,
|
2025-12-15 19:10:21 +05:30
|
|
|
} = useDiagram()
|
2025-12-06 12:46:40 +09:00
|
|
|
const [isMobile, setIsMobile] = useState(false)
|
|
|
|
|
const [isChatVisible, setIsChatVisible] = useState(true)
|
2025-12-07 00:45:19 +09:00
|
|
|
const [drawioUi, setDrawioUi] = useState<"min" | "sketch">("min")
|
2025-12-10 08:21:15 +08:00
|
|
|
const [darkMode, setDarkMode] = useState(false)
|
|
|
|
|
const [isLoaded, setIsLoaded] = useState(false)
|
|
|
|
|
const [closeProtection, setCloseProtection] = useState(false)
|
|
|
|
|
|
|
|
|
|
const chatPanelRef = useRef<ImperativePanelHandle>(null)
|
2025-12-17 20:24:53 +09:00
|
|
|
const isSavingRef = useRef(false)
|
|
|
|
|
const mouseOverDrawioRef = useRef(false)
|
2025-12-18 20:14:10 +08:00
|
|
|
const isMobileRef = useRef(false)
|
2025-12-17 20:24:53 +09:00
|
|
|
|
|
|
|
|
// Reset saving flag when dialog closes (with delay to ignore lingering save events from draw.io)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!showSaveDialog) {
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
|
isSavingRef.current = false
|
|
|
|
|
}, 1000)
|
|
|
|
|
return () => clearTimeout(timeout)
|
|
|
|
|
}
|
|
|
|
|
}, [showSaveDialog])
|
|
|
|
|
|
|
|
|
|
// Handle save from draw.io's built-in save button
|
|
|
|
|
// Note: draw.io sends save events for various reasons (focus changes, etc.)
|
|
|
|
|
// We use mouse position to determine if the user is interacting with draw.io
|
|
|
|
|
const handleDrawioSave = useCallback(() => {
|
|
|
|
|
if (!mouseOverDrawioRef.current) return
|
|
|
|
|
if (isSavingRef.current) return
|
|
|
|
|
isSavingRef.current = true
|
|
|
|
|
setShowSaveDialog(true)
|
|
|
|
|
}, [setShowSaveDialog])
|
2025-12-07 00:45:19 +09:00
|
|
|
|
2025-12-10 08:21:15 +08:00
|
|
|
// Load preferences from localStorage after mount
|
2025-12-07 00:45:19 +09:00
|
|
|
useEffect(() => {
|
2025-12-10 08:21:15 +08:00
|
|
|
const savedUi = localStorage.getItem("drawio-theme")
|
|
|
|
|
if (savedUi === "min" || savedUi === "sketch") {
|
|
|
|
|
setDrawioUi(savedUi)
|
2025-12-05 23:10:48 +09:00
|
|
|
}
|
2025-12-07 00:45:19 +09:00
|
|
|
|
2025-12-10 08:21:15 +08:00
|
|
|
const savedDarkMode = localStorage.getItem("next-ai-draw-io-dark-mode")
|
|
|
|
|
if (savedDarkMode !== null) {
|
|
|
|
|
const isDark = savedDarkMode === "true"
|
|
|
|
|
setDarkMode(isDark)
|
|
|
|
|
document.documentElement.classList.toggle("dark", isDark)
|
|
|
|
|
} else {
|
|
|
|
|
const prefersDark = window.matchMedia(
|
|
|
|
|
"(prefers-color-scheme: dark)",
|
|
|
|
|
).matches
|
|
|
|
|
setDarkMode(prefersDark)
|
|
|
|
|
document.documentElement.classList.toggle("dark", prefersDark)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const savedCloseProtection = localStorage.getItem(
|
|
|
|
|
STORAGE_CLOSE_PROTECTION_KEY,
|
|
|
|
|
)
|
|
|
|
|
if (savedCloseProtection === "true") {
|
2025-12-07 01:39:09 +09:00
|
|
|
setCloseProtection(true)
|
2025-12-06 21:42:28 +09:00
|
|
|
}
|
2025-12-10 08:21:15 +08:00
|
|
|
|
|
|
|
|
setIsLoaded(true)
|
2025-12-07 00:45:19 +09:00
|
|
|
}, [])
|
2025-11-10 09:25:56 +09:00
|
|
|
|
2025-12-15 19:10:21 +05:30
|
|
|
const handleDarkModeChange = async () => {
|
|
|
|
|
await saveDiagramToStorage()
|
2025-12-10 08:21:15 +08:00
|
|
|
const newValue = !darkMode
|
|
|
|
|
setDarkMode(newValue)
|
|
|
|
|
localStorage.setItem("next-ai-draw-io-dark-mode", String(newValue))
|
|
|
|
|
document.documentElement.classList.toggle("dark", newValue)
|
2025-12-15 19:10:21 +05:30
|
|
|
resetDrawioReady()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleDrawioUiChange = async () => {
|
|
|
|
|
await saveDiagramToStorage()
|
|
|
|
|
const newUi = drawioUi === "min" ? "sketch" : "min"
|
|
|
|
|
localStorage.setItem("drawio-theme", newUi)
|
|
|
|
|
setDrawioUi(newUi)
|
2025-12-10 08:21:15 +08:00
|
|
|
resetDrawioReady()
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 20:14:10 +08:00
|
|
|
// Check mobile - save diagram and reset draw.io before crossing breakpoint
|
|
|
|
|
const isInitialRenderRef = useRef(true)
|
2025-11-10 09:25:56 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
const checkMobile = () => {
|
2025-12-18 20:14:10 +08:00
|
|
|
const newIsMobile = window.innerWidth < 768
|
|
|
|
|
// If crossing the breakpoint (not initial render), save diagram and reset draw.io
|
|
|
|
|
if (
|
|
|
|
|
!isInitialRenderRef.current &&
|
|
|
|
|
newIsMobile !== isMobileRef.current
|
|
|
|
|
) {
|
|
|
|
|
// Save diagram before remounting (fire and forget)
|
|
|
|
|
saveDiagramToStorage().catch(() => {
|
|
|
|
|
// Ignore timeout errors during resize
|
|
|
|
|
})
|
|
|
|
|
// Reset draw.io ready state so onLoad triggers again after remount
|
|
|
|
|
resetDrawioReady()
|
|
|
|
|
}
|
|
|
|
|
isMobileRef.current = newIsMobile
|
|
|
|
|
isInitialRenderRef.current = false
|
|
|
|
|
setIsMobile(newIsMobile)
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-11-10 09:25:56 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
checkMobile()
|
|
|
|
|
window.addEventListener("resize", checkMobile)
|
|
|
|
|
return () => window.removeEventListener("resize", checkMobile)
|
2025-12-18 20:14:10 +08:00
|
|
|
}, [saveDiagramToStorage, resetDrawioReady])
|
2025-11-10 09:25:56 +09:00
|
|
|
|
2025-12-05 22:42:39 +09:00
|
|
|
const toggleChatPanel = () => {
|
2025-12-06 12:46:40 +09:00
|
|
|
const panel = chatPanelRef.current
|
2025-12-05 22:42:39 +09:00
|
|
|
if (panel) {
|
|
|
|
|
if (panel.isCollapsed()) {
|
2025-12-06 12:46:40 +09:00
|
|
|
panel.expand()
|
|
|
|
|
setIsChatVisible(true)
|
2025-12-05 22:42:39 +09:00
|
|
|
} else {
|
2025-12-06 12:46:40 +09:00
|
|
|
panel.collapse()
|
|
|
|
|
setIsChatVisible(false)
|
2025-12-05 22:42:39 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-12-05 22:42:39 +09:00
|
|
|
|
2025-12-10 08:21:15 +08:00
|
|
|
// Keyboard shortcut for toggling chat panel
|
2025-11-15 12:09:32 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
2025-12-05 23:10:48 +09:00
|
|
|
if ((event.ctrlKey || event.metaKey) && event.key === "b") {
|
2025-12-06 12:46:40 +09:00
|
|
|
event.preventDefault()
|
|
|
|
|
toggleChatPanel()
|
2025-11-15 12:09:32 +09:00
|
|
|
}
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-11-15 12:09:32 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
window.addEventListener("keydown", handleKeyDown)
|
|
|
|
|
return () => window.removeEventListener("keydown", handleKeyDown)
|
|
|
|
|
}, [])
|
2025-11-15 12:09:32 +09:00
|
|
|
|
2025-12-05 18:42:36 +09:00
|
|
|
// Show confirmation dialog when user tries to leave the page
|
|
|
|
|
useEffect(() => {
|
2025-12-06 21:42:28 +09:00
|
|
|
if (!closeProtection) return
|
|
|
|
|
|
2025-12-05 18:42:36 +09:00
|
|
|
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
|
2025-12-06 12:46:40 +09:00
|
|
|
event.preventDefault()
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2025-12-05 18:42:36 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
window.addEventListener("beforeunload", handleBeforeUnload)
|
2025-12-05 23:10:48 +09:00
|
|
|
return () =>
|
2025-12-06 12:46:40 +09:00
|
|
|
window.removeEventListener("beforeunload", handleBeforeUnload)
|
2025-12-06 21:42:28 +09:00
|
|
|
}, [closeProtection])
|
2025-12-05 18:42:36 +09:00
|
|
|
|
2025-11-17 15:18:29 +09:00
|
|
|
return (
|
2025-12-05 22:42:39 +09:00
|
|
|
<div className="h-screen bg-background relative overflow-hidden">
|
2025-12-05 23:25:59 +09:00
|
|
|
<ResizablePanelGroup
|
2025-12-10 21:32:35 +09:00
|
|
|
id="main-panel-group"
|
2025-12-05 23:25:59 +09:00
|
|
|
direction={isMobile ? "vertical" : "horizontal"}
|
|
|
|
|
className="h-full"
|
|
|
|
|
>
|
2025-12-05 22:42:39 +09:00
|
|
|
{/* Draw.io Canvas */}
|
2025-12-10 21:32:35 +09:00
|
|
|
<ResizablePanel
|
|
|
|
|
id="drawio-panel"
|
|
|
|
|
defaultSize={isMobile ? 50 : 67}
|
|
|
|
|
minSize={20}
|
|
|
|
|
>
|
2025-12-06 12:46:40 +09:00
|
|
|
<div
|
|
|
|
|
className={`h-full relative ${
|
|
|
|
|
isMobile ? "p-1" : "p-2"
|
|
|
|
|
}`}
|
2025-12-17 20:24:53 +09:00
|
|
|
onMouseEnter={() => {
|
|
|
|
|
mouseOverDrawioRef.current = true
|
|
|
|
|
}}
|
|
|
|
|
onMouseLeave={() => {
|
|
|
|
|
mouseOverDrawioRef.current = false
|
|
|
|
|
}}
|
2025-12-06 12:46:40 +09:00
|
|
|
>
|
2025-12-10 08:21:15 +08:00
|
|
|
<div className="h-full rounded-xl overflow-hidden shadow-soft-lg border border-border/30">
|
|
|
|
|
{isLoaded ? (
|
2025-12-07 00:45:19 +09:00
|
|
|
<DrawIoEmbed
|
2025-12-10 08:21:15 +08:00
|
|
|
key={`${drawioUi}-${darkMode}`}
|
2025-12-07 00:45:19 +09:00
|
|
|
ref={drawioRef}
|
|
|
|
|
onExport={handleDiagramExport}
|
2025-12-07 01:39:09 +09:00
|
|
|
onLoad={onDrawioLoad}
|
2025-12-17 20:24:53 +09:00
|
|
|
onSave={handleDrawioSave}
|
2025-12-09 22:00:54 +09:00
|
|
|
baseUrl={drawioBaseUrl}
|
2025-12-07 00:45:19 +09:00
|
|
|
urlParameters={{
|
|
|
|
|
ui: drawioUi,
|
|
|
|
|
spin: true,
|
|
|
|
|
libraries: false,
|
|
|
|
|
saveAndExit: false,
|
|
|
|
|
noExitBtn: true,
|
2025-12-10 08:21:15 +08:00
|
|
|
dark: darkMode,
|
2025-12-07 00:45:19 +09:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
2025-12-10 08:21:15 +08:00
|
|
|
<div className="h-full w-full flex items-center justify-center bg-background">
|
2025-12-07 00:45:19 +09:00
|
|
|
<div className="animate-spin h-8 w-8 border-4 border-primary border-t-transparent rounded-full" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-12-05 22:42:39 +09:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</ResizablePanel>
|
2025-12-03 21:49:34 +09:00
|
|
|
|
2025-12-05 22:42:39 +09:00
|
|
|
<ResizableHandle withHandle />
|
|
|
|
|
|
|
|
|
|
{/* Chat Panel */}
|
|
|
|
|
<ResizablePanel
|
2025-12-18 20:14:10 +08:00
|
|
|
key={isMobile ? "mobile" : "desktop"}
|
2025-12-10 21:32:35 +09:00
|
|
|
id="chat-panel"
|
2025-12-05 22:42:39 +09:00
|
|
|
ref={chatPanelRef}
|
2025-12-05 23:25:59 +09:00
|
|
|
defaultSize={isMobile ? 50 : 33}
|
|
|
|
|
minSize={isMobile ? 20 : 15}
|
|
|
|
|
maxSize={isMobile ? 80 : 50}
|
|
|
|
|
collapsible={!isMobile}
|
|
|
|
|
collapsedSize={isMobile ? 0 : 3}
|
2025-12-05 22:42:39 +09:00
|
|
|
onCollapse={() => setIsChatVisible(false)}
|
|
|
|
|
onExpand={() => setIsChatVisible(true)}
|
|
|
|
|
>
|
2025-12-05 23:25:59 +09:00
|
|
|
<div className={`h-full ${isMobile ? "p-1" : "py-2 pr-2"}`}>
|
2025-12-05 22:42:39 +09:00
|
|
|
<ChatPanel
|
|
|
|
|
isVisible={isChatVisible}
|
|
|
|
|
onToggleVisibility={toggleChatPanel}
|
2025-12-05 23:10:48 +09:00
|
|
|
drawioUi={drawioUi}
|
2025-12-15 19:10:21 +05:30
|
|
|
onToggleDrawioUi={handleDrawioUiChange}
|
2025-12-10 08:21:15 +08:00
|
|
|
darkMode={darkMode}
|
2025-12-15 19:10:21 +05:30
|
|
|
onToggleDarkMode={handleDarkModeChange}
|
2025-12-05 23:25:59 +09:00
|
|
|
isMobile={isMobile}
|
2025-12-06 21:42:28 +09:00
|
|
|
onCloseProtectionChange={setCloseProtection}
|
2025-12-05 22:42:39 +09:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</ResizablePanel>
|
|
|
|
|
</ResizablePanelGroup>
|
2025-03-27 08:24:17 +00:00
|
|
|
</div>
|
2025-12-06 12:46:40 +09:00
|
|
|
)
|
2025-03-26 00:30:00 +00:00
|
|
|
}
|