Skip to content

Commit

Permalink
fix: adding actionData fallback for partial campaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahem committed May 15, 2024
1 parent 9a481a0 commit a5f6a34
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 43 deletions.
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"idb-keyval": "^6.2.1",
"next": "^14.2.3",
"next-themes": "^0.3.0",
"nft-openaction-kit": "^1.0.25",
"nft-openaction-kit": "^1.0.27",
"party-js": "^2.2.0",
"plur": "^5.1.0",
"plyr-react": "^5.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@ import type {
UnknownOpenActionModuleSettings
} from '@hey/lens';
import type { OG } from '@hey/types/misc';
import type { ActionData, PublicationInfo } from 'nft-openaction-kit';
import type { ActionData, PublicationInfo, UIData } from 'nft-openaction-kit';
import type { FC } from 'react';

import ActionInfo from '@components/Shared/Oembed/Nft/ActionInfo';
import DecentOpenActionShimmer from '@components/Shared/Shimmer/DecentOpenActionShimmer';
import getNftOpenActionKit from '@helpers/getNftOpenActionKit';
import { Leafwatch } from '@helpers/leafwatch';
import { CursorArrowRaysIcon } from '@heroicons/react/24/outline';
import { ZERO_ADDRESS } from '@hey/data/constants';
import { PUBLICATION } from '@hey/data/tracking';
import { VerifiedOpenActionModules } from '@hey/data/verified-openaction-modules';
import { isMirrorPublication } from '@hey/helpers/publicationHelpers';
import sanitizeDStorageUrl from '@hey/helpers/sanitizeDStorageUrl';
import stopEventPropagation from '@hey/helpers/stopEventPropagation';
import { Button, Card, Image } from '@hey/ui';
import { Button, Card, Image, Tooltip } from '@hey/ui';
import cn from '@hey/ui/cn';
import { useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { CHAIN, HEY_REFERRAL_PROFILE_ID } from 'src/constants';
import { useNftOaCurrencyStore } from 'src/store/persisted/useNftOaCurrencyStore';
import { useAccount } from 'wagmi';

import { openActionCTA } from '.';
import { OPEN_ACTION_NO_EMBED_TOOLTIP, openActionCTA } from '.';
import DecentOpenActionModule from './Module';

enum ActionDataResponseType {
FULL = 'FULL',
PARTIAL = 'PARTIAL'
}

const formatPublicationData = (
targetPublication: MirrorablePublication
): PublicationInfo => {
Expand Down Expand Up @@ -90,7 +96,23 @@ const FeedEmbed: FC<FeedEmbedProps> = ({
(module) => module.contract.address === VerifiedOpenActionModules.DecentNFT
);

const getActionData = async (): Promise<ActionData> => {
const getUiData = async () => {
const nftOpenActionKit = getNftOpenActionKit();
try {
const uiDataResult = await nftOpenActionKit.generateUiData({
contentURI: og.url
});

return uiDataResult;
} catch (error) {
return null;
}
};

const getActionData = async (): Promise<{
data: ActionData | null | UIData;
type: ActionDataResponseType;
}> => {
const nftOpenActionKit = getNftOpenActionKit();
const pubInfo = formatPublicationData(targetPublication);

Expand Down Expand Up @@ -120,12 +142,27 @@ const FeedEmbed: FC<FeedEmbedProps> = ({
schema: actionData.uiData.tokenStandard || prevNft.schema
}));

return actionData;
return { data: actionData, type: ActionDataResponseType.FULL };
})
.catch(async () => {
return await getUiData().then((uiData) => {
setNft((prevNft) => ({
...prevNft,
chain: uiData?.dstChainId.toString() || prevNft.chain,
collectionName: uiData?.nftName || prevNft.collectionName,
creatorAddress: (uiData?.nftCreatorAddress ||
prevNft.creatorAddress) as `0x{string}`,
mediaUrl: sanitizeDStorageUrl(uiData?.nftUri) || prevNft.mediaUrl,
schema: uiData?.tokenStandard || prevNft.schema
}));

return { data: uiData, type: ActionDataResponseType.PARTIAL };
});
});
};

const {
data: actionData,
data: actionDataResponse,
isLoading: loadingActionData,
refetch
} = useQuery({
Expand All @@ -144,6 +181,9 @@ const FeedEmbed: FC<FeedEmbedProps> = ({
]
});

const actionData = actionDataResponse?.data;
const dataType = actionDataResponse?.type;

useEffect(() => {
refetch();
}, [selectedNftOaCurrency, address, refetch]);
Expand Down Expand Up @@ -178,37 +218,58 @@ const FeedEmbed: FC<FeedEmbedProps> = ({
actionData={actionData}
collectionName={nft.collectionName}
creatorAddress={nft.creatorAddress}
uiData={actionData?.uiData}
uiData={
dataType === ActionDataResponseType.FULL
? actionData?.uiData
: actionData
}
/>
) : null}
<Button
className="w-full sm:w-auto"
onClick={() => {
setShowOpenActionModal(true);
Leafwatch.track(PUBLICATION.OPEN_ACTIONS.DECENT.OPEN_DECENT, {
publication_id: publication.id
});
}}
size="lg"
>
{openActionCTA(actionData.uiData.platformName)}
</Button>
{dataType === ActionDataResponseType.FULL ? (
<Button
className="w-full sm:w-auto"
onClick={() => {
setShowOpenActionModal(true);
Leafwatch.track(PUBLICATION.OPEN_ACTIONS.DECENT.OPEN_DECENT, {
publication_id: publication.id
});
}}
size="lg"
>
{openActionCTA(actionData.platformName)}
</Button>
) : (
<Tooltip
content={<span>{OPEN_ACTION_NO_EMBED_TOOLTIP}</span>}
placement="top"
>
<Button
disabled
icon={<CursorArrowRaysIcon className="size-5" />}
size="md"
>
{'Mint'}
</Button>
</Tooltip>
)}
</div>
) : loadingActionData ? (
<DecentOpenActionShimmer />
) : null}
</Card>
<DecentOpenActionModule
actionData={actionData}
loadingCurrency={loadingActionData}
module={module as UnknownOpenActionModuleSettings}
nft={nft}
onClose={() => setShowOpenActionModal(false)}
publication={targetPublication}
selectedQuantity={selectedQuantity}
setSelectedQuantity={setSelectedQuantity}
show={showOpenActionModal}
/>
{Boolean(actionData) && dataType === ActionDataResponseType.FULL ? (
<DecentOpenActionModule
actionData={actionData}
loadingCurrency={loadingActionData}
module={module as UnknownOpenActionModuleSettings}
nft={nft}
onClose={() => setShowOpenActionModal(false)}
publication={targetPublication}
selectedQuantity={selectedQuantity}
setSelectedQuantity={setSelectedQuantity}
show={showOpenActionModal}
/>
) : null}
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useEffect, useState } from 'react';
import FeedEmbed from './FeedEmbed';

export const OPEN_ACTION_EMBED_TOOLTIP = 'Open action embedded';
export const OPEN_ACTION_NO_EMBED_TOOLTIP = 'Unable to embed open action';
export const OPEN_ACTION_NO_EMBED_TOOLTIP = 'Mint not availabe anymore';

export const openActionCTA = (platformName?: string): string => {
const name = platformName || '';
Expand Down
17 changes: 10 additions & 7 deletions apps/web/src/components/Shared/Oembed/Nft/ActionInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ const ActionInfo: FC<ActionInfoProps> = ({
variables: { request: { for: creatorAddress } }
});

const formattedPrice = formatPrice(
Number(actionData?.actArgumentsFormatted.paymentToken.amount) /
Math.pow(
10,
allowedTokens?.find((t) => t.contractAddress === selectedNftOaCurrency)
?.decimals || 18
const formattedPrice = actionData?.actArgumentsFormatted?.paymentToken?.amount
? formatPrice(
Number(actionData?.actArgumentsFormatted.paymentToken.amount) /
Math.pow(
10,
allowedTokens?.find(
(t) => t.contractAddress === selectedNftOaCurrency
)?.decimals || 18
)
)
);
: null;

if (!creatorAddress && loading) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5f6a34

Please sign in to comment.