From 733e185b77eca3b26d439dcc5326223cdacb6d93 Mon Sep 17 00:00:00 2001 From: aschmidt20 Date: Tue, 27 Feb 2024 01:53:53 -0600 Subject: [PATCH] feat: add open action embedded status to NewPublication --- .../src/components/Composer/LinkPreviews.tsx | 19 ++++- .../components/Composer/NewPublication.tsx | 76 +++++++++++-------- .../components/Shared/Oembed/Nft/index.tsx | 18 ++++- .../src/components/Shared/Oembed/index.tsx | 17 ++++- 4 files changed, 93 insertions(+), 37 deletions(-) diff --git a/apps/web/src/components/Composer/LinkPreviews.tsx b/apps/web/src/components/Composer/LinkPreviews.tsx index 0f9a7ac42825..dbf9e67d33b8 100644 --- a/apps/web/src/components/Composer/LinkPreviews.tsx +++ b/apps/web/src/components/Composer/LinkPreviews.tsx @@ -4,7 +4,15 @@ import Oembed from '@components/Shared/Oembed'; import getURLs from '@hey/lib/getURLs'; import { usePublicationStore } from 'src/store/non-persisted/publication/usePublicationStore'; -const LinkPreviews: FC = () => { +interface LinkPreviewProps { + openActionEmbed: boolean; + openActionEmbedLoading: boolean; +} + +const LinkPreviews: FC = ({ + openActionEmbed, + openActionEmbedLoading +}) => { const publicationContent = usePublicationStore( (state) => state.publicationContent ); @@ -15,7 +23,14 @@ const LinkPreviews: FC = () => { return null; } - return ; + return ( + + ); }; export default LinkPreviews; diff --git a/apps/web/src/components/Composer/NewPublication.tsx b/apps/web/src/components/Composer/NewPublication.tsx index dabe5ed78cbe..817a8c6d0539 100644 --- a/apps/web/src/components/Composer/NewPublication.tsx +++ b/apps/web/src/components/Composer/NewPublication.tsx @@ -111,6 +111,12 @@ interface NewPublicationProps { publication: MirrorablePublication; } +const nftOpenActionKit = new NftOpenActionKit({ + decentApiKey: process.env.NEXT_PUBLIC_DECENT_API_KEY || '', + openSeaApiKey: process.env.NEXT_PUBLIC_OPENSEA_API_KEY || '', + raribleApiKey: process.env.NEXT_PUBLIC_RARIBLE_API_KEY || '' +}); + const NewPublication: FC = ({ publication }) => { const currentProfile = useProfileStore((state) => state.currentProfile); const { isSuspended } = useProfileRestriction(); @@ -205,6 +211,9 @@ const NewPublication: FC = ({ publication }) => { const [isLoading, setIsLoading] = useState(false); const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [publicationContentError, setPublicationContentError] = useState(''); + const [openActionEmbedLoading, setOpenActionEmbedLoading] = + useState(false); + const [openActionEmbed, setOpenActionEmbed] = useState(); const [editor] = useLexicalComposerContext(); const createPoll = useCreatePoll(); @@ -320,6 +329,35 @@ const NewPublication: FC = ({ publication }) => { setPublicationContentError(''); }, [audioPublication]); + useEffect(() => { + const fetchOpenActionEmbed = async () => { + setOpenActionEmbedLoading(true); + const publicationContentUrls = getURLs(publicationContent); + + try { + const calldata = await nftOpenActionKit.detectAndReturnCalldata( + publicationContentUrls[0] + ); + if (calldata) { + setOpenActionEmbed({ + unknownOpenAction: { + address: VerifiedOpenActionModules.DecentNFT, + data: calldata + } + }); + } else { + setOpenActionEmbed(undefined); + } + } catch (error_) { + setOpenActionEmbed(undefined); + setOpenActionEmbedLoading(false); + } + setOpenActionEmbedLoading(false); + }; + + fetchOpenActionEmbed(); + }, [publicationContent]); + useEffect(() => { editor.update(() => { $convertFromMarkdownString(publicationContent); @@ -418,35 +456,8 @@ const NewPublication: FC = ({ publication }) => { // Payload for the open action module const openActionModules = []; - // Flag to determine if open action module can be parsed from URL, if set Momoka will be disabled - let urlsWithParseableActions = false; - - const publicationContentUrls = getURLs(publicationContent); - - const nftOpenActionKit = new NftOpenActionKit({ - decentApiKey: process.env.NEXT_PUBLIC_DECENT_API_KEY || '', - openSeaApiKey: process.env.NEXT_PUBLIC_OPENSEA_API_KEY || '', - raribleApiKey: process.env.NEXT_PUBLIC_RARIBLE_API_KEY || '' - }); - - // Embed action module for publications with URLs that have recognized actions - for (const publicationContentUrl of publicationContentUrls) { - try { - const calldata = await nftOpenActionKit.detectAndReturnCalldata( - publicationContentUrl - ); - if (calldata) { - urlsWithParseableActions = true; - openActionModules.push({ - unknownOpenAction: { - address: VerifiedOpenActionModules.DecentNFT, - data: calldata - } - }); - } - } catch (error_) { - console.log(error_); - } + if (openActionEmbed) { + openActionModules.push(openActionEmbed); } if (collectModule.type) { @@ -469,7 +480,7 @@ const NewPublication: FC = ({ publication }) => { contentURI: `ar://${arweaveId}` }; - if (useMomoka && !urlsWithParseableActions) { + if (useMomoka && !openActionEmbed) { if (canUseLensManager) { if (isComment) { return await createCommentOnMomka( @@ -641,7 +652,10 @@ const NewPublication: FC = ({ publication }) => { /> ) : null} - +
diff --git a/apps/web/src/components/Shared/Oembed/Nft/index.tsx b/apps/web/src/components/Shared/Oembed/Nft/index.tsx index ad0241bad074..b8f21db4903b 100644 --- a/apps/web/src/components/Shared/Oembed/Nft/index.tsx +++ b/apps/web/src/components/Shared/Oembed/Nft/index.tsx @@ -9,7 +9,7 @@ import getNftChainInfo from '@hey/lib/getNftChainInfo'; import { isMirrorPublication } from '@hey/lib/publicationHelpers'; import stopEventPropagation from '@hey/lib/stopEventPropagation'; import { Nft } from '@hey/types/misc'; -import { Button, Card, Tooltip } from '@hey/ui'; +import { Button, Card, Spinner, Tooltip } from '@hey/ui'; import { Leafwatch } from '@lib/leafwatch'; import Link from 'next/link'; @@ -17,10 +17,17 @@ import MintedBy from './MintedBy'; interface NftProps { nft: Nft; + openActionEmbed: boolean; + openActionEmbedLoading: boolean; publication?: AnyPublication; } -const Nft: FC = ({ nft, publication }) => { +const Nft: FC = ({ + nft, + openActionEmbed, + openActionEmbedLoading, + publication +}) => { const targetPublication = publication && isMirrorPublication(publication) ? publication.mirrorOn @@ -77,6 +84,13 @@ const Nft: FC = ({ nft, publication }) => { > Mint + {openActionEmbedLoading ? ( + + ) : openActionEmbed === true ? ( +
Open Action Embedded
+ ) : ( +
No Open Action Embedded
+ )}
diff --git a/apps/web/src/components/Shared/Oembed/index.tsx b/apps/web/src/components/Shared/Oembed/index.tsx index f88416e94ce1..969bf72dadd4 100644 --- a/apps/web/src/components/Shared/Oembed/index.tsx +++ b/apps/web/src/components/Shared/Oembed/index.tsx @@ -14,11 +14,19 @@ import Portal from './Portal'; interface OembedProps { className?: string; + openActionEmbed: boolean; + openActionEmbedLoading: boolean; publication?: AnyPublication; url?: string; } -const Oembed: FC = ({ className = '', publication, url }) => { +const Oembed: FC = ({ + className = '', + openActionEmbed, + openActionEmbedLoading, + publication, + url +}) => { const { data, error, isLoading } = useQuery({ enabled: Boolean(url), queryFn: async () => { @@ -57,7 +65,12 @@ const Oembed: FC = ({ className = '', publication, url }) => { {og.html ? ( ) : og.nft ? ( - + ) : og.portal ? ( ) : (