-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathreducer.ts
141 lines (130 loc) · 4.26 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { type OverlayData, type OverlayItem } from './store';
export type OverlayReducerAction =
| { type: 'INIT' }
| { 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 {
if (state == null) {
if (action.type !== 'INIT') {
throw new Error('This component must be used inside a <OverlayProvider> component.');
}
return {
current: null,
overlayOrderList: [],
overlayData: {},
};
}
switch (action.type) {
case 'ADD': {
const isExisted = state.overlayOrderList.includes(action.overlay.id);
if (isExisted && state.overlayData[action.overlay.id].isOpen === true) {
throw new Error("You can't open the multiple overlays with the same overlayId. Please set a different id.");
}
return {
current: action.overlay.id,
/**
* @description Brings the overlay to the front when reopened after closing without unmounting.
*/
overlayOrderList: [...state.overlayOrderList.filter((item) => item !== action.overlay.id), action.overlay.id],
overlayData: isExisted
? state.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': {
const openedOverlayOrderList = state.overlayOrderList.filter(
(orderedOverlayId) => state.overlayData[orderedOverlayId].isOpen === true
);
const targetIndexInOpenedList = openedOverlayOrderList.findIndex((item) => item === action.overlayId);
/**
* @description If closing the last overlay, specify the overlay before it.
* @description If closing intermediate overlays, specifies the last overlay.
*
* @example open - [1, 2, 3, 4]
* close 2 => current: 4
* close 4 => current: 3
* close 3 => current: 1
* close 1 => current: null
*/
const currentOverlayId =
targetIndexInOpenedList === openedOverlayOrderList.length - 1
? openedOverlayOrderList[targetIndexInOpenedList - 1] ?? null
: openedOverlayOrderList.at(-1) ?? null;
return {
...state,
current: currentOverlayId,
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];
const current = state.current
? remainingOverlays.includes(state.current)
? /**
* @description If `unmount` was executed after `close`
*/
state.current
: /**
* @description If you only run `unmount`, there is no `current` in `remainingOverlays`
*/
remainingOverlays.at(-1) ?? null
: /**
* @description The case where `current` is `null`
*/
null;
return {
current,
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: {} };
}
}
}