stream-chat-react 12.0.0-rc.7 → 12.0.0-rc.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Channel/Channel.js +19 -32
- package/dist/components/ChannelList/ChannelList.js +1 -1
- package/dist/components/Chat/hooks/useCreateChatClient.d.ts +4 -2
- package/dist/components/Chat/hooks/useCreateChatClient.js +5 -4
- package/dist/components/Message/MessageStatus.js +3 -2
- package/dist/components/Message/renderText/rehypePlugins/mentionsMarkdownPlugin.d.ts +1 -1
- package/dist/components/Message/renderText/remarkPlugins/htmlToTextPlugin.d.ts +1 -1
- package/dist/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.d.ts +1 -1
- package/dist/components/Message/renderText/remarkPlugins/keepLineBreaksPlugin.js +1 -5
- package/dist/components/Message/renderText/renderText.d.ts +2 -2
- package/dist/components/Message/renderText/renderText.js +8 -6
- package/dist/components/UtilityComponents/ErrorBoundary.d.ts +16 -0
- package/dist/components/UtilityComponents/ErrorBoundary.js +19 -0
- package/dist/components/UtilityComponents/index.d.ts +1 -0
- package/dist/components/UtilityComponents/index.js +1 -0
- package/dist/{index.cjs.js → index.browser.cjs} +10176 -10966
- package/dist/{index.cjs.js.map → index.browser.cjs.map} +4 -4
- package/dist/index.node.cjs +52570 -0
- package/dist/index.node.cjs.map +7 -0
- package/dist/plugins/Emojis/{index.cjs.js → index.browser.cjs} +1 -1
- package/dist/plugins/Emojis/index.node.cjs +334 -0
- package/dist/plugins/Emojis/index.node.cjs.map +7 -0
- package/dist/plugins/encoders/{mp3.cjs.js → mp3.browser.cjs} +1 -1
- package/dist/plugins/encoders/mp3.node.cjs +111 -0
- package/dist/plugins/encoders/mp3.node.cjs.map +7 -0
- package/package.json +30 -13
- /package/dist/plugins/Emojis/{index.cjs.js.map → index.browser.cjs.map} +0 -0
- /package/dist/plugins/encoders/{mp3.cjs.js.map → mp3.browser.cjs.map} +0 -0
|
@@ -68,7 +68,11 @@ const ChannelInner = (props) => {
|
|
|
68
68
|
const [state, dispatch] = useReducer(channelReducer,
|
|
69
69
|
// channel.initialized === false if client.channel().query() was not called, e.g. ChannelList is not used
|
|
70
70
|
// => Channel will call channel.watch() in useLayoutEffect => state.loading is used to signal the watch() call state
|
|
71
|
-
{
|
|
71
|
+
{
|
|
72
|
+
...initialState,
|
|
73
|
+
hasMore: channel.state.messagePagination.hasPrev,
|
|
74
|
+
loading: !channel.initialized,
|
|
75
|
+
});
|
|
72
76
|
const isMounted = useIsMounted();
|
|
73
77
|
const originalTitle = useRef('');
|
|
74
78
|
const lastRead = useRef();
|
|
@@ -184,7 +188,6 @@ const ChannelInner = (props) => {
|
|
|
184
188
|
useLayoutEffect(() => {
|
|
185
189
|
let errored = false;
|
|
186
190
|
let done = false;
|
|
187
|
-
let channelInitializedExternally = true;
|
|
188
191
|
(async () => {
|
|
189
192
|
if (!channel.initialized && initializeOnMount) {
|
|
190
193
|
try {
|
|
@@ -210,7 +213,6 @@ const ChannelInner = (props) => {
|
|
|
210
213
|
await getChannel({ channel, client, members, options: channelQueryOptions });
|
|
211
214
|
const config = channel.getConfig();
|
|
212
215
|
setChannelConfig(config);
|
|
213
|
-
channelInitializedExternally = false;
|
|
214
216
|
}
|
|
215
217
|
catch (e) {
|
|
216
218
|
dispatch({ error: e, type: 'setError' });
|
|
@@ -222,8 +224,7 @@ const ChannelInner = (props) => {
|
|
|
222
224
|
if (!errored) {
|
|
223
225
|
dispatch({
|
|
224
226
|
channel,
|
|
225
|
-
hasMore:
|
|
226
|
-
hasMoreMessagesProbably(channel.state.messages.length, channelQueryOptions.messages.limit),
|
|
227
|
+
hasMore: channel.state.messagePagination.hasPrev,
|
|
227
228
|
type: 'initStateFromChannel',
|
|
228
229
|
});
|
|
229
230
|
if (client.user?.id && channel.state.read[client.user.id]) {
|
|
@@ -283,7 +284,7 @@ const ChannelInner = (props) => {
|
|
|
283
284
|
dispatch({ hasMore, messages, type: 'loadMoreFinished' });
|
|
284
285
|
}, 2000, { leading: true, trailing: true }), []);
|
|
285
286
|
const loadMore = async (limit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE) => {
|
|
286
|
-
if (!online.current || !window.navigator.onLine || !state.
|
|
287
|
+
if (!online.current || !window.navigator.onLine || !channel.state.messagePagination.hasPrev)
|
|
287
288
|
return 0;
|
|
288
289
|
// prevent duplicate loading events...
|
|
289
290
|
const oldestMessage = state?.messages?.[0];
|
|
@@ -305,12 +306,11 @@ const ChannelInner = (props) => {
|
|
|
305
306
|
dispatch({ loadingMore: false, type: 'setLoadingMore' });
|
|
306
307
|
return 0;
|
|
307
308
|
}
|
|
308
|
-
|
|
309
|
-
loadMoreFinished(hasMoreMessages, channel.state.messages);
|
|
309
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
310
310
|
return queryResponse.messages.length;
|
|
311
311
|
};
|
|
312
312
|
const loadMoreNewer = async (limit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE) => {
|
|
313
|
-
if (!online.current || !window.navigator.onLine || !state.
|
|
313
|
+
if (!online.current || !window.navigator.onLine || !channel.state.messagePagination.hasNext)
|
|
314
314
|
return 0;
|
|
315
315
|
const newestMessage = state?.messages?.[state?.messages?.length - 1];
|
|
316
316
|
if (state.loadingMore || state.loadingMoreNewer)
|
|
@@ -330,9 +330,8 @@ const ChannelInner = (props) => {
|
|
|
330
330
|
dispatch({ loadingMoreNewer: false, type: 'setLoadingMoreNewer' });
|
|
331
331
|
return 0;
|
|
332
332
|
}
|
|
333
|
-
const hasMoreNewerMessages = channel.state.messages !== channel.state.latestMessages;
|
|
334
333
|
dispatch({
|
|
335
|
-
hasMoreNewer:
|
|
334
|
+
hasMoreNewer: channel.state.messagePagination.hasNext,
|
|
336
335
|
messages: channel.state.messages,
|
|
337
336
|
type: 'loadMoreNewerFinished',
|
|
338
337
|
});
|
|
@@ -342,15 +341,9 @@ const ChannelInner = (props) => {
|
|
|
342
341
|
const jumpToMessage = useCallback(async (messageId, messageLimit = DEFAULT_JUMP_TO_PAGE_SIZE, highlightDuration = DEFAULT_HIGHLIGHT_DURATION) => {
|
|
343
342
|
dispatch({ loadingMore: true, type: 'setLoadingMore' });
|
|
344
343
|
await channel.state.loadMessageIntoState(messageId, undefined, messageLimit);
|
|
345
|
-
|
|
346
|
-
* if the message we are jumping to has less than half of the page size older messages,
|
|
347
|
-
* we have jumped to the beginning of the channel.
|
|
348
|
-
*/
|
|
349
|
-
const indexOfMessage = channel.state.messages.findIndex((message) => message.id === messageId);
|
|
350
|
-
const hasMoreMessages = indexOfMessage >= Math.floor(messageLimit / 2);
|
|
351
|
-
loadMoreFinished(hasMoreMessages, channel.state.messages);
|
|
344
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
352
345
|
dispatch({
|
|
353
|
-
hasMoreNewer: channel.state.
|
|
346
|
+
hasMoreNewer: channel.state.messagePagination.hasNext,
|
|
354
347
|
highlightedMessageId: messageId,
|
|
355
348
|
type: 'jumpToMessageFinished',
|
|
356
349
|
});
|
|
@@ -364,9 +357,7 @@ const ChannelInner = (props) => {
|
|
|
364
357
|
}, [channel, loadMoreFinished]);
|
|
365
358
|
const jumpToLatestMessage = useCallback(async () => {
|
|
366
359
|
await channel.state.loadMessageIntoState('latest');
|
|
367
|
-
|
|
368
|
-
const hasMoreOlder = channel.state.messages.length >= 25;
|
|
369
|
-
loadMoreFinished(hasMoreOlder, channel.state.messages);
|
|
360
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
370
361
|
dispatch({
|
|
371
362
|
type: 'jumpToLatestMessage',
|
|
372
363
|
});
|
|
@@ -377,7 +368,6 @@ const ChannelInner = (props) => {
|
|
|
377
368
|
let lastReadMessageId = channelUnreadUiState?.last_read_message_id;
|
|
378
369
|
let firstUnreadMessageId = channelUnreadUiState?.first_unread_message_id;
|
|
379
370
|
let isInCurrentMessageSet = false;
|
|
380
|
-
let hasMoreMessages = true;
|
|
381
371
|
if (firstUnreadMessageId) {
|
|
382
372
|
const result = findInMsgSetById(firstUnreadMessageId, channel.state.messages);
|
|
383
373
|
isInCurrentMessageSet = result.index !== -1;
|
|
@@ -409,27 +399,25 @@ const ChannelInner = (props) => {
|
|
|
409
399
|
}
|
|
410
400
|
catch (e) {
|
|
411
401
|
addNotification(t('Failed to jump to the first unread message'), 'error');
|
|
412
|
-
loadMoreFinished(
|
|
402
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
413
403
|
return;
|
|
414
404
|
}
|
|
415
405
|
const firstMessageWithCreationDate = messages.find((msg) => msg.created_at);
|
|
416
406
|
if (!firstMessageWithCreationDate) {
|
|
417
407
|
addNotification(t('Failed to jump to the first unread message'), 'error');
|
|
418
|
-
loadMoreFinished(
|
|
408
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
419
409
|
return;
|
|
420
410
|
}
|
|
421
411
|
const firstMessageTimestamp = new Date(firstMessageWithCreationDate.created_at).getTime();
|
|
422
412
|
if (lastReadTimestamp < firstMessageTimestamp) {
|
|
423
413
|
// whole channel is unread
|
|
424
414
|
firstUnreadMessageId = firstMessageWithCreationDate.id;
|
|
425
|
-
hasMoreMessages = false;
|
|
426
415
|
}
|
|
427
416
|
else {
|
|
428
417
|
const result = findInMsgSetByDate(channelUnreadUiState.last_read, messages);
|
|
429
418
|
lastReadMessageId = result.target?.id;
|
|
430
|
-
hasMoreMessages = result.index >= Math.floor(queryMessageLimit / 2);
|
|
431
419
|
}
|
|
432
|
-
loadMoreFinished(
|
|
420
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
433
421
|
}
|
|
434
422
|
}
|
|
435
423
|
if (!firstUnreadMessageId && !lastReadMessageId) {
|
|
@@ -446,14 +434,13 @@ const ChannelInner = (props) => {
|
|
|
446
434
|
* we have arrived to the oldest page of the channel
|
|
447
435
|
*/
|
|
448
436
|
const indexOfTarget = channel.state.messages.findIndex((message) => message.id === targetId);
|
|
449
|
-
|
|
450
|
-
loadMoreFinished(hasMoreMessages, channel.state.messages);
|
|
437
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
451
438
|
firstUnreadMessageId =
|
|
452
439
|
firstUnreadMessageId ?? channel.state.messages[indexOfTarget + 1]?.id;
|
|
453
440
|
}
|
|
454
441
|
catch (e) {
|
|
455
442
|
addNotification(t('Failed to jump to the first unread message'), 'error');
|
|
456
|
-
loadMoreFinished(
|
|
443
|
+
loadMoreFinished(channel.state.messagePagination.hasPrev, channel.state.messages);
|
|
457
444
|
return;
|
|
458
445
|
}
|
|
459
446
|
}
|
|
@@ -468,7 +455,7 @@ const ChannelInner = (props) => {
|
|
|
468
455
|
last_read_message_id: lastReadMessageId,
|
|
469
456
|
});
|
|
470
457
|
dispatch({
|
|
471
|
-
hasMoreNewer: channel.state.
|
|
458
|
+
hasMoreNewer: channel.state.messagePagination.hasNext,
|
|
472
459
|
highlightedMessageId: firstUnreadMessageId,
|
|
473
460
|
type: 'jumpToMessageFinished',
|
|
474
461
|
});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StreamChat } from 'stream-chat';
|
|
2
|
+
import type { DefaultGenerics, ExtendableGenerics, OwnUserResponse, StreamChatOptions, TokenOrProvider, UserResponse } from 'stream-chat';
|
|
2
3
|
/**
|
|
3
4
|
* React hook to create, connect and return `StreamChat` client.
|
|
4
5
|
*/
|
|
5
|
-
export declare const useCreateChatClient: <SCG extends ExtendableGenerics = DefaultGenerics>({ apiKey, tokenOrProvider, userData, }: {
|
|
6
|
+
export declare const useCreateChatClient: <SCG extends ExtendableGenerics = DefaultGenerics>({ apiKey, options, tokenOrProvider, userData, }: {
|
|
6
7
|
apiKey: string;
|
|
7
8
|
tokenOrProvider: TokenOrProvider;
|
|
8
9
|
userData: OwnUserResponse<SCG> | UserResponse<SCG>;
|
|
10
|
+
options?: StreamChatOptions;
|
|
9
11
|
}) => StreamChat<SCG> | null;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import { StreamChat
|
|
2
|
+
import { StreamChat } from 'stream-chat';
|
|
3
3
|
/**
|
|
4
4
|
* React hook to create, connect and return `StreamChat` client.
|
|
5
5
|
*/
|
|
6
|
-
export const useCreateChatClient = ({ apiKey, tokenOrProvider, userData, }) => {
|
|
6
|
+
export const useCreateChatClient = ({ apiKey, options, tokenOrProvider, userData, }) => {
|
|
7
7
|
const [chatClient, setChatClient] = useState(null);
|
|
8
8
|
const [cachedUserData, setCachedUserData] = useState(userData);
|
|
9
9
|
if (userData.id !== cachedUserData.id) {
|
|
10
10
|
setCachedUserData(userData);
|
|
11
11
|
}
|
|
12
|
+
const [cachedOptions] = useState(options);
|
|
12
13
|
useEffect(() => {
|
|
13
|
-
const client = new StreamChat(apiKey);
|
|
14
|
+
const client = new StreamChat(apiKey, undefined, cachedOptions);
|
|
14
15
|
let didUserConnectInterrupt = false;
|
|
15
16
|
const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {
|
|
16
17
|
if (!didUserConnectInterrupt)
|
|
@@ -25,6 +26,6 @@ export const useCreateChatClient = ({ apiKey, tokenOrProvider, userData, }) => {
|
|
|
25
26
|
console.log(`Connection for user "${cachedUserData.id}" has been closed`);
|
|
26
27
|
});
|
|
27
28
|
};
|
|
28
|
-
}, [apiKey, cachedUserData, tokenOrProvider]);
|
|
29
|
+
}, [apiKey, cachedUserData, cachedOptions, tokenOrProvider]);
|
|
29
30
|
return chatClient;
|
|
30
31
|
};
|
|
@@ -26,9 +26,10 @@ const UnMemoizedMessageStatus = (props) => {
|
|
|
26
26
|
const sending = message.status === 'sending';
|
|
27
27
|
const delivered = message.status === 'received' && message.id === lastReceivedId && !threadList;
|
|
28
28
|
const deliveredAndRead = !!(readBy?.length && !threadList && !justReadByMe);
|
|
29
|
-
const
|
|
29
|
+
const readersWithoutOwnUser = deliveredAndRead
|
|
30
30
|
? readBy.filter((item) => item.id !== client.user?.id)
|
|
31
31
|
: [];
|
|
32
|
+
const [lastReadUser] = readersWithoutOwnUser;
|
|
32
33
|
return (React.createElement("span", { className: rootClassName, "data-testid": clsx({
|
|
33
34
|
'message-status-read-by': deliveredAndRead,
|
|
34
35
|
'message-status-received': delivered && !deliveredAndRead,
|
|
@@ -47,6 +48,6 @@ const UnMemoizedMessageStatus = (props) => {
|
|
|
47
48
|
(MessageReadStatus ? (React.createElement(MessageReadStatus, null)) : (React.createElement(React.Fragment, null,
|
|
48
49
|
React.createElement(PopperTooltip, { offset: [0, 5], referenceElement: referenceElement, visible: tooltipVisible }, getReadByTooltipText(readBy, t, client, tooltipUserNameMapper)),
|
|
49
50
|
React.createElement(Avatar, { className: 'str-chat__avatar--message-status', image: lastReadUser.image, name: lastReadUser.name || lastReadUser.id, user: lastReadUser }),
|
|
50
|
-
|
|
51
|
+
readersWithoutOwnUser.length > 1 && (React.createElement("span", { className: `str-chat__message-${messageType}-status-number`, "data-testid": 'message-status-read-by-many' }, readersWithoutOwnUser.length)))))));
|
|
51
52
|
};
|
|
52
53
|
export const MessageStatus = React.memo(UnMemoizedMessageStatus);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Nodes } from 'hast-util-find-and-replace/lib';
|
|
2
2
|
import type { UserResponse } from 'stream-chat';
|
|
3
|
-
import type { DefaultStreamChatGenerics } from '../../../../types
|
|
3
|
+
import type { DefaultStreamChatGenerics } from '../../../../types';
|
|
4
4
|
export declare const mentionsMarkdownPlugin: <StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics>(mentioned_users: UserResponse<StreamChatGenerics>[]) => () => (tree: Nodes) => void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Nodes } from '
|
|
1
|
+
import type { Nodes } from 'hast-util-find-and-replace/lib';
|
|
2
2
|
export declare const htmlToTextPlugin: () => (tree: Nodes) => void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Nodes } from '
|
|
1
|
+
import type { Nodes } from 'hast-util-find-and-replace/lib';
|
|
2
2
|
export declare const keepLineBreaksPlugin: () => (tree: Nodes) => void;
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { visit } from 'unist-util-visit';
|
|
2
2
|
import { u } from 'unist-builder';
|
|
3
3
|
const visitor = (node, index, parent) => {
|
|
4
|
-
if (
|
|
5
|
-
return;
|
|
6
|
-
if (typeof parent === 'undefined')
|
|
7
|
-
return;
|
|
8
|
-
if (!node.position)
|
|
4
|
+
if (!(index && parent && node.position))
|
|
9
5
|
return;
|
|
10
6
|
const prevSibling = parent.children.at(index - 1);
|
|
11
7
|
if (!prevSibling?.position)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React, { ComponentType } from 'react';
|
|
2
2
|
import { Options } from 'react-markdown';
|
|
3
|
-
import {
|
|
4
|
-
import type { PluggableList } from 'react-markdown/lib';
|
|
3
|
+
import type { PluggableList } from 'react-markdown/lib/react-markdown';
|
|
5
4
|
import type { UserResponse } from 'stream-chat';
|
|
5
|
+
import { MentionProps } from './componentRenderers';
|
|
6
6
|
import type { DefaultStreamChatGenerics } from '../../../types/types';
|
|
7
7
|
export type RenderTextPluginConfigurator = (defaultPlugins: PluggableList) => PluggableList;
|
|
8
8
|
export declare const defaultAllowedTagNames: Array<keyof JSX.IntrinsicElements | 'emoji' | 'mention'>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import ReactMarkdown, {
|
|
2
|
+
import ReactMarkdown, { uriTransformer } from 'react-markdown';
|
|
3
3
|
import { find } from 'linkifyjs';
|
|
4
4
|
import uniqBy from 'lodash.uniqby';
|
|
5
5
|
import remarkGfm from 'remark-gfm';
|
|
@@ -7,6 +7,7 @@ import { Anchor, Emoji, Mention } from './componentRenderers';
|
|
|
7
7
|
import { detectHttp, escapeRegExp, matchMarkdownLinks, messageCodeBlocks } from './regex';
|
|
8
8
|
import { emojiMarkdownPlugin, mentionsMarkdownPlugin } from './rehypePlugins';
|
|
9
9
|
import { htmlToTextPlugin, keepLineBreaksPlugin } from './remarkPlugins';
|
|
10
|
+
import { ErrorBoundary } from '../../UtilityComponents';
|
|
10
11
|
export const defaultAllowedTagNames = [
|
|
11
12
|
'html',
|
|
12
13
|
'text',
|
|
@@ -42,7 +43,7 @@ function encodeDecode(url) {
|
|
|
42
43
|
return url;
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
|
-
const urlTransform = (uri) => (uri.startsWith('app://') ? uri :
|
|
46
|
+
const urlTransform = (uri) => (uri.startsWith('app://') ? uri : uriTransformer(uri));
|
|
46
47
|
const getPluginsForward = (plugins) => plugins;
|
|
47
48
|
export const markDownRenderers = {
|
|
48
49
|
a: Anchor,
|
|
@@ -106,8 +107,9 @@ export const renderText = (text, mentionedUsers, { allowedTagNames = defaultAllo
|
|
|
106
107
|
if (mentionedUsers?.length) {
|
|
107
108
|
rehypePlugins.push(mentionsMarkdownPlugin(mentionedUsers));
|
|
108
109
|
}
|
|
109
|
-
return (React.createElement(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
return (React.createElement(ErrorBoundary, { fallback: React.createElement(React.Fragment, null, text) },
|
|
111
|
+
React.createElement(ReactMarkdown, { allowedElements: allowedTagNames, components: {
|
|
112
|
+
...markDownRenderers,
|
|
113
|
+
...customMarkDownRenderers,
|
|
114
|
+
}, rehypePlugins: getRehypePlugins(rehypePlugins), remarkPlugins: getRemarkPlugins(remarkPlugins), skipHtml: true, transformLinkUri: urlTransform, unwrapDisallowed: true }, newText)));
|
|
113
115
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
import type { PropsWithChildren, ReactNode } from 'react';
|
|
3
|
+
type ErrorBoundaryProps = PropsWithChildren<{
|
|
4
|
+
fallback?: ReactNode;
|
|
5
|
+
}>;
|
|
6
|
+
export declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
7
|
+
hasError: boolean;
|
|
8
|
+
}> {
|
|
9
|
+
constructor(props: ErrorBoundaryProps);
|
|
10
|
+
static getDerivedStateFromError(): {
|
|
11
|
+
hasError: boolean;
|
|
12
|
+
};
|
|
13
|
+
componentDidCatch(error: unknown, information: unknown): void;
|
|
14
|
+
render(): ReactNode;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
export class ErrorBoundary extends Component {
|
|
3
|
+
constructor(props) {
|
|
4
|
+
super(props);
|
|
5
|
+
this.state = { hasError: false };
|
|
6
|
+
}
|
|
7
|
+
static getDerivedStateFromError() {
|
|
8
|
+
return { hasError: true };
|
|
9
|
+
}
|
|
10
|
+
componentDidCatch(error, information) {
|
|
11
|
+
console.error(error, information);
|
|
12
|
+
}
|
|
13
|
+
render() {
|
|
14
|
+
if (this.state.hasError) {
|
|
15
|
+
return this.props.fallback;
|
|
16
|
+
}
|
|
17
|
+
return this.props.children;
|
|
18
|
+
}
|
|
19
|
+
}
|