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

Fix compressed nft tabs showing up on every account's tab list #345

Merged
merged 1 commit into from
May 31, 2024
Merged
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
48 changes: 38 additions & 10 deletions app/address/[address]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { PublicKey } from '@solana/web3.js';
import { Cluster, ClusterStatus } from '@utils/cluster';
import { FEATURE_PROGRAM_ID } from '@utils/parseFeatureAccount';
import { useClusterPath } from '@utils/url';
import { MetadataPointer, TokenMetadata } from '@validators/accounts/token-extension'
import { MetadataPointer, TokenMetadata } from '@validators/accounts/token-extension';
import Link from 'next/link';
import { redirect, useSelectedLayoutSegment } from 'next/navigation';
import React, { PropsWithChildren, Suspense } from 'react';
Expand All @@ -48,6 +48,7 @@ import useSWRImmutable from 'swr/immutable';
import { Base58EncodedAddress } from 'web3js-experimental';

import { CompressedNftAccountHeader, CompressedNftCard } from '@/app/components/account/CompressedNftCard';
import { useCompressedNft } from '@/app/providers/compressed-nft';
import { FullTokenInfo, getFullTokenInfo } from '@/app/utils/token-info';

const IDENTICON_WIDTH = 64;
Expand Down Expand Up @@ -503,7 +504,7 @@ function MoreSection({ children, tabs }: { children: React.ReactNode; tabs: (JSX
function getTabs(pubkey: PublicKey, account: Account): TabComponent[] {
const address = pubkey.toBase58();
const parsedData = account.data.parsed;
const tabs: Tab[] = [
const tabs: (Tab & { compressed?: boolean })[] = [
{
path: '',
slug: 'history',
Expand Down Expand Up @@ -541,24 +542,23 @@ function getTabs(pubkey: PublicKey, account: Account): TabComponent[] {
tabs.push(...TABS_LOOKUP[`${programTypeKey}:metaplexNFT`]);
}

if (!tabs.find(tab => tab.slug === 'metadata')) {
// Compressed NFT tabs
if ((!account.data.raw || account.data.raw.length === 0) && !account.data.parsed) {
tabs.push(
{
compressed: true,
path: 'metadata',
slug: 'metadata',
title: 'Metadata',
},
{
compressed: true,
path: 'attributes',
slug: 'attributes',
title: 'Attributes',
}
},
{ compressed: true, path: 'compression', slug: 'compression', title: 'Compression' }
);
tabs.push({
path: 'compression',
slug: 'compression',
title: 'Compression',
});
}

const isNFToken = account && isNFTokenAccount(account);
Expand Down Expand Up @@ -595,7 +595,13 @@ function getTabs(pubkey: PublicKey, account: Account): TabComponent[] {

return tabs.map(tab => {
return {
component: <Tab address={address} key={tab.slug} path={tab.path} title={tab.title} />,
component: !tab.compressed ? (
<Tab address={address} key={tab.slug} path={tab.path} title={tab.title} />
) : (
<React.Suspense key={tab.slug} fallback={<></>}>
<CompressedNftLink tab={tab} address={pubkey.toString()} pubkey={pubkey} />
</React.Suspense>
),
tab,
};
});
Expand Down Expand Up @@ -684,3 +690,25 @@ function AccountDataLink({ address, tab, programId }: { address: string; tab: Ta
</li>
);
}

// Checks that a compressed NFT exists at the given address and returns a link to the tab
function CompressedNftLink({ tab, address, pubkey }: { tab: Tab; address: string; pubkey: PublicKey }) {
const { url } = useCluster();
const compressedNft = useCompressedNft({ address: pubkey.toString(), url });
const tabPath = useClusterPath({ pathname: `/address/${address}/${tab.path}` });

const selectedLayoutSegment = useSelectedLayoutSegment();
const isActive = selectedLayoutSegment === tab.path;

if (!compressedNft || !compressedNft.compression.compressed) {
return null;
}

return (
<li key={tab.slug} className="nav-item">
<Link className={`${isActive ? 'active ' : ''}nav-link`} href={tabPath}>
{tab.title}
</Link>
</li>
);
}
Loading