11import React , { useEffect , useState } from 'react' ;
22import configService from '@/service/config' ;
33import { AIType } from '@/typings/ai' ;
4- import { Alert , Button , Form , Input , Radio , RadioChangeEvent } from 'antd' ;
4+ import { Alert , Button , Flex , Form , Input , Radio , RadioChangeEvent } from 'antd' ;
55import i18n from '@/i18n' ;
66import { IAiConfig } from '@/typings/setting' ;
77import { IRole } from '@/typings/user' ;
88import { AIFormConfig , AITypeName } from './aiTypeConfig' ;
99import styles from './index.less' ;
10- import { useUserStore } from '@/store/user'
10+ import { useUserStore } from '@/store/user' ;
11+ import { getLinkBasedOnTimezone } from '@/utils/timezone' ;
1112
1213interface IProps {
1314 handleApplyAiConfig : ( aiConfig : IAiConfig ) => void ;
@@ -21,11 +22,11 @@ function capitalizeFirstLetter(string) {
2122// openAI 的设置项
2223export default function SettingAI ( props : IProps ) {
2324 const [ aiConfig , setAiConfig ] = useState < IAiConfig > ( ) ;
24- const { userInfo } = useUserStore ( state => {
25+ const { userInfo } = useUserStore ( ( state ) => {
2526 return {
26- userInfo : state . curUser
27- }
28- } )
27+ userInfo : state . curUser ,
28+ } ;
29+ } ) ;
2930
3031 useEffect ( ( ) => {
3132 setAiConfig ( props . aiConfig ) ;
@@ -65,6 +66,57 @@ export default function SettingAI(props: IProps) {
6566 }
6667 } ;
6768
69+ const renderAIConfig = ( ) => {
70+ if ( aiConfig ?. aiSqlSource === AIType . CHAT2DBAI ) {
71+ return (
72+ < Flex justify = "center" >
73+ < Button
74+ type = "primary"
75+ onClick = { ( ) => {
76+ const link = getLinkBasedOnTimezone ( ) ;
77+ window . open ( link , '_blank' ) ;
78+ } }
79+ >
80+ { i18n ( 'setting.chat2db.ai.button' ) }
81+ </ Button >
82+ </ Flex >
83+ ) ;
84+ }
85+ return (
86+ < >
87+ < Form layout = "vertical" >
88+ { Object . keys ( AIFormConfig [ aiConfig ?. aiSqlSource ] ) . map ( ( key : string ) => (
89+ < Form . Item
90+ key = { key }
91+ required = { key === 'apiKey' || key === 'secretKey' }
92+ label = { capitalizeFirstLetter ( key ) }
93+ className = { styles . title }
94+ >
95+ < Input
96+ autoComplete = "off"
97+ value = { aiConfig [ key ] }
98+ placeholder = { AIFormConfig [ aiConfig ?. aiSqlSource ] ?. [ key ] }
99+ onChange = { ( e ) => {
100+ setAiConfig ( { ...aiConfig , [ key ] : e . target . value } ) ;
101+ } }
102+ />
103+ </ Form . Item >
104+ ) ) }
105+ </ Form >
106+ { aiConfig . aiSqlSource === AIType . RESTAI && (
107+ < div style = { { margin : '32px 0 ' , fontSize : '12px' , opacity : '0.5' } } > { `Tips: ${ i18n (
108+ 'setting.tab.aiType.custom.tips' ,
109+ ) } `} </ div >
110+ ) }
111+ < div className = { styles . bottomButton } >
112+ < Button type = "primary" onClick = { handleApplyAiConfig } >
113+ { i18n ( 'setting.button.apply' ) }
114+ </ Button >
115+ </ div >
116+ </ >
117+ ) ;
118+ } ;
119+
68120 return (
69121 < >
70122 < div className = { styles . aiSqlSource } >
@@ -78,36 +130,7 @@ export default function SettingAI(props: IProps) {
78130 </ Radio . Group >
79131 </ div >
80132
81- < Form layout = "vertical" >
82- { Object . keys ( AIFormConfig [ aiConfig ?. aiSqlSource ] ) . map ( ( key : string ) => (
83- < Form . Item
84- key = { key }
85- required = { key === 'apiKey' || key === 'secretKey' }
86- label = { capitalizeFirstLetter ( key ) }
87- className = { styles . title }
88- >
89- < Input
90- autoComplete = "off"
91- value = { aiConfig [ key ] }
92- placeholder = { AIFormConfig [ aiConfig ?. aiSqlSource ] ?. [ key ] }
93- onChange = { ( e ) => {
94- setAiConfig ( { ...aiConfig , [ key ] : e . target . value } ) ;
95- } }
96- />
97- </ Form . Item >
98- ) ) }
99- </ Form >
100-
101- { aiConfig . aiSqlSource === AIType . RESTAI && (
102- < div style = { { margin : '32px 0 ' , fontSize : '12px' , opacity : '0.5' } } > { `Tips: ${ i18n (
103- 'setting.tab.aiType.custom.tips' ,
104- ) } `} </ div >
105- ) }
106- < div className = { styles . bottomButton } >
107- < Button type = "primary" onClick = { handleApplyAiConfig } >
108- { i18n ( 'setting.button.apply' ) }
109- </ Button >
110- </ div >
133+ { renderAIConfig ( ) }
111134
112135 { /* {aiConfig?.aiSqlSource === AIType.CHAT2DBAI && !aiConfig.apiKey && <Popularize source="setting" />} */ }
113136 </ >
0 commit comments