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

Optimize chain config #137

Merged
merged 5 commits into from
Apr 30, 2024
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: 11 additions & 14 deletions src/components/Select/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import React from 'react';
import { MenuItem, Select, SelectChangeEvent } from '@mui/material';

import { getCurrentChain } from '../../config/chainConfig';
interface TSelectProps {
handleChange: (value: string) => void;
}
import { Chain } from '../../config/chainConfig';
import useGlobalStore from '../../store/useGlobalStore';

const TSelect: React.FC<TSelectProps> = ({ handleChange }) => {
const [selectedValue, setSelectedValue] = React.useState(getCurrentChain());
const TSelect = () => {
const { chain } = useGlobalStore();

const handleInternalChange = (event: SelectChangeEvent<string>) => {
const value = event.target.value;
setSelectedValue(value);
handleChange(value); // 调用传入的 handleChange 函数
const handleInternalChange = (event: SelectChangeEvent<Chain>) => {
const value = event.target.value as Chain;
useGlobalStore.setState({ chain: value });
};

return (
<Select
value={selectedValue}
<Select<Chain>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有什么问题

value={chain}
onChange={handleInternalChange}
style={{ minWidth: '120px', height: '60px' }}
>
<MenuItem value="arb">arb</MenuItem>
<MenuItem value="bera">bera</MenuItem>
<MenuItem value={Chain.Arb}>{Chain.Arb}</MenuItem>
<MenuItem value={Chain.Bera}>{Chain.Bera}</MenuItem>
</Select>
);
};
Expand Down
52 changes: 23 additions & 29 deletions src/config/chainConfig.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import { XFANS_NETWORK_ARB, XFANS_NETWORK_BERA } from '../constants';
interface ChainConfigType {
export enum Chain {
Arb = 'arb',
Bera = 'bera',
}

export const chainNameMap = {
[Chain.Arb]: 'Arb Sepolia',
[Chain.Bera]: 'Bera Artio',
};

export interface ChainConfigType {
vite_socket_base_url: string;
vite_room_base_url: string;
vite_contract_base_url: string;
vite_base_url: string;
}

const _chainConfig: Record<string, ChainConfigType> = {
arb: {
vite_socket_base_url: import.meta.env.VITE_ARB_SOCKET_BASE_URL!,
vite_room_base_url: import.meta.env.VITE_ARB_ROOM_BASE_URL!,
vite_contract_base_url: import.meta.env.VITE_ARB_CONTRACT_BASE_URL!,
vite_base_url: import.meta.env.VITE_ARB_BASE_URL!,
const chainConfig: Record<Chain, ChainConfigType> = {
[Chain.Arb]: {
vite_socket_base_url: import.meta.env.VITE_ARB_SOCKET_BASE_URL,
vite_room_base_url: import.meta.env.VITE_ARB_ROOM_BASE_URL,
vite_contract_base_url: import.meta.env.VITE_ARB_CONTRACT_BASE_URL,
vite_base_url: import.meta.env.VITE_ARB_BASE_URL,
},
bera: {
vite_socket_base_url: import.meta.env.VITE_BERA_SOCKET_BASE_URL!,
vite_room_base_url: import.meta.env.VITE_BERA_ROOM_BASE_URL!,
vite_contract_base_url: import.meta.env.VITE_BERA_CONTRACT_BASE_URL!,
vite_base_url: import.meta.env.VITE_BERA_BASE_URL!,
[Chain.Bera]: {
vite_socket_base_url: import.meta.env.VITE_BERA_SOCKET_BASE_URL,
vite_room_base_url: import.meta.env.VITE_BERA_ROOM_BASE_URL,
vite_contract_base_url: import.meta.env.VITE_BERA_CONTRACT_BASE_URL,
vite_base_url: import.meta.env.VITE_BERA_BASE_URL,
},
};

console.log('_chainConfig', _chainConfig);
const ChainConfig = () => {
if (getCurrentChain() == '') console.error('chain info undefined!');
return _chainConfig[getCurrentChain()];
};

export const getCurrentChain = () => localStorage.getItem('current_chain') ?? 'arb';
export const setCurrentChain = (chain: string) => {
localStorage.setItem('current_chain', chain);
};

export const getCurrentChainName = () => {
return getCurrentChain() == 'arb' ? XFANS_NETWORK_ARB : XFANS_NETWORK_BERA;
};

export default ChainConfig;
export default chainConfig;
3 changes: 0 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ export enum ContractError {
export const XFANS_CONTENT_WIDTH = 433;
export const XFANS_MIN_WIDTH = 458;

export const XFANS_NETWORK_ARB = 'Arb Sepolia';
export const XFANS_NETWORK_BERA = 'Bera Artio';

// shares 精度单位,输出除以10,输入乘以10
export const SHARE_UNIT_MODIFIER = 10;
6 changes: 0 additions & 6 deletions src/content/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Divider from '@mui/material/Divider';
import Drawer from '@mui/material/Drawer';

import * as toaster from '../../components/Toaster';
import { getCurrentChain, setCurrentChain } from '../../config/chainConfig';
import { XFANS_CONTENT_WIDTH } from '../../constants';
import { XFANS_TOKEN } from '../../constants';
import { ProfileData } from '../../service/login/me';
Expand Down Expand Up @@ -243,11 +242,6 @@ export default function PersistentDrawerRight() {
});
clickLogin();
}}
handleSwitchSelect={(x: string) => {
setCurrentChain(x);
console.log('select chain', getCurrentChain());
checkProfileData();
}}
/>
)}
{page === PageType.Invite && (
Expand Down
10 changes: 2 additions & 8 deletions src/content/loginPage/signInWithXPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ import React, { FC } from 'react';

import { NextButton } from '../../components/buttons/loginButton';
import TSelect from '../../components/Select/index';
import { getCurrentChain, setCurrentChain } from '../../config/chainConfig';

import '../../tailwind.css';

interface SignInWithXPageProps {
handleButtonClick: () => void; // 定义一个函数类型的属性
handleSwitchSelect: (x: string) => void;
showLoading: boolean;
}

const SignInWithXPage: FC<SignInWithXPageProps> = ({
handleButtonClick,
handleSwitchSelect,
showLoading,
}) => {
const SignInWithXPage: FC<SignInWithXPageProps> = ({ handleButtonClick, showLoading }) => {
return (
<div className="h-full min-h-screen w-full items-center justify-center text-center">
<img
Expand Down Expand Up @@ -60,7 +54,7 @@ const SignInWithXPage: FC<SignInWithXPageProps> = ({
'Login With X'
)}
</NextButton>
<TSelect handleChange={handleSwitchSelect} />
<TSelect />
</div>
);
};
Expand Down
12 changes: 6 additions & 6 deletions src/service/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import axios, {
} from 'axios';

import * as toaster from '../components/Toaster';
import ChainConfig, { getCurrentChain } from '../config/chainConfig';
import chainConfig from '../config/chainConfig';
import useGlobalStore from '../store/useGlobalStore';

import { checkStatus } from './checkStatus';
Expand All @@ -33,7 +33,7 @@ export enum ResultEnum {

const config = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: ChainConfig().vite_base_url,
baseURL: chainConfig[useGlobalStore.getState().chain].vite_base_url,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

话说这里之前就有bug把。。切换网络了,这里不会变的,我这里突然想到但是没修还

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个我来想办法解决下,先合了

// 设置超时时间
timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
Expand All @@ -42,7 +42,7 @@ const config = {

const contractConfig = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: ChainConfig().vite_contract_base_url,
baseURL: chainConfig[useGlobalStore.getState().chain].vite_contract_base_url,
// 设置超时时间
timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
Expand All @@ -51,7 +51,7 @@ const contractConfig = {

const chatConfig = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: ChainConfig().vite_room_base_url,
baseURL: chainConfig[useGlobalStore.getState().chain].vite_room_base_url,
// 设置超时时间
timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
Expand All @@ -77,8 +77,8 @@ class RequestHttp {
config.headers.set('Authorization', 'Bearer ' + token);
}
if (chainInfo) {
const chainInfo = getCurrentChain();
config.url = config?.url?.replace(/(\/xfans\/api\/)/, `/xfans/api/${chainInfo}/`);
const currentChain = useGlobalStore.getState().chain;
config.url = config?.url?.replace(/(\/xfans\/api\/)/, `/xfans/api/${currentChain}/`);
}
return config;
},
Expand Down
6 changes: 5 additions & 1 deletion src/store/useGlobalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';

import ChainConfig, { Chain, ChainConfigType } from '../config/chainConfig';

export enum PageType {
Login = 'login',
Invite = 'invite',
Expand All @@ -25,11 +27,12 @@ export interface GlobalStoreProps {
goPage(page: PageType): void;
logout(): void;
userVote: Record<string, boolean> | null;
chain: Chain;
}

const useGlobalStore = create<GlobalStoreProps>()(
persist(
(set) => ({
(set, get) => ({
page: PageType.Login,
token: '',
isShowPrice: false,
Expand All @@ -49,6 +52,7 @@ const useGlobalStore = create<GlobalStoreProps>()(
});
},
userVote: null,
chain: Chain.Arb,
}),
{
name: 'xfans-user-config',
Expand Down
7 changes: 4 additions & 3 deletions src/welcome/Profile/community/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { nanoid } from '@reduxjs/toolkit';
import { io, Socket } from 'socket.io-client';

import { error } from '../../../components/Toaster';
import ChainConfig from '../../../config/chainConfig';
import chainConfig from '../../../config/chainConfig';
import { getMessagesByRoom, ReceiveMessage, SendMessage } from '../../../service/room';
import useGlobalStore from '../../../store/useGlobalStore';

Expand All @@ -13,11 +13,12 @@ export default function useRoom(user: string, room?: string) {
const [messages, setMessages] = useState<ReceiveMessage[]>([]);
const [socket, setSocket] = useState<Socket>();
const [members, setMembers] = useState<CommunityUserInfo[]>([]);
const { chain } = useGlobalStore();

// 建立 socket
useEffect(() => {
if (room == null) return;
const socket = io(ChainConfig().vite_socket_base_url, {
const socket = io(chainConfig[chain].vite_socket_base_url, {
autoConnect: false,
extraHeaders: {
Authorization: 'Bearer ' + useGlobalStore.getState().token,
Expand All @@ -33,7 +34,7 @@ export default function useRoom(user: string, room?: string) {
setMembers([]);
socket.disconnect();
};
}, [room, user]);
}, [chain, room, user]);

useEffect(() => {
function handle() {
Expand Down
7 changes: 5 additions & 2 deletions src/welcome/Wallet/Deposit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { useToggle } from 'ahooks';
import { BackButton, BasicButton, PrimaryButton } from '../../components/Button';
import Modal from '../../components/Modal';
import * as toaster from '../../components/Toaster';
import { getCurrentChainName } from '../../config/chainConfig';
import { chainNameMap } from '../../config/chainConfig';
import useGlobalStore from '../../store/useGlobalStore';
import useGlobalUserStore from '../../store/useGlobalUserStore';

const Deposit = () => {
const [isOpen, { setLeft: close, setRight: open }] = useToggle(false);
const accounts = useGlobalUserStore((state) => state.accounts);
const { chain } = useGlobalStore();

return (
<>
Expand Down Expand Up @@ -38,7 +41,7 @@ const Deposit = () => {
<div className="flex flex-col space-y-[14px]">
<span className="text-base font-medium text-[#919099]">Network</span>
<div className="rounded-[8px] bg-[#F7F9FA] py-[18px] pl-[26px] text-base font-medium text-[#1A1D1F]">
{getCurrentChainName()}
{chainNameMap[chain]}
</div>
</div>
<div className="flex flex-col space-y-[14px]">
Expand Down
7 changes: 5 additions & 2 deletions src/welcome/Wallet/WithDraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import ETHIcon from '../../components/icons/ETHIcon';
import Modal from '../../components/Modal';
import { NumberDisplayer } from '../../components/NumberDisplayer';
import { error, success } from '../../components/Toaster';
import { getCurrentChainName } from '../../config/chainConfig';
import { chainNameMap } from '../../config/chainConfig';
import { ContractError } from '../../constants';
import useAccount from '../../hooks/useAccount';
import { transfer as transferApi } from '../../service/contract/shares';
import useGlobalStore from '../../store/useGlobalStore';

const TextField = styled(MTextField)({
width: '493px',
'& label.Mui-focused': {
Expand Down Expand Up @@ -44,6 +46,7 @@ const WithDraw = ({ onClose }: Props) => {
const { balance, refresh } = useAccount();
const [address, setAddress] = useState('');
const [amount, setAmount] = useState('');
const { chain } = useGlobalStore();
const { run: transfer, loading } = useRequest(() => transferApi(address, amount), {
manual: true,
onSuccess() {
Expand Down Expand Up @@ -95,7 +98,7 @@ const WithDraw = ({ onClose }: Props) => {
<h2 className="xfans-font-sf text-[24px] font-medium text-[#2E2E32]">Withdraw</h2>
<div className="mt-[15px] h-[1px] w-[438px] bg-[#EBEEF0]"></div>
<p className="xfans-font-sf my-6 text-sm text-black text-opacity-50">
{`Send your ETH to another wallet address on the ${getCurrentChainName()}`}
{`Send your ETH to another wallet address on the ${chainNameMap[chain]}`}
</p>

<div className="mb-6 w-full space-y-6">
Expand Down
6 changes: 3 additions & 3 deletions src/welcome/Wallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InfoCircle } from '../../components/icons/InfoCircle';
import { NumberDisplayer } from '../../components/NumberDisplayer';
import * as toaster from '../../components/Toaster';
import TruncateText from '../../components/TruncateText';
import { getCurrentChainName } from '../../config/chainConfig';
import { chainNameMap } from '../../config/chainConfig';
import useAccount from '../../hooks/useAccount';
import { useUserInfo } from '../../service/user';
import { useWalletAccounts } from '../../service/wallet';
Expand All @@ -24,7 +24,7 @@ import WithDraw from './WithDraw';
const Wallet = (props: { back?: () => void; logout?: () => void }) => {
const { openProfile } = useProfileModal((state) => ({ ...state }));

const { isShowPrice } = useGlobalStore((state) => ({ ...state }));
const { isShowPrice, chain } = useGlobalStore((state) => ({ ...state }));
const { userInfo } = useAccount();
const { run: getUserInfo } = useUserInfo();
const [isWithDrawOpen, setIsWithDrawOpen] = useState(false);
Expand Down Expand Up @@ -77,7 +77,7 @@ const Wallet = (props: { back?: () => void; logout?: () => void }) => {
</CopyToClipboard>

<div className="flex items-center space-x-1">
<span className="text-[#919099]">{`Network:${getCurrentChainName()}`}</span>
<span className="text-[#919099]">{`Network:${chainNameMap[chain]}`}</span>
<Network />
</div>
</div>
Expand Down
Loading