This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 425
/
NotificationDisplay.stories.tsx
268 lines (258 loc) · 7.03 KB
/
NotificationDisplay.stories.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2018-2021 Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found at http://www.apache.org/licenses/LICENSE-2.0
// You may not use this file except in compliance with the License.
import { storiesOf } from "@storybook/react";
import moment from "moment";
import { useRef } from "react";
import NotificationDisplay, {
NotificationList,
NotificationModal,
NotificationMessage,
} from "@foxglove/studio-base/components/NotificationDisplay";
import sendNotification from "@foxglove/studio-base/util/sendNotification";
const randomNum = () => Math.floor(Math.random() * 1000);
const addError = () =>
sendNotification(`Another error #${randomNum()}`, "some details", "app", "error");
const addWarning = () =>
sendNotification(`Another warning #${randomNum()}`, "some details", "app", "warn");
const addInfo = () =>
sendNotification(`Another message #${randomNum()}`, "some details", "app", "info");
const NotificationDisplayWrapper = () => (
<div style={{ padding: 10 }}>
<div style={{ width: 300, height: 36 }}>
<NotificationDisplay />
</div>
<AddMoreButtons />
</div>
);
const AddMoreButtons = () => (
<div style={{ paddingTop: 20 }}>
<button onClick={addInfo}>add info</button>
<button onClick={addWarning}>add warning</button>
<button onClick={addError}>add error</button>
</div>
);
storiesOf("components/NotificationDisplay", module)
.addParameters({
chromatic: {
delay: 1000,
},
})
.add("No errors", () => {
return <NotificationDisplayWrapper />;
})
.add("With one error", () => {
class Wrapper extends React.Component<any> {
override componentDidMount() {
sendNotification(
"Something bad happened",
"This error is on purpose - it comes from the story",
"app",
"error",
);
}
override render() {
return <NotificationDisplayWrapper />;
}
}
return <Wrapper />;
})
.add("With one warning", () => {
class Wrapper extends React.Component<any> {
override componentDidMount() {
sendNotification(
"This is the final countdown",
"This warning is on purpose - it comes from the story",
"app",
"warn",
);
}
override render() {
return <NotificationDisplayWrapper />;
}
}
return <Wrapper />;
})
.add("With one message", () => {
class Wrapper extends React.Component<any> {
override componentDidMount() {
sendNotification(
"Here's a helpful tip",
"These are the details of the message",
"user",
"info",
);
}
override render() {
return <NotificationDisplayWrapper />;
}
}
return <Wrapper />;
})
.add("expanded with 4 messages", () => {
const el = useRef<HTMLDivElement>(ReactNull);
React.useLayoutEffect(() => {
sendNotification(
"Something bad happened 1",
"This error is on purpose - it comes from the story",
"app",
"error",
);
sendNotification(
"Something bad happened 2",
"This error is on purpose - it comes from the story",
"app",
"error",
);
sendNotification(
"Just a warning",
"This warning is on purpose - it comes from the story",
"app",
"warn",
);
sendNotification(
"Something bad happened 3",
"This error is on purpose - it comes from the story",
"app",
"error",
);
setImmediate(() => {
el.current?.querySelector<HTMLElement>(".icon")?.click();
});
}, []);
return (
<div style={{ padding: 10 }} ref={el}>
<NotificationDisplayWrapper />
</div>
);
})
.add("Error list", () => {
// make the container very short to test scrolling
const style = { width: 400, height: 150, margin: 20 };
const date = new Date();
const errors: NotificationMessage[] = [
{
id: "1",
message: "Error 1",
details: "Some error details",
read: true,
created: moment(date).subtract(307, "minutes").toDate(),
severity: "error",
},
{
id: "2",
message: "Some very long error message that should be truncated",
details: "Some error details",
read: true,
created: moment(date).subtract(31, "minutes").toDate(),
severity: "error",
},
{
id: "5",
message: "Foo foo baz",
details: "Some error details",
read: false,
created: moment(date).subtract(17, "minutes").toDate(),
severity: "error",
},
{
id: "4",
message: "Warn foo bar baz",
details: "Some warning details",
read: false,
created: moment(date).subtract(11, "minutes").toDate(),
severity: "warn",
},
{
id: "3",
message: "Some fake error",
details: "Foo bar baz this is a long-ish error details string",
read: false,
created: moment(date).subtract(3, "seconds").toDate(),
severity: "error",
},
];
return (
<div style={style}>
<NotificationList
notifications={errors}
onClick={() => {
// no-ops
}}
/>
</div>
);
})
.add("Error Modal", () => {
return (
<NotificationModal
onRequestClose={() => {}}
notification={{
id: "1",
message: "Error 1",
details: "Some error details",
read: false,
created: new Date(),
severity: "error",
}}
/>
);
})
.add("Warning Modal", () => {
return (
<NotificationModal
onRequestClose={() => {}}
notification={{
id: "1",
message: "Warning 1",
details: "Some error details",
read: false,
created: new Date(),
severity: "warn",
}}
/>
);
})
.add("Error Modal without details", () => {
return (
<NotificationModal
onRequestClose={() => {}}
notification={{
id: "1",
message: "Error 1",
details: undefined,
read: false,
created: new Date(),
severity: "error",
}}
/>
);
})
.add("Error Modal with details in React.Node type", () => {
return (
<NotificationModal
onRequestClose={() => {}}
notification={{
id: "1",
message: "Error 1",
details: (
<p>
This is <b style={{ color: "red" }}>customized</b> error detail.
</p>
),
read: false,
created: new Date(),
severity: "error",
}}
/>
);
});