1- import React , { FC , ReactNode , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
1+ import React , { FC , ReactNode , useCallback , useEffect , useRef , useState } from 'react' ;
22
3- import { Controller , useForm } from 'react-hook-form' ;
3+ import { useForm } from 'react-hook-form' ;
4+ import { useTranslation } from 'react-i18next' ;
45
56import Alert from 'app/atoms/Alert' ;
67import FormField from 'app/atoms/FormField' ;
78import FormSubmitButton from 'app/atoms/FormSubmitButton' ;
8- import NoSpaceField from 'app/atoms/NoSpaceField' ;
9- import TabSwitcher from 'app/atoms/TabSwitcher' ;
109import PageLayout from 'app/layouts/PageLayout' ;
11- import { useTranslation } from 'react-i18next' ;
1210import { useMidenContext , useAllAccounts } from 'lib/miden/front' ;
1311import { navigate } from 'lib/woozie' ;
1412
@@ -18,12 +16,6 @@ type ImportAccountProps = {
1816 tabSlug : string | null ;
1917} ;
2018
21- interface ImportTabDescriptor {
22- slug : string ;
23- i18nKey : string ;
24- Form : FC < { } > ;
25- }
26-
2719const ImportAccount : FC < ImportAccountProps > = ( { tabSlug } ) => {
2820 const { t } = useTranslation ( ) ;
2921 const allAccounts = useAllAccounts ( ) ;
@@ -39,27 +31,6 @@ const ImportAccount: FC<ImportAccountProps> = ({ tabSlug }) => {
3931 prevAccLengthRef . current = accLength ;
4032 } , [ allAccounts , updateCurrentAccount ] ) ;
4133
42- const allTabs = useMemo (
43- ( ) =>
44- [
45- {
46- slug : 'private-key' ,
47- i18nKey : 'privateKey' ,
48- Form : ByPrivateKeyForm
49- } ,
50- {
51- slug : 'watch-only' ,
52- i18nKey : 'watchOnlyAccount' ,
53- Form : WatchOnlyForm
54- }
55- ] . filter ( ( x ) : x is ImportTabDescriptor => ! ! x ) ,
56- [ ]
57- ) ;
58- const { slug, Form } = useMemo ( ( ) => {
59- const tab = tabSlug ? allTabs . find ( currentTab => currentTab . slug === tabSlug ) : null ;
60- return tab ?? allTabs [ 0 ] ;
61- } , [ allTabs , tabSlug ] ) ;
62-
6334 return (
6435 < PageLayout
6536 pageTitle = {
@@ -69,9 +40,7 @@ const ImportAccount: FC<ImportAccountProps> = ({ tabSlug }) => {
6940 }
7041 >
7142 < div className = "p-4" >
72- < TabSwitcher className = "m-4" tabs = { allTabs } activeTabSlug = { slug } urlPrefix = "/import-account" />
73-
74- < Form />
43+ < ByPrivateKeyForm />
7544 </ div >
7645 </ PageLayout >
7746 ) ;
@@ -86,7 +55,7 @@ interface ByPrivateKeyFormData {
8655
8756const ByPrivateKeyForm : FC = ( ) => {
8857 const { t } = useTranslation ( ) ;
89- const { importAccount } = useMidenContext ( ) ;
58+ const { importPublicAccountByPrivateKey } = useMidenContext ( ) ;
9059
9160 const {
9261 register,
@@ -96,12 +65,12 @@ const ByPrivateKeyForm: FC = () => {
9665 const [ error , setError ] = useState < ReactNode > ( null ) ;
9766
9867 const onSubmit = useCallback (
99- async ( { privateKey, encPassword } : ByPrivateKeyFormData ) => {
68+ async ( { privateKey } : ByPrivateKeyFormData ) => {
10069 if ( isSubmitting ) return ;
10170
10271 setError ( null ) ;
10372 try {
104- await importAccount ( privateKey . replace ( / \s / g , '' ) , encPassword ) ;
73+ await importPublicAccountByPrivateKey ( privateKey ) ;
10574 } catch ( err : any ) {
10675 console . error ( err ) ;
10776
@@ -110,7 +79,7 @@ const ByPrivateKeyForm: FC = () => {
11079 setError ( err . message ) ;
11180 }
11281 } ,
113- [ importAccount , isSubmitting , setError ]
82+ [ importPublicAccountByPrivateKey , isSubmitting , setError ]
11483 ) ;
11584
11685 return (
@@ -129,7 +98,7 @@ const ByPrivateKeyForm: FC = () => {
12998 { t ( 'privateKey' ) }
13099 </ div >
131100 }
132- placeholder = { t ( 'privateKeyInputPlaceholder' ) }
101+ placeholder = { 'eg. 3b6a27bccebfb65e3...' }
133102 errorCaption = { errors . privateKey ?. message }
134103 className = "resize-none"
135104 onPaste = { ( ) => clearClipboard ( ) }
@@ -154,103 +123,3 @@ const ByPrivateKeyForm: FC = () => {
154123 </ form >
155124 ) ;
156125} ;
157-
158- interface WatchOnlyFormData {
159- viewKey : string ;
160- }
161-
162- const WatchOnlyForm : FC = ( ) => {
163- const { t } = useTranslation ( ) ;
164- const { importWatchOnlyAccount } = useMidenContext ( ) ;
165-
166- const {
167- handleSubmit,
168- control,
169- setValue,
170- getValues,
171- trigger,
172- formState : { errors, isSubmitting }
173- } = useForm < WatchOnlyFormData > ( {
174- mode : 'onChange'
175- } ) ;
176- const [ error , setError ] = useState < ReactNode > ( null ) ;
177-
178- const addressFieldRef = useRef < HTMLTextAreaElement > ( null ) ;
179-
180- const cleanViewKeyField = useCallback ( ( ) => {
181- setValue ( 'viewKey' , '' ) ;
182- trigger ( 'viewKey' ) ;
183- } , [ setValue , trigger ] ) ;
184-
185- const onSubmit = useCallback (
186- async ( { viewKey } : WatchOnlyFormData ) => {
187- if ( isSubmitting ) return ;
188-
189- setError ( null ) ;
190-
191- try {
192- await importWatchOnlyAccount ( viewKey ) ;
193- } catch ( err : any ) {
194- console . error ( err ) ;
195-
196- // Human delay
197- await new Promise ( r => setTimeout ( r , 300 ) ) ;
198- setError ( err . message ) ;
199- }
200- } ,
201- [ importWatchOnlyAccount , isSubmitting , setError ]
202- ) ;
203-
204- return (
205- < form className = "w-full max-w-sm mx-auto my-8" onSubmit = { handleSubmit ( onSubmit ) } style = { { minHeight : '325px' } } >
206- { error && < Alert type = "error" title = { t ( 'error' ) } description = { error } autoFocus className = "mb-6" /> }
207-
208- < Controller
209- name = "viewKey"
210- control = { control }
211- rules = { {
212- required : true ,
213- validate : ( value : any ) => true
214- } }
215- render = { ( { field } ) => (
216- < NoSpaceField
217- { ...field }
218- ref = { addressFieldRef }
219- onFocus = { ( ) => addressFieldRef . current ?. focus ( ) }
220- textarea
221- rows = { 1 }
222- cleanable = { Boolean ( getValues ( ) . viewKey ) }
223- onClean = { cleanViewKeyField }
224- id = "watch-viewKey"
225- label = {
226- < div className = "font-medium -mb-2" style = { { fontSize : '14px' , lineHeight : '20px' } } >
227- { t ( 'viewKeyWatchOnly' ) }
228- </ div >
229- }
230- placeholder = { t ( 'viewKeyInputPlaceholder' ) }
231- errorCaption = { errors . viewKey ?. message }
232- className = "resize-none"
233- />
234- ) }
235- />
236- < div className = "mb-6 text-gray-200" style = { { fontSize : '12px' , lineHeight : '16px' } } >
237- { t ( 'viewKeyInputDescription' ) }
238- </ div >
239-
240- < FormSubmitButton
241- className = "capitalize w-full justify-center"
242- style = { {
243- fontSize : '18px' ,
244- lineHeight : '24px' ,
245- paddingLeft : '0.5rem' ,
246- paddingRight : '0.5rem' ,
247- paddingTop : '12px' ,
248- paddingBottom : '12px'
249- } }
250- loading = { isSubmitting }
251- >
252- { t ( 'importAccount' ) }
253- </ FormSubmitButton >
254- </ form >
255- ) ;
256- } ;
0 commit comments