refactor: Improve code readability and structure in EventProperties, DelayAction, SwapAction, and PreviewSelectionWithUpload components

This commit is contained in:
Vishnu 2025-04-28 09:51:07 +05:30
parent 328b045a7c
commit 5b6badaa52
4 changed files with 167 additions and 117 deletions

View File

@ -1,5 +1,8 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useState } from "react";
import { useSelectedEventData, useSelectedProduct } from "../../../../../store/simulation/useSimulationStore"; import {
useSelectedEventData,
useSelectedProduct,
} from "../../../../../store/simulation/useSimulationStore";
import { useProductStore } from "../../../../../store/simulation/useProductStore"; import { useProductStore } from "../../../../../store/simulation/useProductStore";
import ConveyorMechanics from "./mechanics/conveyorMechanics"; import ConveyorMechanics from "./mechanics/conveyorMechanics";
import VehicleMechanics from "./mechanics/vehicleMechanics"; import VehicleMechanics from "./mechanics/vehicleMechanics";
@ -11,7 +14,9 @@ const EventProperties: React.FC = () => {
const { selectedEventData } = useSelectedEventData(); const { selectedEventData } = useSelectedEventData();
const { getEventByModelUuid } = useProductStore(); const { getEventByModelUuid } = useProductStore();
const { selectedProduct } = useSelectedProduct(); const { selectedProduct } = useSelectedProduct();
const [currentEventData, setCurrentEventData] = useState<EventsSchema | null>(null); const [currentEventData, setCurrentEventData] = useState<EventsSchema | null>(
null
);
const [assetType, setAssetType] = useState<string | null>(null); const [assetType, setAssetType] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
@ -20,41 +25,53 @@ const EventProperties: React.FC = () => {
const type = determineAssetType(event); const type = determineAssetType(event);
setAssetType(type); setAssetType(type);
}, [selectedEventData, selectedProduct]); }, [selectedEventData, selectedProduct]);
const getCurrentEventData = () => { const getCurrentEventData = () => {
if (!selectedEventData?.data || !selectedProduct) return null; if (!selectedEventData?.data || !selectedProduct) return null;
return getEventByModelUuid(selectedProduct.productId, selectedEventData.data.modelUuid) || null; return (
getEventByModelUuid(
selectedProduct.productId,
selectedEventData.data.modelUuid
) ?? null
);
}; };
const determineAssetType = (event: EventsSchema | null) => { const determineAssetType = (event: EventsSchema | null) => {
if (!event) return null; if (!event) return null;
switch (event.type) { switch (event.type) {
case 'transfer': return 'conveyor'; case "transfer":
case 'vehicle': return 'vehicle'; return "conveyor";
case 'roboticArm': return 'roboticArm'; case "vehicle":
case 'machine': return 'machine'; return "vehicle";
case 'storageUnit': return 'storageUnit'; case "roboticArm":
default: return null; return "roboticArm";
case "machine":
return "machine";
case "storageUnit":
return "storageUnit";
default:
return null;
} }
}; };
return ( return (
<div className="event-proprties-wrapper"> <div className="event-proprties-wrapper">
{currentEventData && {currentEventData && (
<> <>
<div className="header"> <div className="header">
<div className="header-value">{selectedEventData?.data.modelName}</div> <div className="header-value">
{selectedEventData?.data.modelName}
</div> </div>
{assetType === 'conveyor' && <ConveyorMechanics />} </div>
{assetType === 'vehicle' && <VehicleMechanics />} {assetType === "conveyor" && <ConveyorMechanics />}
{assetType === 'roboticArm' && <RoboticArmMechanics />} {assetType === "vehicle" && <VehicleMechanics />}
{assetType === 'machine' && <MachineMechanics />} {assetType === "roboticArm" && <RoboticArmMechanics />}
{assetType === 'storageUnit' && <StorageMechanics />} {assetType === "machine" && <MachineMechanics />}
{assetType === "storageUnit" && <StorageMechanics />}
</> </>
} )}
</div> </div>
); );
}; };

View File

@ -9,9 +9,14 @@ interface DelayActionProps {
onChange: (value: string) => void; onChange: (value: string) => void;
} }
const DelayAction: React.FC<DelayActionProps> = ({ value, defaultValue, min, max, onChange }) => { const DelayAction: React.FC<DelayActionProps> = ({
value,
defaultValue,
min,
max,
onChange,
}) => {
return ( return (
<>
<InputWithDropDown <InputWithDropDown
label="Delay" label="Delay"
value={value} value={value}
@ -20,10 +25,9 @@ const DelayAction: React.FC<DelayActionProps> = ({ value, defaultValue, min, max
defaultValue={defaultValue} defaultValue={defaultValue}
max={max} max={max}
activeOption="s" activeOption="s"
onClick={() => { }} onClick={() => {}}
onChange={onChange} onChange={onChange}
/> />
</>
); );
}; };

View File

@ -1,6 +1,5 @@
import React from "react"; import React from "react";
import PreviewSelectionWithUpload from "../../../../../ui/inputs/PreviewSelectionWithUpload"; import PreviewSelectionWithUpload from "../../../../../ui/inputs/PreviewSelectionWithUpload";
import LabledDropdown from "../../../../../ui/inputs/LabledDropdown";
interface SwapActionProps { interface SwapActionProps {
onSelect: (option: string) => void; onSelect: (option: string) => void;
@ -8,19 +7,18 @@ interface SwapActionProps {
options: string[]; options: string[];
} }
const SwapAction: React.FC<SwapActionProps> = ({ onSelect, defaultOption, options }) => { const SwapAction: React.FC<SwapActionProps> = ({
onSelect,
defaultOption,
options,
}) => {
return ( return (
<> <PreviewSelectionWithUpload
{/* <PreviewSelectionWithUpload /> */}
<LabledDropdown
label="Presets" label="Presets"
defaultOption={defaultOption} defaultOption={defaultOption}
options={options} options={options}
onSelect={onSelect} onSelect={onSelect}
/> />
</>
); );
}; };

View File

@ -1,14 +1,32 @@
import React, { useState } from "react"; import React, { useState } from "react";
import LabledDropdown from "./LabledDropdown";
import { ArrowIcon } from "../../icons/ExportCommonIcons"; import { ArrowIcon } from "../../icons/ExportCommonIcons";
import LabledDropdown from "./LabledDropdown";
const PreviewSelectionWithUpload: React.FC = () => { interface PreviewSelectionWithUploadProps {
const [showPreview, setSetshowPreview] = useState(false); preview?: boolean;
upload?: boolean;
label?: string;
onSelect: (option: string) => void;
defaultOption: string;
options: string[];
}
const PreviewSelectionWithUpload: React.FC<PreviewSelectionWithUploadProps> = ({
preview = false,
upload = false,
onSelect,
label,
defaultOption,
options,
}) => {
const [showPreview, setShowPreview] = useState(false);
return ( return (
<div className="preview-selection-with-upload-wrapper"> <div className="preview-selection-with-upload-wrapper">
<div {preview && (
<>
<button
className="input-header-container" className="input-header-container"
onClick={() => setSetshowPreview(!showPreview)} onClick={() => setShowPreview(!showPreview)}
> >
<div className="input-header">Preview</div> <div className="input-header">Preview</div>
<div <div
@ -17,12 +35,15 @@ const PreviewSelectionWithUpload: React.FC = () => {
> >
<ArrowIcon /> <ArrowIcon />
</div> </div>
</div> </button>
{showPreview && ( {showPreview && (
<div className="canvas-wrapper"> <div className="canvas-wrapper">
<div className="canvas-container"></div> <div className="canvas-container"></div>
</div> </div>
)} )}
</>
)}
{upload && (
<div className="asset-selection-container"> <div className="asset-selection-container">
<div className="upload-custom-asset-button"> <div className="upload-custom-asset-button">
<div className="title">Upload Product</div> <div className="title">Upload Product</div>
@ -31,11 +52,21 @@ const PreviewSelectionWithUpload: React.FC = () => {
accept=".glb, .gltf" accept=".glb, .gltf"
id="simulation-product-upload" id="simulation-product-upload"
/> />
<label className="upload-button" htmlFor="simulation-product-upload"> <label
className="upload-button"
htmlFor="simulation-product-upload"
>
Upload here Upload here
</label> </label>
</div> </div>
</div> </div>
)}
<LabledDropdown
label={label}
defaultOption={defaultOption}
options={options}
onSelect={onSelect}
/>
</div> </div>
); );
}; };