|
| 1 | +import { Key, type Messages, SpaceEvents, SpaceInfo, Utils, store } from '@graphprotocol/hypergraph'; |
| 2 | +import { useIdentityToken } from '@privy-io/react-auth'; |
| 3 | +import { useQueryClient } from '@tanstack/react-query'; |
| 4 | +import { useSelector } from '@xstate/store/react'; |
| 5 | +import { Effect } from 'effect'; |
| 6 | +import { useState } from 'react'; |
| 7 | +import { Spinner } from './spinner'; |
| 8 | +import { Button } from './ui/button'; |
| 9 | +import { Input } from './ui/input'; |
| 10 | + |
| 11 | +export function CreateSpace() { |
| 12 | + const [isLoading, setIsLoading] = useState(false); |
| 13 | + const [spaceName, setSpaceName] = useState(''); |
| 14 | + const { identityToken } = useIdentityToken(); |
| 15 | + const accountAddress = useSelector(store, (state) => state.context.accountAddress); |
| 16 | + const keys = useSelector(store, (state) => state.context.keys); |
| 17 | + const queryClient = useQueryClient(); |
| 18 | + |
| 19 | + const createSpace = async () => { |
| 20 | + setIsLoading(true); |
| 21 | + if (!accountAddress || !keys || !identityToken) { |
| 22 | + console.error('Missing required fields', { accountAddress, keys, identityToken }); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + const { encryptionPrivateKey, encryptionPublicKey, signaturePrivateKey, signaturePublicKey } = keys; |
| 28 | + const spaceId = Utils.generateId(); |
| 29 | + |
| 30 | + const spaceEvent = await Effect.runPromise( |
| 31 | + SpaceEvents.createSpace({ |
| 32 | + author: { |
| 33 | + accountAddress, |
| 34 | + encryptionPublicKey, |
| 35 | + signaturePrivateKey, |
| 36 | + signaturePublicKey, |
| 37 | + }, |
| 38 | + spaceId, |
| 39 | + }), |
| 40 | + ); |
| 41 | + const result = Key.createKey({ |
| 42 | + privateKey: Utils.hexToBytes(encryptionPrivateKey), |
| 43 | + publicKey: Utils.hexToBytes(encryptionPublicKey), |
| 44 | + }); |
| 45 | + |
| 46 | + const { infoContent, signature } = SpaceInfo.encryptAndSignSpaceInfo({ |
| 47 | + accountAddress, |
| 48 | + name: spaceName, |
| 49 | + secretKey: Utils.bytesToHex(result.key), |
| 50 | + signaturePrivateKey, |
| 51 | + spaceId, |
| 52 | + }); |
| 53 | + |
| 54 | + const message: Messages.RequestConnectCreateSpaceEvent = { |
| 55 | + type: 'connect-create-space-event', |
| 56 | + event: spaceEvent, |
| 57 | + spaceId: spaceEvent.transaction.id, |
| 58 | + keyBox: { |
| 59 | + accountAddress, |
| 60 | + ciphertext: Utils.bytesToHex(result.keyBoxCiphertext), |
| 61 | + nonce: Utils.bytesToHex(result.keyBoxNonce), |
| 62 | + authorPublicKey: encryptionPublicKey, |
| 63 | + id: Utils.generateId(), |
| 64 | + }, |
| 65 | + infoContent: Utils.bytesToHex(infoContent), |
| 66 | + infoSignature: signature, |
| 67 | + name: spaceName, |
| 68 | + }; |
| 69 | + |
| 70 | + const response = await fetch('http://localhost:3030/connect/spaces', { |
| 71 | + headers: { |
| 72 | + 'privy-id-token': identityToken, |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + }, |
| 75 | + method: 'POST', |
| 76 | + body: JSON.stringify(message), |
| 77 | + }); |
| 78 | + const data = await response.json(); |
| 79 | + if (data.space) { |
| 80 | + queryClient.invalidateQueries({ queryKey: ['spaces'] }); |
| 81 | + setSpaceName(''); |
| 82 | + } else { |
| 83 | + throw new Error('Failed to create space'); |
| 84 | + } |
| 85 | + } catch (error) { |
| 86 | + alert('Failed to create space'); |
| 87 | + console.error(error); |
| 88 | + } finally { |
| 89 | + setIsLoading(false); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + return ( |
| 94 | + <> |
| 95 | + <Input value={spaceName} onChange={(e) => setSpaceName(e.target.value)} /> |
| 96 | + <Button className="home-button" onClick={createSpace} disabled={isLoading}> |
| 97 | + Create Space {isLoading ? <Spinner /> : null} |
| 98 | + </Button> |
| 99 | + </> |
| 100 | + ); |
| 101 | +} |
0 commit comments