Skip to content

Commit 843f0a2

Browse files
committed
fix: resolve leaks in previewModal 3D viewer
1 parent d62e6d1 commit 843f0a2

File tree

2 files changed

+454
-182
lines changed

2 files changed

+454
-182
lines changed

src/components/PreviewModal.tsx

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,14 @@ const PreviewModal: React.FC<{
2828
// fix end---------------------
2929

3030
useEffect(() => {
31-
if (!isOpen) return;
32-
//add spinner
33-
// if (!isOpen || isLoading) return;
34-
35-
// fix start-----------: Get the container element from the ref.
36-
// const container = canvasContainerRef.current;
37-
// if (!container) {
38-
// // This can happen briefly on the first render, so we just wait for the next render.
39-
// return;
40-
// }
41-
// // 3. Check for the required legacy functions on the window object.
42-
// if (
43-
// typeof window.previewdata !== "function" ||
44-
// typeof window.initcanvas_with_container !== "function"
45-
// ) {
46-
// console.error(
47-
// "❌ Legacy preview script functions are not available on the window object."
48-
// );
49-
// return;
50-
// }
51-
52-
// window.previewdata(dataKey, previewIndex, isInternal, false);
53-
// fix end---------------------------------
54-
// clear old canvas
31+
// if (!isOpen) return;
32+
if (!isOpen) {
33+
// Modal just closed — clean up Three.js immediately
34+
if (typeof window.destroyPreview === "function") {
35+
window.destroyPreview();
36+
}
37+
return;
38+
}
5539
const canvasDiv = document.getElementById("canvas");
5640
if (canvasDiv)
5741
while (canvasDiv.firstChild) canvasDiv.removeChild(canvasDiv.firstChild);
@@ -69,6 +53,10 @@ const PreviewModal: React.FC<{
6953

7054
return () => {
7155
clearInterval(interval);
56+
// Component unmounting — clean up Three.js
57+
if (typeof window.destroyPreview === "function") {
58+
window.destroyPreview();
59+
}
7260
};
7361
}, [isOpen, dataKey, previewIndex, isInternal]);
7462

0 commit comments

Comments
 (0)