-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev-wallet): allow key generation on specific index (#2699)
* feat(dev-wallet): allow key generation on specific index * feat(dw): add specific key form --------- Co-authored-by: Javad Khalilian <khalilian.javad@gmail.com>
- Loading branch information
Showing
5 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/apps/dev-wallet/src/pages/keys/Components/AddSpecificKey.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useWallet } from '@/modules/wallet/wallet.hook'; | ||
import { IKeySource } from '@/modules/wallet/wallet.repository'; | ||
import { getErrorMessage } from '@/utils/getErrorMessage'; | ||
import { MonoDoNotDisturb } from '@kadena/kode-icons/system'; | ||
import { | ||
Button, | ||
Heading, | ||
Notification, | ||
Stack, | ||
Text, | ||
TextField, | ||
} from '@kadena/kode-ui'; | ||
import { | ||
RightAside, | ||
RightAsideContent, | ||
RightAsideHeader, | ||
useLayout, | ||
} from '@kadena/kode-ui/patterns'; | ||
import { useState } from 'react'; | ||
|
||
export function AddSpecificKey({ | ||
keySource, | ||
isOpen, | ||
}: { | ||
keySource: IKeySource; | ||
isOpen: boolean; | ||
}) { | ||
const [index, setIndex] = useState(''); | ||
const { createKey } = useWallet(); | ||
const isAvailable = !keySource.keys.find((k) => k.index === parseInt(index)); | ||
const [error, setError] = useState<string | null>(null); | ||
const { setIsRightAsideExpanded } = useLayout(); | ||
return ( | ||
<RightAside isOpen={isOpen}> | ||
<RightAsideHeader label="Add Specific Key" /> | ||
<RightAsideContent> | ||
<Stack gap={'md'} flexDirection={'column'}> | ||
<form | ||
style={{ display: 'contents' }} | ||
onSubmit={async (e) => { | ||
e.preventDefault(); | ||
setError(null); | ||
if (index && isAvailable) { | ||
createKey(keySource, parseInt(index)) | ||
.then(() => { | ||
setIndex(''); | ||
setIsRightAsideExpanded(false); | ||
}) | ||
.catch((e) => { | ||
setError(getErrorMessage(e)); | ||
}); | ||
} | ||
}} | ||
> | ||
<Heading variant="h5">Add {keySource.source} key</Heading> | ||
<TextField | ||
label="Key Index" | ||
value={index} | ||
onChange={(e) => setIndex(e.target.value)} | ||
type="number" | ||
placeholder="e.g 123" | ||
/> | ||
{!isAvailable && ( | ||
<Text> | ||
<Stack gap={'xs'} alignItems={'center'}> | ||
<MonoDoNotDisturb /> This index is already created | ||
</Stack> | ||
</Text> | ||
)} | ||
{error && ( | ||
<Notification intent="negative" role="alert"> | ||
{error} | ||
</Notification> | ||
)} | ||
<Button type="submit" isDisabled={!isAvailable}> | ||
Add Key | ||
</Button> | ||
</form> | ||
</Stack> | ||
</RightAsideContent> | ||
</RightAside> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const getErrorMessage = (e: any) => { | ||
const message = 'message' in e ? e.message : JSON.stringify(e); | ||
if (message) { | ||
return typeof message === 'string' ? message : JSON.stringify(message); | ||
} | ||
return 'Unknown error'; | ||
}; |