Skip to content

Commit 62c8572

Browse files
committed
feat: implement useChainChangeRedirect hook and integrate it into SendMessage and ProtectedData components
1 parent 850832e commit 62c8572

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect, useRef } from 'react';
2+
import { useNavigate } from 'react-router-dom';
3+
import useUserStore from '@/stores/useUser.store';
4+
5+
export function useChainChangeRedirect(redirectPath: string) {
6+
const navigate = useNavigate();
7+
const { chainId } = useUserStore();
8+
const initialChainId = useRef<number | undefined>(undefined);
9+
const hasInitialized = useRef(false);
10+
11+
useEffect(() => {
12+
if (!hasInitialized.current && chainId !== undefined) {
13+
initialChainId.current = chainId;
14+
hasInitialized.current = true;
15+
return;
16+
}
17+
18+
if (
19+
hasInitialized.current &&
20+
initialChainId.current !== undefined &&
21+
chainId !== undefined &&
22+
chainId !== initialChainId.current
23+
) {
24+
navigate(redirectPath);
25+
}
26+
}, [chainId, redirectPath, navigate]);
27+
}

src/views/contact/sendMessage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getWeb3mailClient,
2323
getWeb3telegramClient,
2424
} from '@/externals/iexecSdkClient';
25+
import { useChainChangeRedirect } from '@/hooks/useChainChangeRedirect';
2526
import { useSendMessageStore } from '@/stores/useSendMessage.store';
2627
import useUserStore from '@/stores/useUser.store';
2728
import { pluralize } from '@/utils/pluralize';
@@ -35,6 +36,8 @@ export default function SendMessage() {
3536
protectedDataAddress: Address;
3637
}>();
3738

39+
useChainChangeRedirect('/contacts');
40+
3841
const [formData, setFormData] = useState({
3942
senderName: '',
4043
messageSubject: '',

src/views/myData/protectedData.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DocLink } from '@/components/DocLink';
88
import { PaginatedNavigation } from '@/components/PaginatedNavigation';
99
import { Button, buttonVariants } from '@/components/ui/button';
1010
import { getDataProtectorCoreClient } from '@/externals/iexecSdkClient';
11+
import { useChainChangeRedirect } from '@/hooks/useChainChangeRedirect';
1112
import CheckSMSRequestSuccess from '@/modules/myData/CheckSMSRequestSuccess';
1213
import RevokeAccess from '@/modules/myData/RevokeAccess';
1314
import GrantAccessModal from '@/modules/myData/protectedData/GrantAccessModal';
@@ -49,6 +50,8 @@ export default function ProtectedData() {
4950
const [currentPage, setCurrentPage] = useState(0);
5051
const [isGrantAccessModalOpen, setIsGrantAccessModalOpen] = useState(false);
5152

53+
useChainChangeRedirect('/my-data');
54+
5255
const protectedData = useQuery({
5356
queryKey: ['protectedData', protectedDataAddress, userAddress, chainId],
5457
queryFn: async () => {

0 commit comments

Comments
 (0)