A View that opens an invite
({ resource }: ResourcePageProps)
| 23 | |
| 24 | /** A View that opens an invite */ |
| 25 | function InvitePage({ resource }: ResourcePageProps): JSX.Element { |
| 26 | const [target] = useString(resource, properties.invite.target); |
| 27 | const [usagesLeft] = useNumber(resource, properties.invite.usagesLeft); |
| 28 | const [write] = useBoolean(resource, properties.invite.write); |
| 29 | const history = useHistory(); |
| 30 | const { agent, setAgent } = useSettings(); |
| 31 | const agentResource = useResource(agent?.subject); |
| 32 | const agentTitle = useTitle(agentResource, 15); |
| 33 | |
| 34 | const agentSubject = agent?.subject; |
| 35 | |
| 36 | if (autoAccept && agentSubject && usagesLeft && usagesLeft > 0) { |
| 37 | // Accept the invite if an agent subject is present, but not if the user just pressed the back button |
| 38 | if (history.action != 'POP') { |
| 39 | handleAccept(null, agentSubject); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // When the Invite is accepted, a new Agent might be created. |
| 44 | // When this happens, a new keypair is made, but the subject of the Agent is not yet known. |
| 45 | // It will be created by the server, and will be accessible in the Redirect response. |
| 46 | async function handleNew() { |
| 47 | const keypair = await generateKeyPair(); |
| 48 | const newAgent = new Agent(keypair.privateKey); |
| 49 | setAgent(newAgent); |
| 50 | const publicKey = await newAgent.getPublicKey(); |
| 51 | handleAccept(publicKey); |
| 52 | } |
| 53 | |
| 54 | function handleAccept(publicKey?: string, agent?: string) { |
| 55 | const inviteURL = new URL(resource.getSubject()); |
| 56 | |
| 57 | if (publicKey) { |
| 58 | inviteURL.searchParams.set('public-key', publicKey); |
| 59 | } else { |
| 60 | inviteURL.searchParams.set('agent', agent); |
| 61 | } |
| 62 | history.push(openURL(inviteURL.href)); |
| 63 | } |
| 64 | |
| 65 | return ( |
| 66 | <ContainerNarrow about={resource.getSubject()}> |
| 67 | <h1> |
| 68 | Invite to {write ? 'edit' : 'view'} <ResourceInline subject={target} /> |
| 69 | </h1> |
| 70 | <ValueForm resource={resource} propertyURL={properties.description} /> |
| 71 | {usagesLeft == 0 ? ( |
| 72 | <em>Sorry, this Invite has no usages left. Ask for a new one.</em> |
| 73 | ) : ( |
| 74 | <> |
| 75 | {agentSubject ? ( |
| 76 | <> |
| 77 | <Button |
| 78 | data-test='accept-existing' |
| 79 | onClick={() => handleAccept(null, agentSubject)} |
| 80 | > |
| 81 | Accept as {agentTitle} |
| 82 | </Button> |
nothing calls this directly
no test coverage detected