check fix data

This commit is contained in:
Hamid
2026-05-17 00:50:23 -07:00
parent 7589a26706
commit 5bbd677f41
3 changed files with 78 additions and 23 deletions
+33 -8
View File
@@ -1,4 +1,4 @@
import { type FC, useEffect, useState } from 'react'
import { type FC, useEffect, useRef, useState } from 'react'
import Konva from 'konva'
import { clx } from '@/helpers/utils'
import EditorSidebar from './components/EditorSidebar'
@@ -23,33 +23,59 @@ const Editor: FC = () => {
}, [])
const [isLayersPanelOpen, setIsLayersPanelOpen] = useState(false)
const [isInitialContentReady, setIsInitialContentReady] = useState(false)
const { loadPages, pages, documentSettings, objects, currentPageId } = useEditorStore()
const loadedCatalogIdRef = useRef<string | null>(null)
const skipNextAutosaveRef = useRef(true)
const { loadPages, resetEditor, pages, documentSettings, objects, currentPageId } = useEditorStore()
const { data, isFetched } = useGetCatalogById(id!)
const { scheduleUpdate, isSaving } = useUpdateCatalog()
const { scheduleUpdate, cancelPendingUpdate, isSaving } = useUpdateCatalog()
// هنگام تعویض کاتالوگ: لغو ذخیرهٔ معلق، پاک‌سازی store، و منتظر بارگذاری مجدد بمان
useEffect(() => {
if (!isFetched) return
cancelPendingUpdate()
loadedCatalogIdRef.current = null
skipNextAutosaveRef.current = true
setIsInitialContentReady(false)
resetEditor()
}, [id, cancelPendingUpdate, resetEditor])
if (data?.data?.content) {
// بارگذاری محتوا فقط یک‌بار برای هر id (نه در هر refetch)
useEffect(() => {
if (!id || !isFetched) return
if (loadedCatalogIdRef.current === id) return
const rawContent = data?.data?.content
if (rawContent) {
try {
const json = JSON.parse(data.data.content)
const json = JSON.parse(rawContent)
if (Array.isArray(json)) {
loadPages(json)
} else if (json?.pages) {
loadPages(json.pages, json.documentSettings)
} else {
console.error('Invalid catalog content JSON: missing pages')
return
}
} catch (error) {
console.error('Invalid catalog content JSON:', error)
return
}
} else {
resetEditor()
}
loadedCatalogIdRef.current = id
skipNextAutosaveRef.current = true
setIsInitialContentReady(true)
}, [data?.data?.content, isFetched, loadPages])
}, [id, isFetched, data?.data?.content, loadPages, resetEditor])
useEffect(() => {
if (!id) return
if (!isInitialContentReady) return
if (!pages || pages.length === 0) return
if (skipNextAutosaveRef.current) {
skipNextAutosaveRef.current = false
return
}
const pagesToSave = currentPageId
? pages.map((p) =>
@@ -75,7 +101,6 @@ const Editor: FC = () => {
<div className="fixed bottom-10 left-10 z-50 rounded-full shadow-sm">
<div className="flex items-center gap-2 text-sm text-gray-700">
<span className="h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-black" />
{/* <span>در حال ذخیره ...</span> */}
</div>
</div>
) : null}