Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bnl 2 #18096

Merged
merged 1 commit into from
Jun 21, 2024
Merged

bnl 2 #18096

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 59 additions & 51 deletions apps/wallet/src/ui/app/components/buynlarge/HomePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,79 @@ import { useNavigate } from 'react-router-dom';

import { Text } from '../../shared/text';
import Close from './close.svg';
import { useBuyNLargeAsset } from './useBuyNLargeAsset';
import { useConfig } from './useConfig';
import { useBuyNLargeAssets } from './useBuyNLargeAssets';

const SEEN_KEY = 'buy-n-large-seen';
const SEEN_KEY = 'buy-n-large-seen-v2';

export function BuyNLargeHomePanel() {
const navigate = useNavigate();
const [seen, setSeen] = useState(() => {
const [seen, setSeen] = useState<string[]>(() => {
const stored = localStorage.getItem(SEEN_KEY);
if (stored) {
return JSON.parse(stored);
}
return false;
return [];
});
const config = useConfig();

const { asset } = useBuyNLargeAsset();

if (seen || !config || !config.enabled || !asset) return null;
const bnl = useBuyNLargeAssets();

return (
<div>
<div
role="button"
onClick={() => {
navigate(
`/nft-details?${new URLSearchParams({
objectId: asset.data?.objectId ?? '',
}).toString()}`,
);
<>
{bnl.map((item) => {
if (!item || !item.enabled || !item.asset || seen.includes(item?.objectType)) return null;

return (
<div>
<div
role="button"
onClick={() => {
navigate(
`/nft-details?${new URLSearchParams({
objectId: item.asset?.data?.objectId ?? '',
}).toString()}`,
);

ampli.clickedCollectibleCard({
objectId: asset?.data?.objectId ?? '',
collectibleType: asset?.data?.type ?? '',
sourceScreen: 'HomePanel',
});
}}
className="bg-[#2249E3] flex flex-row items-center rounded-xl px-4 py-3 gap-4 w-full"
>
<div className="w-8 h-8">
<img src={config.homeImage} alt="" className="w-full h-full object-contain" />
</div>
ampli.clickedCollectibleCard({
objectId: item.asset?.data?.objectId ?? '',
collectibleType: item.asset?.data?.type ?? '',
sourceScreen: 'HomePanel',
});
}}
className="flex flex-row items-center rounded-xl px-4 py-3 gap-4 w-full"
style={{
backgroundColor: item.backgroundColor,
}}
>
<div className="w-8 h-8">
<img src={item.homeImage} alt="" className="w-full h-full object-contain" />
</div>

<div className="flex-1">
<Text variant="body" weight="medium" color="white">
{config.homeDescription}
</Text>
</div>
<div className="flex-1">
<Text variant="body" weight="medium" color="white">
{item.homeDescription}
</Text>
</div>

<div>
<button
type="button"
aria-label="Close"
className="bg-transparent p-0 m-0 border-none"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
localStorage.setItem(SEEN_KEY, JSON.stringify(true));
setSeen(true);
}}
>
<Close className="text-content-onColor" width={16} height={16} />
</button>
</div>
</div>
</div>
<div>
<button
type="button"
aria-label="Close"
className="bg-transparent p-0 m-0 border-none"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
const nextSeen = [...new Set([...seen, item?.objectType])];
localStorage.setItem(SEEN_KEY, JSON.stringify(nextSeen));
setSeen(nextSeen);
}}
>
<Close className="text-content-onColor" width={16} height={16} />
</button>
</div>
</div>
</div>
);
})}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ import { useSuiClientQuery } from '@mysten/dapp-kit';
import { useActiveAddress } from '../../hooks';
import { useConfig } from './useConfig';

export function useBuyNLargeAsset() {
export function useBuyNLargeAssets() {
const config = useConfig();
const address = useActiveAddress();
const { data } = useSuiClientQuery(
'getOwnedObjects',
{
owner: address ?? '',
filter: { StructType: config?.objectType ?? '' },
filter: { MatchAny: config.map(({ objectType }) => ({ StructType: objectType ?? '' })) },
options: { showDisplay: true, showType: true },
},
{
enabled: !!address && config?.enabled,
enabled: !!address && config.some(({ enabled }) => enabled),
},
);

return { objectType: config?.enabled ? config?.objectType : null, asset: data?.data[0] };
return config
?.map((item) => {
if (!item.enabled) return null;
return {
...item,
asset: data?.data.find((x) => x.data?.type === item.objectType),
};
})
.filter((x) => !!x);
}
3 changes: 2 additions & 1 deletion apps/wallet/src/ui/app/components/buynlarge/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ type BuyNLargeConfig = {
sheetDescription: string;
homeDescription: string;
homeImage: string;
backgroundColor: string;
};

export function useConfig() {
return useFeatureValue<BuyNLargeConfig | null>('buynlarge', null);
return useFeatureValue<BuyNLargeConfig[]>('buynlargev2', []);
}
23 changes: 14 additions & 9 deletions apps/wallet/src/ui/app/hooks/useGetNFTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useKioskClient } from '@mysten/core/src/hooks/useKioskClient';
import { type SuiObjectData } from '@mysten/sui/client';
import { useMemo } from 'react';

import { useBuyNLargeAsset } from '../components/buynlarge/useBuyNLargeAsset';
import { useBuyNLargeAssets } from '../components/buynlarge/useBuyNLargeAssets';
import { useHiddenAssets } from '../pages/home/hidden-assets/HiddenAssetsProvider';

type OwnedAssets = {
Expand All @@ -22,7 +22,7 @@ export enum AssetFilterTypes {

export function useGetNFTs(address?: string | null) {
const kioskClient = useKioskClient();
const { asset, objectType } = useBuyNLargeAsset();
const bnl = useBuyNLargeAssets();
const {
data,
isPending,
Expand All @@ -35,9 +35,12 @@ export function useGetNFTs(address?: string | null) {
} = useGetOwnedObjects(
address,
{
MatchNone: objectType
? [{ StructType: '0x2::coin::Coin' }, { StructType: objectType }]
: [{ StructType: '0x2::coin::Coin' }],
MatchNone: [
{ StructType: '0x2::coin::Coin' },
...(bnl
.filter((item) => !!item?.objectType)
.map((item) => ({ StructType: item?.objectType })) as { StructType: string }[]),
],
},
50,
);
Expand All @@ -62,12 +65,14 @@ export function useGetNFTs(address?: string | null) {
return acc;
}, ownedAssets);

if (asset?.data) {
groupedAssets?.visual.unshift(asset.data);
}
bnl.forEach((item) => {
if (item?.asset?.data) {
groupedAssets?.visual.unshift(item.asset.data);
}
});

return groupedAssets;
}, [hiddenAssetIds, data?.pages, kioskClient.network, asset]);
}, [hiddenAssetIds, data?.pages, kioskClient.network, bnl]);

return {
data: assets,
Expand Down
9 changes: 4 additions & 5 deletions apps/wallet/src/ui/app/pages/home/nft-details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { LabelValuesContainer } from '_components/LabelValuesContainer';
import Loading from '_components/loading';
import { NFTDisplayCard } from '_components/nft-display';
import { useGetNFTMeta, useNFTBasicData, useOwnedNFT } from '_hooks';
import { useBuyNLargeAsset } from '_src/ui/app/components/buynlarge/useBuyNLargeAsset';
import { useConfig } from '_src/ui/app/components/buynlarge/useConfig';
import { useBuyNLargeAssets } from '_src/ui/app/components/buynlarge/useBuyNLargeAssets';
import { useExplorerLink } from '_src/ui/app/hooks/useExplorerLink';
import { useUnlockedGuard } from '_src/ui/app/hooks/useUnlockedGuard';
import PageTitle from '_src/ui/app/shared/PageTitle';
Expand Down Expand Up @@ -44,8 +43,7 @@ function NFTDetailsPage() {
const kioskItem = data?.list.find((k) => k.data?.objectId === nftId);

const navigate = useNavigate();
const buyNLargeConfig = useConfig();
const { objectType } = useBuyNLargeAsset();
const bnl = useBuyNLargeAssets();

// Extract either the attributes, or use the top-level NFT fields:
const metaFields =
Expand Down Expand Up @@ -80,7 +78,8 @@ function NFTDetailsPage() {
const isGuardLoading = useUnlockedGuard();
const isPending = isNftLoading || isPendingDisplay || isGuardLoading;

const isBuyNLarge = objectData?.type === objectType;
const buyNLargeConfig = bnl.find((item) => item?.objectType === objectData?.type);
const isBuyNLarge = !!buyNLargeConfig;

return (
<div
Expand Down
9 changes: 5 additions & 4 deletions apps/wallet/src/ui/app/pages/home/nfts/VisualAssets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { ErrorBoundary } from '_components/error-boundary';
import { ampli } from '_src/shared/analytics/ampli';
import { useBuyNLargeAsset } from '_src/ui/app/components/buynlarge/useBuyNLargeAsset';
import { useBuyNLargeAssets } from '_src/ui/app/components/buynlarge/useBuyNLargeAssets';
import { NFTDisplayCard } from '_src/ui/app/components/nft-display';
import { Button } from '_src/ui/app/shared/ButtonUI';
import { getKioskIdFromOwnerCap, isKioskOwnerToken } from '@mysten/core';
Expand All @@ -17,7 +17,7 @@ import { useHiddenAssets } from '../hidden-assets/HiddenAssetsProvider';
export default function VisualAssets({ items }: { items: SuiObjectData[] }) {
const { hideAsset } = useHiddenAssets();
const kioskClient = useKioskClient();
const { objectType } = useBuyNLargeAsset();
const bnl = useBuyNLargeAssets();

return (
<div className="grid w-full grid-cols-2 gap-x-3.5 gap-y-4">
Expand All @@ -43,7 +43,8 @@ export default function VisualAssets({ items }: { items: SuiObjectData[] }) {
>
<div className="group">
<div className="w-full h-full justify-center z-10 absolute pointer-events-auto text-gray-60 transition-colors duration-200 p-0">
{!isKioskOwnerToken(kioskClient.network, object) && object.type !== objectType ? (
{!isKioskOwnerToken(kioskClient.network, object) &&
!bnl.some((item) => item?.objectType !== object.type) ? (
<div className="absolute top-2 right-3 rounded-md h-8 w-8 opacity-0 group-hover:opacity-100">
<Button
variant="hidden"
Expand All @@ -64,7 +65,7 @@ export default function VisualAssets({ items }: { items: SuiObjectData[] }) {
</div>
<ErrorBoundary>
<NFTDisplayCard
hideLabel={object.type === objectType}
hideLabel={bnl.some((item) => item?.objectType === object.type)}
objectId={object.objectId}
size="lg"
animateHover
Expand Down
Loading