From 1d50c7d3b8c6ed0efd27159d711f9f6fd2ad8915 Mon Sep 17 00:00:00 2001 From: "shiweisong.ssw" Date: Wed, 5 Jul 2023 11:34:55 +0800 Subject: [PATCH 1/2] feat: change the style --- datacenter/app/datastores/page.tsx | 41 ++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/datacenter/app/datastores/page.tsx b/datacenter/app/datastores/page.tsx index b42accfc2..5c399f780 100644 --- a/datacenter/app/datastores/page.tsx +++ b/datacenter/app/datastores/page.tsx @@ -4,10 +4,9 @@ import { useRouter } from 'next/navigation' import React, { useState, useEffect } from 'react' import { InboxOutlined } from '@ant-design/icons' import CheckCircleOutlinedIcon from '@mui/icons-material/CheckCircleOutlined' -import AddBoxOutlinedIcon from '@mui/icons-material/AddBoxOutlined'; import ContentPasteSearchOutlinedIcon from '@mui/icons-material/ContentPasteSearchOutlined'; import type { UploadProps } from 'antd' -import { message, Upload, Popover } from 'antd' +import { message, Upload } from 'antd' import { useColorScheme, Modal, @@ -115,7 +114,7 @@ const Index = () => { width: '100%', height: '100%', }} - className='bg-[#EEF0F5] dark:bg-[#212121]' + className='bg-[#F1F2F5] dark:bg-[#212121]' > { > { flexShrink: 0, flexGrow: 0, cursor: 'pointer', + borderRadius: '16px', '&: hover': { boxShadow: '0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);' } }} onClick={() => setIsAddKnowledgeSpaceModalShow(true)} - className='bg-[#E0E4ED] dark:bg-[#484848]' - >Space + className='bg-[#E9EBEE] dark:bg-[#484848]' + > + + + space + {knowledgeSpaceList.map((item: any, index: number) => ( { padding: '30px 20px 40px', marginRight: '30px', marginBottom: '30px', - borderTop: '3px solid rgb(82, 196, 26)', + borderTop: '4px solid rgb(84, 164, 248)', flexShrink: 0, flexGrow: 0, cursor: 'pointer', + borderRadius: '10px', '&: hover': { boxShadow: '0 10px 15px -3px rgba(0,0,0,.1),0 4px 6px -4px rgba(0,0,0,.1);' } @@ -184,7 +205,7 @@ const Index = () => { marginBottom: '10px', fontWeight: 'bold', color: 'black' - }}>{item.name} + }}>{item.name} { > {item.vector_type} Vector @@ -214,7 +235,7 @@ const Index = () => { > {item.owner} Owner @@ -228,7 +249,7 @@ const Index = () => { > {item.docs || 0} Docs From 6f3ec57b65390cc0e866f38dbf9b0896f1625934 Mon Sep 17 00:00:00 2001 From: "shiweisong.ssw" Date: Wed, 5 Jul 2023 13:46:12 +0800 Subject: [PATCH 2/2] feat: refactor the request --- datacenter/app/datastores/page.tsx | 127 ++++++----------------------- datacenter/utils/request.ts | 38 +++++++++ 2 files changed, 62 insertions(+), 103 deletions(-) diff --git a/datacenter/app/datastores/page.tsx b/datacenter/app/datastores/page.tsx index 5c399f780..0874707d5 100644 --- a/datacenter/app/datastores/page.tsx +++ b/datacenter/app/datastores/page.tsx @@ -22,6 +22,7 @@ import { Typography, styled } from '@/lib/mui' +import { sendSpaceGetRequest, sendSpacePostRequest, sendSpaceUploadPostRequest } from '@/utils/request'; const { Dragger } = Upload @@ -91,17 +92,7 @@ const Index = () => { } useEffect(() => { async function fetchData() { - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/space/list`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({}) - } - ) - const data = await res.json() + const data = await sendSpacePostRequest('/knowledge/space/list') if (data.success) { setKnowledgeSpaceList(data.data) } @@ -315,36 +306,19 @@ const Index = () => { message.error('please input the name') return } - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/space/add`, + const data = await sendSpacePostRequest( + `/knowledge/space/add`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ name: knowledgeSpaceName, vector_type: 'Chroma', owner: 'keting', desc: 'test1' - }) } ) - const data = await res.json() if (data.success) { message.success('success') setActiveStep(1) - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/space/list`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({}) - } - ) - const data = await res.json() + const data = await sendSpacePostRequest('/knowledge/space/list') if (data.success) { setKnowledgeSpaceList(data.data) } @@ -481,37 +455,18 @@ const Index = () => { message.error('Please input the Web Page URL') return } - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/add`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - doc_name: documentName, - content: webPageUrl, - doc_type: 'URL' - }) - } - ) - const data = await res.json() + const data = await sendSpacePostRequest(`/knowledge/${knowledgeSpaceName}/document/add`, { + doc_name: documentName, + content: webPageUrl, + doc_type: 'URL' + }) if (data.success) { message.success('success') setIsAddKnowledgeSpaceModalShow(false) synchChecked && - fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/sync`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - doc_ids: [data.data] - }) - } - ) + sendSpacePostRequest(`/knowledge/${knowledgeSpaceName}/document/sync`, { + doc_ids: [data.data] + }) } else { message.error(data.err_msg || 'failed') } @@ -524,30 +479,15 @@ const Index = () => { formData.append('doc_name', documentName) formData.append('doc_file', originFileObj) formData.append('doc_type', 'DOCUMENT') - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/upload`, - { - method: 'POST', - body: formData - } - ) - const data = await res.json() + + const data = await sendSpaceUploadPostRequest(`/knowledge/${knowledgeSpaceName}/document/upload`, formData); if (data.success) { message.success('success') setIsAddKnowledgeSpaceModalShow(false) synchChecked && - fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/sync`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - doc_ids: [data.data] - }) - } - ) + sendSpacePostRequest(`/knowledge/${knowledgeSpaceName}/document/sync`, { + doc_ids: [data.data] + }); } else { message.error(data.err_msg || 'failed') } @@ -556,38 +496,19 @@ const Index = () => { message.error('Please input the text') return } - const res = await fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/add`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - doc_name: documentName, + const data = await sendSpacePostRequest(`/knowledge/${knowledgeSpaceName}/document/add`, { + doc_name: documentName, source: textSource, content: text, doc_type: 'TEXT' - }) - } - ) - const data = await res.json() + }) if (data.success) { message.success('success') setIsAddKnowledgeSpaceModalShow(false) synchChecked && - fetch( - `${process.env.API_BASE_URL}/knowledge/${knowledgeSpaceName}/document/sync`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - doc_ids: [data.data] - }) - } - ) + sendSpacePostRequest(`/knowledge/${knowledgeSpaceName}/document/sync`, { + doc_ids: [data.data] + }) } else { message.error(data.err_msg || 'failed') } diff --git a/datacenter/utils/request.ts b/datacenter/utils/request.ts index a8a6fa1c2..2a5551d97 100644 --- a/datacenter/utils/request.ts +++ b/datacenter/utils/request.ts @@ -38,6 +38,24 @@ export const sendGetRequest = (url: string, qs?: { [key: string]: any }) => { }); } +export const sendSpaceGetRequest = (url: string, qs?: { [key: string]: any }) => { + if (qs) { + const str = Object.keys(qs) + .filter(k => qs[k] !== undefined && qs[k] !== '') + .map(k => `${k}=${qs[k]}`) + .join('&'); + if (str) { + url += `?${str}`; + } + } + return axios.get(url, { + headers: DEFAULT_HEADERS + }).then(res => res).catch(err => { + message.error(err); + Promise.reject(err); + }); +} + export const sendPostRequest = (url: string, body?: any) => { const reqBody = sanitizeBody(body); return axios.post("/api" + url, { @@ -47,4 +65,24 @@ export const sendPostRequest = (url: string, body?: any) => { message.error(err); Promise.reject(err); }); +} + +export const sendSpacePostRequest = (url: string, body?: any) => { + const reqBody = sanitizeBody(body); + return axios.post(url, { + body: reqBody, + headers: DEFAULT_HEADERS + }).then(res => res).catch(err => { + message.error(err); + Promise.reject(err); + }); +} + +export const sendSpaceUploadPostRequest = (url: string, body?: any) => { + return axios.post(url, { + body: body, + }).then(res => res).catch(err => { + message.error(err); + Promise.reject(err); + }); } \ No newline at end of file