150 lines
4.9 KiB
TypeScript
150 lines
4.9 KiB
TypeScript
import { create } from 'zustand';
|
|
import { immer } from 'zustand/middleware/immer';
|
|
|
|
interface StorageUnitStore {
|
|
storageUnits: StorageUnitStatus[];
|
|
|
|
addStorageUnit: (productId: string, storageUnit: StorageEventSchema) => void;
|
|
removeStorageUnit: (modelUuid: string) => void;
|
|
updateStorageUnit: (
|
|
modelUuid: string,
|
|
updates: Partial<Omit<StorageUnitStatus, 'modelUuid' | 'productId'>>
|
|
) => void;
|
|
clearStorageUnits: () => void;
|
|
|
|
setStorageUnitActive: (modelUuid: string, isActive: boolean) => void;
|
|
setStorageUnitState: (modelUuid: string, newState: StorageUnitStatus['state']) => void;
|
|
|
|
updateStorageUnitLoad: (modelUuid: string, incrementBy: number) => void;
|
|
|
|
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
|
|
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
|
|
|
|
getStorageUnitById: (modelUuid: string) => StorageUnitStatus | undefined;
|
|
getStorageUnitsByProduct: (productId: string) => StorageUnitStatus[];
|
|
getStorageUnitsBystate: (state: string) => StorageUnitStatus[];
|
|
getActiveStorageUnits: () => StorageUnitStatus[];
|
|
getIdleStorageUnits: () => StorageUnitStatus[];
|
|
getFullStorageUnits: () => StorageUnitStatus[];
|
|
getEmptyStorageUnits: () => StorageUnitStatus[];
|
|
}
|
|
|
|
export const useStorageUnitStore = create<StorageUnitStore>()(
|
|
immer((set, get) => ({
|
|
storageUnits: [],
|
|
|
|
addStorageUnit: (productId, storageUnit) => {
|
|
set((state) => {
|
|
const exists = state.storageUnits.some(s => s.modelUuid === storageUnit.modelUuid);
|
|
if (!exists) {
|
|
state.storageUnits.push({
|
|
...storageUnit,
|
|
productId,
|
|
isActive: false,
|
|
idleTime: 0,
|
|
activeTime: 0,
|
|
currentLoad: 0,
|
|
state: 'idle',
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
removeStorageUnit: (modelUuid) => {
|
|
set((state) => {
|
|
state.storageUnits = state.storageUnits.filter(s => s.modelUuid !== modelUuid);
|
|
});
|
|
},
|
|
|
|
updateStorageUnit: (modelUuid, updates) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
Object.assign(unit, updates);
|
|
}
|
|
});
|
|
},
|
|
|
|
clearStorageUnits: () => {
|
|
set(() => ({
|
|
storageUnits: [],
|
|
}));
|
|
},
|
|
|
|
setStorageUnitActive: (modelUuid, isActive) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
unit.isActive = isActive;
|
|
}
|
|
});
|
|
},
|
|
|
|
setStorageUnitState: (modelUuid, newState) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
unit.state = newState;
|
|
}
|
|
});
|
|
},
|
|
|
|
updateStorageUnitLoad: (modelUuid, incrementBy) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
unit.currentLoad += incrementBy;
|
|
}
|
|
});
|
|
},
|
|
|
|
incrementActiveTime: (modelUuid, incrementBy) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
unit.activeTime += incrementBy;
|
|
}
|
|
});
|
|
},
|
|
|
|
incrementIdleTime: (modelUuid, incrementBy) => {
|
|
set((state) => {
|
|
const unit = state.storageUnits.find(s => s.modelUuid === modelUuid);
|
|
if (unit) {
|
|
unit.idleTime += incrementBy;
|
|
}
|
|
});
|
|
},
|
|
|
|
getStorageUnitById: (modelUuid) => {
|
|
return get().storageUnits.find(s => s.modelUuid === modelUuid);
|
|
},
|
|
|
|
getStorageUnitsByProduct: (productId) => {
|
|
return get().storageUnits.filter(s => s.productId === productId);
|
|
},
|
|
|
|
getStorageUnitsBystate: (state) => {
|
|
return get().storageUnits.filter(s => s.state === state);
|
|
},
|
|
|
|
getActiveStorageUnits: () => {
|
|
return get().storageUnits.filter(s => s.isActive);
|
|
},
|
|
|
|
getIdleStorageUnits: () => {
|
|
return get().storageUnits.filter(s => !s.isActive && s.state === 'idle');
|
|
},
|
|
|
|
getFullStorageUnits: () => {
|
|
return get().storageUnits.filter(
|
|
s => s.currentLoad >= s.point.action.storageCapacity
|
|
);
|
|
},
|
|
|
|
getEmptyStorageUnits: () => {
|
|
return get().storageUnits.filter(s => s.currentLoad === 0);
|
|
},
|
|
}))
|
|
);
|