2025-03-25 12:04:20 +00:00
import { toast } from 'react-toastify' ;
import * as THREE from 'three' ;
import * as Types from "../../../../types/world/worldTypes" ;
2025-04-14 12:46:53 +00:00
import * as SimulationTypes from "../../../../types/simulation" ;
2025-03-25 12:04:20 +00:00
// import { deleteFloorItem } from '../../../../services/factoryBuilder/assest/floorAsset/deleteFloorItemApi';
import { Socket } from 'socket.io-client' ;
2025-04-01 08:55:42 +00:00
import { getFloorAssets } from '../../../../services/factoryBuilder/assest/floorAsset/getFloorItemsApi' ;
2025-03-25 12:04:20 +00:00
async function DeleteFloorItems (
itemsGroup : Types.RefGroup ,
hoveredDeletableFloorItem : Types.RefMesh ,
setFloorItems : Types.setFloorItemSetState ,
2025-04-05 04:42:28 +00:00
setSimulationStates : any ,
2025-03-25 12:04:20 +00:00
socket : Socket < any >
) : Promise < void > {
////////// Deleting the hovered Floor GLTF from the scene (itemsGroup.current) and from the floorItems and also update it in the localstorage //////////
if ( hoveredDeletableFloorItem . current ) {
const email = localStorage . getItem ( 'email' )
const organization = ( email ! . split ( "@" ) [ 1 ] ) . split ( "." ) [ 0 ] ;
2025-04-01 08:55:42 +00:00
const items = await getFloorAssets ( organization ) ;
2025-03-25 12:04:20 +00:00
const removedItem = items . find (
( item : { modeluuid : string } ) = > item . modeluuid === hoveredDeletableFloorItem . current ? . uuid
) ;
if ( ! removedItem ) {
return
}
//REST
// const response = await deleteFloorItem(organization, removedItem.modeluuid, removedItem.modelname);
//SOCKET
const data = {
organization : organization ,
modeluuid : removedItem.modeluuid ,
modelname : removedItem.modelname ,
socketId : socket.id
}
2025-04-01 08:55:42 +00:00
const response = socket . emit ( 'v2:model-asset:delete' , data )
2025-03-25 12:04:20 +00:00
if ( response ) {
const updatedItems = items . filter (
( item : { modeluuid : string } ) = > item . modeluuid !== hoveredDeletableFloorItem . current ? . uuid
) ;
const storedItems = JSON . parse ( localStorage . getItem ( "FloorItems" ) || '[]' ) ;
const updatedStoredItems = storedItems . filter ( ( item : { modeluuid : string } ) = > item . modeluuid !== hoveredDeletableFloorItem . current ? . uuid ) ;
localStorage . setItem ( "FloorItems" , JSON . stringify ( updatedStoredItems ) ) ;
if ( hoveredDeletableFloorItem . current ) {
// Traverse and dispose of resources
hoveredDeletableFloorItem . current . traverse ( ( child : THREE.Object3D ) = > {
if ( child instanceof THREE . Mesh ) {
if ( child . geometry ) child . geometry . dispose ( ) ;
if ( Array . isArray ( child . material ) ) {
child . material . forEach ( ( material ) = > {
if ( material . map ) material . map . dispose ( ) ;
material . dispose ( ) ;
} ) ;
} else if ( child . material ) {
if ( child . material . map ) child . material . map . dispose ( ) ;
child . material . dispose ( ) ;
}
}
} ) ;
// Remove the object from the scene
itemsGroup . current . remove ( hoveredDeletableFloorItem . current ) ;
}
setFloorItems ( updatedItems ) ;
2025-04-03 14:16:52 +00:00
2025-04-14 12:46:53 +00:00
setSimulationStates ( ( prevEvents : ( SimulationTypes . ConveyorEventsSchema | SimulationTypes . VehicleEventsSchema | SimulationTypes . StaticMachineEventsSchema | SimulationTypes . ArmBotEventsSchema ) [ ] ) = > {
2025-04-03 14:16:52 +00:00
const updatedEvents = ( prevEvents || [ ] ) . filter ( event = > event . modeluuid !== removedItem . modeluuid ) ;
return updatedEvents ;
} ) ;
2025-03-25 12:04:20 +00:00
toast . success ( "Model Removed!" ) ;
}
}
}
export default DeleteFloorItems ;