From b2c29397428ff86d0df628199f17096f476c8bcf Mon Sep 17 00:00:00 2001 From: Samuel Newman <10959775+mozzius@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:24:33 +0200 Subject: [PATCH] add error boundary --- .../com/util/post-embeds/VideoEmbed.web.tsx | 92 +++++++++++++++++-- .../util/post-embeds/VideoEmbedInner.web.tsx | 4 +- 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/src/view/com/util/post-embeds/VideoEmbed.web.tsx b/src/view/com/util/post-embeds/VideoEmbed.web.tsx index 26b5c66c83..6c5d0018db 100644 --- a/src/view/com/util/post-embeds/VideoEmbed.web.tsx +++ b/src/view/com/util/post-embeds/VideoEmbed.web.tsx @@ -1,9 +1,15 @@ -import React, {useEffect, useRef, useState} from 'react' +import React, {useCallback, useEffect, useRef, useState} from 'react' import {View} from 'react-native' +import {msg, Trans} from '@lingui/macro' +import {useLingui} from '@lingui/react' import {atoms as a, useTheme} from '#/alf' +import {Button, ButtonText} from '#/components/Button' +import {Text} from '#/components/Typography' +import {ErrorBoundary} from '../ErrorBoundary' import {useActiveVideoView} from './ActiveVideoContext' import {VideoEmbedInner} from './VideoEmbedInner' +import {HLSUnsupportedError} from './VideoEmbedInner.web' export function VideoEmbed({source}: {source: string}) { const t = useTheme() @@ -31,6 +37,16 @@ export function VideoEmbed({source}: {source: string}) { return () => observer.disconnect() }, [sendPosition]) + const [key, setKey] = useState(0) + const renderError = useCallback( + (error: unknown) => ( + setKey(key + 1)} /> + ), + [key], + ) + + console.log(key) + return (
evt.stopPropagation()}> - + + +
) } + +function VideoError({error, retry}: {error: unknown; retry: () => void}) { + const t = useTheme() + const {_} = useLingui() + + const isHLS = error instanceof HLSUnsupportedError + + return ( + + + {isHLS ? ( + + Your browser does not support the video format. Please try a + different browser. + + ) : ( + + An error occurred while loading the video. Please try again later. + + )} + + {!isHLS && ( + + )} + + ) +} diff --git a/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx b/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx index 74d583f95a..dd7cedde5a 100644 --- a/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx +++ b/src/view/com/util/post-embeds/VideoEmbedInner.web.tsx @@ -87,7 +87,7 @@ export function VideoPlayer({ useEffect(() => { if (!ref.current) return - if (!Hls.isSupported()) throw new UnsupportedError() + if (!Hls.isSupported()) throw new HLSUnsupportedError() const hls = new Hls({capLevelToPlayerSize: true}) hlsRef.current = hls @@ -154,7 +154,7 @@ export function VideoPlayer({ ) } -export class UnsupportedError extends Error { +export class HLSUnsupportedError extends Error { constructor() { super('HLS is not supported') }