/
actionButtonsSlice.ts
157 lines (139 loc) · 4.4 KB
/
actionButtonsSlice.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { get, set } from 'lodash';
import { ReactElement, ReactNode } from 'react';
import { KubeObject } from '../lib/k8s/cluster';
export type HeaderActionType =
| ((...args: any[]) => JSX.Element | null | ReactNode)
| null
| ReactElement
| ReactNode;
export type DetailsViewFunc = HeaderActionType;
export type AppBarActionType =
| ((...args: any[]) => JSX.Element | null | ReactNode)
| null
| ReactElement
| ReactNode;
export type HeaderAction = {
id: string;
action?: HeaderActionType;
};
export type AppBarAction = {
id: string;
action?: AppBarActionType;
};
export enum DefaultHeaderAction {
RESTART = 'RESTART',
DELETE = 'DELETE',
EDIT = 'EDIT',
SCALE = 'SCALE',
POD_LOGS = 'POD_LOGS',
POD_TERMINAL = 'POD_TERMINAL',
POD_ATTACH = 'POD_ATTACH',
NODE_TOGGLE_CORDON = 'NODE_TOGGLE_CORDON',
NODE_DRAIN = 'NODE_DRAIN',
}
export enum DefaultAppBarAction {
CLUSTER = 'CLUSTER',
NOTIFICATION = 'NOTIFICATION',
SETTINGS = 'SETTINGS',
USER = 'USER',
}
type HeaderActionFuncType = (
resource: KubeObject | null,
actions: HeaderAction[]
) => HeaderAction[];
export type HeaderActionsProcessor = {
id: string;
processor: HeaderActionFuncType;
};
export type AppBarActionsProcessorArgs = { actions: AppBarAction[] };
export type AppBarActionProcessorType = (info: AppBarActionsProcessorArgs) => AppBarAction[];
export type AppBarActionsProcessor = {
id: string;
processor: AppBarActionProcessorType;
};
export interface HeaderActionState {
headerActions: HeaderAction[];
headerActionsProcessors: HeaderActionsProcessor[];
appBarActions: AppBarAction[];
appBarActionsProcessors: AppBarActionsProcessor[];
}
const initialState: HeaderActionState = {
headerActions: [],
headerActionsProcessors: [],
appBarActions: [],
appBarActionsProcessors: [],
};
/**
* Normalizes a header actions processor by ensuring it has an 'id' and a processor function.
*
* If the processor is passed as a function, it will be wrapped in an object with a generated ID.
*
* @param action - The payload action containing the header actions processor.
* @returns The normalized header actions processor.
*/
function _normalizeProcessor<Processor, ProcessorProcessor>(
action: PayloadAction<Processor | ProcessorProcessor>
) {
let headerActionsProcessor: Processor = action.payload as Processor;
if (
get(headerActionsProcessor, 'id') === undefined &&
typeof headerActionsProcessor === 'function'
) {
const headerActionsProcessor2: unknown = {
id: '',
processor: headerActionsProcessor,
};
headerActionsProcessor = headerActionsProcessor2 as Processor;
}
set(
headerActionsProcessor as Object,
'id',
get(headerActionsProcessor, 'id') || `generated-id-${Date.now().toString(36)}`
);
return headerActionsProcessor;
}
export const actionButtonsSlice = createSlice({
name: 'actionButtons',
initialState,
reducers: {
setDetailsViewHeaderAction(state, action: PayloadAction<HeaderActionType | HeaderAction>) {
let headerAction = action.payload as HeaderAction;
if (headerAction.id === undefined) {
if (headerAction.action === undefined) {
headerAction = { id: '', action: headerAction };
} else {
headerAction = { id: '', action: headerAction.action };
}
}
headerAction.id = headerAction.id || `generated-id-${Date.now().toString(36)}`;
state.headerActions.push(headerAction);
},
addDetailsViewHeaderActionsProcessor(
state,
action: PayloadAction<HeaderActionsProcessor | HeaderActionsProcessor['processor']>
) {
state.headerActionsProcessors.push(
_normalizeProcessor<HeaderActionsProcessor, HeaderActionsProcessor['processor']>(action)
);
},
setAppBarAction(state, action: PayloadAction<AppBarAction | AppBarAction>) {
state.appBarActions.push(action.payload);
},
setAppBarActionsProcessor(
state,
action: PayloadAction<AppBarActionsProcessor | AppBarActionsProcessor['processor']>
) {
state.appBarActionsProcessors.push(
_normalizeProcessor<AppBarActionsProcessor, AppBarActionsProcessor['processor']>(action)
);
},
},
});
export const {
setDetailsViewHeaderAction,
addDetailsViewHeaderActionsProcessor,
setAppBarAction,
setAppBarActionsProcessor,
} = actionButtonsSlice.actions;
export default actionButtonsSlice.reducer;