Skip to content

Commit 60ea9de

Browse files
refactor: improve code quality
1 parent 62c8572 commit 60ea9de

File tree

6 files changed

+75
-36
lines changed

6 files changed

+75
-36
lines changed

src/components/ui/use-toast.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,28 @@ type ToasterToast = ToastProps & {
1212
action?: ToastActionElement;
1313
};
1414

15-
const actionTypes = {
16-
ADD_TOAST: 'ADD_TOAST',
17-
UPDATE_TOAST: 'UPDATE_TOAST',
18-
DISMISS_TOAST: 'DISMISS_TOAST',
19-
REMOVE_TOAST: 'REMOVE_TOAST',
20-
} as const;
21-
2215
let count = 0;
2316

2417
function genId() {
2518
count = (count + 1) % Number.MAX_SAFE_INTEGER;
2619
return count.toString();
2720
}
2821

29-
type ActionType = typeof actionTypes;
30-
3122
type Action =
3223
| {
33-
type: ActionType['ADD_TOAST'];
24+
type: 'ADD_TOAST';
3425
toast: ToasterToast;
3526
}
3627
| {
37-
type: ActionType['UPDATE_TOAST'];
28+
type: 'UPDATE_TOAST';
3829
toast: Partial<ToasterToast>;
3930
}
4031
| {
41-
type: ActionType['DISMISS_TOAST'];
32+
type: 'DISMISS_TOAST';
4233
toastId?: ToasterToast['id'];
4334
}
4435
| {
45-
type: ActionType['REMOVE_TOAST'];
36+
type: 'REMOVE_TOAST';
4637
toastId?: ToasterToast['id'];
4738
};
4839

src/config/config.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ export const LOCAL_STORAGE_PREFIX = 'Web3Messaging';
88
export const CONTACT_URL =
99
'https://airtable.com/appDiKrXe5wJgGpdP/pagm2GF2eNdX2ysw3/form';
1010

11+
// Chain ID constants
12+
export const BELLECOUR_CHAIN_ID = 134;
13+
export const ARBITRUM_CHAIN_ID = 42161;
14+
15+
// Workerpool configuration
16+
export const WORKERPOOL_ADDRESS_OR_ENS = 'prod-v8-learn.main.pools.iexec.eth';
17+
export const WORKERPOOL_MAX_PRICE = 0.1 * 1e9; // 0.1 RLC in wei
18+
1119
/**
1220
* See smart-contract transactions:
1321
* https://blockscout-bellecour.iex.ec/address/0x781482C39CcE25546583EaC4957Fb7Bf04C277D2
1422
*
1523
* See all idapps in the whitelist:
1624
* https://explorer.iex.ec/bellecour/address/0x0c6c77a11068db9fadfba25182e02863361f58da
1725
*/
18-
export const WORKERPOOL_ADDRESS_OR_ENS = 'prod-v8-learn.main.pools.iexec.eth';
19-
2026
export const SUPPORTED_CHAINS = [
2127
{
22-
id: 134,
28+
id: BELLECOUR_CHAIN_ID,
2329
name: 'Bellecour',
2430
slug: 'bellecour',
2531
color: '#F4942566',
@@ -37,7 +43,7 @@ export const SUPPORTED_CHAINS = [
3743
},
3844
},
3945
{
40-
id: 42161,
46+
id: ARBITRUM_CHAIN_ID,
4147
name: 'Arbitrum',
4248
slug: 'arbitrum-mainnet',
4349
color: '#F4942566',

src/hooks/useWatchAccount.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ export function useWatchAccount() {
1515
useUserStore();
1616

1717
useEffect(() => {
18-
// Update userStore
1918
setConnector(connector);
2019
setIsConnected(isConnected);
2120
setAddress(address);
22-
if (accountChain?.id && chainId !== accountChain?.id) {
23-
setTimeout(() => {
24-
setChainId(accountChain?.id);
25-
}, 10);
21+
22+
if (accountChain?.id && chainId !== accountChain.id) {
23+
setChainId(accountChain.id);
2624
}
2725

2826
// Update dataProtector client
@@ -31,5 +29,16 @@ export function useWatchAccount() {
3129
return;
3230
}
3331
cleanIExecSDKs();
34-
}, [connector, status, address, accountChain]);
32+
}, [
33+
connector,
34+
status,
35+
address,
36+
accountChain,
37+
chainId,
38+
isConnected,
39+
setConnector,
40+
setIsConnected,
41+
setAddress,
42+
setChainId,
43+
]);
3544
}

src/utils/chain.utils.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ export function getSupportedChains() {
44
return SUPPORTED_CHAINS;
55
}
66

7-
export function getSubgraphUrl(chainId: number) {
8-
const subgraphUrl = getSupportedChains().find(
9-
(chain) => chain.id === chainId
10-
)?.subgraphUrl;
11-
if (!subgraphUrl) {
7+
export function getSubgraphUrl(chainId: number): string {
8+
const chain = getSupportedChains().find((chain) => chain.id === chainId);
9+
if (!chain?.subgraphUrl) {
1210
throw new Error(`Subgraph URL not found for chain ID: ${chainId}`);
1311
}
14-
return subgraphUrl;
12+
return chain.subgraphUrl;
1513
}
1614

17-
export function getWhitelistAddresses(chainId: number | undefined) {
15+
export function getWhitelistAddresses(chainId: number | undefined): {
16+
web3mail: string;
17+
web3telegram: string;
18+
} {
1819
if (chainId === undefined) {
1920
throw new Error('Chain ID is required to get whitelist addresses');
2021
}
22+
2123
const chain = getSupportedChains().find((chain) => chain.id === chainId);
2224
if (!chain?.whitelist) {
2325
throw new Error(`Whitelist not found for chain ID: ${chainId}`);
2426
}
27+
2528
return chain.whitelist;
2629
}
2730

@@ -33,7 +36,24 @@ export function getChainFromId(id: number | undefined) {
3336
return getSupportedChains().find((c) => c.id === id);
3437
}
3538

36-
export function getBlockExplorerUrl(chainId: number) {
39+
export function getBlockExplorerUrl(chainId: number): string {
40+
const chain = getChainFromId(chainId);
41+
if (!chain?.blockExplorerUrl) {
42+
console.warn(
43+
`Block explorer URL not found for chain ID: ${chainId}, using default`
44+
);
45+
return 'https://blockscout.com/';
46+
}
47+
return chain.blockExplorerUrl;
48+
}
49+
50+
// Helper function to check if a chain is supported
51+
export function isChainSupported(chainId: number): boolean {
52+
return getSupportedChains().some((chain) => chain.id === chainId);
53+
}
54+
55+
// Helper function to get chain name
56+
export function getChainName(chainId: number): string {
3757
const chain = getChainFromId(chainId);
38-
return chain?.blockExplorerUrl ?? 'https://blockscout.com/';
58+
return chain?.name || `Unknown Chain (${chainId})`;
3959
}

src/utils/getUserFriendlyStatues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function getUserFriendlyStatues(status: string) {
2-
const statusMessages = {
2+
const statusMessages: { [key: string]: string } = {
33
DEPLOY_PROTECTED_DATA:
44
'Create protected data into DataProtector registry smart-contract',
55
PUSH_SECRET_TO_SMS: 'Push protected data encryption key to iExec SMS',

src/views/contact/sendMessage.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { WORKERPOOL_ADDRESS_OR_ENS } from '@/config/config';
1+
import {
2+
WORKERPOOL_ADDRESS_OR_ENS,
3+
BELLECOUR_CHAIN_ID,
4+
WORKERPOOL_MAX_PRICE,
5+
} from '@/config/config';
26
import { Address } from '@/types';
37
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
48
import { useState } from 'react';
@@ -73,6 +77,13 @@ export default function SendMessage() {
7377
protectedData.data?.schema &&
7478
getDataType(protectedData.data?.schema) === 'mail';
7579

80+
// Helper function to create workerpool configuration
81+
const getWorkerpoolConfig = () => ({
82+
workerpoolAddressOrEns:
83+
chainId === BELLECOUR_CHAIN_ID ? WORKERPOOL_ADDRESS_OR_ENS : undefined,
84+
workerpoolMaxPrice: WORKERPOOL_MAX_PRICE,
85+
});
86+
7687
const handleChange = (e: { target: { name: string; value: string } }) => {
7788
const { name, value } = e.target;
7889
setFormData((prevData) => ({
@@ -116,7 +127,8 @@ export default function SendMessage() {
116127
senderName: formData.senderName,
117128
contentType: formData.contentType,
118129
emailContent: formData.messageContent,
119-
workerpoolAddressOrEns: WORKERPOOL_ADDRESS_OR_ENS,
130+
workerpoolAddressOrEns: getWorkerpoolConfig().workerpoolAddressOrEns,
131+
workerpoolMaxPrice: getWorkerpoolConfig().workerpoolMaxPrice,
120132
});
121133
return sendMail;
122134
} else {
@@ -126,7 +138,8 @@ export default function SendMessage() {
126138
protectedData: protectedDataAddress!,
127139
senderName: formData.senderName,
128140
telegramContent: formData.messageContent,
129-
workerpoolAddressOrEns: WORKERPOOL_ADDRESS_OR_ENS,
141+
workerpoolAddressOrEns: getWorkerpoolConfig().workerpoolAddressOrEns,
142+
workerpoolMaxPrice: getWorkerpoolConfig().workerpoolMaxPrice,
130143
});
131144
return sendTelegram;
132145
}

0 commit comments

Comments
 (0)