Skip to content

Commit 6fabeed

Browse files
authored
Merge pull request #333 from GetStream/CRNS-155/undefined-channel-fix
CRNS-155/Handle undefined channel
2 parents b4dbf07 + a37205d commit 6fabeed

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/components/Channel/Channel.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ const Channel = (props) => {
3737
disableIfFrozenChannel = true,
3838
disableKeyboardCompatibleView = false,
3939
emojiData = emojiDataDefault,
40+
EmptyStateIndicator = EmptyStateIndicatorDefault,
4041
KeyboardCompatibleView = KeyboardCompatibleViewDefault,
42+
LoadingErrorIndicator = LoadingErrorIndicatorDefault,
43+
LoadingIndicator = LoadingIndicatorDefault,
4144
} = props;
4245

4346
const { client } = useContext(ChatContext);
@@ -74,7 +77,7 @@ const Channel = (props) => {
7477
const [threadHasMore, setThreadHasMore] = useState(true);
7578
const [threadLoadingMore, setThreadLoadingMore] = useState(false);
7679
const [threadMessages, setThreadMessages] = useState(
77-
channel.state?.threads?.[props.thread?.id] || [],
80+
channel?.state?.threads?.[props.thread?.id] || [],
7881
);
7982
const [typing, setTyping] = useState(Immutable({}));
8083
const [watcherCount, setWatcherCount] = useState();
@@ -85,7 +88,7 @@ const Channel = (props) => {
8588

8689
return () => {
8790
client.off('connection.recovered', handleEvent);
88-
channel.off?.(handleEvent);
91+
channel?.off?.(handleEvent);
8992
handleEventStateThrottled.cancel();
9093
loadMoreFinishedDebounced.cancel();
9194
loadMoreThreadFinishedDebounced.cancel();
@@ -192,7 +195,7 @@ const Channel = (props) => {
192195
const initChannel = async () => {
193196
let initError = false;
194197

195-
if (!channel.initialized) {
198+
if (!channel.initialized && channel.cid) {
196199
try {
197200
await channel.watch();
198201
} catch (e) {
@@ -456,9 +459,8 @@ const Channel = (props) => {
456459

457460
const channelContext = {
458461
channel,
459-
disabled: channel.data?.frozen && disableIfFrozenChannel,
460-
EmptyStateIndicator:
461-
props.EmptyStateIndicator || EmptyStateIndicatorDefault,
462+
disabled: channel?.data?.frozen && disableIfFrozenChannel,
463+
EmptyStateIndicator,
462464
error,
463465
eventHistory,
464466
lastRead,
@@ -500,6 +502,10 @@ const Channel = (props) => {
500502
threadMessages,
501503
};
502504

505+
if (!channel || error) {
506+
return <LoadingErrorIndicator error={error} listType='message' />;
507+
}
508+
503509
if (!channel?.cid || !channel.watch) {
504510
return (
505511
<Text style={{ fontWeight: 'bold', padding: 16 }} testID='no-channel'>
@@ -508,13 +514,7 @@ const Channel = (props) => {
508514
);
509515
}
510516

511-
if (error) {
512-
const { LoadingErrorIndicator = LoadingErrorIndicatorDefault } = props;
513-
return <LoadingErrorIndicator error={error} listType='message' />;
514-
}
515-
516517
if (loading) {
517-
const { LoadingIndicator = LoadingIndicatorDefault } = props;
518518
return <LoadingIndicator listType='message' />;
519519
}
520520

src/components/Channel/__tests__/Channel.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('Channel', () => {
7676
cleanup();
7777
});
7878

79-
it('should render simple text if the channel prop is not provided', async () => {
79+
it('should render a simple text error if the channel id does not exist', async () => {
8080
const nullChannel = { ...channel, cid: null };
8181
const { getByTestId } = renderComponent({ channel: nullChannel });
8282

0 commit comments

Comments
 (0)