1- import React , { FC , ReactNode , useCallback , useEffect , useRef , useState } from 'react' ;
1+ import React , { FC , ReactNode , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22
33import { useForm } from 'react-hook-form' ;
44import { useTranslation } from 'react-i18next' ;
55
66import Alert from 'app/atoms/Alert' ;
77import FormField from 'app/atoms/FormField' ;
88import FormSubmitButton from 'app/atoms/FormSubmitButton' ;
9+ import { ACCOUNT_NAME_PATTERN } from 'app/defaults' ;
910import PageLayout from 'app/layouts/PageLayout' ;
1011import { useMidenContext , useAllAccounts } from 'lib/miden/front' ;
1112import { navigate } from 'lib/woozie' ;
@@ -39,7 +40,7 @@ const ImportAccount: FC<ImportAccountProps> = ({ tabSlug }) => {
3940 </ >
4041 }
4142 >
42- < div className = "p -4" >
43+ < div className = "px -4" >
4344 < ByPrivateKeyForm />
4445 </ div >
4546 </ PageLayout >
@@ -50,27 +51,37 @@ export default ImportAccount;
5051
5152interface ByPrivateKeyFormData {
5253 privateKey : string ;
54+ name ?: string ;
5355 encPassword ?: string ;
5456}
5557
5658const ByPrivateKeyForm : FC = ( ) => {
5759 const { t } = useTranslation ( ) ;
5860 const { importPublicAccountByPrivateKey } = useMidenContext ( ) ;
59-
61+ const allAccounts = useAllAccounts ( ) ;
6062 const {
6163 register,
6264 handleSubmit,
65+ setValue,
6366 formState : { errors, isSubmitting }
6467 } = useForm < ByPrivateKeyFormData > ( ) ;
6568 const [ error , setError ] = useState < ReactNode > ( null ) ;
6669
70+ const computedDefaultName = useMemo ( ( ) => {
71+ return `Imported Acc ${ allAccounts . length + 1 } ` ;
72+ } , [ allAccounts ] ) ;
73+
74+ useEffect ( ( ) => {
75+ setValue ( 'name' , computedDefaultName ) ;
76+ } , [ computedDefaultName , setValue ] ) ;
77+
6778 const onSubmit = useCallback (
68- async ( { privateKey } : ByPrivateKeyFormData ) => {
79+ async ( { privateKey, name } : ByPrivateKeyFormData ) => {
6980 if ( isSubmitting ) return ;
7081
7182 setError ( null ) ;
7283 try {
73- await importPublicAccountByPrivateKey ( privateKey ) ;
84+ await importPublicAccountByPrivateKey ( privateKey , name ) ;
7485 } catch ( err : any ) {
7586 console . error ( err ) ;
7687
@@ -84,13 +95,34 @@ const ByPrivateKeyForm: FC = () => {
8495
8596 return (
8697 < form className = "w-full max-w-sm mx-auto my-8" onSubmit = { handleSubmit ( onSubmit ) } style = { { minHeight : '325px' } } >
98+ < FormField
99+ { ...register ( 'name' , {
100+ pattern : {
101+ value : ACCOUNT_NAME_PATTERN ,
102+ message : t ( 'accountNameInputTitle' )
103+ }
104+ } ) }
105+ label = {
106+ < div className = "font-medium -mb-2" style = { { fontSize : '14px' , lineHeight : '20px' } } >
107+ { t ( 'accountName' ) }
108+ </ div >
109+ }
110+ id = "create-account-name"
111+ type = "text"
112+ placeholder = { computedDefaultName }
113+ errorCaption = { errors . name ?. message }
114+ autoFocus
115+ />
116+ < div className = "text-gray-200 mb-8" style = { { fontSize : '12px' , lineHeight : '16px' } } >
117+ { t ( 'accountNameInputDescription' ) }
118+ </ div >
87119 { error && < Alert type = "error" title = { t ( 'error' ) } autoFocus description = { error } className = "mb-6" /> }
88120
89121 < FormField
90122 { ...register ( 'privateKey' , { required : t ( 'required' ) } ) }
91123 secret
92124 textarea
93- rows = { 1 }
125+ rows = { 5 }
94126 name = "privateKey"
95127 id = "importacc-privatekey"
96128 label = {
0 commit comments