158 lines
4.9 KiB
TypeScript
158 lines
4.9 KiB
TypeScript
|
import { create } from 'zustand';
|
||
|
import { immer } from 'zustand/middleware/immer';
|
||
|
|
||
|
interface StorageUnitStatus extends StorageSchemaEvent {
|
||
|
productId: string;
|
||
|
isActive: boolean;
|
||
|
idleTime: number;
|
||
|
activeTime: number;
|
||
|
currentLoad: number;
|
||
|
}
|
||
|
|
||
|
interface StorageUnitStore {
|
||
|
storageUnits: Record<string, StorageUnitStatus>;
|
||
|
|
||
|
// Actions
|
||
|
addStorageUnit: (productId: string, storageUnit: StorageSchemaEvent) => void;
|
||
|
removeStorageUnit: (modelUuid: string) => void;
|
||
|
updateStorageUnit: (
|
||
|
modelUuid: string,
|
||
|
updates: Partial<Omit<StorageUnitStatus, 'modelUuid' | 'productId'>>
|
||
|
) => void;
|
||
|
|
||
|
// Status updates
|
||
|
setStorageUnitActive: (modelUuid: string, isActive: boolean) => void;
|
||
|
setStorageUnitState: (modelUuid: string, newState: StorageUnitStatus['state']) => void;
|
||
|
|
||
|
// Load updates
|
||
|
updateStorageUnitLoad: (modelUuid: string, load: number) => void;
|
||
|
|
||
|
// Time tracking
|
||
|
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
|
||
|
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
|
||
|
|
||
|
// Helpers
|
||
|
getStorageUnitById: (modelUuid: string) => StorageUnitStatus | undefined;
|
||
|
getStorageUnitsByProduct: (productId: string) => StorageUnitStatus[];
|
||
|
getActiveStorageUnits: () => StorageUnitStatus[];
|
||
|
getIdleStorageUnits: () => StorageUnitStatus[];
|
||
|
getFullStorageUnits: () => StorageUnitStatus[];
|
||
|
getEmptyStorageUnits: () => StorageUnitStatus[];
|
||
|
}
|
||
|
|
||
|
export const useStorageUnitStore = create<StorageUnitStore>()(
|
||
|
immer((set, get) => ({
|
||
|
storageUnits: {},
|
||
|
|
||
|
// Actions
|
||
|
addStorageUnit: (productId, storageUnit) => {
|
||
|
set((state) => {
|
||
|
state.storageUnits[storageUnit.modelUuid] = {
|
||
|
...storageUnit,
|
||
|
productId,
|
||
|
isActive: false,
|
||
|
idleTime: 0,
|
||
|
activeTime: 0,
|
||
|
currentLoad: 0, // Initialize currentLoad to 0
|
||
|
state: 'idle',
|
||
|
};
|
||
|
});
|
||
|
},
|
||
|
|
||
|
removeStorageUnit: (modelUuid) => {
|
||
|
set((state) => {
|
||
|
delete state.storageUnits[modelUuid];
|
||
|
});
|
||
|
},
|
||
|
|
||
|
updateStorageUnit: (modelUuid, updates) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
Object.assign(storageUnit, updates);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Status updates
|
||
|
setStorageUnitActive: (modelUuid, isActive) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
storageUnit.isActive = isActive;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setStorageUnitState: (modelUuid, newState) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
storageUnit.state = newState;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Load updates
|
||
|
updateStorageUnitLoad: (modelUuid, load) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
storageUnit.currentLoad = load;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Time tracking
|
||
|
incrementActiveTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
storageUnit.activeTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
incrementIdleTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const storageUnit = state.storageUnits[modelUuid];
|
||
|
if (storageUnit) {
|
||
|
storageUnit.idleTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Helpers
|
||
|
getStorageUnitById: (modelUuid) => {
|
||
|
return get().storageUnits[modelUuid];
|
||
|
},
|
||
|
|
||
|
getStorageUnitsByProduct: (productId) => {
|
||
|
return Object.values(get().storageUnits).filter(
|
||
|
s => s.productId === productId
|
||
|
);
|
||
|
},
|
||
|
|
||
|
getActiveStorageUnits: () => {
|
||
|
return Object.values(get().storageUnits).filter(s => s.isActive);
|
||
|
},
|
||
|
|
||
|
getIdleStorageUnits: () => {
|
||
|
return Object.values(get().storageUnits).filter(
|
||
|
s => !s.isActive && s.state === 'idle'
|
||
|
);
|
||
|
},
|
||
|
|
||
|
getFullStorageUnits: () => {
|
||
|
return Object.values(get().storageUnits).filter(
|
||
|
s => s.currentLoad >= s.point.action.storageCapacity
|
||
|
);
|
||
|
},
|
||
|
|
||
|
getEmptyStorageUnits: () => {
|
||
|
return Object.values(get().storageUnits).filter(
|
||
|
s => s.currentLoad === 0
|
||
|
);
|
||
|
},
|
||
|
}))
|
||
|
);
|