Enhance material management: add material history tracking and update despawn handler
This commit is contained in:
parent
9383296684
commit
149dcf0765
|
@ -2,7 +2,7 @@ import { useCallback } from "react";
|
|||
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
||||
|
||||
export function useDespawnHandler() {
|
||||
const { addMaterial, getMaterialById, setMaterial } = useMaterialStore();
|
||||
const { addMaterial, getMaterialById, removeMaterial } = useMaterialStore();
|
||||
|
||||
const deSpawnLogStatus = (materialUuid: string, status: string) => {
|
||||
console.log(`${materialUuid}, ${status}`);
|
||||
|
@ -14,9 +14,11 @@ export function useDespawnHandler() {
|
|||
const material = getMaterialById(materialId);
|
||||
if (!material) return;
|
||||
|
||||
removeMaterial(material.materialId);
|
||||
|
||||
deSpawnLogStatus(material.materialId, `Despawned`);
|
||||
|
||||
}, [addMaterial, getMaterialById, setMaterial]);
|
||||
}, [addMaterial, getMaterialById, removeMaterial]);
|
||||
|
||||
return {
|
||||
handleDespawn,
|
||||
|
|
|
@ -83,13 +83,6 @@ function MaterialInstance({ material }: { material: MaterialSchema }) {
|
|||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (material.current && material.next) {
|
||||
// console.log('current: ', material.current.pointUuid);
|
||||
// console.log('next: ', material.next.pointUuid);
|
||||
}
|
||||
}, [material])
|
||||
|
||||
const callTrigger = () => {
|
||||
if (!material.next) return;
|
||||
const fromModel = getEventByModelUuid(selectedProduct.productId, material.next.modelUuid);
|
||||
|
|
|
@ -3,12 +3,16 @@ import MaterialInstance from './instance/materialInstance'
|
|||
import { useMaterialStore } from '../../../../store/simulation/useMaterialStore';
|
||||
|
||||
function MaterialInstances() {
|
||||
const { materials } = useMaterialStore();
|
||||
const { materials, materialHistory } = useMaterialStore();
|
||||
|
||||
useEffect(() => {
|
||||
// console.log('materials: ', materials);
|
||||
}, [materials])
|
||||
|
||||
useEffect(() => {
|
||||
// console.log('materialHistory: ', materialHistory);
|
||||
}, [materialHistory])
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import { immer } from 'zustand/middleware/immer';
|
|||
|
||||
type MaterialsStore = {
|
||||
materials: MaterialsSchema;
|
||||
materialHistory: MaterialHistorySchema;
|
||||
|
||||
addMaterial: (material: MaterialSchema) => MaterialSchema | undefined;
|
||||
removeMaterial: (materialId: string) => MaterialSchema | undefined;
|
||||
|
@ -41,11 +42,13 @@ type MaterialsStore = {
|
|||
getMaterialByCurrentPointUuid: (currentPointUuid: string) => MaterialSchema | undefined;
|
||||
getMaterialsByPoint: (pointUuid: string) => MaterialSchema[];
|
||||
getMaterialsByModel: (modelUuid: string) => MaterialSchema[];
|
||||
getMaterialHistory: () => MaterialHistorySchema;
|
||||
};
|
||||
|
||||
export const useMaterialStore = create<MaterialsStore>()(
|
||||
immer((set, get) => ({
|
||||
materials: [],
|
||||
materialHistory: [],
|
||||
|
||||
addMaterial: (material) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
|
@ -60,8 +63,13 @@ export const useMaterialStore = create<MaterialsStore>()(
|
|||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
state.materials.filter(m => m.materialId !== material.materialId);
|
||||
state.materials = state.materials.filter(m => m.materialId !== materialId);
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
|
||||
state.materialHistory.push({
|
||||
material,
|
||||
removedAt: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
});
|
||||
return updatedMaterial;
|
||||
|
@ -242,5 +250,9 @@ export const useMaterialStore = create<MaterialsStore>()(
|
|||
m.next?.modelUuid === modelUuid
|
||||
);
|
||||
},
|
||||
|
||||
getMaterialHistory: () => {
|
||||
return get().materialHistory;
|
||||
},
|
||||
}))
|
||||
);
|
|
@ -219,4 +219,11 @@ interface MaterialSchema {
|
|||
} | null;
|
||||
}
|
||||
|
||||
type MaterialsSchema = MaterialSchema[];
|
||||
type MaterialsSchema = MaterialSchema[];
|
||||
|
||||
interface MaterialHistoryEntry {
|
||||
material: MaterialSchema;
|
||||
removedAt: string;
|
||||
}
|
||||
|
||||
type MaterialHistorySchema = MaterialHistoryEntry[];
|
Loading…
Reference in New Issue