"use client" import { usePathname, useRouter } from "next/navigation" import { Suspense, useCallback, useEffect, useRef, useState } from "react" import { DrawIoEmbed } from "react-drawio" import type { ImperativePanelHandle } from "react-resizable-panels" import ChatPanel from "@/components/chat-panel" import { STORAGE_CLOSE_PROTECTION_KEY } from "@/components/settings-dialog" import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable" import { useDiagram } from "@/contexts/diagram-context" import { i18n, type Locale } from "@/lib/i18n/config" const drawioBaseUrl = process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || "https://embed.diagrams.net" export default function Home() { const { drawioRef, handleDiagramExport, onDrawioLoad, resetDrawioReady } = useDiagram() const router = useRouter() const pathname = usePathname() // Extract current language from pathname (e.g., "/zh/about" → "zh") const currentLang = (pathname.split("/")[1] || i18n.defaultLocale) as Locale const [isMobile, setIsMobile] = useState(false) const [isChatVisible, setIsChatVisible] = useState(true) const [drawioUi, setDrawioUi] = useState<"min" | "sketch">("min") const [darkMode, setDarkMode] = useState(false) const [isLoaded, setIsLoaded] = useState(false) const [isDrawioReady, setIsDrawioReady] = useState(false) const [closeProtection, setCloseProtection] = useState(false) const chatPanelRef = useRef(null) const isMobileRef = useRef(false) // Load preferences from localStorage after mount useEffect(() => { // Restore saved locale and redirect if needed const savedLocale = localStorage.getItem("next-ai-draw-io-locale") if (savedLocale && i18n.locales.includes(savedLocale as Locale)) { const pathParts = pathname.split("/").filter(Boolean) const currentLocale = pathParts[0] if (currentLocale !== savedLocale) { pathParts[0] = savedLocale router.replace(`/${pathParts.join("/")}`) return // Wait for redirect } } const savedUi = localStorage.getItem("drawio-theme") if (savedUi === "min" || savedUi === "sketch") { setDrawioUi(savedUi) } 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") { setCloseProtection(true) } setIsLoaded(true) }, [pathname, router]) const handleDrawioLoad = useCallback(() => { setIsDrawioReady(true) onDrawioLoad() }, [onDrawioLoad]) const handleDarkModeChange = () => { const newValue = !darkMode setDarkMode(newValue) localStorage.setItem("next-ai-draw-io-dark-mode", String(newValue)) document.documentElement.classList.toggle("dark", newValue) setIsDrawioReady(false) resetDrawioReady() } const handleDrawioUiChange = () => { const newUi = drawioUi === "min" ? "sketch" : "min" localStorage.setItem("drawio-theme", newUi) setDrawioUi(newUi) setIsDrawioReady(false) resetDrawioReady() } // Check mobile - reset draw.io before crossing breakpoint const isInitialRenderRef = useRef(true) useEffect(() => { const checkMobile = () => { const newIsMobile = window.innerWidth < 768 if ( !isInitialRenderRef.current && newIsMobile !== isMobileRef.current ) { setIsDrawioReady(false) resetDrawioReady() } isMobileRef.current = newIsMobile isInitialRenderRef.current = false setIsMobile(newIsMobile) } checkMobile() window.addEventListener("resize", checkMobile) return () => window.removeEventListener("resize", checkMobile) }, [resetDrawioReady]) const toggleChatPanel = () => { const panel = chatPanelRef.current if (panel) { if (panel.isCollapsed()) { panel.expand() setIsChatVisible(true) } else { panel.collapse() setIsChatVisible(false) } } } // Keyboard shortcut for toggling chat panel useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if ((event.ctrlKey || event.metaKey) && event.key === "b") { event.preventDefault() toggleChatPanel() } } window.addEventListener("keydown", handleKeyDown) return () => window.removeEventListener("keydown", handleKeyDown) }, []) // Show confirmation dialog when user tries to leave the page useEffect(() => { if (!closeProtection) return const handleBeforeUnload = (event: BeforeUnloadEvent) => { event.preventDefault() return "" } window.addEventListener("beforeunload", handleBeforeUnload) return () => window.removeEventListener("beforeunload", handleBeforeUnload) }, [closeProtection]) return (
{isLoaded && (
)} {(!isLoaded || !isDrawioReady) && (
Draw.io panel is loading...
)}
{/* Chat Panel */} setIsChatVisible(false)} onExpand={() => setIsChatVisible(true)} >
Loading chat...
} >
) }