Refactor SelectFloorPlan and LayoutModel components to use Zustand for layout management and improve code clarity
This commit is contained in:
parent
bcd648f71b
commit
bf8d90b18c
|
@ -1,23 +1,24 @@
|
|||
import React, { useState } from "react";
|
||||
import React from "react";
|
||||
import useLayoutStore from "../../store/builder/uselayoutStore";
|
||||
|
||||
const SelectFloorPlan: React.FC = () => {
|
||||
const [preset, setPreset] = useState("");
|
||||
const { currentLayout, setLayout } = useLayoutStore();
|
||||
return (
|
||||
<div className="select-floorplane-wrapper">
|
||||
Don't have an idea? Use these presets!
|
||||
<div className="presets-container">
|
||||
<button
|
||||
className={`preset ${preset === "1" ? "active" : ""}`}
|
||||
className={`preset ${currentLayout === "layout1" ? "active" : ""}`}
|
||||
onClick={() => {
|
||||
setPreset("1");
|
||||
setLayout("layout1");
|
||||
}}
|
||||
>
|
||||
Preset 1
|
||||
</button>
|
||||
<button
|
||||
className={`preset ${preset === "2" ? "active" : ""}`}
|
||||
className={`preset ${currentLayout === "layout2" ? "active" : ""}`}
|
||||
onClick={() => {
|
||||
setPreset("2");
|
||||
setLayout("layout2");
|
||||
}}
|
||||
>
|
||||
Preset 2
|
||||
|
|
|
@ -11,25 +11,24 @@ const modelPaths: Record<string, string> = {
|
|||
};
|
||||
|
||||
function LayoutModel() {
|
||||
const { toggleView } = useToggleView();
|
||||
const { currentLayout } = useLayoutStore();
|
||||
if (!currentLayout) return null;
|
||||
const { toggleView } = useToggleView();
|
||||
const { currentLayout } = useLayoutStore();
|
||||
|
||||
const path = modelPaths[currentLayout];
|
||||
const gltf = useGLTF(path);
|
||||
const cloned = useMemo(() => gltf?.scene?.clone(), [gltf]);
|
||||
// Always call the hook, but ensure it has a valid fallback
|
||||
const path = currentLayout ? modelPaths[currentLayout] : modelPaths.layout1;
|
||||
|
||||
return (
|
||||
<>
|
||||
{toggleView &&
|
||||
<group position={[0,0,0]} scale={[0,0,0]} dispose={null}>
|
||||
<primitive
|
||||
object={cloned}
|
||||
/>
|
||||
</group>
|
||||
}
|
||||
</>
|
||||
);
|
||||
// Explicitly cast the return type to ensure it's not an array
|
||||
const gltf = useGLTF(path) as any;
|
||||
|
||||
const cloned = useMemo(() => gltf.scene.clone(), [gltf]);
|
||||
|
||||
if (!currentLayout || !toggleView) return null;
|
||||
|
||||
return (
|
||||
<group position={[0, 0, 0]} dispose={null}>
|
||||
<primitive object={cloned} />
|
||||
</group>
|
||||
);
|
||||
}
|
||||
|
||||
export default LayoutModel;
|
||||
export default LayoutModel;
|
||||
|
|
Loading…
Reference in New Issue