-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperspectiveSlice.ts
53 lines (45 loc) · 1.42 KB
/
perspectiveSlice.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
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { nanoid } from "nanoid";
export type Panel = {
label: string;
id: string;
};
export interface Perspective {
panels: Panel[];
}
export const initialState: Perspective = {
panels: []
};
export const perspectiveSlice = createSlice({
name: "perspective",
initialState,
reducers: {
openPanel: (state, action: PayloadAction<string | undefined>) => {
const leftPanelId = action.payload;
const id = nanoid(3);
const panel = { label: "Panel-" + id, id };
if (!leftPanelId) {
state.panels = state.panels.concat(panel);
} else {
// split
const leftPanelIndex = state.panels.findIndex(
(p) => p.id === leftPanelId
);
const head = state.panels.slice(0, leftPanelIndex + 1);
const tail = state.panels.slice(leftPanelIndex + 1);
state.panels = head.concat([panel]).concat(tail);
}
},
closePanel: (state, action: PayloadAction<string>) => {
const newPanels = state.panels.filter((_p) => _p.id !== action.payload);
if (newPanels.length === 0) {
state.panels = [];
} else {
state.panels = newPanels;
}
}
}
});
export const { openPanel, closePanel } = perspectiveSlice.actions;
const PerspectiveReducer = perspectiveSlice.reducer;
export default PerspectiveReducer;