11import { NavigationPage } from '@/components/navigation/NavigationPage' ;
22import { getParams , setParams as setParamsGlobal } from '@/library/powersync/ConnectionManager' ;
3- import { Box , Button , Grid , IconButton , styled , TextField } from '@mui/material' ;
3+ import {
4+ Box ,
5+ Button ,
6+ Grid ,
7+ IconButton ,
8+ styled ,
9+ TextField ,
10+ Select ,
11+ MenuItem ,
12+ InputLabel ,
13+ FormControl
14+ } from '@mui/material' ;
415import { FormEvent , useState } from 'react' ;
516import DeleteIcon from '@mui/icons-material/Delete' ;
617import AddIcon from '@mui/icons-material/Add' ;
718
19+ const typeForValue = ( value : unknown ) => {
20+ if ( Array . isArray ( value ) ) return 'array' ;
21+ return typeof value ;
22+ } ;
23+
824const jsonToObjectArray = ( json : Object ) => {
925 const entrySet = Object . entries ( json ) ;
10- return entrySet . map ( ( [ key , value ] ) => ( {
11- key,
12- value
13- } ) ) ;
26+ return entrySet . map ( ( [ key , value ] ) => {
27+ const type = typeForValue ( value ) ;
28+ return {
29+ key,
30+ value : type === 'array' || type === 'object' ? JSON . stringify ( value ) : value ,
31+ type
32+ } ;
33+ } ) ;
34+ } ;
35+
36+ const CONVERTERS = {
37+ string : ( v ) => v ,
38+ number : ( v ) => Number ( v ) ,
39+ boolean : ( v ) => v === 'true' ,
40+ array : ( v ) => JSON . parse ( v ) ,
41+ object : ( v ) => JSON . parse ( v )
1442} ;
1543
1644function ClientParamsPage ( ) {
1745 const [ params , setParams ] = useState ( jsonToObjectArray ( getParams ( ) ) ) ;
1846
47+ const convertValueForSave = ( t , stringValue : string ) => CONVERTERS [ t ] ( stringValue ) ;
48+
1949 const onSubmit = ( e : FormEvent < HTMLFormElement > ) => {
2050 e . preventDefault ( ) ;
2151 e . stopPropagation ( ) ;
2252
23- const newParams = params . reduce ( ( curr , item ) => ( { ...curr , [ `${ item . key } ` ] : item . value } ) , { } ) ;
53+ const newParams = params . reduce (
54+ ( curr , item ) => ( { ...curr , [ `${ item . key } ` ] : convertValueForSave ( item . type , item . value ) } ) ,
55+ { }
56+ ) ;
2457 setParamsGlobal ( newParams ) ;
2558 } ;
2659
@@ -33,34 +66,51 @@ function ClientParamsPage() {
3366 setParams ( ( a ) => [ ...a , { key : '' , value : '' } ] ) ;
3467 } ;
3568
36- const changeValue = ( idx : number , value : string , currKey : string ) => {
37- replace ( idx , { key : currKey , value } ) ;
69+ const changeValue = ( idx : number , value : string , currKey : string , type : string ) => {
70+ replace ( idx , { key : currKey , value, type } ) ;
71+ } ;
72+
73+ const changeKey = ( idx : number , key : string , currValue : unknown , type : string ) => {
74+ replace ( idx , { key, value : currValue , type } ) ;
3875 } ;
3976
40- const changeKey = ( idx : number , key : string , currValue : unknown ) => {
41- replace ( idx , { key, value : currValue } ) ;
77+ const changeType = ( idx : number , key : string , value : unknown , newType : string ) => {
78+ replace ( idx , { key, value, type : newType } ) ;
4279 } ;
4380
4481 return (
4582 < NavigationPage title = "Client Parameters" >
4683 < S . MainContainer >
4784 < form onSubmit = { onSubmit } >
48- { params . map ( ( { key, value } , idx ) => (
85+ { params . map ( ( { key, value, type } : { key : string ; value : string ; type : string } , idx : number ) => (
4986 < S . CenteredGrid container >
5087 < S . CenteredGrid item xs = { 12 } md = { 10 } >
5188 < TextField
5289 label = "Key"
5390 value = { key }
5491 sx = { { margin : '10px' } }
55- onChange = { ( v ) => changeKey ( idx , v . target . value , value ) }
92+ onChange = { ( v : { target : { value : string } } ) => changeKey ( idx , v . target . value , value , type ) }
5693 />
5794 < TextField
5895 label = "Value"
5996 value = { value }
6097 sx = { { margin : '10px' } }
61- onChange = { ( v ) => changeValue ( idx , v . target . value , key ) }
98+ onChange = { ( v : { target : { value : string } } ) => changeValue ( idx , v . target . value , key , type ) }
6299 />
63-
100+ < FormControl sx = { { margin : '10px' , width : '125px' , minWidth : '95px' } } >
101+ < InputLabel id = "demo-simple-select-label" > Type</ InputLabel >
102+ < Select
103+ labelId = "demo-simple-select-label"
104+ value = { type }
105+ label = "Type"
106+ onChange = { ( v : { target : { value : string } } ) => changeType ( idx , key , value , v . target . value ) } >
107+ < MenuItem value = { 'string' } > String</ MenuItem >
108+ < MenuItem value = { 'number' } > Number</ MenuItem >
109+ < MenuItem value = { 'array' } > Array</ MenuItem >
110+ < MenuItem value = { 'object' } > Object</ MenuItem >
111+ < MenuItem value = { 'boolean' } > boolean</ MenuItem >
112+ </ Select >
113+ </ FormControl >
64114 < IconButton sx = { { margin : '10px' } } color = "error" onClick = { ( ) => removeIdx ( idx ) } >
65115 < DeleteIcon />
66116 </ IconButton >
0 commit comments