Skip to content

Commit 1cb29dd

Browse files
committed
feat: add reset metamask permission when button clicked
1 parent 0ab1f5b commit 1cb29dd

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

src/components/connect-metamask.tsx

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export const ConnectMetmask = ({}: ConnectMetmaskProps) => {
3232
}))
3333
);
3434
const isLoading = provider.loading || network.loading || address.loading || balance.loading;
35-
const isActive =
36-
provider.data && ARBITRUM_NETWORK.some((item) => item.chainId === network.data) && address.data && balance.data;
3735

3836
/**
3937
* 1. user가 arbitrum chain에 연결되어 있다면 user의 현재 network 정보를 가져오고, project의 network를 user의 network로 변경
@@ -42,6 +40,7 @@ export const ConnectMetmask = ({}: ConnectMetmaskProps) => {
4240
*/
4341
const getAccountInfo = async () => {
4442
const networkFetched = await fetchNetwork();
43+
console.log("networkFetched", networkFetched);
4544
const arbitrumNetwork = ARBITRUM_NETWORK.find((item) => item.chainId === networkFetched);
4645
if (arbitrumNetwork) {
4746
project.setNetwork(arbitrumNetwork);
@@ -52,6 +51,19 @@ export const ConnectMetmask = ({}: ConnectMetmaskProps) => {
5251
}
5352
};
5453

54+
const removePermission = async () => {
55+
if (provider.data) {
56+
try {
57+
await provider.data.request({
58+
method: "wallet_requestPermissions",
59+
params: [{ eth_accounts: {}, "endowment:permitted-chains": {} }],
60+
});
61+
} catch (error) {
62+
console.log(error);
63+
}
64+
}
65+
};
66+
5567
const traceSwitchProjectNetwork = async () => {
5668
await switchNetwork(project.network.data.chainId);
5769
await fetchAddress();
@@ -92,20 +104,6 @@ export const ConnectMetmask = ({}: ConnectMetmaskProps) => {
92104
await fetchNetwork();
93105
}
94106
};
95-
const traceSwitchAccount = async (accounts: unknown) => {
96-
if (Array.isArray(accounts) && accounts.length > 0) {
97-
await fetchAddress();
98-
await fetchBalance();
99-
}
100-
};
101-
const traceSwitchNetwork = async (chainId: unknown) => {
102-
if (typeof chainId === "string") {
103-
const targetNetwork = ARBITRUM_NETWORK.find((network) => network.chainId === chainId);
104-
if (!targetNetwork) return;
105-
project.setNetwork(targetNetwork);
106-
await fetchBalance();
107-
}
108-
};
109107

110108
useEffect(() => {
111109
if (!provider.data) {
@@ -115,24 +113,22 @@ export const ConnectMetmask = ({}: ConnectMetmaskProps) => {
115113

116114
getAccountInfo();
117115

118-
provider.data.on("accountsChanged", traceSwitchAccount);
119-
provider.data.on("chainChanged", traceSwitchNetwork);
116+
// window.addEventListener("message", traceSwitchNetwork);
120117

121-
return () => {
122-
if (!provider.data) return;
123-
provider.data.removeListener("accountsChanged", traceSwitchAccount);
124-
provider.data.removeListener("chainChanged", traceSwitchNetwork);
125-
};
118+
// return () => {
119+
// window.removeEventListener("message", traceSwitchNetwork);
120+
// };
126121
}, []);
127122

128123
return (
129124
<div className="flex flex-col gap-2">
130125
<Button
131-
className={`px-[1.25rem] py-[0.75rem] w-full relative flex justify-center items-center ${
132-
isActive ? "bg-metamask-active" : "bg-metamask-default"
133-
} border-0 rounded-sm overflow-hidden`}
126+
className={`px-[1.25rem] py-[0.75rem] w-full relative flex justify-center items-center bg-metamask-default border-0 rounded-sm overflow-hidden`}
134127
disabled={isLoading}
135-
onClick={getAccountInfo}
128+
onClick={async () => {
129+
await removePermission();
130+
await getAccountInfo();
131+
}}
136132
>
137133
<img className="w-[25px] mr-[10px]" src={metamask} alt="metamask" />
138134
<b>Connect to MetaMask</b>

src/components/contracts/contract-address.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const ContractAddress = ({}: ContractAddressProps) => {
9090
>
9191
<Button
9292
className="relative border-0"
93-
variant="primary"
93+
variant="secondary"
9494
size="sm"
9595
disabled={contractLoading}
9696
onClick={handleAtAddressOnClick}

src/components/project/index.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,28 @@ const UploadCode = () => {
568568

569569
const Icon = ({ verified }: { verified: boolean | null }) =>
570570
!address || verified === null ? (
571-
<input
572-
className="size-4"
573-
type="checkbox"
574-
id="uploadCodeCheckbox"
575-
disabled={deployLoading}
576-
checked={upload}
577-
onChange={handleUploadOnChange}
578-
/>
571+
<div className="m-0 flex items-center">
572+
<input
573+
className="peer hidden"
574+
type="checkbox"
575+
id="uploadCodeCheckbox"
576+
disabled={deployLoading}
577+
checked={upload}
578+
onChange={handleUploadOnChange}
579+
/>
580+
<label
581+
htmlFor="uploadCodeCheckbox"
582+
className={`
583+
m-0
584+
w-4 h-4
585+
border-[1px] border-gray-500 rounded-sm
586+
bg-gray-700
587+
peer-checked:bg-checked-circle
588+
peer-checked:bg-no-repeat
589+
peer-checked:bg-center
590+
`}
591+
/>
592+
</div>
579593
) : verified ? (
580594
<FaCheck className="text-success cursor-pointer" onClick={reset} />
581595
) : (

tailwind.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ module.exports = {
33
content: ["./src/**/*.{js,jsx,ts,tsx}"],
44
theme: {
55
extend: {
6+
backgroundImage: {
7+
"checked-circle":
8+
"url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-2 -2 12 12'%3e%3cpath fill='%23fff' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3e%3c/svg%3e\")",
9+
},
610
colors: {
711
"metamask-active": "#007aa6",
812
"metamask-default": "#2a2c3f",

0 commit comments

Comments
 (0)