2025-03-25 17:34:20 +05:30
import { toast } from 'react-toastify' ;
import * as THREE from 'three' ;
import * as Types from "../../../../types/world/worldTypes" ;
import { getFloorItems } from '../../../../services/factoryBuilder/assest/floorAsset/getFloorItemsApi' ;
// import { deleteFloorItem } from '../../../../services/factoryBuilder/assest/floorAsset/deleteFloorItemApi';
import { Socket } from 'socket.io-client' ;
async function DeleteFloorItems (
itemsGroup : Types.RefGroup ,
hoveredDeletableFloorItem : Types.RefMesh ,
setFloorItems : Types.setFloorItemSetState ,
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 ] ;
const items = await getFloorItems ( organization ) ;
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
}
const response = socket . emit ( 'v1:FloorItems:delete' , data )
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 ) ;
toast . success ( "Model Removed!" ) ;
}
}
}
export default DeleteFloorItems ;