|
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { Alert, Button, Form } from 'react-bootstrap'; |
| 3 | +import { useNavigate } from 'react-router-dom'; |
| 4 | +import Page from '../components/Page'; |
| 5 | +import useLNC from '../hooks/useLNC'; |
| 6 | + |
| 7 | +const Connect: React.FC = () => { |
| 8 | + const { lnc, connect } = useLNC(); |
| 9 | + const navigate = useNavigate(); |
| 10 | + const [phrase, setPhrase] = useState(''); |
| 11 | + const [password, setPassword] = useState(''); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + const [error, setError] = useState(''); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + // preload the WASM file when this component is mounted |
| 17 | + lnc.preload(); |
| 18 | + }, [lnc]); |
| 19 | + |
| 20 | + const handleSubmit = useCallback( |
| 21 | + (e: React.FormEvent<HTMLFormElement>) => { |
| 22 | + // wrap LNC calls into an async function |
| 23 | + const connectAsync = async () => { |
| 24 | + e.preventDefault(); |
| 25 | + try { |
| 26 | + setLoading(true); |
| 27 | + setError(''); |
| 28 | + if (!phrase || !password) throw new Error('Enter a phrase and password'); |
| 29 | + |
| 30 | + // connect to the litd node via LNC |
| 31 | + await connect(phrase, password); |
| 32 | + |
| 33 | + navigate('/'); |
| 34 | + } catch (err) { |
| 35 | + setError((err as Error).message); |
| 36 | + // tslint:disable-next-line: no-console |
| 37 | + console.error(err); |
| 38 | + } finally { |
| 39 | + setLoading(false); |
| 40 | + } |
| 41 | + }; |
| 42 | + connectAsync(); |
| 43 | + }, |
| 44 | + [phrase, password, navigate, connect], |
| 45 | + ); |
| 46 | + |
| 47 | + return ( |
| 48 | + <Page> |
| 49 | + <h2>Connect to Lightning Terminal</h2> |
| 50 | + |
| 51 | + {error && <Alert variant="danger">{error}</Alert>} |
| 52 | + |
| 53 | + <Form onSubmit={handleSubmit}> |
| 54 | + <Form.Group className="mb-3" controlId="formBasicEmail"> |
| 55 | + <Form.Label>Pairing Phrase</Form.Label> |
| 56 | + <Form.Control |
| 57 | + autoComplete="off" |
| 58 | + value={phrase} |
| 59 | + onChange={e => setPhrase(e.target.value)} |
| 60 | + disabled={loading} |
| 61 | + /> |
| 62 | + <Form.Text className="text-muted"> |
| 63 | + Obtain a new pairing phrase from <code>litd</code> and enter it here |
| 64 | + </Form.Text> |
| 65 | + </Form.Group> |
| 66 | + <Form.Group className="mb-3" controlId="formBasicPassword"> |
| 67 | + <Form.Label>Create Password</Form.Label> |
| 68 | + <Form.Control |
| 69 | + type="password" |
| 70 | + autoComplete="new-password" |
| 71 | + value={password} |
| 72 | + onChange={e => setPassword(e.target.value)} |
| 73 | + disabled={loading} |
| 74 | + /> |
| 75 | + <Form.Text className="text-muted"> |
| 76 | + lnc-web stores connection data in localStorage. This password will be used to |
| 77 | + encrypt the data at rest. |
| 78 | + </Form.Text> |
| 79 | + </Form.Group> |
| 80 | + <Button variant="primary" type="submit" disabled={loading}> |
| 81 | + Submit |
| 82 | + </Button> |
| 83 | + </Form> |
| 84 | + </Page> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export default Connect; |
0 commit comments