2023-08-03 07:53:58 +00:00
|
|
|
/**
|
|
|
|
* Asserts that a case in a switch statement is unreachable.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Exhaustively checks `user.role` type ("admin" | "user") and will not compile
|
|
|
|
* // if a new role is added without adding a case to the switch statement.
|
|
|
|
* switch (user.role) {
|
|
|
|
* case "admin":
|
|
|
|
* break;
|
|
|
|
* case "user":
|
|
|
|
* break;
|
|
|
|
* default:
|
|
|
|
* assertUnreachable(user.role);
|
|
|
|
* }
|
|
|
|
*/
|
2023-09-16 03:24:51 +00:00
|
|
|
import { DateRange } from 'react-day-picker'
|
|
|
|
|
2023-08-03 07:53:58 +00:00
|
|
|
export function assertUnreachable(value: never): never {
|
2023-08-30 07:49:18 +00:00
|
|
|
throw new Error(`Unreachable case: ${value}`)
|
2023-08-03 07:53:58 +00:00
|
|
|
}
|
2023-08-16 11:22:22 +00:00
|
|
|
|
|
|
|
export const convertSecondsToTimerFormat = (seconds: number) => {
|
2023-08-30 07:49:18 +00:00
|
|
|
const minutes = Math.floor(seconds / 60)
|
|
|
|
const remainingSeconds = seconds % 60
|
2024-01-29 08:24:57 +00:00
|
|
|
return `${String(minutes).padStart(2, '0')}:${String(
|
|
|
|
remainingSeconds,
|
|
|
|
).padStart(2, '0')}`
|
2023-08-30 07:49:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 06:53:54 +00:00
|
|
|
export const formatNumbersWithComa = (n: number): string => {
|
2024-03-08 06:08:11 +00:00
|
|
|
const isWholeNumber = n % 1 === 0
|
|
|
|
const numberString = isWholeNumber ? n.toString() : n.toFixed(2)
|
|
|
|
const parts = numberString.split('.')
|
2023-09-26 10:14:44 +00:00
|
|
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
|
|
return parts.join('.')
|
|
|
|
}
|
2023-09-16 03:24:51 +00:00
|
|
|
|
|
|
|
export const getMonthIndicesFromRange = (range: DateRange) => {
|
|
|
|
if (!range.from || !range.to) return [0, 11]
|
|
|
|
|
|
|
|
return [range.from.getMonth(), range.to.getMonth()]
|
2023-08-30 07:49:18 +00:00
|
|
|
}
|
2023-09-22 14:10:10 +00:00
|
|
|
|
|
|
|
export const getFormattedValidatorAddress = (address: string) => {
|
|
|
|
// zQ3asdf9d4Gs0 -> zQ3...9d4Gs0
|
|
|
|
const start = address.slice(0, 3)
|
|
|
|
const end = address.slice(-6)
|
|
|
|
return `${start}...${end}`
|
|
|
|
}
|
2023-09-22 14:33:14 +00:00
|
|
|
|
|
|
|
export const getFormattedWalletAddress = (address: string) => {
|
|
|
|
// 0xb9dasdfc35 -> 0xb9d...c35
|
|
|
|
return `${address.slice(0, 5)}...${address.slice(-3)}`
|
|
|
|
}
|
2023-10-11 18:43:25 +00:00
|
|
|
|
2023-10-11 18:45:48 +00:00
|
|
|
export const formatToFixed4 = (value: string) => {
|
|
|
|
const decimalPart = value.split('.')[1]
|
2023-10-11 18:43:25 +00:00
|
|
|
const decimalLength = decimalPart ? decimalPart.length : 0
|
|
|
|
|
|
|
|
if (decimalLength > 4) {
|
2023-10-11 18:45:48 +00:00
|
|
|
return Number(value).toFixed(4)
|
2023-10-11 18:43:25 +00:00
|
|
|
}
|
2023-10-11 18:45:48 +00:00
|
|
|
return value
|
2023-10-11 18:43:25 +00:00
|
|
|
}
|
2023-10-18 05:27:38 +00:00
|
|
|
|
|
|
|
export const getHeightPercentages = (amountOfElements: number) => {
|
2023-10-18 05:37:56 +00:00
|
|
|
let percentages = 100
|
2023-10-18 05:27:38 +00:00
|
|
|
|
2023-10-18 05:37:56 +00:00
|
|
|
if (amountOfElements > 1) {
|
2023-10-18 05:27:38 +00:00
|
|
|
percentages = 100 / amountOfElements
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${percentages}%`
|
2023-10-18 05:37:56 +00:00
|
|
|
}
|
2024-01-24 07:46:40 +00:00
|
|
|
|
|
|
|
export const isAddressValid = (address: string) => {
|
|
|
|
return address.length > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isPortValid = (port: string) => {
|
|
|
|
if (port.length === 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !isNaN(Number(port))
|
|
|
|
}
|
2024-01-24 18:39:20 +00:00
|
|
|
|
2024-01-18 15:25:58 +00:00
|
|
|
export const copyFunction = (text: string) => {
|
|
|
|
navigator.clipboard.writeText(text)
|
2024-01-18 16:50:07 +00:00
|
|
|
}
|
2024-02-27 12:37:22 +00:00
|
|
|
|
|
|
|
export const getDepositTitle = ({
|
|
|
|
isChainParity,
|
|
|
|
isTransactionConfirmation,
|
|
|
|
}: {
|
|
|
|
isChainParity: boolean
|
|
|
|
isTransactionConfirmation: boolean
|
|
|
|
}) => {
|
|
|
|
if (isChainParity) {
|
|
|
|
return 'Activate Validators'
|
|
|
|
} else if (isTransactionConfirmation) {
|
|
|
|
return 'Transaction Confirmation'
|
|
|
|
} else {
|
|
|
|
return 'Deposit Funds'
|
|
|
|
}
|
|
|
|
}
|