mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useEffect, useState} from 'react'
2
3import {useAgent} from '#/state/session'
4import * as apilib from 'lib/api/index'
5import {getLinkMeta} from 'lib/link-meta/link-meta'
6import {ComposerOpts} from 'state/shell/composer'
7
8export function useExternalLinkFetch({}: {
9 setQuote: (opts: ComposerOpts['quote']) => void
10}) {
11 const agent = useAgent()
12 const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
13 undefined,
14 )
15
16 useEffect(() => {
17 let aborted = false
18 const cleanup = () => {
19 aborted = true
20 }
21 if (!extLink) {
22 return cleanup
23 }
24 if (!extLink.meta) {
25 getLinkMeta(agent, extLink.uri).then(meta => {
26 if (aborted) {
27 return
28 }
29 setExtLink({
30 uri: extLink.uri,
31 isLoading: !!meta.image,
32 meta,
33 })
34 })
35 return cleanup
36 }
37 if (extLink.isLoading) {
38 setExtLink({
39 ...extLink,
40 isLoading: false, // done
41 })
42 }
43 return cleanup
44 }, [extLink, agent])
45
46 return {extLink, setExtLink}
47}