fix: persist diagram-only saves and ref typing

This commit is contained in:
dayuan.jiang
2026-01-25 14:21:14 +09:00
parent 7877b39fa1
commit 384221c498
3 changed files with 21 additions and 13 deletions

View File

@@ -11,6 +11,7 @@ import {
} from "@/components/ui/resizable"
import { useDiagram } from "@/contexts/diagram-context"
import { i18n, type Locale } from "@/lib/i18n/config"
import { isIndexedDBAvailable } from "@/lib/session-storage"
export default function Home() {
const {
@@ -31,6 +32,7 @@ export default function Home() {
const [isLoaded, setIsLoaded] = useState(false)
const [isDrawioReady, setIsDrawioReady] = useState(false)
const [isElectron, setIsElectron] = useState(false)
const [canPersist, setCanPersist] = useState(false)
const [drawioBaseUrl, setDrawioBaseUrl] = useState(
process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || "https://embed.diagrams.net",
)
@@ -81,6 +83,7 @@ export default function Home() {
setDrawioBaseUrl(`${window.location.origin}/drawio/index.html`)
}
setCanPersist(isIndexedDBAvailable())
setIsLoaded(true)
}, [pathname, router])
@@ -92,12 +95,12 @@ export default function Home() {
const handleDrawioAutoSave = useCallback(
(data: { xml?: string }) => {
handleDiagramAutoSave(data)
// Clear modified state to avoid beforeunload prompts in embed mode
if (drawioRef && "current" in drawioRef) {
// Only suppress modified state when persistence is available
if (canPersist) {
drawioRef.current?.status({ message: "", modified: false })
}
},
[drawioRef, handleDiagramAutoSave],
[canPersist, drawioRef, handleDiagramAutoSave],
)
const handleDarkModeChange = () => {
@@ -195,14 +198,20 @@ export default function Home() {
onExport={handleDiagramExport}
onLoad={handleDrawioLoad}
baseUrl={drawioBaseUrl}
configuration={{ confirmExit: false }}
configuration={
canPersist
? { confirmExit: false }
: undefined
}
urlParameters={{
ui: drawioUi,
spin: false,
libraries: false,
// Disable draw.io modified state to avoid beforeunload prompts
modified: false,
keepmodified: false,
// Disable modified tracking only when persistence is available
...(canPersist && {
modified: false,
keepmodified: false,
}),
saveAndExit: false,
noSaveBtn: true,
noExitBtn: true,

View File

@@ -137,9 +137,7 @@ export default function ChatPanel({
const onFetchChart = (saveToHistory = true) => {
return Promise.race([
new Promise<string>((resolve) => {
if (resolverRef && "current" in resolverRef) {
resolverRef.current = resolve
}
resolverRef.current = resolve
if (saveToHistory) {
onExport()
} else {
@@ -730,7 +728,8 @@ export default function ChatPanel({
const handleVisibilityChange = async () => {
if (
document.visibilityState === "hidden" &&
messagesRef.current.length > 0
(messagesRef.current.length > 0 ||
isRealDiagram(chartXMLRef.current))
) {
try {
// Attempt to save session - browser may not wait for completion

View File

@@ -20,8 +20,8 @@ interface DiagramContextType {
loadDiagram: (chart: string, skipValidation?: boolean) => string | null
handleExport: () => void
handleExportWithoutHistory: () => void
resolverRef: React.Ref<((value: string) => void) | null>
drawioRef: React.Ref<DrawIoEmbedRef | null>
resolverRef: React.MutableRefObject<((value: string) => void) | null>
drawioRef: React.MutableRefObject<DrawIoEmbedRef | null>
handleDiagramExport: (data: any) => void
handleDiagramAutoSave: (data: { xml?: string }) => void
clearDiagram: () => void