Skip to content

Commit

Permalink
Always allow manual entry of NFTs and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Dec 24, 2024
1 parent 01399a9 commit ab83c4a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 38 deletions.
19 changes: 7 additions & 12 deletions src/components/selectors/DropdownSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ export function DropdownSelector<T>({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='start' className={width}>
{manualInput && (
<>
<div className='p-2'>{manualInput}</div>
<DropdownMenuSeparator />
</>
)}
{setPage && (
<>
<>
{!!setPage && (
<DropdownMenuLabel>
<div className='flex items-center justify-between'>
<span>
Expand Down Expand Up @@ -94,10 +88,11 @@ export function DropdownSelector<T>({
</div>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
</>
)}
<div className='max-h-[300px] overflow-y-auto'>
)}
{manualInput && <div className='p-2'>{manualInput}</div>}
{!!setPage || (manualInput && <DropdownMenuSeparator />)}
</>
<div className='max-h-[260px] overflow-y-auto'>
{loadedItems.length === 0 ? (
<div className='p-4 text-center text-sm text-muted-foreground'>
No items available
Expand Down
30 changes: 29 additions & 1 deletion src/components/selectors/NftSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { commands, NftRecord } from '@/bindings';
import { useErrors } from '@/hooks/useErrors';
import { nftUri } from '@/lib/nftUri';
import { addressInfo } from '@/lib/utils';
import { useWalletState } from '@/state';
import { useEffect, useState } from 'react';
import { Input } from '../ui/input';
import { DropdownSelector } from './DropdownSelector';

export interface NftSelectorProps {
Expand Down Expand Up @@ -45,6 +47,14 @@ export function NftSelector({

useEffect(() => {
if (value && selectedNft?.launcher_id !== value) {
try {
if (addressInfo(value).puzzleHash.length !== 64) {
return setSelectedNft(null);
}
} catch {
return setSelectedNft(null);
}

commands
.getNft({ nft_id: value })
.then((data) => setSelectedNft(data.nft))
Expand All @@ -57,7 +67,13 @@ export function NftSelector({
useEffect(() => {
const nftsToFetch = [...nfts.map((nft) => nft.launcher_id)];
if (value && !nfts.find((nft) => nft.launcher_id === value)) {
nftsToFetch.push(value);
try {
if (addressInfo(value).puzzleHash.length === 64) {
nftsToFetch.push(value);
}
} catch {
// The checksum failed
}
}

Promise.all(
Expand Down Expand Up @@ -90,6 +106,18 @@ export function NftSelector({
setSelectedNft(nft);
}}
className={className}
manualInput={
<Input
placeholder='Enter NFT id'
value={value || ''}
onChange={(e) => {
onChange(e.target.value);
setSelectedNft(
nfts.find((nft) => nft.launcher_id === e.target.value) ?? null,
);
}}
/>
}
renderItem={(nft) => (
<div className='flex items-center gap-2 w-full'>
<img
Expand Down
42 changes: 18 additions & 24 deletions src/components/selectors/TokenSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CatRecord, commands } from '@/bindings';
import { useErrors } from '@/hooks/useErrors';
import { useWalletState } from '@/state';
import { useEffect, useState } from 'react';
import { Input } from '../ui/input';
import { DropdownSelector } from './DropdownSelector';
Expand All @@ -10,17 +9,14 @@ export interface TokenSelectorProps {
onChange: (value: string) => void;
disabled?: string[];
className?: string;
allowManualInput?: boolean;
}

export function TokenSelector({
value,
onChange,
disabled = [],
className,
allowManualInput = false,
}: TokenSelectorProps) {
const walletState = useWalletState();
const { addError } = useErrors();

const [tokens, setTokens] = useState<CatRecord[]>([]);
Expand Down Expand Up @@ -55,26 +51,24 @@ export function TokenSelector({
}}
className={className}
manualInput={
allowManualInput && (
<Input
placeholder='Enter asset id'
value={value || ''}
onChange={(e) => {
onChange(e.target.value);
setSelectedToken(
tokens.find((token) => token.asset_id === e.target.value) ?? {
name: 'Unknown',
asset_id: e.target.value,
icon_url: null,
balance: 0,
ticker: null,
description: null,
visible: true,
},
);
}}
/>
)
<Input
placeholder='Enter asset id'
value={value || ''}
onChange={(e) => {
onChange(e.target.value);
setSelectedToken(
tokens.find((token) => token.asset_id === e.target.value) ?? {
name: 'Unknown',
asset_id: e.target.value,
icon_url: null,
balance: 0,
ticker: null,
description: null,
visible: true,
},
);
}}
/>
}
renderItem={(token) => (
<div className='flex items-center gap-2 w-full'>
Expand Down
1 change: 0 additions & 1 deletion src/pages/MakeOffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,6 @@ function AssetSelector({
.filter((amount) => amount.asset_id !== cat.asset_id)
.map((amount) => amount.asset_id)}
className='rounded-r-none'
allowManualInput={!offering}
/>
<TokenAmountInput
id={`${prefix}-cat-${i}-amount`}
Expand Down

0 comments on commit ab83c4a

Please sign in to comment.