|
1 | 1 | <script lang="ts"> |
| 2 | + import BallotSummary from "./BallotSummary.svelte"; |
2 | 3 | import CopyEncryptedBallotForm from "./CopyEncryptedBallotForm.svelte"; |
3 | 4 | import FillBallotForm from "./FillBallotForm.svelte"; |
4 | 5 | import GitHubCredentials from "./GitHubCredentials.svelte"; |
5 | 6 | import FindPrForm from "./FindPRForm.svelte"; |
6 | 7 |
|
7 | | - let encryptDataPromise = new Promise(() => {}) as Promise<never>; |
| 8 | + import encryptData from "@node-core/caritat-crypto/encrypt"; |
| 9 | + import uint8ArrayToBase64 from "./uint8ArrayToBase64.ts"; |
| 10 | + const textEncoder = |
| 11 | + typeof TextEncoder === "undefined" ? { encode() {} } : new TextEncoder(); |
| 12 | +
|
| 13 | + let ballot: string | undefined; |
| 14 | + let encryptDataPromise = new Promise<never>(() => {}); |
| 15 | + let shouldSummarize = false; |
| 16 | + let ballotSummary = new Promise<never>(() => {}); |
8 | 17 |
|
9 | 18 | let url = globalThis.location?.hash.slice(1); |
10 | 19 |
|
|
23 | 32 | step = url ? Math.max(step, 1) : 0; |
24 | 33 | }); |
25 | 34 |
|
26 | | - function registerEncryptedBallot(promise) { |
27 | | - encryptDataPromise = promise; |
28 | | - promise.then( |
29 | | - () => { |
30 | | - step = 2; |
31 | | - }, |
32 | | - () => { |
33 | | - step = Math.min(step, 1); |
34 | | - } |
35 | | - ); |
| 35 | + function maybeUpdateSummary() { |
| 36 | + ballotSummary = shouldSummarize && ballot ? (async () => { |
| 37 | + // Lazy-loading as the summary is only a nice-to-have. |
| 38 | + const { getSummarizedBallot } = await import("./ballotSummary.ts"); |
| 39 | + return getSummarizedBallot(ballot); |
| 40 | + })() : new Promise<never>(() => {}); |
| 41 | + } |
| 42 | + function registerBallot(ballotContent, publicKey) { |
| 43 | + encryptDataPromise = (async () => { |
| 44 | + const { encryptedSecret, saltedCiphertext } = await encryptData( |
| 45 | + textEncoder.encode(ballotContent) as Uint8Array, |
| 46 | + await publicKey |
| 47 | + ); |
| 48 | + return JSON.stringify({ |
| 49 | + encryptedSecret: uint8ArrayToBase64(new Uint8Array(encryptedSecret)), |
| 50 | + data: uint8ArrayToBase64(saltedCiphertext), |
| 51 | + }); |
| 52 | + })(); |
| 53 | + ballot = ballotContent; |
| 54 | + step = 2; |
| 55 | + maybeUpdateSummary(); |
| 56 | + } |
| 57 | +
|
| 58 | + function onSummaryToggle(e) { |
| 59 | + shouldSummarize = e.newState === 'open'; |
| 60 | + maybeUpdateSummary(); |
36 | 61 | } |
37 | 62 | </script> |
38 | 63 |
|
|
48 | 73 | <FindPrForm {url} /> |
49 | 74 | </details> |
50 | 75 | <details open={step === 1}> |
51 | | - <FillBallotForm {url} {username} {token} {registerEncryptedBallot} /> |
| 76 | + <FillBallotForm {url} {username} {token} {registerBallot} /> |
| 77 | +</details> |
| 78 | +<details open={shouldSummarize} on:toggle={onSummaryToggle}> |
| 79 | + <BallotSummary {ballotSummary} /> |
52 | 80 | </details> |
53 | 81 | <details open={step === 2}> |
54 | | - <CopyEncryptedBallotForm {encryptDataPromise} {url} /> |
| 82 | + <CopyEncryptedBallotForm {ballot} {encryptDataPromise} {url} /> |
55 | 83 | </details> |
0 commit comments