11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { zodResolver } from "@hookform/resolvers/zod" ;
33import { ColumnDef , SortingState } from "@tanstack/react-table" ;
4- import { ArrowUpDown , Lock , MoreHorizontal } from "lucide-react" ;
5- import { useState } from "react" ;
4+ import { ArrowUpDown , Lock , Plus , MoreHorizontal } from "lucide-react" ;
5+ import React , { useState } from "react" ;
66import { useForm , FormProvider } from "react-hook-form" ;
77import { useTranslation } from "react-i18next" ;
88import { toast } from "react-toastify" ;
@@ -42,14 +42,22 @@ const FormSchema = z.object({
4242 apiKey : z . string ( ) ,
4343} ) ;
4444
45- function CredentialsOpenai ( ) {
45+ interface CredentialsOpenaiProps {
46+ onCredentialsUpdate ?: ( ) => void ;
47+ showText ?: boolean ;
48+ }
49+
50+ function CredentialsOpenai ( {
51+ onCredentialsUpdate,
52+ showText = true ,
53+ } : CredentialsOpenaiProps ) {
4654 const { t } = useTranslation ( ) ;
4755 const { instance } = useInstance ( ) ;
4856
4957 const { createOpenaiCreds, deleteOpenaiCreds } = useManageOpenai ( ) ;
5058 const [ open , setOpen ] = useState ( false ) ;
5159 const [ sorting , setSorting ] = useState < SortingState > ( [ ] ) ;
52- const { data : creds , refetch : refetchCreds } = useFindOpenaiCreds ( {
60+ const { data : creds } = useFindOpenaiCreds ( {
5361 instanceName : instance ?. name ,
5462 enabled : open ,
5563 } ) ;
@@ -79,19 +87,17 @@ function CredentialsOpenai() {
7987 data : credsData ,
8088 } ) ;
8189 toast . success ( t ( "openai.toast.success.credentialsCreate" ) ) ;
82- onReset ( ) ;
90+ form . reset ( ) ;
91+ if ( onCredentialsUpdate ) {
92+ onCredentialsUpdate ( ) ;
93+ }
8394 // eslint-disable-next-line @typescript-eslint/no-explicit-any
8495 } catch ( error : any ) {
8596 console . error ( "Error:" , error ) ;
8697 toast . error ( `Error: ${ error ?. response ?. data ?. response ?. message } ` ) ;
8798 }
8899 } ;
89100
90- function onReset ( ) {
91- form . reset ( ) ;
92- refetchCreds ( ) ;
93- }
94-
95101 const handleDelete = async ( id : string ) => {
96102 if ( ! instance ?. name ) {
97103 toast . error ( "Instance not found." ) ;
@@ -103,7 +109,9 @@ function CredentialsOpenai() {
103109 instanceName : instance ?. name ,
104110 } ) ;
105111 toast . success ( t ( "openai.toast.success.credentialsDelete" ) ) ;
106- refetchCreds ( ) ;
112+ if ( onCredentialsUpdate ) {
113+ onCredentialsUpdate ( ) ;
114+ }
107115 } catch ( error : any ) {
108116 console . error ( "Error:" , error ) ;
109117 toast . error ( `Error: ${ error ?. response ?. data ?. response ?. message } ` ) ;
@@ -171,45 +179,58 @@ function CredentialsOpenai() {
171179 return (
172180 < Dialog open = { open } onOpenChange = { setOpen } >
173181 < DialogTrigger asChild >
174- < Button variant = "secondary" size = "sm" >
175- < Lock size = { 16 } className = "mr-1" />
176- < span className = "hidden md:inline" >
177- { t ( "openai.credentials.title" ) }
178- </ span >
182+ < Button variant = "secondary" size = "sm" type = "button" >
183+ { showText ? (
184+ < >
185+ < Lock size = { 16 } className = "mr-1" />
186+ < span className = "hidden md:inline" >
187+ { t ( "openai.credentials.title" ) }
188+ </ span >
189+ </ >
190+ ) : (
191+ < Plus size = { 16 } />
192+ ) }
179193 </ Button >
180194 </ DialogTrigger >
181- < DialogContent
182- className = "overflow-y-auto sm:max-h-[600px] sm:max-w-[740px]"
183- onCloseAutoFocus = { onReset }
184- >
195+ < DialogContent className = "overflow-y-auto sm:max-h-[600px] sm:max-w-[740px]" >
185196 < DialogHeader >
186197 < DialogTitle > { t ( "openai.credentials.title" ) } </ DialogTitle >
187198 </ DialogHeader >
188199 < FormProvider { ...form } >
189- < form
190- onSubmit = { form . handleSubmit ( onSubmit ) }
191- className = "w-full space-y-6"
200+ < div
201+ onClick = { ( e ) => e . stopPropagation ( ) }
202+ onSubmit = { ( e ) => e . stopPropagation ( ) }
203+ onKeyDown = { ( e ) => e . stopPropagation ( ) }
192204 >
193- < div >
194- < div className = "grid gap-3 md:grid-cols-2" >
195- < FormInput
196- name = "name"
197- label = { t ( "openai.credentials.table.name" ) }
198- >
199- < Input />
200- </ FormInput >
201- < FormInput
202- name = "apiKey"
203- label = { t ( "openai.credentials.table.apiKey" ) }
204- >
205- < Input type = "password" />
206- </ FormInput >
205+ < form
206+ onSubmit = { ( e ) => {
207+ e . preventDefault ( ) ;
208+ e . stopPropagation ( ) ;
209+ form . handleSubmit ( onSubmit ) ( e ) ;
210+ } }
211+ className = "w-full space-y-6"
212+ >
213+ < div >
214+ < div className = "grid gap-3 md:grid-cols-2" >
215+ < FormInput
216+ name = "name"
217+ label = { t ( "openai.credentials.table.name" ) }
218+ >
219+ < Input />
220+ </ FormInput >
221+ < FormInput
222+ name = "apiKey"
223+ label = { t ( "openai.credentials.table.apiKey" ) }
224+ >
225+ < Input type = "password" />
226+ </ FormInput >
227+ </ div >
207228 </ div >
208- </ div >
209- < DialogFooter >
210- < Button type = "submit" > { t ( "openai.button.save" ) } </ Button >
211- </ DialogFooter >
212- </ form >
229+ < DialogFooter >
230+ < Button type = "submit" > { t ( "openai.button.save" ) } </ Button >
231+ </ DialogFooter >
232+ </ form >
233+ </ div >
213234 </ FormProvider >
214235 < Separator />
215236 < div >
0 commit comments