-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathreducer.ts
81 lines (77 loc) · 2.17 KB
/
reducer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { type OverlayData, type OverlayItem } from './store';
export type OverlayReducerAction =
| { type: 'ADD'; overlay: OverlayItem }
| { type: 'OPEN'; overlayId: string }
| { type: 'CLOSE'; overlayId: string }
| { type: 'REMOVE'; overlayId: string }
| { type: 'CLOSE_ALL' }
| { type: 'REMOVE_ALL' };
export function overlayReducer(state: OverlayData, action: OverlayReducerAction): OverlayData {
switch (action.type) {
case 'ADD': {
return {
current: action.overlay.id,
overlayOrderList: [...state.overlayOrderList, action.overlay.id],
overlayData: {
...state.overlayData,
[action.overlay.id]: action.overlay,
},
};
}
case 'OPEN': {
return {
...state,
overlayData: {
...state.overlayData,
[action.overlayId]: {
...state.overlayData[action.overlayId],
isOpen: true,
},
},
};
}
case 'CLOSE': {
return {
...state,
overlayData: {
...state.overlayData,
[action.overlayId]: {
...state.overlayData[action.overlayId],
isOpen: false,
},
},
};
}
case 'REMOVE': {
const remainingOverlays = state.overlayOrderList.filter((item) => item !== action.overlayId);
if (state.overlayOrderList.length === remainingOverlays.length) {
return state;
}
const copiedOverlayData = { ...state.overlayData };
delete copiedOverlayData[action.overlayId];
return {
current: remainingOverlays.at(-1) ?? null,
overlayOrderList: remainingOverlays,
overlayData: copiedOverlayData,
};
}
case 'CLOSE_ALL': {
return {
...state,
overlayData: Object.keys(state.overlayData).reduce(
(prev, curr) => ({
...prev,
[curr]: {
...state.overlayData[curr],
isOpen: false,
} satisfies OverlayItem,
}),
{} satisfies Record<string, OverlayItem>
),
};
}
case 'REMOVE_ALL': {
return { current: null, overlayOrderList: [], overlayData: {} };
}
}
}