|
| 1 | +import { memo, useEffect, useState } from 'react'; |
| 2 | +import type { Retro } from '../../shared/api-entities'; |
| 3 | +import { useSubmissionCallback } from '../../hooks/useSubmissionCallback'; |
| 4 | +import { realAutoFocus } from '../../helpers/realAutoFocus'; |
| 5 | +import { retroApiKeyService } from '../../api/api'; |
| 6 | +import { Popup } from '../common/Popup'; |
| 7 | +import { Alert } from '../common/Alert'; |
| 8 | +import './CreateReadonlyUrlPopup.css'; |
| 9 | + |
| 10 | +interface PropsT { |
| 11 | + retro: Retro; |
| 12 | + retroToken: string; |
| 13 | + isOpen: boolean; |
| 14 | + onSave: () => void; |
| 15 | + onClose: () => void; |
| 16 | +} |
| 17 | + |
| 18 | +const DEFAULT_NAME = 'Read-Only Link'; |
| 19 | + |
| 20 | +export const CreateReadonlyUrlPopup = memo( |
| 21 | + ({ retro, retroToken, isOpen, onSave, onClose }: PropsT) => { |
| 22 | + const { protocol, host } = document.location; |
| 23 | + const [url, setURL] = useState(''); |
| 24 | + const [name, setName] = useState(DEFAULT_NAME); |
| 25 | + const [performCreate, sending, error, resetError] = useSubmissionCallback( |
| 26 | + async () => { |
| 27 | + const created = await retroApiKeyService.create({ |
| 28 | + retro, |
| 29 | + retroToken, |
| 30 | + name, |
| 31 | + scopes: ['read'], |
| 32 | + }); |
| 33 | + setURL(`${protocol}//${host}/watch/${retro.id}#${created.key}`); |
| 34 | + onSave(); |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (isOpen) { |
| 40 | + setURL(''); |
| 41 | + setName(DEFAULT_NAME); |
| 42 | + resetError(); |
| 43 | + } |
| 44 | + }, [isOpen]); |
| 45 | + |
| 46 | + return ( |
| 47 | + <Popup |
| 48 | + title="New Read-Only URL" |
| 49 | + isOpen={isOpen} |
| 50 | + keys={{ Enter: url ? onClose : performCreate, Escape: onClose }} |
| 51 | + onClose={onClose} |
| 52 | + > |
| 53 | + {url ? ( |
| 54 | + <div className="popup-add-readonly-url"> |
| 55 | + <p> |
| 56 | + URL created. Make a note of this; it will not be possible to read |
| 57 | + it again. |
| 58 | + </p> |
| 59 | + <p className="url-output"> |
| 60 | + <a href={url} target="_blank" rel="noopener noreferrer"> |
| 61 | + {url} |
| 62 | + </a> |
| 63 | + </p> |
| 64 | + <section className="dialog-options"> |
| 65 | + <button |
| 66 | + type="button" |
| 67 | + className="global-button primary" |
| 68 | + onClick={onClose} |
| 69 | + > |
| 70 | + Close |
| 71 | + </button> |
| 72 | + </section> |
| 73 | + </div> |
| 74 | + ) : ( |
| 75 | + <form |
| 76 | + className="global-form popup-add-readonly-url" |
| 77 | + onSubmit={performCreate} |
| 78 | + > |
| 79 | + <p> |
| 80 | + Read-only URLs can be used to display live retros on shared |
| 81 | + devices, such as a smart TV or a meeting room computer. You can |
| 82 | + create multiple read-only URLs at once, and revoke any URL at any |
| 83 | + time. |
| 84 | + </p> |
| 85 | + <label> |
| 86 | + Reference Name (this does not appear in the URL) |
| 87 | + <input |
| 88 | + ref={realAutoFocus} |
| 89 | + name="api-key-name" |
| 90 | + type="text" |
| 91 | + placeholder="URL name" |
| 92 | + value={name} |
| 93 | + onChange={(e) => setName(e.currentTarget.value)} |
| 94 | + autoComplete="off" |
| 95 | + required |
| 96 | + /> |
| 97 | + </label> |
| 98 | + <section className="dialog-options"> |
| 99 | + <button type="button" className="global-button" onClick={onClose}> |
| 100 | + Cancel |
| 101 | + </button> |
| 102 | + <button |
| 103 | + type="submit" |
| 104 | + className="global-button primary" |
| 105 | + disabled={sending} |
| 106 | + > |
| 107 | + Create |
| 108 | + </button> |
| 109 | + <Alert message={error} /> |
| 110 | + </section> |
| 111 | + </form> |
| 112 | + )} |
| 113 | + </Popup> |
| 114 | + ); |
| 115 | + }, |
| 116 | +); |
0 commit comments