54f424d047
* Just use the first picture every time * Add missing testIDs * Various test fixes * Use simplified link fetcher for e2e * Disable tests for now-n * Update test-env creation
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import {useState, useEffect} from 'react'
|
|
import * as apilib from 'lib/api/index'
|
|
import {getLinkMeta} from 'lib/link-meta/link-meta'
|
|
import {ComposerOpts} from 'state/shell/composer'
|
|
import {getAgent} from '#/state/session'
|
|
|
|
export function useExternalLinkFetch({}: {
|
|
setQuote: (opts: ComposerOpts['quote']) => void
|
|
}) {
|
|
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
|
|
undefined,
|
|
)
|
|
|
|
useEffect(() => {
|
|
let aborted = false
|
|
const cleanup = () => {
|
|
aborted = true
|
|
}
|
|
if (!extLink) {
|
|
return cleanup
|
|
}
|
|
if (!extLink.meta) {
|
|
getLinkMeta(getAgent(), extLink.uri).then(meta => {
|
|
if (aborted) {
|
|
return
|
|
}
|
|
setExtLink({
|
|
uri: extLink.uri,
|
|
isLoading: !!meta.image,
|
|
meta,
|
|
})
|
|
})
|
|
return cleanup
|
|
}
|
|
if (extLink.isLoading) {
|
|
setExtLink({
|
|
...extLink,
|
|
isLoading: false, // done
|
|
})
|
|
}
|
|
return cleanup
|
|
}, [extLink])
|
|
|
|
return {extLink, setExtLink}
|
|
}
|