|
| 1 | +import { Pencil1Icon } from '@radix-ui/react-icons'; |
| 2 | +import { Blockquote, Box, Button, Code, DataList, Dialog, Flex, TextField } from '@radix-ui/themes'; |
| 3 | +import { getBase64Decoder } from '@solana/web3.js'; |
| 4 | +import type { ReadonlyUint8Array } from '@wallet-standard/core'; |
| 5 | +import type { SyntheticEvent } from 'react'; |
| 6 | +import { useRef, useState } from 'react'; |
| 7 | + |
| 8 | +import { ErrorDialog } from '../components/ErrorDialog'; |
| 9 | + |
| 10 | +type Props = Readonly<{ |
| 11 | + signMessage(message: ReadonlyUint8Array): Promise<ReadonlyUint8Array>; |
| 12 | +}>; |
| 13 | + |
| 14 | +export function BaseSignMessageFeaturePanel({ signMessage }: Props) { |
| 15 | + const { current: NO_ERROR } = useRef(Symbol()); |
| 16 | + const [isSigningMessage, setIsSigningMessage] = useState(false); |
| 17 | + const [error, setError] = useState<unknown | typeof NO_ERROR>(NO_ERROR); |
| 18 | + const [lastSignature, setLastSignature] = useState<ReadonlyUint8Array | undefined>(); |
| 19 | + const [text, setText] = useState<string>(); |
| 20 | + return ( |
| 21 | + <Flex asChild gap="2" direction={{ initial: 'column', sm: 'row' }} style={{ width: '100%' }}> |
| 22 | + <form |
| 23 | + onSubmit={async e => { |
| 24 | + e.preventDefault(); |
| 25 | + setError(NO_ERROR); |
| 26 | + setIsSigningMessage(true); |
| 27 | + try { |
| 28 | + const signature = await signMessage(new TextEncoder().encode(text)); |
| 29 | + setLastSignature(signature); |
| 30 | + } catch (e) { |
| 31 | + setLastSignature(undefined); |
| 32 | + setError(e); |
| 33 | + } finally { |
| 34 | + setIsSigningMessage(false); |
| 35 | + } |
| 36 | + }} |
| 37 | + > |
| 38 | + <Box flexGrow="1"> |
| 39 | + <TextField.Root |
| 40 | + placeholder="Write a message to sign" |
| 41 | + onChange={(e: SyntheticEvent<HTMLInputElement>) => setText(e.currentTarget.value)} |
| 42 | + value={text} |
| 43 | + > |
| 44 | + <TextField.Slot> |
| 45 | + <Pencil1Icon /> |
| 46 | + </TextField.Slot> |
| 47 | + </TextField.Root> |
| 48 | + </Box> |
| 49 | + <Dialog.Root |
| 50 | + open={!!lastSignature} |
| 51 | + onOpenChange={open => { |
| 52 | + if (!open) { |
| 53 | + setLastSignature(undefined); |
| 54 | + } |
| 55 | + }} |
| 56 | + > |
| 57 | + <Dialog.Trigger> |
| 58 | + <Button |
| 59 | + color={error ? undefined : 'red'} |
| 60 | + disabled={!text} |
| 61 | + loading={isSigningMessage} |
| 62 | + type="submit" |
| 63 | + > |
| 64 | + Sign Message |
| 65 | + </Button> |
| 66 | + </Dialog.Trigger> |
| 67 | + {lastSignature ? ( |
| 68 | + <Dialog.Content |
| 69 | + onClick={e => { |
| 70 | + e.stopPropagation(); |
| 71 | + }} |
| 72 | + > |
| 73 | + <Dialog.Title>You Signed a Message!</Dialog.Title> |
| 74 | + <DataList.Root orientation={{ initial: 'vertical', sm: 'horizontal' }}> |
| 75 | + <DataList.Item> |
| 76 | + <DataList.Label minWidth="88px">Message</DataList.Label> |
| 77 | + <DataList.Value> |
| 78 | + <Blockquote>{text}</Blockquote> |
| 79 | + </DataList.Value> |
| 80 | + </DataList.Item> |
| 81 | + <DataList.Item> |
| 82 | + <DataList.Label minWidth="88px">Signature</DataList.Label> |
| 83 | + <DataList.Value> |
| 84 | + <Code truncate>{getBase64Decoder().decode(lastSignature)}</Code> |
| 85 | + </DataList.Value> |
| 86 | + </DataList.Item> |
| 87 | + </DataList.Root> |
| 88 | + <Flex gap="3" mt="4" justify="end"> |
| 89 | + <Dialog.Close> |
| 90 | + <Button>Cool!</Button> |
| 91 | + </Dialog.Close> |
| 92 | + </Flex> |
| 93 | + </Dialog.Content> |
| 94 | + ) : null} |
| 95 | + </Dialog.Root> |
| 96 | + {error !== NO_ERROR ? ( |
| 97 | + <ErrorDialog error={error} onClose={() => setError(NO_ERROR)} title="Failed to sign message" /> |
| 98 | + ) : null} |
| 99 | + </form> |
| 100 | + </Flex> |
| 101 | + ); |
| 102 | +} |
0 commit comments