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: broken balances page #11230

Merged
merged 1 commit into from
Jan 23, 2025
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
19 changes: 8 additions & 11 deletions packages/page-assets/src/Balances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ interface Props {

function Balances ({ className, infos = [] }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [infoIndex, setInfoIndex] = useState(0);
const [selectedAssetValue, setSelectedAssetValue] = useState('0');
const [info, setInfo] = useState<AssetInfoComplete | null>(null);
const balances = useBalances(info?.id);
const maxNumLength = Number.MAX_SAFE_INTEGER.toString().length;

const headerRef = useRef<([React.ReactNode?, string?, number?] | false)[]>([
[t('accounts'), 'start'],
Expand All @@ -43,9 +42,9 @@ function Balances ({ className, infos = [] }: Props): React.ReactElement<Props>
const assetOptions = useMemo(
() => completeInfos.map(({ id, metadata }) => ({
text: `${metadata.name.toUtf8()} (${formatNumber(id)})`,
value: id.toString().length < maxNumLength ? id.toNumber() : id.toString()
value: id.toString()
})),
[completeInfos, maxNumLength]
[completeInfos]
);

const siFormat = useMemo(
Expand All @@ -66,18 +65,16 @@ function Balances ({ className, infos = [] }: Props): React.ReactElement<Props>
);

useEffect((): void => {
const info = infoIndex >= 0
? completeInfos.find(({ id }) => id.toString() === infoIndex.toString()) ?? null
: null;
const info = completeInfos.find(({ id }) => id.toString() === selectedAssetValue);

// if no info found (usually happens on first load), select the first one automatically
if (!info) {
setInfo(completeInfos.at(0) ?? null);
setInfoIndex(completeInfos.at(0)?.id?.toNumber() ?? 0);
setSelectedAssetValue(completeInfos.at(0)?.id?.toString() ?? '0');
} else {
setInfo(info);
}
}, [completeInfos, infoIndex]);
}, [completeInfos, selectedAssetValue]);

return (
<StyledDiv className={className}>
Expand All @@ -88,10 +85,10 @@ function Balances ({ className, infos = [] }: Props): React.ReactElement<Props>
<Dropdown
isFull
label={t('the asset to query for balances')}
onChange={setInfoIndex}
onChange={setSelectedAssetValue}
onSearch={onSearch}
options={assetOptions}
value={infoIndex}
value={selectedAssetValue}
/>
)
: undefined
Expand Down
Loading