-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.tsx
152 lines (124 loc) 路 3.56 KB
/
index.tsx
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
import React from 'react';
import uuid from 'uuid/v4';
import Core from '@airbnb/lunar';
import withBoundary from '@airbnb/lunar/lib/composers/withBoundary';
import componentName from '@airbnb/lunar/lib/prop-types/componentName';
import Layout from './components/Layout';
import Toasts from './components/Toasts';
import AppContext from './components/AppContext';
import { Breadcrumb, ToastType, Toast } from './types';
export * from './types';
export type Props = {
/** Application to render. */
children: NonNullable<React.ReactNode>;
/** Name of the entire application or project. */
name: string;
};
export type State = {
breadcrumbs: Breadcrumb[];
data: { [id: string]: object };
toasts: Toast[];
};
export { AppContext };
export class AppShell extends React.Component<Props, State> {
static propTypes = {
name: componentName.isRequired,
};
static defaultProps = {
name: Core.settings.name,
};
state = {
breadcrumbs: [],
data: {},
toasts: [],
};
addBreadcrumb = (label: string, props: Breadcrumb['props'] = {}) => {
const id = uuid();
this.setState(prevState => ({
breadcrumbs: [
...prevState.breadcrumbs,
{
id,
label,
props,
},
],
}));
return id;
};
addPageData = (data: object, customID: string = '') => {
const id = customID || uuid();
this.setState(prevState => ({
data: {
...prevState.data,
[id]: data,
},
}));
return id;
};
addToast = (message: string | Error, type: ToastType, props: Toast['props'] = {}) => {
const id = props.id || uuid();
this.setState(prevState => ({
toasts: [
...prevState.toasts.filter(toast => toast.id !== id),
{
id,
message,
props: {
...props,
[type]: true,
},
},
],
}));
return id;
};
addRefreshToast = (message: string) => this.addToast(message, 'refresh', { duration: 0 });
addInfoToast = (message: string, props?: Toast['props']) => this.addToast(message, 'info', props);
addSuccessToast = (message: string, props?: Toast['props']) =>
this.addToast(message, 'success', props);
addFailureToast = (message: string | Error, props?: Toast['props']) =>
this.addToast(message, 'danger', props);
removeBreadcrumb = (id: string) => {
this.setState(prevState => ({
breadcrumbs: prevState.breadcrumbs.filter(crumb => crumb.id !== id),
}));
};
removePageData = (id: string) => {
this.setState(prevState => {
const data = { ...prevState.data };
delete data[id];
return { data };
});
};
removeToast = (id: string) => {
this.setState(prevState => ({
toasts: prevState.toasts.filter(toast => toast.id !== id),
}));
};
render() {
const { children } = this.props;
const context = {
addBreadcrumb: this.addBreadcrumb,
addPageData: this.addPageData,
addInfoToast: this.addInfoToast,
addFailureToast: this.addFailureToast,
addSuccessToast: this.addSuccessToast,
addRefreshToast: this.addRefreshToast,
breadcrumbs: this.state.breadcrumbs,
data: this.state.data,
name: this.props.name,
removeBreadcrumb: this.removeBreadcrumb,
removePageData: this.removePageData,
removeToast: this.removeToast,
toasts: this.state.toasts,
};
return (
<AppContext.Provider value={context}>
<Layout>{children}</Layout>
<Toasts />
</AppContext.Provider>
);
}
}
export default withBoundary('App')(AppShell);