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

ShowHideGame #800

Merged
merged 1 commit into from
Oct 25, 2024
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
135 changes: 115 additions & 20 deletions packages/app/features/home/TokenBalanceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
BigHeading,
styled,
AnimatePresence,
View,
type XStackProps,
} from '@my/ui'
import { EyeOff, Eye } from '@tamagui/lucide-icons'
import formatAmount from 'app/utils/formatAmount'
Expand Down Expand Up @@ -37,32 +39,48 @@ export const TokenBalanceCard = () => {
const formattedBalance = formatAmount(totalBalance, 9, 0)

const { isPriceHidden, toggleIsPriceHidden } = useIsPriceHidden()
const { isGameVisible, presses, increaseScore, time } = useShowHideGame()

const onShowHidePress = () => {
toggleIsPriceHidden()
increaseScore()
}

return (
<XStack w={'100%'} zIndex={4}>
<YStack jc={'center'} gap={'$4'} w={'100%'}>
<XStack ai={'center'} gap="$2.5" width={'100%'} onPress={toggleIsPriceHidden}>
<XStack ai={'center'} gap="$2.5">
<AnimatePresence exitBeforeEnter>
{isPriceHidden ? <HiddenSquare /> : <GreenSquare />}
</AnimatePresence>
<Label
fontSize={'$4'}
zIndex={1}
fontWeight={'500'}
textTransform={'uppercase'}
lineHeight={0}
col={'$color10'}
>
Total Balance
</Label>
<YStack w="fit-content" gap={'$2.5'}>
<XStack ai={'center'} gap="$2.5" width={'100%'} onPress={onShowHidePress}>
<XStack ai={'center'} gap="$2.5">
<AnimatePresence exitBeforeEnter>
{isPriceHidden ? <HiddenSquare /> : <GreenSquare />}
</AnimatePresence>
<Label
fontSize={'$4'}
zIndex={1}
fontWeight={'500'}
textTransform={'uppercase'}
lineHeight={0}
col={'$color10'}
>
Total Balance
</Label>
</XStack>
{isPriceHidden ? (
<EyeOff color={'$color9'} size={'$1'} />
) : (
<Eye color={'$color11'} size={'$1'} />
)}
</XStack>
{isPriceHidden ? (
<EyeOff color={'$color9'} size={'$1'} />
) : (
<Eye color={'$color11'} size={'$1'} />
{isGameVisible && (
<XStack w="100%" gap={'$2'} jc={'space-between'} ai={'center'} my="auto">
<Paragraph fontSize={'$6'} fontWeight={'500'} zIndex={1} color={'$color10'}>
{presses}
</Paragraph>
<ShowHideGameStopwatch time={time} />
</XStack>
)}
</XStack>
</YStack>
<XStack style={{ color: 'white' }} gap={'$2.5'} mt={'$3'}>
{(() => {
switch (true) {
Expand Down Expand Up @@ -152,3 +170,80 @@ const useIsPriceHidden = () => {

return { isPriceHidden, toggleIsPriceHidden }
}

const useShowHideGame = () => {
const countToStop = 100
const countToVisible = 10

const [isGameVisible, setIsGameVisible] = useState<boolean>(false)
const [presses, setPresses] = useState<number>(0)

const [isPaused, setIsPaused] = useState(false)
const [time, setTime] = useState(0)

useEffect(() => {
if (presses >= countToVisible) {
setIsGameVisible(true)
}
if (presses >= countToStop) {
setIsPaused(true)
}

let interval: NodeJS.Timeout | undefined = undefined
if (isPaused === false) {
interval = setInterval(() => {
setTime((time) => time + 10)
}, 10)
} else {
clearInterval(interval)
}
return () => {
clearInterval(interval)
}
}, [presses, isPaused])

const onPress = () => {
if (isPaused === false) {
setPresses(presses + 1)
}
}

return { isGameVisible, presses, increaseScore: onPress, time }
}

const ShowHideGameStopwatch = ({ time, ...props }: XStackProps & { time: number }) => {
return (
<XStack {...props}>
<Paragraph
fontSize={'$4'}
zIndex={1}
fontWeight={'500'}
textTransform={'uppercase'}
lineHeight={0}
col={'$color10'}
>
{`0 + ${Math.floor((time / 60000) % 60)}`.slice(-2)}:
</Paragraph>
<Paragraph
fontSize={'$4'}
zIndex={1}
fontWeight={'500'}
textTransform={'uppercase'}
lineHeight={0}
col={'$color10'}
>
{`0 + ${Math.floor((time / 1000) % 60)}`.slice(-2)}.
</Paragraph>
<Paragraph
fontSize={'$4'}
zIndex={1}
fontWeight={'500'}
textTransform={'uppercase'}
lineHeight={0}
col={'$color10'}
>
{`0 + ${Math.floor((time / 10) % 100)}`.slice(-2)}
</Paragraph>
</XStack>
)
}
Loading