({
client,
onClose,
onCreated,
}: OAuthClientModalProps)
| 102 | } |
| 103 | |
| 104 | const OAuthClientModal = ({ |
| 105 | client, |
| 106 | onClose, |
| 107 | onCreated, |
| 108 | }: OAuthClientModalProps): ReactElement => { |
| 109 | const isEdit = !!client; |
| 110 | const [name, setName] = useState(client?.client_name ?? ''); |
| 111 | const [redirectUris, setRedirectUris] = useState( |
| 112 | client?.redirect_uris.join('\n') ?? '', |
| 113 | ); |
| 114 | const [homepage, setHomepage] = useState(client?.client_uri ?? ''); |
| 115 | const [logo, setLogo] = useState(client?.logo_uri ?? ''); |
| 116 | const { mutateAsync: createClient, isPending: isCreating } = |
| 117 | useCreateOAuthClient(); |
| 118 | const { mutateAsync: updateClient, isPending: isUpdating } = |
| 119 | useUpdateOAuthClient(); |
| 120 | const { displayToast } = useToastNotification(); |
| 121 | const uris = parseRedirectUris(redirectUris); |
| 122 | const canSubmit = |
| 123 | name.trim().length >= OAUTH_APP_NAME_MIN_LENGTH && uris.length > 0; |
| 124 | |
| 125 | const handleSubmit = async () => { |
| 126 | const input: OAuthClientInput = { |
| 127 | client_name: name.trim(), |
| 128 | redirect_uris: uris, |
| 129 | ...(homepage.trim() && { client_uri: homepage.trim() }), |
| 130 | ...(logo.trim() && { logo_uri: logo.trim() }), |
| 131 | }; |
| 132 | |
| 133 | try { |
| 134 | if (client) { |
| 135 | await updateClient({ clientId: client.client_id, update: input }); |
| 136 | displayToast('App updated'); |
| 137 | onClose(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | onCreated(await createClient(input)); |
| 142 | } catch (err) { |
| 143 | displayToast( |
| 144 | getErrorMessage(err, 'Failed to save the app. Please try again.'), |
| 145 | ); |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | return ( |
| 150 | <Modal isOpen onRequestClose={onClose} size={ModalSize.Small}> |
| 151 | <ModalHeader title={isEdit ? 'Edit OAuth app' : 'Create OAuth app'} /> |
| 152 | <ModalBody className="flex flex-col gap-4"> |
| 153 | <TextField |
| 154 | label="App name" |
| 155 | inputId="oauth-app-name" |
| 156 | name="client_name" |
| 157 | value={name} |
| 158 | onChange={(e) => setName(e.target.value)} |
| 159 | placeholder="e.g., My daily.dev integration" |
| 160 | maxLength={OAUTH_APP_NAME_MAX_LENGTH} |
| 161 | /> |
nothing calls this directly
no test coverage detected