Skip to content

Commit 6b71761

Browse files
authored
Merge pull request #8 from anwen/feat/menu
refactor: improve login logic when load website
2 parents 9ed4619 + 4c6b8fe commit 6b71761

25 files changed

+514
-412
lines changed

.eslintrc

-25
This file was deleted.

.eslintrc.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "next/core-web-vitals",
3+
"rules": {
4+
"semi": [2, "never"],
5+
"space-before-blocks": "error",
6+
"space-before-function-paren": [2, "never"],
7+
"object-curly-spacing": ["error", "always"],
8+
"indent": ["error", 2]
9+
}
10+
}

components/Navigation.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Link from "next/link";
1+
import Link from "next/link"
22
import { useRouter } from "next/router"
33

44
const config = [
@@ -34,10 +34,9 @@ const config = [
3434

3535
export const Navigation = () => {
3636
const router = useRouter()
37-
console.log(router.asPath);
3837
const isActivePath = (path: string) => router.asPath === path
3938
return (
40-
<div className="flex mt-4">
39+
<div className="flex mt-4 grow">
4140
{ config.map((item) => {
4241
return (
4342
<Link key={item.path} href={item.path}>

constants/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const RPC_URLS = [
2+
"https://speedy-nodes-nyc.moralis.io/cebf590f4bcd4f12d78ee1d4/polygon/mumbai",
3+
]
4+
5+
export const BLOCK_EXPLORER_URLS = ["https://explorer-mumbai.maticvigil.com/"]
6+
export const SUPPORT_NETWORKS = [80001]
7+
export const STORAGE_KEY_ACCOUNT = 'ethAccount'
8+
export const STORAGE_KEY_ACCOUNT_SIG = 'sig_login'
9+
export const DOMAIN = {
10+
name: "DwebLab Alpha",
11+
version: "1",
12+
chainId: 80001,
13+
}
14+
15+
export const signInfo = {
16+
types: {
17+
Message: [{name: "content", type: "string"}],
18+
},
19+
message: {content: "Sign this msg to login"}
20+
}

context/web3Context.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {createContext, Dispatch, useContext} from "react"
2+
3+
type StateType = {
4+
provider?: any
5+
web3Provider?: any
6+
account?: string
7+
chainId?: number
8+
}
9+
10+
export const initialWeb3State: StateType = {
11+
provider: null,
12+
web3Provider: null,
13+
account: null,
14+
chainId: null,
15+
}
16+
17+
type ActionType =
18+
| {
19+
type: 'SET_WEB3_PROVIDER'
20+
provider?: StateType['provider']
21+
web3Provider?: StateType['web3Provider']
22+
account?: StateType['account']
23+
chainId?: StateType['chainId']
24+
}
25+
| {
26+
type: 'SET_ACCOUNT'
27+
account?: StateType['account']
28+
}
29+
| {
30+
type: 'SET_CHAIN_ID'
31+
chainId?: StateType['chainId']
32+
}
33+
| {
34+
type: 'RESET_WEB3_PROVIDER'
35+
}
36+
37+
export function web3Reducer(state: StateType, action: ActionType): StateType {
38+
switch (action.type) {
39+
case 'SET_WEB3_PROVIDER':
40+
return {
41+
...state,
42+
provider: action.provider,
43+
web3Provider: action.web3Provider,
44+
account: action.account,
45+
chainId: action.chainId,
46+
}
47+
case 'SET_ACCOUNT':
48+
return {
49+
...state,
50+
account: action.account,
51+
}
52+
case 'SET_CHAIN_ID':
53+
return {
54+
...state,
55+
chainId: action.chainId,
56+
}
57+
case 'RESET_WEB3_PROVIDER':
58+
return initialWeb3State
59+
default:
60+
throw new Error()
61+
}
62+
}
63+
64+
65+
export const Web3Context = createContext<{ state: StateType, dispatch: Dispatch<ActionType> | undefined}>({ state: initialWeb3State, dispatch: undefined});
66+
export function useWeb3Context() {
67+
return useContext(Web3Context);
68+
}

hooks/useAccount.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useWeb3Context} from "../context/web3Context";
2+
3+
export const useAccount = () => {
4+
const { state} = useWeb3Context()
5+
return state.account
6+
}

hooks/useChainId.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useWeb3Context} from "../context/web3Context";
2+
3+
export const useChainId = () => {
4+
const { state: { chainId }} = useWeb3Context()
5+
return chainId
6+
}

hooks/useProvider.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useWeb3Context} from "../context/web3Context";
2+
3+
export const useProvider = () => {
4+
const { state: { provider }} = useWeb3Context()
5+
return provider
6+
}

hooks/useWeb3.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useWeb3Context} from "../context/web3Context";
2+
3+
export const useWeb3 = () => {
4+
const { state: { web3Provider }} = useWeb3Context()
5+
return web3Provider
6+
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@react-hookz/web": "^12.0.4",
1717
"@textile/eth-storage": "^1.0.0",
1818
"axios": "^0.24.0",
19+
"bignumber.js": "^9.0.2",
1920
"ethers": "^5.5.2",
2021
"graphql-tag": "^2.12.6",
2122
"ipfs-http-client": "^55.0.0",
@@ -27,6 +28,7 @@
2728
"react-dom": "^17.0.2",
2829
"react-hook-form": "^7.22.5",
2930
"react-markdown": "^7.1.1",
31+
"web3": "^1.6.1",
3032
"web3modal": "^1.9.4",
3133
"yup": "^0.32.11"
3234
},

0 commit comments

Comments
 (0)