Skip to content

Commit

Permalink
Preselect mascot choice based on choice query param
Browse files Browse the repository at this point in the history
  • Loading branch information
lubej committed Mar 14, 2024
1 parent 9ee758b commit a8b2433
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 13 additions & 3 deletions frontend/src/pages/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useWeb3 } from '../../hooks/useWeb3.ts'
import { Alert } from '../../components/Alert'
import { StringUtils } from '../../utils/string.utils.ts'
import { useAppState } from '../../hooks/useAppState.ts'
import { Navigate } from 'react-router-dom'
import { Navigate, useSearchParams } from 'react-router-dom'
import { DateUtils } from '../../utils/date.utils.ts'
import { MascotChoices } from '../../types'
import { NumberUtils } from '../../utils/number.utils.ts'
Expand All @@ -25,7 +25,14 @@ export const HomePage: FC = () => {
setPreviousVoteForCurrentWallet,
} = useAppState()

const [selectedChoice, setSelectedChoice] = useState<MascotChoices | null>(null)
const [searchParams] = useSearchParams()
const preselectedMascotChoice = searchParams.get('choice') ?? null
const preselectedMascotChoiceNullableInt = NumberUtils.toNullableInt(preselectedMascotChoice)
const preselectMascotChoice = NumberUtils.isValidMascotChoiceId(preselectedMascotChoiceNullableInt)
? preselectedMascotChoiceNullableInt
: null

const [selectedChoice, setSelectedChoice] = useState<MascotChoices | null>(preselectMascotChoice)
const [pageStatus, setPageStatus] = useState<
'loading' | 'error' | 'success' | 'insufficient-balance' | 'vote'
>('vote')
Expand All @@ -38,7 +45,10 @@ export const HomePage: FC = () => {
}, [account])

useEffect(() => {
setSelectedChoice(previousVote)
if (!NumberUtils.isValidMascotChoiceId(selectedChoice)) {
setSelectedChoice(previousVote)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [previousVote])

const actionBtnLabelContent = useMemo(() => {
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/utils/number.utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { MascotChoices } from '../types'

export abstract class NumberUtils {
// Compatible with https://github.com/MetaMask/metamask-extension/blob/v10.7.0/ui/helpers/utils/icon-factory.js#L84-L88
static jsNumberForAddress(address: string) {
const addr = address.slice(2, 10)
return parseInt(addr, 16)
}

static isValidMascotChoiceId(choiceId: number | null) {
static toNullableInt(from: string | null | undefined): number | null {
if (from === null || from === undefined) {
return null
}

const int = parseInt(from, 10)

return Number.isNaN(int) || !Number.isFinite(int) ? null : int
}

static isValidMascotChoiceId(choiceId: number | null): choiceId is MascotChoices {
if (choiceId === null) return false

return Number.isInteger(choiceId) && choiceId >= 0 && choiceId <= 2
Expand Down

0 comments on commit a8b2433

Please sign in to comment.