Skip to content

Commit

Permalink
✨ add token icon in swap selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg committed Sep 7, 2023
1 parent bf17a14 commit 1cc383e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
8 changes: 8 additions & 0 deletions batcher-ui/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const nextConfig = {

return config;
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'ipfs.io',
},
],
},
};

module.exports = nextConfig;
62 changes: 40 additions & 22 deletions batcher-ui/src/components/SelectPair.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import * as Select from '@radix-ui/react-select';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
Expand All @@ -10,24 +10,35 @@ import { useSelector } from 'react-redux';
import { currentSwapSelector } from 'src/reducers';
import { useDispatch } from 'react-redux';
import { changePair } from 'src/actions';
import { getTokensMetadata } from 'src/utils/utils';
import Image from 'next/image';

const SelectPair = () => {
const { swap, isReverse } = useSelector(currentSwapSelector);
const dispatch = useDispatch();

const availableTokens = ['tzBTC', 'USDT', 'EURL'];
const [availableTokens, setAvailableTokens] = useState<any[]>([]);

useEffect(() => {
getTokensMetadata().then(
(tokens: { name: string; address: string; icon: string | undefined }[]) =>
setAvailableTokens(tokens)
);
}, []);

return (
<Select.Root
// value={availableTokens[1].name}
value={isReverse ? swap.to.name : swap.from.token.name}
onValueChange={value => {
console.warn(value);
//TODO: change this when we had more pair
// const parsedValue = value === 'USDt'
const pair =
value === 'tzBTC' ? `tzBTC/${swap.to.name}` : `tzBTC/${value}`;
const reversed = value !== 'tzBTC';
dispatch(changePair(pair, reversed));
}}>
<Select.Trigger className="flex items-center text-dark w-[90px] justify-center rounded px-2 mr-1 text-base gap-2 bg-white hover:bg-hovergray outline-none">
<Select.Trigger className="flex items-center text-dark w-[140px] justify-center rounded px-2 mr-1 text-base gap-2 bg-white hover:bg-hovergray outline-none">
<Select.Value
placeholder={isReverse ? swap.to.name : swap.from.token.name}
/>
Expand All @@ -37,30 +48,37 @@ const SelectPair = () => {
</Select.Trigger>
<Select.Portal className="w-[7rem]">
<Select.Content className="overflow-hidden bg-white rounded-md text-dark">
<Select.ScrollUpButton className="flex items-center justify-center h-[25px] bg-[red] cursor-default">
<Select.ScrollUpButton className="flex items-center justify-center h-[25px] cursor-default">
<FontAwesomeIcon icon={faChevronUp} />
</Select.ScrollUpButton>
<Select.Viewport className="p-2">
<Select.Group>
{availableTokens
// .filter(a =>
// isReverse ? swap.to.name !== a : swap.from.token.name !== a
// )
.map(t => (
<SelectItem
value={t}
key={t}
disabled={
isReverse
? swap.to.name === t
: swap.from.token.name === t
}>
{t}
</SelectItem>
))}
{availableTokens.map(t => (
<SelectItem
value={t.name}
key={t.name}
disabled={
isReverse
? swap.to.name === t.name
: swap.from.token.name === t.name
}>
<div className="flex items-center">
{t.icon && (
<Image
src={t.icon}
alt={`${t.name} icon`}
width={24}
height={24}
style={{ paddingRight: 4 }}
/>
)}
<p>{t.name}</p>
</div>
</SelectItem>
))}
</Select.Group>
</Select.Viewport>
<Select.ScrollDownButton className="flex items-center justify-center h-[25px] bg-[pink]text-violet11 cursor-default">
<Select.ScrollDownButton className="flex items-center justify-center h-[25px] cursor-default">
<FontAwesomeIcon icon={faChevronDown} />
</Select.ScrollDownButton>
</Select.Content>
Expand Down
24 changes: 24 additions & 0 deletions batcher-ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ export const getBigMapByIdAndTokenPair = (
);
};

export const getTokensMetadata = async () => {
const storage = await getStorage();
const validTokens = storage['valid_tokens'];
return Promise.all(
Object.values(validTokens).map(async token => {
const icon = await fetch(
`${process.env.NEXT_PUBLIC_TZKT_URI_API}/v1/tokens?contract=${token.address}`
)
.then(t => t.json())
.then(([t]) =>
t.metadata.thumbnailUri
? `https://ipfs.io/ipfs/${t.metadata.thumbnailUri.split('//')[1]}`
: undefined
);

return {
name: token.name,
address: token.address,
icon,
};
})
);
};

// ----- FETCH CONTRACT INFORMATIONS AND PARSING ------

export const getPairsInformations = async (
Expand Down

0 comments on commit 1cc383e

Please sign in to comment.