Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve login logic when load website #8

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions .eslintrc

This file was deleted.

10 changes: 10 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "next/core-web-vitals",
"rules": {
"semi": [2, "never"],
"space-before-blocks": "error",
"space-before-function-paren": [2, "never"],
"object-curly-spacing": ["error", "always"],
"indent": ["error", 2]
}
}
5 changes: 2 additions & 3 deletions components/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from "next/link";
import Link from "next/link"
import { useRouter } from "next/router"

const config = [
Expand Down Expand Up @@ -34,10 +34,9 @@ const config = [

export const Navigation = () => {
const router = useRouter()
console.log(router.asPath);
const isActivePath = (path: string) => router.asPath === path
return (
<div className="flex mt-4">
<div className="flex mt-4 grow">
{ config.map((item) => {
return (
<Link key={item.path} href={item.path}>
Expand Down
20 changes: 20 additions & 0 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const RPC_URLS = [
"https://speedy-nodes-nyc.moralis.io/cebf590f4bcd4f12d78ee1d4/polygon/mumbai",
]

export const BLOCK_EXPLORER_URLS = ["https://explorer-mumbai.maticvigil.com/"]
export const SUPPORT_NETWORKS = [80001]
export const STORAGE_KEY_ACCOUNT = 'ethAccount'
export const STORAGE_KEY_ACCOUNT_SIG = 'sig_login'
export const DOMAIN = {
name: "DwebLab Alpha",
version: "1",
chainId: 80001,
}

export const signInfo = {
types: {
Message: [{name: "content", type: "string"}],
},
message: {content: "Sign this msg to login"}
}
68 changes: 68 additions & 0 deletions context/web3Context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {createContext, Dispatch, useContext} from "react"

type StateType = {
provider?: any
web3Provider?: any
account?: string
chainId?: number
}

export const initialWeb3State: StateType = {
provider: null,
web3Provider: null,
account: null,
chainId: null,
}

type ActionType =
| {
type: 'SET_WEB3_PROVIDER'
provider?: StateType['provider']
web3Provider?: StateType['web3Provider']
account?: StateType['account']
chainId?: StateType['chainId']
}
| {
type: 'SET_ACCOUNT'
account?: StateType['account']
}
| {
type: 'SET_CHAIN_ID'
chainId?: StateType['chainId']
}
| {
type: 'RESET_WEB3_PROVIDER'
}

export function web3Reducer(state: StateType, action: ActionType): StateType {
switch (action.type) {
case 'SET_WEB3_PROVIDER':
return {
...state,
provider: action.provider,
web3Provider: action.web3Provider,
account: action.account,
chainId: action.chainId,
}
case 'SET_ACCOUNT':
return {
...state,
account: action.account,
}
case 'SET_CHAIN_ID':
return {
...state,
chainId: action.chainId,
}
case 'RESET_WEB3_PROVIDER':
return initialWeb3State
default:
throw new Error()
}
}


export const Web3Context = createContext<{ state: StateType, dispatch: Dispatch<ActionType> | undefined}>({ state: initialWeb3State, dispatch: undefined});
export function useWeb3Context() {
return useContext(Web3Context);
}
6 changes: 6 additions & 0 deletions hooks/useAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useWeb3Context} from "../context/web3Context";

export const useAccount = () => {
const { state} = useWeb3Context()
return state.account
}
6 changes: 6 additions & 0 deletions hooks/useChainId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useWeb3Context} from "../context/web3Context";

export const useChainId = () => {
const { state: { chainId }} = useWeb3Context()
return chainId
}
6 changes: 6 additions & 0 deletions hooks/useProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useWeb3Context} from "../context/web3Context";

export const useProvider = () => {
const { state: { provider }} = useWeb3Context()
return provider
}
6 changes: 6 additions & 0 deletions hooks/useWeb3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useWeb3Context} from "../context/web3Context";

export const useWeb3 = () => {
const { state: { web3Provider }} = useWeb3Context()
return web3Provider
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@react-hookz/web": "^12.0.4",
"@textile/eth-storage": "^1.0.0",
"axios": "^0.24.0",
"bignumber.js": "^9.0.2",
"ethers": "^5.5.2",
"graphql-tag": "^2.12.6",
"ipfs-http-client": "^55.0.0",
Expand All @@ -27,6 +28,7 @@
"react-dom": "^17.0.2",
"react-hook-form": "^7.22.5",
"react-markdown": "^7.1.1",
"web3": "^1.6.1",
"web3modal": "^1.9.4",
"yup": "^0.32.11"
},
Expand Down
Loading