Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/state/flashes.ts
7458 views
1
import { Action, action } from 'easy-peasy';
2
import { FlashMessageType } from '@/components/MessageBox';
3
import { httpErrorToHuman } from '@/api/http';
4
5
export interface FlashStore {
6
items: FlashMessage[];
7
addFlash: Action<FlashStore, FlashMessage>;
8
addError: Action<FlashStore, { message: string; key?: string }>;
9
clearAndAddHttpError: Action<FlashStore, { error?: Error | any | null; key?: string }>;
10
clearFlashes: Action<FlashStore, string | void>;
11
}
12
13
export interface FlashMessage {
14
id?: string;
15
key?: string;
16
type: FlashMessageType;
17
title?: string;
18
message: string;
19
}
20
21
const flashes: FlashStore = {
22
items: [],
23
24
addFlash: action((state, payload) => {
25
state.items.push(payload);
26
}),
27
28
addError: action((state, payload) => {
29
state.items.push({ type: 'error', title: 'Error', ...payload });
30
}),
31
32
clearAndAddHttpError: action((state, payload) => {
33
if (!payload.error) {
34
state.items = [];
35
} else {
36
console.error(payload.error);
37
38
state.items = [
39
{
40
type: 'error',
41
title: 'Error',
42
key: payload.key,
43
message: httpErrorToHuman(payload.error),
44
},
45
];
46
}
47
}),
48
49
clearFlashes: action((state, payload) => {
50
state.items = payload ? state.items.filter((flashes) => flashes.key !== payload) : [];
51
}),
52
};
53
54
export default flashes;
55
56