MCPcopy Create free account
hub / github.com/dailydotdev/apps / usePostImage

Function usePostImage

packages/shared/src/hooks/post/usePostImage.ts:5–35  ·  view source on GitHub ↗
(post: Post)

Source from the content-addressed store, hash-verified

3import type { Post } from '../../graphql/posts';
4
5export 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 ]);

Calls

no outgoing calls

Tested by

no test coverage detected