Fix channel list item not to cause a crash if myChannel is not found

This commit is contained in:
Elias Nahum 2022-03-22 23:44:15 -03:00
parent 077d5b9e95
commit 52da02bc7d
No known key found for this signature in database
GPG key ID: E038DB71E0B61702
2 changed files with 13 additions and 7 deletions

View file

@ -54,7 +54,7 @@ type Props = {
isActive: boolean;
isOwnDirectMessage: boolean;
isMuted: boolean;
myChannel: MyChannelModel;
myChannel?: MyChannelModel;
collapsed: boolean;
}
@ -65,7 +65,7 @@ const ChannelListItem = ({channel, isActive, isOwnDirectMessage, isMuted, myChan
const serverUrl = useServerUrl();
// Make it brighter if it's not muted, and highlighted or has unreads
const bright = !isMuted && (myChannel.isUnread || myChannel.mentionsCount > 0);
const bright = !isMuted && (myChannel?.isUnread || (myChannel?.mentionsCount ?? 0) > 0);
const sharedValue = useSharedValue(collapsed && !bright);
@ -80,7 +80,11 @@ const ChannelListItem = ({channel, isActive, isOwnDirectMessage, isMuted, myChan
};
});
const switchChannels = () => switchToChannelById(serverUrl, myChannel.id);
const switchChannels = () => {
if (myChannel) {
switchToChannelById(serverUrl, myChannel.id);
}
};
const membersCount = useMemo(() => {
if (channel.type === General.GM_CHANNEL) {
return channel.displayName?.split(',').length;
@ -101,7 +105,7 @@ const ChannelListItem = ({channel, isActive, isOwnDirectMessage, isMuted, myChan
displayName = formatMessage({id: 'channel_header.directchannel.you', defaultMessage: '{displayName} (you)'}, {displayName});
}
if (channel.deleteAt > 0 && !isActive) {
if ((channel.deleteAt > 0 && !isActive) || !myChannel) {
return null;
}

View file

@ -4,7 +4,7 @@
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import {combineLatest, of as of$} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {catchError, switchMap} from 'rxjs/operators';
import {General} from '@constants';
import {MM_TABLES, SYSTEM_IDENTIFIERS} from '@constants/database';
@ -22,12 +22,14 @@ const {SERVER: {MY_CHANNEL, SYSTEM}} = MM_TABLES;
const {CURRENT_USER_ID} = SYSTEM_IDENTIFIERS;
const enhance = withObservables(['channelId'], ({channelId, database}: {channelId: string} & WithDatabaseArgs) => {
const myChannel = database.get<MyChannelModel>(MY_CHANNEL).findAndObserve(channelId);
const myChannel = database.get<MyChannelModel>(MY_CHANNEL).findAndObserve(channelId).pipe(
catchError(() => of$(undefined)),
);
const currentUserId = database.get<SystemModel>(SYSTEM).findAndObserve(CURRENT_USER_ID).pipe(
switchMap(({value}) => of$(value)),
);
const channel = myChannel.pipe(switchMap((my) => my.channel.observe()));
const channel = myChannel.pipe(switchMap((my) => (my ? my.channel.observe() : of$(undefined))));
const settings = channel.pipe(switchMap((c) => c.settings.observe()));
const isOwnDirectMessage = combineLatest([currentUserId, channel]).pipe(