2021-03-16 01:27:19 +03:00
|
|
|
import { DeleteOutlined } from '@ant-design/icons';
|
2021-07-09 21:42:01 +03:00
|
|
|
import { Button, Checkbox, Input, Modal, Space, Table, Typography } from 'antd';
|
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FormStatusIndicator } from '../../components/config/FormStatusIndicator';
|
2021-03-16 01:27:19 +03:00
|
|
|
import {
|
|
|
|
API_EXTERNAL_ACTIONS,
|
2021-07-09 21:42:01 +03:00
|
|
|
postConfigUpdateToAPI,
|
2021-03-16 01:27:19 +03:00
|
|
|
RESET_TIMEOUT,
|
2022-04-20 05:57:27 +03:00
|
|
|
} from '../../utils/config-constants';
|
|
|
|
import { createInputStatus, STATUS_ERROR, STATUS_SUCCESS } from '../../utils/input-statuses';
|
|
|
|
import { ServerStatusContext } from '../../utils/server-status-context';
|
|
|
|
import isValidUrl, { DEFAULT_TEXTFIELD_URL_PATTERN } from '../../utils/urls';
|
2021-03-16 01:27:19 +03:00
|
|
|
|
|
|
|
const { Title, Paragraph } = Typography;
|
|
|
|
let resetTimer = null;
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onCancel: () => void;
|
|
|
|
onOk: any; // todo: make better type
|
|
|
|
visible: boolean;
|
|
|
|
}
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
const NewActionModal = (props: Props) => {
|
2021-03-16 01:27:19 +03:00
|
|
|
const { onOk, onCancel, visible } = props;
|
|
|
|
|
|
|
|
const [actionUrl, setActionUrl] = useState('');
|
|
|
|
const [actionTitle, setActionTitle] = useState('');
|
|
|
|
const [actionDescription, setActionDescription] = useState('');
|
|
|
|
const [actionIcon, setActionIcon] = useState('');
|
|
|
|
const [actionColor, setActionColor] = useState('');
|
|
|
|
const [openExternally, setOpenExternally] = useState(false);
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
onOk(actionUrl, actionTitle, actionDescription, actionIcon, actionColor, openExternally);
|
2021-05-23 09:27:51 +03:00
|
|
|
setActionUrl('');
|
|
|
|
setActionTitle('');
|
|
|
|
setActionDescription('');
|
|
|
|
setActionIcon('');
|
|
|
|
setActionColor('');
|
|
|
|
setOpenExternally(false);
|
2021-03-16 01:27:19 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 07:26:27 +03:00
|
|
|
function canSave(): Boolean {
|
|
|
|
try {
|
|
|
|
const validationObject = new URL(actionUrl);
|
|
|
|
if (validationObject.protocol !== 'https:') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValidUrl(actionUrl) && actionTitle !== '';
|
|
|
|
}
|
|
|
|
|
2021-03-16 01:27:19 +03:00
|
|
|
const okButtonProps = {
|
2021-03-18 07:26:27 +03:00
|
|
|
disabled: !canSave(),
|
2021-03-16 01:27:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const onOpenExternallyChanged = checkbox => {
|
|
|
|
setOpenExternally(checkbox.target.checked);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title="Create New Action"
|
|
|
|
visible={visible}
|
|
|
|
onOk={save}
|
|
|
|
onCancel={onCancel}
|
|
|
|
okButtonProps={okButtonProps}
|
|
|
|
>
|
|
|
|
<div>
|
2021-03-23 06:43:25 +03:00
|
|
|
Add the URL for the external action you want to present.{' '}
|
|
|
|
<strong>Only HTTPS urls are supported.</strong>
|
|
|
|
<p>
|
2022-01-14 02:17:09 +03:00
|
|
|
<a
|
|
|
|
href="https://owncast.online/thirdparty/actions/"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
2021-03-23 06:43:25 +03:00
|
|
|
Read more about external actions.
|
|
|
|
</a>
|
|
|
|
</p>
|
2021-03-16 01:27:19 +03:00
|
|
|
<p>
|
|
|
|
<Input
|
|
|
|
value={actionUrl}
|
|
|
|
required
|
|
|
|
placeholder="https://myserver.com/action (required)"
|
2021-05-23 09:27:51 +03:00
|
|
|
onChange={input => setActionUrl(input.currentTarget.value.trim())}
|
|
|
|
type="url"
|
|
|
|
pattern={DEFAULT_TEXTFIELD_URL_PATTERN}
|
2021-03-16 01:27:19 +03:00
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<Input
|
|
|
|
value={actionTitle}
|
|
|
|
required
|
|
|
|
placeholder="Your action title (required)"
|
|
|
|
onChange={input => setActionTitle(input.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<Input
|
|
|
|
value={actionDescription}
|
|
|
|
placeholder="Optional description"
|
|
|
|
onChange={input => setActionDescription(input.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<Input
|
|
|
|
value={actionIcon}
|
|
|
|
placeholder="https://myserver.com/action/icon.png (optional)"
|
|
|
|
onChange={input => setActionIcon(input.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<Input
|
|
|
|
type="color"
|
|
|
|
value={actionColor}
|
|
|
|
onChange={input => setActionColor(input.currentTarget.value)}
|
|
|
|
/>
|
2021-03-18 07:26:27 +03:00
|
|
|
Optional background color of the action button.
|
2021-03-16 01:27:19 +03:00
|
|
|
</p>
|
|
|
|
<Checkbox
|
|
|
|
checked={openExternally}
|
|
|
|
defaultChecked={openExternally}
|
|
|
|
onChange={onOpenExternallyChanged}
|
|
|
|
>
|
|
|
|
Open in a new tab instead of within your page.
|
|
|
|
</Checkbox>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2021-03-16 01:27:19 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
const Actions = () => {
|
2021-03-16 01:27:19 +03:00
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
|
|
|
|
const { externalActions } = serverConfig;
|
|
|
|
const [actions, setActions] = useState([]);
|
|
|
|
const [isModalVisible, setIsModalVisible] = useState(false);
|
|
|
|
const [submitStatus, setSubmitStatus] = useState(null);
|
|
|
|
|
|
|
|
const resetStates = () => {
|
|
|
|
setSubmitStatus(null);
|
|
|
|
resetTimer = null;
|
|
|
|
clearTimeout(resetTimer);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setActions(externalActions || []);
|
|
|
|
}, [externalActions]);
|
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
async function save(actionsData) {
|
|
|
|
await postConfigUpdateToAPI({
|
|
|
|
apiPath: API_EXTERNAL_ACTIONS,
|
|
|
|
data: { value: actionsData },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({ fieldName: 'externalActions', value: actionsData, path: '' });
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_SUCCESS, 'Updated.'));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
2021-03-16 01:27:19 +03:00
|
|
|
},
|
2021-07-09 21:42:01 +03:00
|
|
|
onError: (message: string) => {
|
|
|
|
console.log(message);
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, message));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
2021-03-16 01:27:19 +03:00
|
|
|
},
|
2021-07-09 21:42:01 +03:00
|
|
|
});
|
|
|
|
}
|
2021-03-16 01:27:19 +03:00
|
|
|
|
|
|
|
async function handleDelete(action) {
|
2021-07-09 21:42:01 +03:00
|
|
|
const actionsData = [...actions];
|
2021-03-16 01:27:19 +03:00
|
|
|
const index = actions.findIndex(item => item.url === action.url);
|
|
|
|
actionsData.splice(index, 1);
|
|
|
|
|
|
|
|
try {
|
2021-07-09 21:42:01 +03:00
|
|
|
setActions(actionsData);
|
|
|
|
save(actionsData);
|
2021-03-16 01:27:19 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleSave(
|
|
|
|
url: string,
|
|
|
|
title: string,
|
|
|
|
description: string,
|
|
|
|
icon: string,
|
|
|
|
color: string,
|
|
|
|
openExternally: boolean,
|
|
|
|
) {
|
|
|
|
try {
|
2021-07-09 21:42:01 +03:00
|
|
|
const actionsData = [...actions];
|
2021-03-18 07:26:27 +03:00
|
|
|
const updatedActions = actionsData.concat({
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
icon,
|
|
|
|
color,
|
|
|
|
openExternally,
|
|
|
|
});
|
2021-03-16 01:27:19 +03:00
|
|
|
setActions(updatedActions);
|
|
|
|
await save(updatedActions);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const showCreateModal = () => {
|
|
|
|
setIsModalVisible(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleModalSaveButton = (
|
|
|
|
actionUrl: string,
|
|
|
|
actionTitle: string,
|
|
|
|
actionDescription: string,
|
|
|
|
actionIcon: string,
|
|
|
|
actionColor: string,
|
|
|
|
openExternally: boolean,
|
|
|
|
) => {
|
|
|
|
setIsModalVisible(false);
|
|
|
|
handleSave(actionUrl, actionTitle, actionDescription, actionIcon, actionColor, openExternally);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleModalCancelButton = () => {
|
|
|
|
setIsModalVisible(false);
|
|
|
|
};
|
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: '',
|
|
|
|
key: 'delete',
|
|
|
|
render: (text, record) => (
|
|
|
|
<Space size="middle">
|
|
|
|
<Button onClick={() => handleDelete(record)} icon={<DeleteOutlined />} />
|
|
|
|
</Space>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'title',
|
|
|
|
key: 'title',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Description',
|
|
|
|
dataIndex: 'description',
|
|
|
|
key: 'description',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'URL',
|
|
|
|
dataIndex: 'url',
|
|
|
|
key: 'url',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Icon',
|
|
|
|
dataIndex: 'icon',
|
|
|
|
key: 'icon',
|
|
|
|
render: (url: string) => (url ? <img style={{ width: '2vw' }} src={url} alt="" /> : null),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Color',
|
|
|
|
dataIndex: 'color',
|
|
|
|
key: 'color',
|
|
|
|
render: (color: string) =>
|
|
|
|
color ? <div style={{ backgroundColor: color, height: '30px' }}>{color}</div> : null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Opens',
|
|
|
|
dataIndex: 'openExternally',
|
|
|
|
key: 'openExternally',
|
|
|
|
render: (openExternally: boolean) => (openExternally ? 'In a new tab' : 'In a modal'),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-03-16 01:27:19 +03:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Title>External Actions</Title>
|
2021-04-05 06:06:26 +03:00
|
|
|
<Paragraph>
|
|
|
|
External action URLs are 3rd party UI you can display, embedded, into your Owncast page when
|
|
|
|
a user clicks on a button to launch your action.
|
|
|
|
</Paragraph>
|
2021-03-16 01:27:19 +03:00
|
|
|
<Paragraph>
|
|
|
|
Read more about how to use actions, with examples, at{' '}
|
2021-05-23 09:27:51 +03:00
|
|
|
<a
|
|
|
|
href="https://owncast.online/thirdparty/?source=admin"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
our documentation
|
|
|
|
</a>
|
|
|
|
.
|
2021-03-16 01:27:19 +03:00
|
|
|
</Paragraph>
|
|
|
|
|
2021-05-23 09:27:51 +03:00
|
|
|
<Table
|
|
|
|
rowKey={record => `${record.title}-${record.url}`}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={actions}
|
|
|
|
pagination={false}
|
|
|
|
/>
|
2021-03-16 01:27:19 +03:00
|
|
|
<br />
|
|
|
|
<Button type="primary" onClick={showCreateModal}>
|
|
|
|
Create New Action
|
|
|
|
</Button>
|
|
|
|
<FormStatusIndicator status={submitStatus} />
|
|
|
|
|
|
|
|
<NewActionModal
|
|
|
|
visible={isModalVisible}
|
|
|
|
onOk={handleModalSaveButton}
|
|
|
|
onCancel={handleModalCancelButton}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
|
|
|
export default Actions;
|