Files
explore/ui/tokenInstance/TokenInstancePageTitle.tsx

108 lines
3.0 KiB
TypeScript

import React from 'react';
import type { TokenInfo, TokenInstance } from 'types/api/token';
import { useMultichainContext } from 'lib/contexts/multichain';
import { getTokenTypeName } from 'lib/token/tokenTypes';
import { Link } from 'toolkit/next/link';
import { Tag } from '@luxfi/ui/tag';
import * as regexp from 'toolkit/utils/regexp';
import AddressQrCode from 'ui/address/details/AddressQrCode';
import AccountActionsMenu from 'ui/shared/AccountActionsMenu/AccountActionsMenu';
import AddressAddToWallet from 'ui/shared/address/AddressAddToWallet';
import TokenEntity from 'ui/shared/entities/token/TokenEntity';
import PageTitle from 'ui/shared/Page/PageTitle';
interface Props {
isLoading: boolean;
token: TokenInfo | undefined;
instance: TokenInstance | undefined;
hash: string | undefined;
}
const TokenInstancePageTitle = ({ isLoading, token, instance, hash }: Props) => {
const multichainContext = useMultichainContext();
const title = (() => {
if (typeof instance?.metadata?.name === 'string') {
return instance.metadata.name;
}
if (!instance) {
return `Unknown token instance`;
}
if (token?.name || token?.symbol) {
return (token.name || token.symbol) + ' #' + instance.id;
}
return `ID ${ instance.id }`;
})();
const tokenTag = token ? <Tag loading={ isLoading }>{ getTokenTypeName(token.type) }</Tag> : null;
const appLink = (() => {
if (!instance?.external_app_url) {
return null;
}
try {
const url = regexp.URL_PREFIX.test(instance.external_app_url) ?
new URL(instance.external_app_url) :
new URL('https://' + instance.external_app_url);
return (
<Link external href={ url.toString() } variant="underlaid" loading={ isLoading } className="ml-0 lg:ml-auto">
{ url.hostname || instance.external_app_url }
</Link>
);
} catch (error) {
return (
<Link external href={ instance.external_app_url } variant="underlaid" loading={ isLoading } className="ml-0 lg:ml-auto">
View in app
</Link>
);
}
})();
const address = {
hash: hash || '',
is_contract: true,
implementations: null,
watchlist_names: [],
watchlist_address_id: null,
};
const titleSecondRow = (
<div>
{ token && (
<TokenEntity
token={ token }
isLoading={ isLoading }
noSymbol
noCopy
jointSymbol
variant="subheading"
className="w-auto"
chain={ multichainContext?.chain }
/>
) }
{ !isLoading && token && <AddressAddToWallet token={ token } tokenId={ instance?.id } variant="button"/> }
<AddressQrCode hash={ address.hash } isLoading={ isLoading }/>
<AccountActionsMenu isLoading={ isLoading } showUpdateMetadataItem/>
{ appLink }
</div>
);
return (
<PageTitle
title={ title }
contentAfter={ tokenTag }
secondRow={ titleSecondRow }
isLoading={ isLoading }
/>
);
};
export default React.memo(TokenInstancePageTitle);