-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from novasamatech/feat/dashboard-layout
Feat: dashboard layout
- Loading branch information
Showing
34 changed files
with
542 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React, { createContext, useContext, useState } from 'react'; | ||
import { HexString } from '../types'; | ||
|
||
export interface IContext { | ||
publicKey?: HexString; | ||
setPublicKey: React.Dispatch<React.SetStateAction<HexString | undefined>>; | ||
} | ||
|
||
export const GlobalContext = createContext<IContext>({ setPublicKey: () => {} }); | ||
|
||
export const GlobalStateProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [publicKey, setPublicKey] = useState<HexString>(); | ||
|
||
const value = { publicKey, setPublicKey }; | ||
|
||
return <GlobalContext.Provider value={value}>{children}</GlobalContext.Provider>; | ||
}; | ||
|
||
export const useGlobalContext = () => useContext(GlobalContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
const ZERO_BALANCE = '0'; | ||
|
||
type FormattedBalance = { | ||
formattedValue: string; | ||
suffix: string; | ||
decimalPlaces: number; | ||
}; | ||
|
||
const enum Suffix { | ||
MILLIONS = 'M', | ||
BILLIONS = 'B', | ||
TRILLIONS = 'T', | ||
} | ||
|
||
export const enum Decimal { | ||
SMALL_NUMBER = 5, | ||
BIG_NUMBER = 2, | ||
} | ||
|
||
// format balance from spektr | ||
|
||
export const formatBalance = (balance = '0', precision = 0): FormattedBalance => { | ||
const BNWithConfig = BigNumber.clone(); | ||
BNWithConfig.config({ | ||
// HOOK: for divide with decimal part | ||
DECIMAL_PLACES: precision || Decimal.SMALL_NUMBER, | ||
ROUNDING_MODE: BNWithConfig.ROUND_DOWN, | ||
FORMAT: { | ||
decimalSeparator: '.', | ||
groupSeparator: '', | ||
}, | ||
}); | ||
const TEN = new BNWithConfig(10); | ||
const bnPrecision = new BNWithConfig(precision); | ||
const bnBalance = new BNWithConfig(balance).div(TEN.pow(bnPrecision)); | ||
let divider = new BNWithConfig(1); | ||
let decimalPlaces = 0; | ||
let suffix = ''; | ||
|
||
if (bnBalance.lt(1)) { | ||
decimalPlaces = Math.max(precision - balance.toString().length + 1, 5); | ||
} else if (bnBalance.lt(10)) { | ||
decimalPlaces = Decimal.SMALL_NUMBER; | ||
} else if (bnBalance.lt(1_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
} else if (bnBalance.lt(1_000_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(6)); | ||
suffix = Suffix.MILLIONS; | ||
} else if (bnBalance.lt(1_000_000_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(9)); | ||
suffix = Suffix.BILLIONS; | ||
} else { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(12)); | ||
suffix = Suffix.TRILLIONS; | ||
} | ||
|
||
return { | ||
formattedValue: new BNWithConfig(bnBalance).div(divider).decimalPlaces(decimalPlaces).toFormat(), | ||
suffix, | ||
decimalPlaces, | ||
}; | ||
}; | ||
|
||
const getDecimalPlaceForFirstNonZeroChar = (value: string, nonZeroDigits = 3) => { | ||
const decimalPart = value.toString().split('.')[1]; | ||
|
||
return Math.max((decimalPart || '').search(/[1-9]/) + nonZeroDigits, 5); | ||
}; | ||
|
||
export const formatFiatBalance = (balance = '0', precision = 0): FormattedBalance => { | ||
if (Number(balance) === 0 || isNaN(Number(balance))) { | ||
return { formattedValue: ZERO_BALANCE, suffix: '', decimalPlaces: 0 }; | ||
} | ||
const BNWithConfig = BigNumber.clone(); | ||
BNWithConfig.config({ | ||
ROUNDING_MODE: BNWithConfig.ROUND_DOWN, | ||
FORMAT: { | ||
decimalSeparator: '.', | ||
groupSeparator: '', | ||
}, | ||
}); | ||
|
||
const bnPrecision = new BNWithConfig(precision); | ||
const TEN = new BNWithConfig(10); | ||
const bnBalance = new BNWithConfig(balance).div(TEN.pow(bnPrecision)); | ||
|
||
let divider = new BNWithConfig(1); | ||
let suffix = ''; | ||
let decimalPlaces: number; | ||
|
||
if (bnBalance.lt(1)) { | ||
// if number has more than 7 digits in decimal part BigNumber.toString returns number in scientific notation | ||
decimalPlaces = getDecimalPlaceForFirstNonZeroChar(bnBalance.toFixed()); | ||
} else if (bnBalance.lt(10)) { | ||
decimalPlaces = Decimal.SMALL_NUMBER; | ||
} else if (bnBalance.lt(1_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
} else if (bnBalance.lt(1_000_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(6)); | ||
suffix = Suffix.MILLIONS; | ||
} else if (bnBalance.lt(1_000_000_000_000)) { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(9)); | ||
suffix = Suffix.BILLIONS; | ||
} else { | ||
decimalPlaces = Decimal.BIG_NUMBER; | ||
divider = TEN.pow(new BNWithConfig(12)); | ||
suffix = Suffix.TRILLIONS; | ||
} | ||
|
||
const bnFiatBalance = new BNWithConfig(bnBalance).div(divider).decimalPlaces(decimalPlaces).toFormat(); | ||
|
||
return { | ||
formattedValue: bnFiatBalance, | ||
suffix, | ||
decimalPlaces, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { cnTw } from '@/common/utils/twMerge'; | ||
import { formatBalance } from '@/common/utils/balance'; | ||
import Icon from '../Icon/Icon'; | ||
import { CaptionText } from '../Typography'; | ||
|
||
type Props = { | ||
value: string; | ||
asset: any; | ||
className?: string; | ||
showIcon?: boolean; | ||
imgClassName?: string; | ||
wrapperClassName?: string; | ||
}; | ||
|
||
export const AssetBalance = ({ value, asset, className, imgClassName }: Props) => { | ||
const { precision, symbol } = asset; | ||
const { formattedValue, suffix } = formatBalance(value, precision); | ||
|
||
console.log(suffix, formattedValue); | ||
|
||
return ( | ||
<span className={cnTw('flex items-center gap-x-2', className)}> | ||
<Icon name={symbol} size={40} alt="logo" className={imgClassName} /> | ||
<CaptionText>{symbol}</CaptionText> | ||
<CaptionText> | ||
{formattedValue} {suffix} | ||
</CaptionText> | ||
</span> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { AssetWithBalance } from '@/common/chainRegistry/types'; | ||
import { AssetBalance } from './Asset'; | ||
|
||
type Props = { | ||
assets: AssetWithBalance[]; | ||
}; | ||
|
||
const AssetsList = ({ assets }: Props) => ( | ||
<div className="flex flex-col gap-6 mt-4"> | ||
{assets.map((asset) => ( | ||
<AssetBalance | ||
asset={asset} | ||
value={asset.balance} | ||
className="grid grid-cols-[50px,1fr,auto] items-center" | ||
key={asset.assetId} | ||
/> | ||
))} | ||
</div> | ||
); | ||
|
||
export default AssetsList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.