You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.7 KiB
166 lines
6.7 KiB
|
6 years ago
|
import React, { useMemo, useCallback } from 'react';
|
||
|
6 years ago
|
import { Field, TextInput, Box, ToggleSwitch, Icon, TextAreaInput, FieldGroup, Margins } from '@rocket.chat/fuselage';
|
||
|
|
|
||
|
5 years ago
|
import { useTranslation } from '../../../contexts/TranslationContext';
|
||
|
|
import { useAbsoluteUrl } from '../../../contexts/ServerContext';
|
||
|
|
import { useHighlightedCode } from '../../../hooks/useHighlightedCode';
|
||
|
6 years ago
|
import { useExampleData } from './exampleIncomingData';
|
||
|
5 years ago
|
import Page from '../../../components/Page';
|
||
|
6 years ago
|
|
||
|
|
export default function IncomingWebhookForm({ formValues, formHandlers, extraData = {}, append, ...props }) {
|
||
|
|
const t = useTranslation();
|
||
|
|
|
||
|
|
const absoluteUrl = useAbsoluteUrl();
|
||
|
|
|
||
|
|
const {
|
||
|
|
enabled,
|
||
|
|
channel,
|
||
|
|
username,
|
||
|
|
name,
|
||
|
|
alias,
|
||
|
|
avatarUrl,
|
||
|
|
emoji,
|
||
|
|
scriptEnabled,
|
||
|
|
script,
|
||
|
|
} = formValues;
|
||
|
|
|
||
|
|
const {
|
||
|
|
handleEnabled,
|
||
|
|
handleChannel,
|
||
|
|
handleUsername,
|
||
|
|
handleName,
|
||
|
|
handleAlias,
|
||
|
|
handleAvatarUrl,
|
||
|
|
handleEmoji,
|
||
|
|
handleScriptEnabled,
|
||
|
|
handleScript,
|
||
|
|
} = formHandlers;
|
||
|
|
|
||
|
|
const url = absoluteUrl(`hooks/${ extraData._id }/${ extraData.token }`);
|
||
|
|
|
||
|
6 years ago
|
const additionalFields = useMemo(() => ({
|
||
|
|
...alias && { alias },
|
||
|
|
...emoji && { emoji },
|
||
|
|
...avatarUrl && { avatar: avatarUrl },
|
||
|
|
}), [alias, avatarUrl, emoji]);
|
||
|
|
|
||
|
6 years ago
|
const [exampleData, curlData] = useExampleData({
|
||
|
6 years ago
|
additionalFields,
|
||
|
6 years ago
|
url,
|
||
|
6 years ago
|
});
|
||
|
6 years ago
|
|
||
|
6 years ago
|
const hilightedExampleJson = useHighlightedCode('json', JSON.stringify(exampleData, null, 2));
|
||
|
6 years ago
|
|
||
|
6 years ago
|
return <Page.ScrollableContent pb='x24' mi='neg-x24' is='form' onSubmit={useCallback((e) => e.preventDefault(), [])} qa-admin-user-edit='form' { ...props }>
|
||
|
6 years ago
|
<Margins block='x16'>
|
||
|
|
<FieldGroup width='x600' alignSelf='center'>
|
||
|
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label display='flex' justifyContent='space-between' w='full'>
|
||
|
|
{t('Enabled')}
|
||
|
|
<ToggleSwitch checked={enabled} onChange={handleEnabled} />
|
||
|
|
</Field.Label>
|
||
|
6 years ago
|
</Field>, [enabled, handleEnabled, t])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{t('Name_optional')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={name} onChange={handleName}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('You_should_name_it_to_easily_manage_your_integrations')}</Field.Hint>
|
||
|
6 years ago
|
</Field>, [t, name, handleName])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{t('Post_to_Channel')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={channel} onChange={handleChannel} addon={<Icon name='at' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('Messages_that_are_sent_to_the_Incoming_WebHook_will_be_posted_here')}</Field.Hint>
|
||
|
|
<Field.Hint dangerouslySetInnerHTML={{ __html: t('Start_with_s_for_user_or_s_for_channel_Eg_s_or_s', '@', '#', '@john', '#general') }} />
|
||
|
6 years ago
|
</Field>, [channel, handleChannel, t])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{t('Post_as')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={username} onChange={handleUsername} addon={<Icon name='user' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('Choose_the_username_that_this_integration_will_post_as')}</Field.Hint>
|
||
|
|
<Field.Hint>{t('Should_exists_a_user_with_this_username')}</Field.Hint>
|
||
|
6 years ago
|
</Field>, [t, username, handleUsername])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{`${ t('Alias') } (${ t('optional') })`}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={alias} onChange={handleAlias} addon={<Icon name='edit' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('Choose_the_alias_that_will_appear_before_the_username_in_messages')}</Field.Hint>
|
||
|
6 years ago
|
</Field>, [alias, handleAlias, t])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{`${ t('Avatar_URL') } (${ t('optional') })`}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={avatarUrl} onChange={handleAvatarUrl} addon={<Icon name='user-rounded' size='x20' alignSelf='center'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('You_can_change_a_different_avatar_too')}</Field.Hint>
|
||
|
|
<Field.Hint>{t('Should_be_a_URL_of_an_image')}</Field.Hint>
|
||
|
6 years ago
|
</Field>, [avatarUrl, handleAvatarUrl, t])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{`${ t('Emoji') } (${ t('optional') })`}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={emoji} onChange={handleEmoji} addon={<Icon name='emoji' size='x20' alignSelf='center'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('You_can_use_an_emoji_as_avatar')}</Field.Hint>
|
||
|
|
<Field.Hint dangerouslySetInnerHTML={{ __html: t('Example_s', ':ghost:') }} />
|
||
|
6 years ago
|
</Field>, [emoji, handleEmoji, t])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label display='flex' justifyContent='space-between' w='full'>
|
||
|
|
{t('Script_Enabled')}
|
||
|
|
<ToggleSwitch checked={scriptEnabled} onChange={handleScriptEnabled} />
|
||
|
|
</Field.Label>
|
||
|
6 years ago
|
</Field>, [t, scriptEnabled, handleScriptEnabled])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{t('Script')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextAreaInput rows={10} flexGrow={1} value={script} onChange={handleScript} addon={<Icon name='code' size='x20' alignSelf='center'/>}/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field>, [t, script, handleScript])}
|
||
|
6 years ago
|
{useMemo(() => !extraData._id && <><Field>
|
||
|
|
<Field.Label>{t('Webhook_URL')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={t('Will_be_available_here_after_saving')} addon={<Icon name='permalink' size='x20'/>} disabled/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('Send_your_JSON_payloads_to_this_URL')}</Field.Hint>
|
||
|
|
</Field>
|
||
|
|
<Field>
|
||
|
|
<Field.Label>{t('Token')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={t('Will_be_available_here_after_saving')} addon={<Icon name='key' size='x20'/>} disabled/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field></>, [extraData._id, t])}
|
||
|
6 years ago
|
{useMemo(() => extraData._id && <><Field>
|
||
|
|
<Field.Label>{t('Webhook_URL')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={url} addon={<Icon name='permalink' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Hint>{t('Send_your_JSON_payloads_to_this_URL')}</Field.Hint>
|
||
|
|
</Field>
|
||
|
|
<Field>
|
||
|
|
<Field.Label>{t('Token')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={`${ extraData._id }/${ extraData.token }`} addon={<Icon name='key' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field></>, [extraData._id, extraData.token, t, url])}
|
||
|
6 years ago
|
{useMemo(() => <Field>
|
||
|
|
<Field.Label>{t('Example_payload')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<Box fontScale='p1' withRichContent flexGrow={1}>
|
||
|
|
<pre><code dangerouslySetInnerHTML={{ __html: hilightedExampleJson }}></code></pre>
|
||
|
|
</Box>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field>, [hilightedExampleJson, t])}
|
||
|
6 years ago
|
{useMemo(() => extraData._id && <Field>
|
||
|
|
<Field.Label>{t('Curl')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput flexGrow={1} value={curlData} addon={<Icon name='code' size='x20'/>}/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field>, [curlData, extraData._id, t])}
|
||
|
6 years ago
|
{ append }
|
||
|
|
</FieldGroup>
|
||
|
|
</Margins>
|
||
|
|
</Page.ScrollableContent>;
|
||
|
|
}
|