fix: guard isConvoActive against unloaded convo
Fixes a TypeError ('Cannot read property view of undefined') in
MessagesList getFooterState. The convo status can transition into an
active state (e.g. Backgrounded when the screen blurs before setup()
resolves) while this.convo is still undefined. The snapshot builds those
states with a non-null assertion (this.convo!), so isConvoActive returned
true and the conversation rendered MessagesList against an undefined
convo. Require convo to be present, matching the non-optional convo field
on every ActiveConvoStates member.
This commit is contained in:
@@ -21,12 +21,19 @@ export type ActiveConvoStates =
|
||||
* Checks if a `Convo` has a `status` that is "active", meaning the chat is
|
||||
* loaded and ready to be used, or its in a suspended or background state, and
|
||||
* ready for resumption.
|
||||
*
|
||||
* The `convo` object must also be present. The status can transition into an
|
||||
* active state before the convo has finished loading (e.g. `Initializing`
|
||||
* receives a `Background` event before `setup()` resolves), and every
|
||||
* `ActiveConvoStates` member declares `convo` as non-optional, so we guard
|
||||
* against that race here rather than crashing downstream consumers.
|
||||
*/
|
||||
export function isConvoActive(convo: ConvoState): convo is ActiveConvoStates {
|
||||
return (
|
||||
convo.status === ConvoStatus.Ready ||
|
||||
convo.status === ConvoStatus.Backgrounded ||
|
||||
convo.status === ConvoStatus.Suspended ||
|
||||
convo.status === ConvoStatus.Disabled
|
||||
convo.convo !== undefined &&
|
||||
(convo.status === ConvoStatus.Ready ||
|
||||
convo.status === ConvoStatus.Backgrounded ||
|
||||
convo.status === ConvoStatus.Suspended ||
|
||||
convo.status === ConvoStatus.Disabled)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user