Skip to content

Commit

Permalink
[🐴] Handle errors, improve styling (#3937)
Browse files Browse the repository at this point in the history
* Handle errors, improve styling

* Remove old UI
  • Loading branch information
estrattonbailey authored May 10, 2024
1 parent 1821a99 commit 195c9f1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 78 deletions.
58 changes: 36 additions & 22 deletions src/screens/Messages/Conversation/MessageListError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {useLingui} from '@lingui/react'

import {ConvoItem, ConvoItemError} from '#/state/messages/convo/types'
import {atoms as a, useTheme} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {ArrowRotateCounterClockwise_Stroke2_Corner0_Rounded as Refresh} from '#/components/icons/ArrowRotateCounterClockwise'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
import {InlineLinkText} from '#/components/Link'
import {Text} from '#/components/Typography'

export function MessageListError({
Expand All @@ -21,39 +22,52 @@ export function MessageListError({
[ConvoItemError.Network]: _(
msg`There was an issue connecting to the chat.`,
),
[ConvoItemError.HistoryFailed]: _(msg`Failed to load past messages.`),
[ConvoItemError.PollFailed]: _(
[ConvoItemError.FirehoseFailed]: _(
msg`This chat was disconnected due to a network error.`,
),
[ConvoItemError.HistoryFailed]: _(msg`Failed to load past messages.`),
[ConvoItemError.PendingFailed]: _(msg`Failed to send message(s).`),
}[item.code]
}, [_, item.code])

return (
<View style={[a.py_md, a.align_center]}>
<View style={[a.py_lg, a.align_center]}>
<View
style={[
a.flex_row,
a.align_center,
a.pt_md,
a.pb_lg,
a.px_3xl,
a.justify_between,
a.gap_lg,
a.py_md,
a.px_lg,
a.rounded_md,
t.atoms.bg_contrast_25,
{maxWidth: 300},
{maxWidth: 400},
]}>
<CircleInfo size="lg" fill={t.palette.negative_400} />
<Text style={[a.pt_sm, a.leading_snug]}>
{message}{' '}
<InlineLinkText
to="#"
label={_(msg`Press to retry`)}
onPress={e => {
e.preventDefault()
item.retry()
return false
}}>
{_(msg`Retry.`)}
</InlineLinkText>
</Text>
<View style={[a.flex_row, a.align_start, a.justify_between, a.gap_sm]}>
<CircleInfo
size="sm"
fill={t.palette.negative_400}
style={[{top: 3}]}
/>
<View style={[a.flex_1, {maxWidth: 200}]}>
<Text style={[a.leading_snug]}>{message}</Text>
</View>
</View>

<Button
label={_(msg`Press to retry`)}
size="small"
variant="ghost"
color="secondary"
onPress={e => {
e.preventDefault()
item.retry()
return false
}}>
<ButtonText>{_(msg`Retry`)}</ButtonText>
<ButtonIcon icon={Refresh} position="right" />
</Button>
</View>
</View>
)
Expand Down
24 changes: 0 additions & 24 deletions src/screens/Messages/Conversation/MessagesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {runOnJS, useSharedValue} from 'react-native-reanimated'
import {ReanimatedScrollEvent} from 'react-native-reanimated/lib/typescript/reanimated2/hook/commonTypes'
import {useSafeAreaInsets} from 'react-native-safe-area-context'
import {AppBskyRichtextFacet, RichText} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {shortenLinks} from '#/lib/strings/rich-text-manip'
import {isIOS} from '#/platform/detection'
Expand All @@ -22,7 +20,6 @@ import {List} from 'view/com/util/List'
import {MessageInput} from '#/screens/Messages/Conversation/MessageInput'
import {MessageListError} from '#/screens/Messages/Conversation/MessageListError'
import {atoms as a, useBreakpoints} from '#/alf'
import {Button, ButtonText} from '#/components/Button'
import {MessageItem} from '#/components/dms/MessageItem'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
Expand All @@ -41,25 +38,6 @@ function MaybeLoader({isLoading}: {isLoading: boolean}) {
)
}

function RetryButton({onPress}: {onPress: () => unknown}) {
const {_} = useLingui()

return (
<View style={{alignItems: 'center'}}>
<Button
label={_(msg`Press to Retry`)}
onPress={onPress}
variant="ghost"
color="negative"
size="small">
<ButtonText>
<Trans>Press to Retry</Trans>
</ButtonText>
</Button>
</View>
)
}

function renderItem({item}: {item: ConvoItem}) {
if (item.type === 'message' || item.type === 'pending-message') {
return (
Expand All @@ -71,8 +49,6 @@ function renderItem({item}: {item: ConvoItem}) {
)
} else if (item.type === 'deleted-message') {
return <Text>Deleted message</Text>
} else if (item.type === 'pending-retry') {
return <RetryButton onPress={item.retry} />
} else if (item.type === 'error-recoverable') {
return <MessageListError item={item} />
}
Expand Down
70 changes: 45 additions & 25 deletions src/state/messages/convo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export class Convo {
// throw new Error('UNCOMMENT TO TEST INIT FAILURE')
this.dispatch({event: ConvoDispatchEvent.Ready})
} catch (e: any) {
logger.error('Convo: setup() failed')
logger.error(e, {context: 'Convo: setup failed'})

this.dispatch({
event: ConvoDispatchEvent.Error,
Expand All @@ -413,6 +413,7 @@ export class Convo {
},
},
})
this.commit()
}
}

Expand Down Expand Up @@ -500,7 +501,7 @@ export class Convo {
this.sender = sender || this.sender
this.recipients = recipients || this.recipients
} catch (e: any) {
logger.error(`Convo: failed to refresh convo`)
logger.error(e, {context: `Convo: failed to refresh convo`})

this.footerItems.set(ConvoItemError.Network, {
type: 'error-recoverable',
Expand Down Expand Up @@ -601,17 +602,17 @@ export class Convo {
}

onFirehoseConnect() {
this.footerItems.delete(ConvoItemError.PollFailed)
this.footerItems.delete(ConvoItemError.FirehoseFailed)
this.commit()
}

onFirehoseError(error?: MessagesEventBusError) {
this.footerItems.set(ConvoItemError.PollFailed, {
this.footerItems.set(ConvoItemError.FirehoseFailed, {
type: 'error-recoverable',
key: ConvoItemError.PollFailed,
code: ConvoItemError.PollFailed,
key: ConvoItemError.FirehoseFailed,
code: ConvoItemError.FirehoseFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PollFailed)
this.footerItems.delete(ConvoItemError.FirehoseFailed)
this.commit()
error?.retry()
},
Expand Down Expand Up @@ -772,13 +773,21 @@ export class Convo {
await this.processPendingMessages()

this.commit()
} catch (e) {
this.footerItems.set('pending-retry', {
type: 'pending-retry',
key: 'pending-retry',
retry: this.batchRetryPendingMessages.bind(this),
} catch (e: any) {
logger.error(e, {context: `Convo: failed to send message`})
this.footerItems.set(ConvoItemError.PendingFailed, {
type: 'error-recoverable',
key: ConvoItemError.PendingFailed,
code: ConvoItemError.PendingFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PendingFailed)
this.commit()
this.batchRetryPendingMessages()
},
})
this.commit()
} finally {
this.isProcessingPendingMessages = false
}
}

Expand All @@ -789,10 +798,8 @@ export class Convo {
logger.DebugContext.convo,
)

this.footerItems.delete('pending-retry')
this.commit()

try {
// throw new Error('UNCOMMENT TO TEST RETRY')
const messageArray = Array.from(this.pendingMessages.values())
const {data} = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.sendMessageBatch(
Expand Down Expand Up @@ -831,11 +838,23 @@ export class Convo {
}

this.commit()
} catch (e) {
this.footerItems.set('pending-retry', {
type: 'pending-retry',
key: 'pending-retry',
retry: this.batchRetryPendingMessages.bind(this),

logger.debug(
`Convo: sent ${this.pendingMessages.size} pending messages`,
{},
logger.DebugContext.convo,
)
} catch (e: any) {
logger.error(e, {context: `Convo: failed to batch retry messages`})
this.footerItems.set(ConvoItemError.PendingFailed, {
type: 'error-recoverable',
key: ConvoItemError.PendingFailed,
code: ConvoItemError.PendingFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PendingFailed)
this.commit()
this.batchRetryPendingMessages()
},
})
this.commit()
}
Expand All @@ -862,7 +881,8 @@ export class Convo {
},
)
})
} catch (e) {
} catch (e: any) {
logger.error(e, {context: `Convo: failed to delete message`})
this.deletedMessages.delete(messageId)
this.commit()
throw e
Expand All @@ -875,10 +895,6 @@ export class Convo {
getItems(): ConvoItem[] {
const items: ConvoItem[] = []

this.headerItems.forEach(item => {
items.push(item)
})

this.pastMessages.forEach(m => {
if (ChatBskyConvoDefs.isMessageView(m)) {
items.unshift({
Expand All @@ -897,6 +913,10 @@ export class Convo {
}
})

this.headerItems.forEach(item => {
items.unshift(item)
})

this.newMessages.forEach(m => {
if (ChatBskyConvoDefs.isMessageView(m)) {
items.push({
Expand Down
22 changes: 15 additions & 7 deletions src/state/messages/convo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ export enum ConvoStatus {
}

export enum ConvoItemError {
HistoryFailed = 'historyFailed',
PollFailed = 'pollFailed',
/**
* Generic error
*/
Network = 'network',
/**
* Error connecting to event firehose
*/
FirehoseFailed = 'firehoseFailed',
/**
* Error fetching past messages
*/
HistoryFailed = 'historyFailed',
/**
* Error sending new message
*/
PendingFailed = 'pendingFailed',
}

export enum ConvoErrorCode {
Expand Down Expand Up @@ -88,11 +101,6 @@ export type ConvoItem =
| ChatBskyConvoDefs.DeletedMessageView
| null
}
| {
type: 'pending-retry'
key: string
retry: () => void
}
| {
type: 'error-recoverable'
key: string
Expand Down

0 comments on commit 195c9f1

Please sign in to comment.