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";
|
import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore";
|
||||||
|
|
||||||
export function useDespawnHandler() {
|
export function useDespawnHandler() {
|
||||||
const { addMaterial, getMaterialById, setMaterial } = useMaterialStore();
|
const { addMaterial, getMaterialById, removeMaterial } = useMaterialStore();
|
||||||
|
|
||||||
const deSpawnLogStatus = (materialUuid: string, status: string) => {
|
const deSpawnLogStatus = (materialUuid: string, status: string) => {
|
||||||
console.log(`${materialUuid}, ${status}`);
|
console.log(`${materialUuid}, ${status}`);
|
||||||
|
@ -14,9 +14,11 @@ export function useDespawnHandler() {
|
||||||
const material = getMaterialById(materialId);
|
const material = getMaterialById(materialId);
|
||||||
if (!material) return;
|
if (!material) return;
|
||||||
|
|
||||||
|
removeMaterial(material.materialId);
|
||||||
|
|
||||||
deSpawnLogStatus(material.materialId, `Despawned`);
|
deSpawnLogStatus(material.materialId, `Despawned`);
|
||||||
|
|
||||||
}, [addMaterial, getMaterialById, setMaterial]);
|
}, [addMaterial, getMaterialById, removeMaterial]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handleDespawn,
|
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 = () => {
|
const callTrigger = () => {
|
||||||
if (!material.next) return;
|
if (!material.next) return;
|
||||||
const fromModel = getEventByModelUuid(selectedProduct.productId, material.next.modelUuid);
|
const fromModel = getEventByModelUuid(selectedProduct.productId, material.next.modelUuid);
|
||||||
|
|
|
@ -3,12 +3,16 @@ import MaterialInstance from './instance/materialInstance'
|
||||||
import { useMaterialStore } from '../../../../store/simulation/useMaterialStore';
|
import { useMaterialStore } from '../../../../store/simulation/useMaterialStore';
|
||||||
|
|
||||||
function MaterialInstances() {
|
function MaterialInstances() {
|
||||||
const { materials } = useMaterialStore();
|
const { materials, materialHistory } = useMaterialStore();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// console.log('materials: ', materials);
|
// console.log('materials: ', materials);
|
||||||
}, [materials])
|
}, [materials])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// console.log('materialHistory: ', materialHistory);
|
||||||
|
}, [materialHistory])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { immer } from 'zustand/middleware/immer';
|
||||||
|
|
||||||
type MaterialsStore = {
|
type MaterialsStore = {
|
||||||
materials: MaterialsSchema;
|
materials: MaterialsSchema;
|
||||||
|
materialHistory: MaterialHistorySchema;
|
||||||
|
|
||||||
addMaterial: (material: MaterialSchema) => MaterialSchema | undefined;
|
addMaterial: (material: MaterialSchema) => MaterialSchema | undefined;
|
||||||
removeMaterial: (materialId: string) => MaterialSchema | undefined;
|
removeMaterial: (materialId: string) => MaterialSchema | undefined;
|
||||||
|
@ -41,11 +42,13 @@ type MaterialsStore = {
|
||||||
getMaterialByCurrentPointUuid: (currentPointUuid: string) => MaterialSchema | undefined;
|
getMaterialByCurrentPointUuid: (currentPointUuid: string) => MaterialSchema | undefined;
|
||||||
getMaterialsByPoint: (pointUuid: string) => MaterialSchema[];
|
getMaterialsByPoint: (pointUuid: string) => MaterialSchema[];
|
||||||
getMaterialsByModel: (modelUuid: string) => MaterialSchema[];
|
getMaterialsByModel: (modelUuid: string) => MaterialSchema[];
|
||||||
|
getMaterialHistory: () => MaterialHistorySchema;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useMaterialStore = create<MaterialsStore>()(
|
export const useMaterialStore = create<MaterialsStore>()(
|
||||||
immer((set, get) => ({
|
immer((set, get) => ({
|
||||||
materials: [],
|
materials: [],
|
||||||
|
materialHistory: [],
|
||||||
|
|
||||||
addMaterial: (material) => {
|
addMaterial: (material) => {
|
||||||
let updatedMaterial: MaterialSchema | undefined;
|
let updatedMaterial: MaterialSchema | undefined;
|
||||||
|
@ -60,8 +63,13 @@ export const useMaterialStore = create<MaterialsStore>()(
|
||||||
set((state) => {
|
set((state) => {
|
||||||
const material = state.materials.find(m => m.materialId === materialId);
|
const material = state.materials.find(m => m.materialId === materialId);
|
||||||
if (material) {
|
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));
|
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||||
|
|
||||||
|
state.materialHistory.push({
|
||||||
|
material,
|
||||||
|
removedAt: new Date().toISOString()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return updatedMaterial;
|
return updatedMaterial;
|
||||||
|
@ -242,5 +250,9 @@ export const useMaterialStore = create<MaterialsStore>()(
|
||||||
m.next?.modelUuid === modelUuid
|
m.next?.modelUuid === modelUuid
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getMaterialHistory: () => {
|
||||||
|
return get().materialHistory;
|
||||||
|
},
|
||||||
}))
|
}))
|
||||||
);
|
);
|
|
@ -220,3 +220,10 @@ interface MaterialSchema {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MaterialsSchema = MaterialSchema[];
|
type MaterialsSchema = MaterialSchema[];
|
||||||
|
|
||||||
|
interface MaterialHistoryEntry {
|
||||||
|
material: MaterialSchema;
|
||||||
|
removedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type MaterialHistorySchema = MaterialHistoryEntry[];
|
Loading…
Reference in New Issue