diff --git a/frontend/components/Main/MainRewards.tsx b/frontend/components/Main/MainRewards.tsx index 585bf8ae2..b9f9a1423 100644 --- a/frontend/components/Main/MainRewards.tsx +++ b/frontend/components/Main/MainRewards.tsx @@ -1,6 +1,6 @@ import { Button, Col, Flex, Modal, Row, Skeleton, Tag, Typography } from 'antd'; import Image from 'next/image'; -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; import { useTimeout } from 'usehooks-ts'; @@ -10,7 +10,7 @@ import { useBalance } from '@/hooks'; import { useElectronApi } from '@/hooks/useElectronApi'; import { useReward } from '@/hooks/useReward'; -import { Confetti } from '../../common-util/Confetti'; +import { ConfettiAnimation } from '../common/ConfettiAnimation'; const { Text, Title } = Typography; @@ -84,6 +84,7 @@ const NotifyRewards = () => { const [canShowNotification, setCanShowNotification] = useState(false); + // TODO: remove just to test the notification useTimeout(() => setCanShowNotification(true), 1000); useEffect(() => { @@ -92,10 +93,12 @@ const NotifyRewards = () => { if (!isEligibleForRewards) return; if (hasAlreadyNotified) return; + if (!availableRewardsForEpochEther) return; - setCanShowNotification(!!availableRewardsForEpochEther); + setCanShowNotification(true); }, [isEligibleForRewards, availableRewardsForEpochEther, showNotification]); + // hook to show app notification useEffect(() => { if (!canShowNotification) return; @@ -103,9 +106,12 @@ const NotifyRewards = () => { 'Your agent earned its first staking rewards!', `Congratulations! Your agent just got the first reward for you! Your current balance: ${availableRewardsForEpochEther} OLAS`, ); + }, [canShowNotification, availableRewardsForEpochEther, showNotification]); + const closeNotificationModal = useCallback(() => { + setCanShowNotification(false); // TODO: add setter for hasAlreadyNotified - }, [canShowNotification, availableRewardsForEpochEther, showNotification]); + }, []); if (!canShowNotification) return null; @@ -113,17 +119,22 @@ const NotifyRewards = () => { setCanShowNotification(false)} + onCancel={closeNotificationModal} footer={[ , ]} > - + { +export const ConfettiAnimation = () => { const animationInstance = useRef(null); - const makeShot = (particleRatio, opts) => { - animationInstance && - animationInstance.current({ - ...opts, - origin: { y: 0.45 }, - particleCount: Math.floor(200 * particleRatio), - }); - }; + const makeShot = useCallback((particleRatio, opts) => { + if (!animationInstance.current) return; - const fire = () => { + animationInstance.current({ + ...opts, + origin: { y: 0.45 }, + particleCount: Math.floor(200 * particleRatio), + }); + }, []); + + const fire = useCallback(() => { makeShot(0.25, { spread: 26, startVelocity: 55 }); makeShot(0.2, { spread: 60 }); makeShot(0.35, { spread: 80, decay: 0.91, scalar: 0.8 }); makeShot(0.1, { spread: 100, startVelocity: 25, decay: 0.92, scalar: 1.2 }); makeShot(0.1, { spread: 100, startVelocity: 45 }); - }; + }, [makeShot]); - const getInstance = (instance) => { + const getInstance = useCallback((instance) => { animationInstance.current = instance; - }; + }, []); + // Fire confetti every 2.5 seconds useInterval(() => fire(), 2500); return ;