@@ -9,7 +9,6 @@ import { Button } from '@/components/ui/button'
99import { Form , FormControl , FormField , FormItem , FormLabel , FormMessage } from '@/components/ui/form'
1010import { Input } from '@/components/ui/input'
1111import { Label } from '@/components/ui/label'
12- import { useStore } from '@nanostores/react'
1312import { YesIcon } from '@/components/ui/icon'
1413import { useTranslation } from 'react-i18next'
1514import { useQuery } from '@tanstack/react-query'
@@ -54,29 +53,26 @@ const IPField = ({
5453 control,
5554 name,
5655 label,
57- defaultValue,
5856} : {
5957 control : Control < any >
6058 name : string
6159 label : string
62- defaultValue ?: string
6360} ) => {
6461 const { t } = useTranslation ( )
6562
6663 return (
6764 < FormField
6865 name = { name }
6966 control = { control }
70- render = { ( { field } ) => (
67+ render = { ( { field, fieldState } ) => (
7168 < FormItem >
7269 < FormLabel > { t ( label ) } </ FormLabel >
7370 < FormControl >
7471 < Input placeholder = "127.0.0.1" { ...field } />
7572 </ FormControl >
76- < FormMessage / >
73+ < FormMessage > { fieldState . error ?. message && t ( fieldState . error ?. message ) } </ FormMessage >
7774 </ FormItem >
7875 ) }
79- defaultValue = { defaultValue }
8076 />
8177 )
8278}
@@ -85,29 +81,29 @@ const PortField = ({
8581 control,
8682 name,
8783 label,
88- defaultValue,
8984} : {
9085 control : Control < any >
9186 name : string
9287 label : string
93- defaultValue ?: number
9488} ) => {
9589 const { t } = useTranslation ( )
9690
9791 return (
9892 < FormField
9993 name = { name }
10094 control = { control }
101- render = { ( { field } ) => (
95+ render = { ( { field, fieldState } ) => {
96+ const k = t ( fieldState . error ?. message as any )
97+ console . log ( k )
98+ return (
10299 < FormItem >
103100 < FormLabel > { t ( label ) } </ FormLabel >
104101 < FormControl >
105102 < Input placeholder = "8080" { ...field } />
106103 </ FormControl >
107104 < FormMessage />
108105 </ FormItem >
109- ) }
110- defaultValue = { defaultValue }
106+ ) } }
111107 />
112108 )
113109}
@@ -116,29 +112,26 @@ const SecretKeyField = ({
116112 control,
117113 name,
118114 label,
119- defaultValue,
120115} : {
121116 control : Control < any >
122117 name : string
123118 label : string
124- defaultValue ?: string
125119} ) => {
126120 const { t } = useTranslation ( )
127121
128122 return (
129123 < FormField
130124 name = { name }
131125 control = { control }
132- render = { ( { field } ) => (
126+ render = { ( { field, fieldState } ) => (
133127 < FormItem >
134128 < FormLabel > { t ( label ) } </ FormLabel >
135129 < FormControl >
136130 < Input placeholder = "secret key" { ...field } />
137131 </ FormControl >
138- < FormMessage / >
132+ < FormMessage > { fieldState . error ?. message && t ( fieldState . error ?. message ) } </ FormMessage >
139133 </ FormItem >
140134 ) }
141- defaultValue = { defaultValue }
142135 />
143136 )
144137}
@@ -148,6 +141,11 @@ export const TCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
148141 const [ _ , setTCPConfig ] = useState < TCPProxyConfig | undefined > ( )
149142 const form = useForm < z . infer < typeof TCPConfigSchema > > ( {
150143 resolver : zodResolver ( TCPConfigSchema ) ,
144+ defaultValues : {
145+ remotePort : defaultConfig ?. remotePort || 4321 ,
146+ localIP : defaultConfig ?. localIP || '127.0.0.1' ,
147+ localPort : defaultConfig ?. localPort || 1234 ,
148+ }
151149 } )
152150
153151 useEffect ( ( ) => {
@@ -208,19 +206,16 @@ export const TCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
208206 control = { form . control }
209207 name = "localPort"
210208 label = "proxy.form.local_port"
211- defaultValue = { defaultConfig ?. localPort || 1234 }
212209 />
213210 < IPField
214211 control = { form . control }
215212 name = "localIP"
216213 label = "proxy.form.local_ip"
217- defaultValue = { defaultConfig ?. localIP || '127.0.0.1' }
218214 />
219215 < PortField
220216 control = { form . control }
221217 name = "remotePort"
222218 label = "proxy.form.remote_port"
223- defaultValue = { defaultConfig ?. remotePort || 4321 }
224219 />
225220 < Button type = "submit" disabled = { isSaveDisabled } variant = { 'outline' } >
226221 < YesIcon className = { `mr-2 h-4 w-4 ${ isSaveDisabled ? '' : 'hidden' } ` } > </ YesIcon >
@@ -236,6 +231,11 @@ export const STCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
236231 const [ _ , setSTCPConfig ] = useState < STCPProxyConfig | undefined > ( )
237232 const form = useForm < z . infer < typeof STCPConfigSchema > > ( {
238233 resolver : zodResolver ( STCPConfigSchema ) ,
234+ defaultValues : {
235+ localPort : defaultConfig ?. localPort || 1234 ,
236+ localIP : defaultConfig ?. localIP || '127.0.0.1' ,
237+ secretKey : defaultConfig ?. secretKey ,
238+ }
239239 } )
240240
241241 useEffect ( ( ) => {
@@ -266,12 +266,12 @@ export const STCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
266266
267267 const { t } = useTranslation ( )
268268
269- const { data : server } = useQuery ( {
270- queryKey : [ 'getServer' , serverID ] ,
271- queryFn : ( ) => {
272- return getServer ( { serverId : serverID } )
273- } ,
274- } )
269+ // const { data: server } = useQuery({
270+ // queryKey: ['getServer', serverID],
271+ // queryFn: () => {
272+ // return getServer({ serverId: serverID })
273+ // },
274+ // })
275275
276276 return (
277277 < Form { ...form } >
@@ -280,15 +280,17 @@ export const STCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
280280 control = { form . control }
281281 name = "localPort"
282282 label = "proxy.form.local_port"
283- defaultValue = { defaultConfig ?. localPort || 1234 }
284283 />
285284 < IPField
286285 control = { form . control }
287286 name = "localIP"
288287 label = "proxy.form.local_ip"
289- defaultValue = { defaultConfig ?. localIP || '127.0.0.1' }
290288 />
291- < SecretKeyField control = { form . control } name = "secretKey" label = "proxy.form.secret_key" defaultValue = { defaultConfig ?. secretKey } />
289+ < SecretKeyField
290+ control = { form . control }
291+ name = "secretKey"
292+ label = "proxy.form.secret_key"
293+ />
292294 < Button type = "submit" disabled = { isSaveDisabled } variant = { 'outline' } >
293295 < YesIcon className = { `mr-2 h-4 w-4 ${ isSaveDisabled ? '' : 'hidden' } ` } > </ YesIcon >
294296 { t ( 'proxy.form.save_changes' ) }
@@ -299,9 +301,15 @@ export const STCPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
299301}
300302
301303export const UDPProxyForm : React . FC < ProxyFormProps > = ( { serverID, clientID, defaultProxyConfig, proxyName, clientProxyConfigs, setClientProxyConfigs } ) => {
304+ const defaultConfig = defaultProxyConfig as UDPProxyConfig
302305 const [ _ , setUDPConfig ] = useState < UDPProxyConfig | undefined > ( )
303306 const form = useForm < z . infer < typeof UDPConfigSchema > > ( {
304307 resolver : zodResolver ( UDPConfigSchema ) ,
308+ defaultValues : {
309+ localPort : defaultConfig ?. localPort || 1234 ,
310+ localIP : defaultConfig ?. localIP || '127.0.0.1' ,
311+ remotePort : defaultConfig ?. remotePort || 1234 ,
312+ }
305313 } )
306314
307315 useEffect ( ( ) => {
@@ -344,9 +352,8 @@ export const UDPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
344352 < form onSubmit = { form . handleSubmit ( onSubmit ) } className = "space-y-4" >
345353 < Label className = "text-sm font-medium" > { t ( 'proxy.form.access_method' ) } </ Label >
346354 < p className = "text-sm border rounded p-2 my-2 font-mono overflow-auto" >
347- { `${ server ?. server ?. ip } :${ ( defaultProxyConfig as UDPProxyConfig ) . remotePort } -> ${
348- defaultProxyConfig ?. localIP
349- } :${ defaultProxyConfig ?. localPort } `}
355+ { `${ server ?. server ?. ip } :${ ( defaultProxyConfig as UDPProxyConfig ) . remotePort } -> ${ defaultProxyConfig ?. localIP
356+ } :${ defaultProxyConfig ?. localPort } `}
350357 </ p >
351358 < FormField
352359 control = { form . control }
@@ -360,7 +367,6 @@ export const UDPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
360367 < FormMessage />
361368 </ FormItem >
362369 ) }
363- defaultValue = { defaultProxyConfig === undefined ? 1234 : defaultProxyConfig . localPort }
364370 />
365371 < FormField
366372 control = { form . control }
@@ -374,7 +380,6 @@ export const UDPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
374380 < FormMessage />
375381 </ FormItem >
376382 ) }
377- defaultValue = { defaultProxyConfig === undefined ? '127.0.0.1' : defaultProxyConfig . localIP }
378383 />
379384 < FormField
380385 control = { form . control }
@@ -388,7 +393,6 @@ export const UDPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, def
388393 < FormMessage />
389394 </ FormItem >
390395 ) }
391- defaultValue = { defaultProxyConfig === undefined ? 4321 : ( defaultProxyConfig as UDPProxyConfig ) . remotePort }
392396 />
393397 < Button type = "submit" disabled = { isSaveDisabled } variant = { 'outline' } >
394398 < YesIcon className = { `mr-2 h-4 w-4 ${ isSaveDisabled ? '' : 'hidden' } ` } > </ YesIcon >
@@ -452,9 +456,8 @@ export const HTTPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
452456 < form onSubmit = { form . handleSubmit ( onSubmit ) } className = "space-y-4" >
453457 < Label className = "text-sm font-medium" > { t ( 'proxy.form.access_method' ) } </ Label >
454458 < p className = "text-sm border rounded p-2 my-2 font-mono overflow-auto" >
455- { `http://${ ( defaultProxyConfig as HTTPProxyConfig ) . subdomain } .${ serverConfig ?. subDomainHost } :${
456- serverConfig ?. vhostHTTPPort
457- } -> ${ defaultProxyConfig ?. localIP } :${ defaultProxyConfig ?. localPort } `}
459+ { `http://${ ( defaultProxyConfig as HTTPProxyConfig ) . subdomain } .${ serverConfig ?. subDomainHost } :${ serverConfig ?. vhostHTTPPort
460+ } -> ${ defaultProxyConfig ?. localIP } :${ defaultProxyConfig ?. localPort } `}
458461 </ p >
459462 < FormField
460463 control = { form . control }
@@ -468,7 +471,6 @@ export const HTTPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
468471 < FormMessage />
469472 </ FormItem >
470473 ) }
471- defaultValue = { defaultProxyConfig === undefined ? 1234 : defaultProxyConfig . localPort }
472474 />
473475 < FormField
474476 control = { form . control }
@@ -482,7 +484,6 @@ export const HTTPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
482484 < FormMessage />
483485 </ FormItem >
484486 ) }
485- defaultValue = { defaultProxyConfig === undefined ? '127.0.0.1' : defaultProxyConfig . localIP }
486487 />
487488 < FormField
488489 control = { form . control }
@@ -496,7 +497,6 @@ export const HTTPProxyForm: React.FC<ProxyFormProps> = ({ serverID, clientID, de
496497 < FormMessage />
497498 </ FormItem >
498499 ) }
499- defaultValue = { defaultProxyConfig === undefined ? '' : ( defaultProxyConfig as HTTPProxyConfig ) . subdomain }
500500 />
501501 < Button type = "submit" disabled = { isSaveDisabled } variant = { 'outline' } >
502502 < YesIcon className = { `mr-2 h-4 w-4 ${ isSaveDisabled ? '' : 'hidden' } ` } > </ YesIcon >
0 commit comments