Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GIFs] Add error boundary to GIF picker #3643

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/components/dialogs/GifSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
useSetExternalEmbedPref,
} from '#/state/preferences'
import {Gif, useGifphySearch, useGiphyTrending} from '#/state/queries/giphy'
import {ErrorScreen} from '#/view/com/util/error/ErrorScreen'
import {ErrorBoundary} from '#/view/com/util/ErrorBoundary'
import {atoms as a, useBreakpoints, useTheme} from '#/alf'
import * as Dialog from '#/components/Dialog'
import * as TextField from '#/components/forms/TextField'
Expand Down Expand Up @@ -54,13 +56,18 @@ export function GifSelectDialog({
break
}

const renderErrorBoundary = useCallback(
(error: any) => <DialogError details={String(error)} />,
[],
)

return (
<Dialog.Outer
control={control}
nativeOptions={{sheet: {snapPoints}}}
onClose={onClose}>
<Dialog.Handle />
{content}
<ErrorBoundary renderError={renderErrorBoundary}>{content}</ErrorBoundary>
</Dialog.Outer>
)
}
Expand Down Expand Up @@ -357,3 +364,31 @@ function GiphyConsentPrompt({control}: {control: Dialog.DialogControlProps}) {
</Dialog.ScrollableInner>
)
}

function DialogError({details}: {details?: string}) {
const {_} = useLingui()
const control = Dialog.useDialogContext()

return (
<Dialog.ScrollableInner style={a.gap_md} label={_(msg`An error occured`)}>
<Dialog.Close />
<ErrorScreen
title={_(msg`Oh no!`)}
message={_(
msg`There was an unexpected issue in the application. Please let us know if this happened to you!`,
)}
details={details}
/>
<Button
label={_(msg`Close dialog`)}
onPress={() => control.close()}
color="primary"
size="medium"
variant="solid">
<ButtonText>
<Trans>Close</Trans>
</ButtonText>
</Button>
</Dialog.ScrollableInner>
)
}
12 changes: 9 additions & 3 deletions src/view/com/util/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, {Component, ErrorInfo, ReactNode} from 'react'
import {ErrorScreen} from './error/ErrorScreen'
import {CenteredView} from './Views'
import {msg} from '@lingui/macro'
import {logger} from '#/logger'
import {useLingui} from '@lingui/react'

import {logger} from '#/logger'
import {ErrorScreen} from './error/ErrorScreen'
import {CenteredView} from './Views'

interface Props {
children?: ReactNode
renderError?: (error: any) => ReactNode
}

interface State {
Expand All @@ -30,6 +32,10 @@ export class ErrorBoundary extends Component<Props, State> {

public render() {
if (this.state.hasError) {
if (this.props.renderError) {
return this.props.renderError(this.state.error)
}

return (
<CenteredView style={{height: '100%', flex: 1}}>
<TranslatedErrorScreen details={this.state.error.toString()} />
Expand Down
Loading