Skip to content

Commit

Permalink
[Updated] Explicitly type location.state
Browse files Browse the repository at this point in the history
  • Loading branch information
greedyboi committed Jan 24, 2024
1 parent ad47f74 commit f1a3b54
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/layout/MainLayout/MainLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { isMobile } from 'react-device-detect';
import { Outlet, ScrollRestoration, useLocation } from 'react-router-dom';
import { useDispatch, useSelector, useStore } from 'react-redux';
Expand Down Expand Up @@ -32,6 +32,12 @@ const MainLayout = () => {
const store = useStore();
const visibilityState = useVisibilityState();

const autoConnect = useMemo(() => {
const state = location.state as { autoConnect?: boolean };

return state.autoConnect;
}, [location]);

useEffect(() => {
const autoConnect = async (provider: WalletProvider) => {
await dispatch.wallet.connectWallet({ provider, silent: enableAutoConnect }).finally(() => null);
Expand Down Expand Up @@ -68,7 +74,7 @@ const MainLayout = () => {
setEnableAutoConnect(false);
}

if (location.state?.autoConnect) {
if (autoConnect) {
setEnableAutoConnect(true);
}
}, [location]);
Expand Down
12 changes: 9 additions & 3 deletions src/pages/Deposit/Deposit.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';

import { useParams, unstable_useBlocker as useBlocker, useBeforeUnload, useLocation } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
Expand Down Expand Up @@ -70,6 +70,12 @@ const Deposit = () => {
const prevStep = usePrevious(currentStep);
const { isDark } = useColorScheme();

const amountToDeposit = useMemo(() => {
const state = location.state as { amountToDeposit?: string };

return state.amountToDeposit;
}, [location]);

const wcConfig: WalletClientContext = {
userAddress: lumWallet?.address,
connectWallet: () => {
Expand Down Expand Up @@ -150,7 +156,7 @@ const Deposit = () => {

const transferForm = useFormik({
initialValues: {
amount: location.state?.amountToDeposit || '',
amount: amountToDeposit || '',
},
validationSchema: yup.object().shape({
amount: yup
Expand Down Expand Up @@ -973,7 +979,7 @@ const Deposit = () => {
price={prices?.[denom || ''] || 0}
lumWallet={lumWallet}
otherWallets={otherWallets}
amountFromLocationState={location.state?.amountToDeposit}
amountFromLocationState={Number(amountToDeposit)}
/>
{isShareStep && (
<Lottie
Expand Down
16 changes: 12 additions & 4 deletions src/pages/MySavings/MySavings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Navigate, useLocation } from 'react-router-dom';
import numeral from 'numeral';
Expand Down Expand Up @@ -48,9 +48,15 @@ const MySavings = () => {
const location = useLocation();
const dispatch = useDispatch<Dispatch>();

const leaderboardPoolId = useMemo(() => {
const state = location.state as { leaderboardPoolId?: string };

return state.leaderboardPoolId;
}, [location]);

const [assetToTransferOut, setAssetToTransferOut] = useState<string | null>(null);
const [depositToLeave, setDepositToLeave] = useState<DepositModel | null>(null);
const [leaderboardSelectedPoolId, setLeaderboardSelectedPoolId] = useState<string | null>(pools && pools.length > 0 ? location.state?.leaderboardPoolId || pools[0].poolId.toString() : null);
const [leaderboardSelectedPoolId, setLeaderboardSelectedPoolId] = useState<string | null>(pools && pools.length > 0 ? leaderboardPoolId || pools[0].poolId.toString() : null);
const [leaderboardPage, setLeaderboardPage] = useState(0);
const [userRankItems, setUserRankItems] = useState<LeaderboardItemModel[]>();
const [prizesHistoryPage, setPrizesHistoryPage] = useState(1);
Expand Down Expand Up @@ -82,14 +88,16 @@ const MySavings = () => {
getLeaderboardUserRank().finally(() => null);
}

if (location.state?.leaderboardPoolId && leaderboardSectionRef.current) {
if (leaderboardPoolId && leaderboardSectionRef.current) {
leaderboardSectionRef.current.scrollIntoView();
}
}, [isReloadingInfos, leaderboardPool, lumWallet]);

useEffect(() => {
if (pools && pools.length > 0) {
setLeaderboardSelectedPoolId(location.state?.leaderboardPoolId || pools[0].poolId.toString());
const poolId = leaderboardPoolId || pools[0].poolId.toString();

setLeaderboardSelectedPoolId(poolId);
ScrollTrigger.refresh();
}
}, [pools]);
Expand Down

0 comments on commit f1a3b54

Please sign in to comment.