-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
FeedEmbed.tsx
283 lines (257 loc) · 9.23 KB
/
FeedEmbed.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import type {
MirrorablePublication,
UnknownOpenActionModuleSettings
} from '@hey/lens';
import type { OG } from '@hey/types/misc';
import type { ActionData, PublicationInfo, UIData } from 'nft-openaction-kit';
import type { FC } from 'react';
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 { REWARDS_PROFILE_ID, ZERO_ADDRESS } from '@hey/data/constants';
import { PUBLICATION } from '@hey/data/tracking';
import { VerifiedOpenActionModules } from '@hey/data/verified-openaction-modules';
import sanitizeDStorageUrl from '@hey/helpers/sanitizeDStorageUrl';
import stopEventPropagation from '@hey/helpers/stopEventPropagation';
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 { createTrackedSelector } from 'react-tracked';
import { CHAIN } from 'src/constants';
import { useNftOaCurrencyStore } from 'src/store/persisted/useNftOaCurrencyStore';
import { useAccount } from 'wagmi';
import { create } from 'zustand';
import { OPEN_ACTION_NO_EMBED_TOOLTIP, openActionCTA } from '.';
import ActionInfo from './ActionInfo';
import DecentOpenActionModule from './Module';
interface State {
activeOpenActionModal: null | string;
selectedQuantity: number;
setActiveOpenActionModal: (activeOpenActionModal: null | string) => void;
setSelectedQuantity: (selectedQuantity: number) => void;
}
const store = create<State>((set) => ({
activeOpenActionModal: null,
selectedQuantity: 1,
setActiveOpenActionModal: (activeOpenActionModal) =>
set({ activeOpenActionModal }),
setSelectedQuantity: (selectedQuantity) => set({ selectedQuantity })
}));
export const useNftOpenActionStore = createTrackedSelector(store);
enum ActionDataResponseType {
FULL = 'FULL',
PARTIAL = 'PARTIAL'
}
const formatPublicationData = (
targetPublication: MirrorablePublication
): PublicationInfo => {
const [profileId, pubId] = targetPublication.id.split('-');
const unknownModules =
targetPublication.openActionModules as UnknownOpenActionModuleSettings[];
const actionModules = unknownModules.map(
(module) => module.contract.address
) as string[];
const actionModulesInitDatas = unknownModules.map(
(module) => module.initializeCalldata
) as string[];
return {
actionModules,
actionModulesInitDatas,
profileId: parseInt(profileId, 16).toString(),
pubId: parseInt(pubId, 16).toString()
};
};
interface FeedEmbedProps {
og: OG;
publication: MirrorablePublication;
}
const FeedEmbed: FC<FeedEmbedProps> = ({ og, publication }) => {
const { address } = useAccount();
const { selectedNftOaCurrency } = useNftOaCurrencyStore();
const { selectedQuantity, setActiveOpenActionModal } =
useNftOpenActionStore();
const [nft, setNft] = useState({
chain: og.nft?.chain || null,
collectionName: og.nft?.collectionName || '',
contractAddress: og.nft?.contractAddress || ZERO_ADDRESS,
creatorAddress: og.nft?.creatorAddress || ZERO_ADDRESS,
description: og.description || '',
endTime: null,
mediaUrl: og.nft?.mediaUrl || og.image || '',
mintCount: null,
mintStatus: null,
mintUrl: null,
schema: og.nft?.schema || '',
sourceUrl: og.url
});
const [isNftCoverLoaded, setIsNftCoverLoaded] = useState(false);
const module = publication.openActionModules.find(
(module) => module.contract.address === VerifiedOpenActionModules.DecentNFT
);
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(publication);
return await nftOpenActionKit
.actionDataFromPost({
executingClientProfileId: REWARDS_PROFILE_ID,
paymentToken: selectedNftOaCurrency,
post: pubInfo,
profileId: publication.by.id,
profileOwnerAddress: publication.by.ownedBy.address,
quantity: selectedQuantity,
senderAddress: address || ZERO_ADDRESS,
sourceUrl: og.url,
srcChainId: CHAIN.id.toString()
})
.then((actionData) => {
setNft((prevNft) => ({
...prevNft,
chain: actionData.uiData.dstChainId.toString() || prevNft.chain,
collectionName: actionData.uiData.nftName || prevNft.collectionName,
creatorAddress: (actionData.uiData.nftCreatorAddress ||
prevNft.creatorAddress) as `0x{string}`,
mediaUrl:
sanitizeDStorageUrl(actionData.uiData.nftUri) || prevNft.mediaUrl,
schema: actionData.uiData.tokenStandard || prevNft.schema
}));
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: actionDataResponse,
isLoading: loadingActionData,
refetch
} = useQuery({
enabled:
Boolean(module) &&
Boolean(selectedNftOaCurrency) &&
Boolean(address) &&
Boolean(publication.id),
queryFn: getActionData,
queryKey: [
'getActionData',
selectedNftOaCurrency,
selectedQuantity,
address,
publication?.id
]
});
const { data: uiData } = useQuery({
queryFn: getUiData,
queryKey: ['uiData', publication.id],
staleTime: Infinity
});
const actionData = actionDataResponse?.data;
const dataType = actionDataResponse?.type;
useEffect(() => {
refetch();
}, [selectedNftOaCurrency, address, refetch]);
if (!module) {
return null;
}
return (
<>
<Card className="mt-3" forceRounded onClick={stopEventPropagation}>
<div className="relative h-[350px] max-h-[350px] w-full overflow-hidden rounded-t-xl">
<Image
alt={nft.collectionName}
className="absolute inset-0 h-full w-full scale-110 object-cover blur-2xl filter"
src={nft.mediaUrl}
/>
<div className="absolute inset-0 bg-white opacity-20" />
<Image
alt={nft.collectionName}
className={cn(
'relative h-full w-full object-contain transition-opacity duration-500',
isNftCoverLoaded ? 'visible opacity-100' : 'invisible opacity-0'
)}
onLoad={() => setIsNftCoverLoaded(true)}
src={nft.mediaUrl}
/>
</div>
{actionData && Boolean(nft) && !loadingActionData ? (
<div className="flex items-center justify-between border-t px-4 py-2 dark:border-gray-700">
<ActionInfo
collectionName={nft.collectionName}
uiData={
dataType === ActionDataResponseType.FULL
? (actionData as ActionData)?.uiData
: (actionData as UIData)
}
/>
{dataType === ActionDataResponseType.FULL ? (
<Button
className="px-4 py-1"
icon={<CursorArrowRaysIcon className="size-4" />}
onClick={() => {
setActiveOpenActionModal(publication.id);
Leafwatch.track(PUBLICATION.OPEN_ACTIONS.DECENT.OPEN_DECENT, {
publication_id: publication.id
});
}}
size="sm"
>
{openActionCTA((actionData as ActionData).uiData?.platformName)}
</Button>
) : (
<Tooltip
content={<span>{OPEN_ACTION_NO_EMBED_TOOLTIP}</span>}
placement="top"
>
<Button
className="px-4 py-1"
disabled
icon={<CursorArrowRaysIcon className="size-4" />}
size="sm"
>
Mint
</Button>
</Tooltip>
)}
</div>
) : loadingActionData ? (
<DecentOpenActionShimmer />
) : null}
</Card>
<DecentOpenActionModule
actionData={actionData as ActionData | undefined}
loadingActionData={loadingActionData}
module={module as UnknownOpenActionModuleSettings}
nft={nft}
publication={publication}
uiData={uiData}
/>
</>
);
};
export default FeedEmbed;