mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
still bugs
This commit is contained in:
@@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from 'react';
|
||||
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
|
||||
import './App.css';
|
||||
import { LoginContext, loginContextDefault, GlobalConfContext } from './utils/context';
|
||||
import { LoginStatus, GlobalConf } from './utils/type';
|
||||
import { LoginStatus, GlobalConf, AllPlatformConf } from './utils/type';
|
||||
import { Admin } from './pages/admin';
|
||||
import { getGlobalConf } from './api/config';
|
||||
import { Auth } from './pages/auth';
|
||||
@@ -27,19 +27,20 @@ function LoginSwitch() {
|
||||
|
||||
function App() {
|
||||
const [loginStatus, setLogin] = useState(loginContextDefault.login);
|
||||
const [globalConf, setGlobalConf] = useState<GlobalConf>({platformConf: []});
|
||||
const [globalConf, setGlobalConf] = useState<GlobalConf>({platformConf: {} as AllPlatformConf, loaded: false});
|
||||
// const globalConfContext = useContext(GlobalConfContext);
|
||||
const save = (login: LoginStatus) => setLogin(_ => login);
|
||||
useEffect(() => {
|
||||
const fetchGlobalConf = async () => {
|
||||
const res = await getGlobalConf();
|
||||
setGlobalConf(_ => res);
|
||||
setGlobalConf(_ => {return {...res, loaded: true}});
|
||||
};
|
||||
fetchGlobalConf();
|
||||
}, []);
|
||||
return (
|
||||
<LoginContext.Provider value={{login: loginStatus, save}}>
|
||||
<GlobalConfContext.Provider value={globalConf}>
|
||||
{ globalConf.loaded &&
|
||||
<Router>
|
||||
<Switch>
|
||||
<Route path="/auth/:code">
|
||||
@@ -50,6 +51,7 @@ function App() {
|
||||
</Route>
|
||||
</Switch>
|
||||
</Router>
|
||||
}
|
||||
</GlobalConfContext.Provider>
|
||||
</LoginContext.Provider>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import axios from "axios";
|
||||
import { GlobalConf, TokenResp } from "../utils/type";
|
||||
import { GlobalConf, TokenResp, SubscribeResp, TargetNameResp } from "../utils/type";
|
||||
import { baseUrl } from './utils';
|
||||
|
||||
export async function getGlobalConf(): Promise<GlobalConf> {
|
||||
@@ -11,3 +11,13 @@ export async function auth(token: string): Promise<TokenResp> {
|
||||
const res = await axios.get<TokenResp>(`${baseUrl}auth`, {params: {token}});
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function getSubscribe(): Promise<SubscribeResp> {
|
||||
const res = await axios.get(`${baseUrl}subs`);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function getTargetName(platformName: string, target: string): Promise<TargetNameResp> {
|
||||
const res = await axios.get(`${baseUrl}target_name`, {params: {platformName, target}});
|
||||
return res.data;
|
||||
}
|
||||
|
||||
@@ -18,4 +18,23 @@ axios.interceptors.request.use(function (config) {
|
||||
return config;
|
||||
}, function (error) {
|
||||
return Promise.reject(error);
|
||||
})
|
||||
});
|
||||
|
||||
axios.interceptors.response.use(function (response) {
|
||||
// const data = response.data;
|
||||
// const parseToMap = (item: any): any => {
|
||||
// if (item instanceof Array) {
|
||||
// return item.map(parseToMap);
|
||||
// } else if (item instanceof Object) {
|
||||
// let res = new Map();
|
||||
// for (const key of Object.keys(item)) {
|
||||
// res.set(key, parseToMap(item[key]));
|
||||
// }
|
||||
// return res;
|
||||
// } else {
|
||||
// return item;
|
||||
// }
|
||||
// }
|
||||
// response.data = parseToMap(data);
|
||||
return response;
|
||||
});
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import React, { FC, useContext, useState } from "react";
|
||||
import React, { ReactElement, useContext, useEffect, useState } from "react";
|
||||
import { LoginContext, GlobalConfContext } from "../utils/context";
|
||||
import { Layout, Menu } from 'antd';
|
||||
import { SubscribeConfig } from '../utils/type';
|
||||
import { SettingOutlined, BugOutlined } from '@ant-design/icons';
|
||||
import { Layout, Menu, Empty, Collapse, Card, Tag, Row, Col, Form, Tooltip, Button, Modal, Select,
|
||||
Input} from 'antd';
|
||||
import { SubscribeConfig, SubscribeResp, PlatformConfig } from '../utils/type';
|
||||
import { SettingOutlined, BugOutlined, DeleteOutlined, CopyOutlined, PlusOutlined } from '@ant-design/icons';
|
||||
import { getSubscribe, getTargetName } from '../api/config';
|
||||
import './admin.css';
|
||||
|
||||
export function Admin() {
|
||||
@@ -26,7 +28,7 @@ export function Admin() {
|
||||
<div style={{margin: '24px', background: '#fff', minHeight: '640px'}}>
|
||||
{
|
||||
tab == 'manage' ?
|
||||
<div>123</div>
|
||||
<ConfigPage tab={tab}/>
|
||||
: null
|
||||
}
|
||||
</div>
|
||||
@@ -35,13 +37,113 @@ export function Admin() {
|
||||
)
|
||||
}
|
||||
|
||||
function ConfigPage() {
|
||||
const [ configData, setConfigData ] = useState<Array<SubscribeConfig>>([
|
||||
{
|
||||
platform: 'weibo',
|
||||
target: '123333',
|
||||
catetories: [1, 2],
|
||||
tags: []
|
||||
}
|
||||
]);
|
||||
interface ConfigPageProp {
|
||||
tab: string
|
||||
}
|
||||
function ConfigPage(prop: ConfigPageProp) {
|
||||
const [ configData, setConfigData ] = useState<SubscribeResp>({});
|
||||
const [ showModal, setShowModal ] = useState<boolean>(false);
|
||||
const globalConf = useContext(GlobalConfContext);
|
||||
useEffect(() => {
|
||||
getSubscribe()
|
||||
.then(res => {
|
||||
setConfigData(_ => res);
|
||||
});
|
||||
}, [prop.tab]);
|
||||
const clickNew = (e: React.MouseEvent<HTMLButtonElement>) => {
|
||||
setShowModal(_ => true);
|
||||
e.stopPropagation();
|
||||
}
|
||||
const genCard = (config: SubscribeConfig) => {
|
||||
const platformConf = globalConf.platformConf[config.targetType] as PlatformConfig;
|
||||
return (
|
||||
<Col span={6} key={`${config.targetType}-${config.target}`}>
|
||||
<Card title={`${platformConf.name} - ${config.targetName}`}
|
||||
actions={[
|
||||
<Tooltip title="删除"><DeleteOutlined /></Tooltip>,
|
||||
<Tooltip title="添加到其他群"><CopyOutlined /></Tooltip>
|
||||
]}>
|
||||
<Form labelCol={{ span: 4 }}>
|
||||
<Form.Item label="订阅类型">
|
||||
{Object.keys(platformConf.categories).length > 0 ?
|
||||
config.cats.map((catKey: number) => (<Tag color="green" key={catKey}>{platformConf.categories[catKey]}</Tag>)) :
|
||||
<Tag color="red">不支持类型</Tag>}
|
||||
</Form.Item>
|
||||
<Form.Item label="订阅Tag">
|
||||
{platformConf.enabledTag ? config.tags.length > 0 ? config.tags.map(tag => (<Tag color="green" key={tag}>{tag}</Tag>)) : (<Tag color="blue">全部标签</Tag>) :
|
||||
<Tag color="red">不支持Tag</Tag>}
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Card>
|
||||
</Col>
|
||||
)
|
||||
}
|
||||
if (Object.keys(configData).length === 0) {
|
||||
return <Empty />
|
||||
} else {
|
||||
let groups: Array<ReactElement> = [];
|
||||
for (let key of Object.keys(configData)) {
|
||||
let value = configData[key];
|
||||
groups.push(
|
||||
<Collapse.Panel header={<span>{`${key} - ${value.name}`}<Button style={{float: "right"}} onClick={clickNew}>添加</Button></span>} key={key}>
|
||||
<Row gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }} align="middle">
|
||||
{value.subscribes.map(genCard)}
|
||||
</Row>
|
||||
</Collapse.Panel>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<Collapse>
|
||||
{groups}
|
||||
</Collapse>
|
||||
<AddModal showModal={showModal} setShowModal={(s: boolean) => setShowModal(_ => s)} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
interface AddModalProp {
|
||||
showModal: boolean,
|
||||
setShowModal: (s: boolean) => void
|
||||
}
|
||||
function AddModal(prop: AddModalProp) {
|
||||
const [ confirmLoading, setConfirmLoading ] = useState<boolean>(false);
|
||||
const { platformConf } = useContext(GlobalConfContext);
|
||||
const [ hasTarget, setHasTarget ] = useState(false);
|
||||
const [ form ] = Form.useForm();
|
||||
const changePlatformSelect = (platform: string) => {
|
||||
setHasTarget(_ => platformConf[platform].hasTarget);
|
||||
if (! platformConf[platform].hasTarget) {
|
||||
getTargetName(platform, 'default')
|
||||
.then(res => {
|
||||
console.log(res)
|
||||
form.setFieldsValue(() => { return {
|
||||
targetName: res.targetName
|
||||
}})
|
||||
})
|
||||
}
|
||||
}
|
||||
const handleSubmit = (value: any) => {
|
||||
console.log(value);
|
||||
}
|
||||
|
||||
return <Modal title="添加订阅" visible={prop.showModal}
|
||||
confirmLoading={confirmLoading} onCancel={() => prop.setShowModal(false)}>
|
||||
<Form form={form} labelCol={{ span: 6 }} name="b">
|
||||
<Form.Item label="平台" name="platformType" rules={[]}>
|
||||
<Select style={{ width: '80%' }} onChange={changePlatformSelect}>
|
||||
{Object.keys(platformConf).map(platformName =>
|
||||
<Select.Option key={platformName} value={platformName}>{platformConf[platformName].name}</Select.Option>
|
||||
)}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item label="账号" name="target" rules={[]}>
|
||||
<Input placeholder={hasTarget ? "获取方式见文档" : "此平台不需要账号"} disabled={! hasTarget} style={{ width: "80%" }} />
|
||||
</Form.Item>
|
||||
<Form.Item label="账号名称" name="targetName">
|
||||
<Input style={{ width: "80%" }} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
}
|
||||
|
||||
@@ -14,4 +14,4 @@ export const loginContextDefault: LoginContextType = {
|
||||
};
|
||||
|
||||
export const LoginContext = createContext(loginContextDefault);
|
||||
export const GlobalConfContext = createContext<GlobalConf>({platformConf: []});
|
||||
export const GlobalConfContext = createContext<GlobalConf>({platformConf: {}, loaded: false});
|
||||
|
||||
@@ -18,20 +18,30 @@ export type LoginContextType = {
|
||||
}
|
||||
|
||||
export interface SubscribeConfig {
|
||||
platform: string
|
||||
targetType: string
|
||||
target?: string
|
||||
catetories: Array<number>
|
||||
targetName: string
|
||||
cats: Array<number>
|
||||
tags: Array<string>
|
||||
}
|
||||
|
||||
export interface GlobalConf {
|
||||
platformConf: Array<PlatformConfig>
|
||||
platformConf: AllPlatformConf,
|
||||
loaded: boolean
|
||||
}
|
||||
|
||||
export interface AllPlatformConf {
|
||||
[idx: string]: PlatformConfig;
|
||||
}
|
||||
|
||||
interface CategoryConfig {
|
||||
[idx: number]: string
|
||||
}
|
||||
|
||||
export interface PlatformConfig {
|
||||
name: string
|
||||
catetories: Map<number, string>,
|
||||
enableTag: boolean,
|
||||
categories: CategoryConfig
|
||||
enabledTag: boolean,
|
||||
platformName: string,
|
||||
hasTarget: boolean
|
||||
}
|
||||
@@ -43,3 +53,16 @@ export interface TokenResp {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
|
||||
export interface SubscribeGroupDetail {
|
||||
name: string,
|
||||
subscribes: Array<SubscribeConfig>
|
||||
}
|
||||
|
||||
export interface SubscribeResp {
|
||||
[idx: string]: SubscribeGroupDetail
|
||||
}
|
||||
|
||||
export interface TargetNameResp {
|
||||
targetName: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user