(post: Post)
| 3 | import type { Post } from '../../graphql/posts'; |
| 4 | |
| 5 | export const usePostImage = (post: Post): string | undefined => |
| 6 | useMemo(() => { |
| 7 | if (post?.type === PostType.SocialTwitter && post?.subType === 'thread') { |
| 8 | return undefined; |
| 9 | } |
| 10 | |
| 11 | const baseImage = post?.sharedPost?.image ?? post?.image; |
| 12 | |
| 13 | if (baseImage) { |
| 14 | return baseImage; |
| 15 | } |
| 16 | |
| 17 | if (!post?.contentHtml || typeof DOMParser === 'undefined') { |
| 18 | return undefined; |
| 19 | } |
| 20 | |
| 21 | const parser = new DOMParser(); |
| 22 | const doc = parser.parseFromString(post.contentHtml, 'text/html'); |
| 23 | const imgTag = doc.querySelector('img'); |
| 24 | if (imgTag) { |
| 25 | return imgTag.getAttribute('src') ?? undefined; |
| 26 | } |
| 27 | |
| 28 | return undefined; |
| 29 | }, [ |
| 30 | post?.contentHtml, |
| 31 | post?.image, |
| 32 | post?.sharedPost?.image, |
| 33 | post?.subType, |
| 34 | post?.type, |
| 35 | ]); |
no outgoing calls
no test coverage detected