Compare commits

...

2 Commits

Author SHA1 Message Date
Eric Bailey 3fa271dc16 Add to e2e 2024-05-02 10:53:58 -05:00
Eric Bailey ff51374334 Better error messages for failed link cards
(cherry picked from commit 8cfdc094e1251b6d9490fabf80ae58c12241fcab)
2024-05-02 10:52:17 -05:00
3 changed files with 26 additions and 12 deletions
+18 -8
View File
@@ -1,8 +1,11 @@
import {BskyAgent} from '@atproto/api'
import {isBskyAppUrl} from '../strings/url-helpers'
import {extractBskyMeta} from './bsky'
import {msg} from '@lingui/macro'
import {I18nContext} from '@lingui/react'
import {LINK_META_PROXY} from 'lib/constants'
import {getGiphyMetaUri} from 'lib/strings/embed-player'
import {isBskyAppUrl} from '../strings/url-helpers'
import {extractBskyMeta} from './bsky'
export enum LikelyType {
HTML,
@@ -26,7 +29,8 @@ export interface LinkMeta {
export async function getLinkMeta(
agent: BskyAgent,
url: string,
timeout = 15e3,
timeout: number,
i18n: I18nContext['i18n'],
): Promise<LinkMeta> {
if (isBskyAppUrl(url)) {
return extractBskyMeta(agent, url)
@@ -58,9 +62,11 @@ export async function getLinkMeta(
return meta
}
const controller = new AbortController()
const signal = controller.signal
try {
const controller = new AbortController()
const to = setTimeout(() => controller.abort(), timeout || 5e3)
const to = setTimeout(() => controller.abort('TIMEOUT'), timeout || 5e3)
const response = await fetch(
`${LINK_META_PROXY(agent.service.toString() || '')}${encodeURIComponent(
@@ -82,9 +88,13 @@ export async function getLinkMeta(
meta.image = image
meta.title = title
} catch (e) {
// failed
console.error(e)
meta.error = e instanceof Error ? e.toString() : 'Failed to fetch link'
if (signal.aborted) {
meta.error = i18n._(
msg`We were unable to fetch a preview for this URL because the request timed out.`,
)
} else {
meta.error = i18n._(msg`We were unable to fetch a preview for this URL.`)
}
}
return meta
@@ -1,4 +1,5 @@
import {useEffect, useState} from 'react'
import {useLingui} from '@lingui/react'
import {useAgent} from '#/state/session'
import * as apilib from 'lib/api/index'
@@ -8,6 +9,7 @@ import {ComposerOpts} from 'state/shell/composer'
export function useExternalLinkFetch({}: {
setQuote: (opts: ComposerOpts['quote']) => void
}) {
const {i18n} = useLingui()
const {getAgent} = useAgent()
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
undefined,
@@ -22,7 +24,7 @@ export function useExternalLinkFetch({}: {
return cleanup
}
if (!extLink.meta) {
getLinkMeta(getAgent(), extLink.uri).then(meta => {
getLinkMeta(getAgent(), extLink.uri, 5e3, i18n).then(meta => {
if (aborted) {
return
}
@@ -41,7 +43,7 @@ export function useExternalLinkFetch({}: {
})
}
return cleanup
}, [extLink, getAgent])
}, [extLink, getAgent, i18n])
return {extLink, setExtLink}
}
@@ -1,4 +1,5 @@
import {useEffect, useState} from 'react'
import {useLingui} from '@lingui/react'
import {logger} from '#/logger'
import {useFetchDid} from '#/state/queries/handle'
@@ -26,6 +27,7 @@ export function useExternalLinkFetch({
}: {
setQuote: (opts: ComposerOpts['quote']) => void
}) {
const {i18n} = useLingui()
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
undefined,
)
@@ -95,7 +97,7 @@ export function useExternalLinkFetch({
},
)
} else {
getLinkMeta(getAgent(), extLink.uri).then(meta => {
getLinkMeta(getAgent(), extLink.uri, 15e3, i18n).then(meta => {
if (aborted) {
return
}
@@ -137,7 +139,7 @@ export function useExternalLinkFetch({
})
}
return cleanup
}, [extLink, setQuote, getPost, fetchDid, getAgent])
}, [extLink, setQuote, getPost, fetchDid, getAgent, i18n])
return {extLink, setExtLink}
}