From 644c747a9538df5c168365a56f74b3ee53045f1e Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 31 Oct 2024 12:04:45 -0500 Subject: [PATCH] Handle errors, short posts --- .../util/post-embeds/ExternalLinkEmbed.tsx | 52 ++++++++++---- src/view/com/util/post-embeds/GithubGist.tsx | 71 +++++++++++++------ 2 files changed, 89 insertions(+), 34 deletions(-) diff --git a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx index 8053d89a65..90f1798ad8 100644 --- a/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx +++ b/src/view/com/util/post-embeds/ExternalLinkEmbed.tsx @@ -7,7 +7,10 @@ import {useLingui} from '@lingui/react' import {parseAltFromGIFDescription} from '#/lib/gif-alt-text' import {shareUrl} from '#/lib/sharing' -import {parseEmbedPlayerFromUrl} from '#/lib/strings/embed-player' +import { + EmbedPlayerParams, + parseEmbedPlayerFromUrl, +} from '#/lib/strings/embed-player' import {toNiceDomain} from '#/lib/strings/url-helpers' import {isNative} from '#/platform/detection' import {useExternalEmbedsPrefs} from '#/state/preferences' @@ -32,11 +35,7 @@ export const ExternalLinkEmbed = ({ style?: StyleProp hideAlt?: boolean }) => { - const {_} = useLingui() - const t = useTheme() const externalEmbedPrefs = useExternalEmbedsPrefs() - const niceUrl = toNiceDomain(link.uri) - const imageUri = link.thumb const embedPlayerParams = React.useMemo(() => { const params = parseEmbedPlayerFromUrl(link.uri) @@ -44,13 +43,6 @@ export const ExternalLinkEmbed = ({ return params } }, [link.uri, externalEmbedPrefs]) - const hasMedia = Boolean(imageUri || embedPlayerParams) - - const onShareExternal = useCallback(() => { - if (link.uri && isNative) { - shareUrl(link.uri) - } - }, [link.uri]) if (embedPlayerParams?.source === 'tenor') { const parsedAlt = parseAltFromGIFDescription(link.description) @@ -78,6 +70,38 @@ export const ExternalLinkEmbed = ({ ) } + return ( + + ) +} + +export function ExternalLinkEmbedCard({ + link, + onOpen, + embedPlayerParams, + style, +}: { + link: AppBskyEmbedExternal.ViewExternal + onOpen?: () => void + embedPlayerParams?: EmbedPlayerParams + style?: StyleProp +}) { + const t = useTheme() + const {_} = useLingui() + const niceUrl = toNiceDomain(link.uri) + const hasMedia = Boolean(link.thumb || embedPlayerParams) + + const onShareExternal = useCallback(() => { + if (link.uri && isNative) { + shareUrl(link.uri) + } + }, [link.uri]) + return ( - {imageUri && !embedPlayerParams ? ( + {link.thumb && !embedPlayerParams ? ( ) : undefined} diff --git a/src/view/com/util/post-embeds/GithubGist.tsx b/src/view/com/util/post-embeds/GithubGist.tsx index 763e646b93..e52e9880d1 100644 --- a/src/view/com/util/post-embeds/GithubGist.tsx +++ b/src/view/com/util/post-embeds/GithubGist.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {LayoutChangeEvent,ScrollView, View} from 'react-native' +import {LayoutChangeEvent, ScrollView, View} from 'react-native' import {LinearGradient} from 'expo-linear-gradient' import {AppBskyEmbedExternal} from '@atproto/api' import {msg} from '@lingui/macro' @@ -9,6 +9,7 @@ import {useQuery} from '@tanstack/react-query' import {shareUrl} from '#/lib/sharing' import {toNiceDomain} from '#/lib/strings/url-helpers' import {isNative} from '#/platform/detection' +import {ExternalLinkEmbedCard} from '#/view/com/util/post-embeds/ExternalLinkEmbed' import {atoms as a, useTheme} from '#/alf' import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Divider} from '#/components/Divider' @@ -19,6 +20,8 @@ import {Link} from '#/components/Link' import {Loader} from '#/components/Loader' import {Text} from '#/components/Typography' +const MAX_HEIGHT = 400 + export function GithubGist({ info, id, @@ -28,10 +31,10 @@ export function GithubGist({ }) { const t = useTheme() const {_} = useLingui() - const [footerHeight, setFooterHeight] = React.useState(100) - const [showMore, setShowMore] = React.useState(false) + const [footerHeight, setFooterHeight] = React.useState(97) // magic # + const [showMore, setShowMore] = React.useState() - const {data: files, isLoading} = useQuery({ + const {data: files, error} = useQuery({ queryKey: ['gist', id], async queryFn() { const url = `https://api.github.com/gists/${id}` @@ -62,10 +65,25 @@ export function GithubGist({ [setFooterHeight], ) + const onCodeLayout = React.useCallback( + (e: LayoutChangeEvent) => { + if (e.nativeEvent.layout.height < 400) { + setShowMore(true) + } else { + setShowMore(false) + } + }, + [setShowMore], + ) + const onShowMore = React.useCallback(() => { setShowMore(true) }, [setShowMore]) + if (error) { + return + } + return ( - {isLoading || !files ? ( - + {error ? null : !files ? ( // handled above + ) : ( - + style={[ + a.pt_lg, + { + maxHeight: + showMore === undefined + ? MAX_HEIGHT + : showMore + ? undefined + : MAX_HEIGHT, + }, + ]}> + - {!showMore && } + {showMore === false && } - {!showMore && ( - + + {showMore === false && ( - - - )} + )} + +