Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 102 lines 3.0 kB view raw
1import { 2 article, 3 audio, 4 image, 5 textOnly, 6 video 7} from "@lens-protocol/metadata"; 8import { useCallback } from "react"; 9import { usePostAttachmentStore } from "@/store/non-persisted/post/usePostAttachmentStore"; 10import { usePostLicenseStore } from "@/store/non-persisted/post/usePostLicenseStore"; 11import { usePostVideoStore } from "@/store/non-persisted/post/usePostVideoStore"; 12import { usePostAudioStore } from "../store/non-persisted/post/usePostAudioStore"; 13 14interface UsePostMetadataProps { 15 baseMetadata: any; 16} 17 18const usePostMetadata = () => { 19 const { videoDurationInSeconds, videoThumbnail } = usePostVideoStore(); 20 const { audioPost } = usePostAudioStore(); 21 const { license } = usePostLicenseStore(); 22 const { attachments } = usePostAttachmentStore(); 23 24 const formatAttachments = () => 25 attachments.slice(1).map(({ mimeType, uri }) => ({ 26 item: uri, 27 type: mimeType 28 })); 29 30 const getMetadata = useCallback( 31 ({ baseMetadata }: UsePostMetadataProps) => { 32 const primaryAttachment = attachments[0]; 33 const hasAttachments = Boolean(primaryAttachment); 34 const isImage = primaryAttachment?.type === "Image"; 35 const isAudio = primaryAttachment?.type === "Audio"; 36 const isVideo = primaryAttachment?.type === "Video"; 37 38 if (!hasAttachments) { 39 return baseMetadata.content?.length > 2000 40 ? article(baseMetadata) 41 : textOnly(baseMetadata); 42 } 43 44 const attachmentsToBeUploaded = formatAttachments(); 45 46 if (isImage) { 47 return image({ 48 ...baseMetadata, 49 ...(attachmentsToBeUploaded.length > 0 && { 50 attachments: attachmentsToBeUploaded 51 }), 52 image: { 53 ...(license && { license }), 54 item: primaryAttachment.uri, 55 type: primaryAttachment.mimeType 56 } 57 }); 58 } 59 60 if (isAudio) { 61 return audio({ 62 ...baseMetadata, 63 ...(attachmentsToBeUploaded.length > 0 && { 64 attachments: attachmentsToBeUploaded 65 }), 66 audio: { 67 ...(audioPost.artist && { 68 artist: audioPost.artist 69 }), 70 cover: audioPost.cover, 71 item: primaryAttachment.uri, 72 type: primaryAttachment.mimeType, 73 ...(license && { license }) 74 } 75 }); 76 } 77 78 if (isVideo) { 79 return video({ 80 ...baseMetadata, 81 ...(attachmentsToBeUploaded.length > 0 && { 82 attachments: attachmentsToBeUploaded 83 }), 84 video: { 85 cover: videoThumbnail.url, 86 duration: Number.parseInt(videoDurationInSeconds, 10), 87 item: primaryAttachment.uri, 88 type: primaryAttachment.mimeType, 89 ...(license && { license }) 90 } 91 }); 92 } 93 94 return null; 95 }, 96 [attachments, videoDurationInSeconds, audioPost, videoThumbnail, license] 97 ); 98 99 return getMetadata; 100}; 101 102export default usePostMetadata;