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

Bara chain #134

Merged
merged 2 commits into from
Apr 29, 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
15 changes: 11 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
VITE_SOCKET_BASE_URL="wss://dev-chat-xfans-api.buidlerdao.xyz"
VITE_ROOM_BASE_URL="https://dev-chat-xfans-api.buidlerdao.xyz"
VITE_CONTRACT_BASE_URL="https://test-mpc-xfans-api.buidlerdao.xyz"
VITE_BASE_URL="https://test-api.xfans.tech"
# arb
VITE_ARB_SOCKET_BASE_URL="wss://dev-chat-xfans-api.buidlerdao.xyz"
VITE_ARB_ROOM_BASE_URL="https://dev-chat-xfans-api.buidlerdao.xyz"
VITE_ARB_CONTRACT_BASE_URL="http://localhost:3001"
VITE_ARB_BASE_URL="https://test-api.xfans.tech"

# bera
VITE_BERA_SOCKET_BASE_URL="wss://dev-chat-xfans-api.buidlerdao.xyz"
VITE_BERA_ROOM_BASE_URL="https://dev-chat-xfans-api.buidlerdao.xyz"
VITE_BERA_CONTRACT_BASE_URL="http://localhost:3001"
VITE_BERA_BASE_URL="https://test-bera-api.xfans.tech"

29 changes: 29 additions & 0 deletions src/components/Select/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { Select, MenuItem, SelectChangeEvent } from '@mui/material';

interface TSelectProps {
handleChange: (value: string) => void;
}

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

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

return (
<Select
value={selectedValue}
onChange={handleInternalChange}
style={{ minWidth: '120px', height: '60px' }}
>
<MenuItem value="arb">arb</MenuItem>

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.

临时代码

<MenuItem value="bera">bera</MenuItem>
</Select>
);
};

export default TSelect;
34 changes: 34 additions & 0 deletions src/config/chainConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interface ChainConfigType {

Choose a reason for hiding this comment

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

搞个 store 把,类似 global store,或者直接放进去也行

Copy link
Author

Choose a reason for hiding this comment

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

store试过了 放到json中会报错,只能放到function中

Choose a reason for hiding this comment

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

你这里又直接用 localStorage 了啊

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!,
},
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 default ChainConfig;
10 changes: 9 additions & 1 deletion src/content/loginPage/signInWithXPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { FC } from 'react';

import { NextButton } from '../../components/buttons/loginButton';

import TSelect from '../../components/Select/index';
import { setCurrentChain, getCurrentChain } from '../../config/chainConfig';
import '../../tailwind.css';

interface SignInWithXPageProps {
Expand Down Expand Up @@ -53,6 +54,13 @@ const SignInWithXPage: FC<SignInWithXPageProps> = ({ handleButtonClick, showLoad
'Login With X'
)}
</NextButton>
<TSelect

Choose a reason for hiding this comment

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

这个能不能判断下 dev 环境才展示

Copy link
Author

Choose a reason for hiding this comment

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

临时代码

handleChange={(x: string) => {
console.log('select', x);
setCurrentChain(x);
console.log('getCurrentChain', getCurrentChain());
}}
/>
</div>
);
};
Expand Down
15 changes: 10 additions & 5 deletions src/service/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as toaster from '../components/Toaster';
import useGlobalStore from '../store/useGlobalStore';

import { checkStatus } from './checkStatus';
import ChainConfig, { getCurrentChain } from '../config/chainConfig';

// 请求响应参数(不包含data)
export interface Result {
Expand All @@ -32,7 +33,7 @@ export enum ResultEnum {

const config = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: import.meta.env.VITE_BASE_URL,
baseURL: ChainConfig().vite_base_url,
// 设置超时时间
timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
Expand All @@ -41,7 +42,7 @@ const config = {

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

const chatConfig = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: import.meta.env.VITE_ROOM_BASE_URL,
baseURL: ChainConfig().vite_room_base_url,
// 设置超时时间
timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
Expand All @@ -60,7 +61,7 @@ const chatConfig = {
class RequestHttp {
showMsg: boolean;
service: AxiosInstance;
public constructor(config: AxiosRequestConfig<ResultData<any>>) {
public constructor(config: AxiosRequestConfig<ResultData<any>>, chainInfo?: boolean) {
// instantiation
this.service = axios.create(config);
this.showMsg = true;
Expand All @@ -75,6 +76,10 @@ class RequestHttp {
if (config.headers && typeof config.headers.set === 'function') {
config.headers.set('Authorization', 'Bearer ' + token);
}
if (chainInfo) {
const chainInfo = getCurrentChain();
config.url = config?.url?.replace(/(\/xfans\/api\/)/, `/xfans/api/${chainInfo}/`);
}
return config;
},
(error: AxiosError) => {
Expand Down Expand Up @@ -155,5 +160,5 @@ class RequestHttp {
}

export default new RequestHttp(config);
export const contractRequestHttp = new RequestHttp(contractConfig);
export const contractRequestHttp = new RequestHttp(contractConfig, true);
export const roomRequestHttp = new RequestHttp(chatConfig);
4 changes: 2 additions & 2 deletions src/welcome/Profile/community/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { io, Socket } from 'socket.io-client';
import { error } from '../../../components/Toaster';
import { getMessagesByRoom, ReceiveMessage, SendMessage } from '../../../service/room';
import useGlobalStore from '../../../store/useGlobalStore';

import ChainConfig from '../../../config/chainConfig';
import { ToasterMessageType } from './constants';

export default function useRoom(user: string, room?: string) {
Expand All @@ -16,7 +16,7 @@ export default function useRoom(user: string, room?: string) {
// 建立 socket
useEffect(() => {
if (room == null) return;
const socket = io(import.meta.env.VITE_SOCKET_BASE_URL, {
const socket = io(ChainConfig().vite_socket_base_url, {
autoConnect: false,
extraHeaders: {
Authorization: 'Bearer ' + useGlobalStore.getState().token,
Expand Down
Loading