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 invalid fields on github application #119

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
71 changes: 67 additions & 4 deletions src/components/cards/AppInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import AccountSelectionDialog from '@/components/ui/ledger-account-select'
import { Modal } from '@/components/ui/modal'
import ProgressBar from '@/components/ui/progress-bar'
import { Spinner } from '@/components/ui/spinner'
import calculateAmountToRequest from '@/helpers/calculateAmountToRefill'
import calculateAmountToRequest, {
validateAmount,
} from '@/helpers/calculateAmountToRefill'
import useApplicationActions from '@/hooks/useApplicationActions'
import { useAllocator } from '@/lib/AllocatorProvider'
import { stateColor, stateMapping } from '@/lib/constants'
Expand Down Expand Up @@ -40,7 +42,13 @@ import TextField from '@mui/material/TextField'
import axios from 'axios'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/navigation'
import { useCallback, useEffect, useState } from 'react'
import {
useCallback,
useEffect,
useMemo,
useState,
type ReactNode,
} from 'react'
import { toast } from 'react-toastify'
import AllocatorBalance from '../AllocatorBalance'

Expand Down Expand Up @@ -92,7 +100,7 @@ const AppInfoCard: React.FC<ComponentProps> = ({
mutationRemovePendingAllocation,
} = useApplicationActions(initialApplication, repo, owner)
const [buttonText, setButtonText] = useState('')
const [modalMessage, setModalMessage] = useState<string | null>(null)
const [modalMessage, setModalMessage] = useState<ReactNode | null>(null)
const [error, setError] = useState<boolean>(false)
const [walletConnected, setWalletConnected] = useState(false)
const [isWalletConnecting, setIsWalletConnecting] = useState(false)
Expand Down Expand Up @@ -608,6 +616,30 @@ const AppInfoCard: React.FC<ComponentProps> = ({
}))
}

const totalAmountIsValid = useMemo(
() => validateAmount(application.Datacap['Total Requested Amount']),
[application.Datacap],
)
const weeklyAllocationRequestIsValid = useMemo(
() => validateAmount(application.Datacap['Weekly Allocation']),
[application.Datacap],
)

const createInvalidFieldsModalContent = (
totalAmountIsValid: boolean,
weeklyAllocationRequestIsValid: boolean,
): string => {
let modalMessage = ''
if (!totalAmountIsValid) modalMessage = '"Total Requested Amount"'
if (!weeklyAllocationRequestIsValid) {
if (!totalAmountIsValid) modalMessage += ' and '
modalMessage += '"Weekly Allocation"'
}
modalMessage +=
' is not a valid amount format. Please navigate to the application below and update the field. (e.g 100TiB, 600.46GiB, 1.5PiB)'
return modalMessage
}

useEffect(() => {
// if not the first allocation, prefill the amount with ssa bot suggested value
if (
Expand All @@ -623,7 +655,38 @@ const AppInfoCard: React.FC<ComponentProps> = ({
isDialogOpen: false,
}))
}
}, [application])

if (!totalAmountIsValid || !weeklyAllocationRequestIsValid) {
const modalMessage = createInvalidFieldsModalContent(
totalAmountIsValid,
weeklyAllocationRequestIsValid,
)

const link = (
<a
target="_blank noopener noreferrer"
href={`https://github.com/${owner}/${repo}/issues/${application['Issue Number']}`}
>
https://github.com/{owner}/{repo}/issues/
{application['Issue Number']}
</a>
)
setModalMessage(
<>
{modalMessage}
<br />
{link}
</>,
)
setError(true)
}
}, [
application,
totalAmountIsValid,
weeklyAllocationRequestIsValid,
owner,
repo,
])

const stateLabel =
stateMapping[application.Lifecycle.State as keyof typeof stateMapping] ??
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from './button'
interface ModalProps {
message?: string
message?: React.ReactNode
onClose: () => void
error?: boolean
}
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/calculateAmountToRefill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,8 @@ const splitString = (input: string): [string, string] => {

return ['0', 'B']
}

export const validateAmount = (amount: string): boolean => {
const regex = /^(\d+(\.\d+)?)([A-Za-z]iB)$/
return regex.test(amount)
}
17 changes: 11 additions & 6 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import ByteConverter from '@wtfcode/byte-converter'
import {
type Application,
type AllocationRequest,
type Application,
type ByteConverterAutoscaleOptions,
} from '@/type'
import ByteConverter from '@wtfcode/byte-converter'
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs))
Expand Down Expand Up @@ -44,8 +44,13 @@ export function anyToBytes(inputDatacap: string): number {
.replace(/\s*/g, '')
const ext = formatDc.replace(/[0-9.]/g, '')
const datacap = formatDc.replace(/[^0-9.]/g, '')
const bytes = byteConverter.convert(parseFloat(datacap), ext, 'B')
return bytes
try {
const bytes = byteConverter.convert(parseFloat(datacap), ext, 'B')
return bytes
} catch (e) {
console.error(e)
return 0
}
}

export const getLastDatacapAllocation = (
Expand Down