2021-02-14 12:46:59 +03:00
|
|
|
import { Button, Collapse } from 'antd';
|
2021-02-01 11:05:09 +03:00
|
|
|
import classNames from 'classnames';
|
2022-12-28 05:48:21 +03:00
|
|
|
import React, { useContext, useState, useEffect } from 'react';
|
2023-01-10 07:57:29 +03:00
|
|
|
import { UpdateArgs } from '../../../../types/config-section';
|
|
|
|
import { ServerStatusContext } from '../../../../utils/server-status-context';
|
|
|
|
import { AlertMessageContext } from '../../../../utils/alert-message-context';
|
2021-02-04 10:24:12 +03:00
|
|
|
|
2021-02-01 11:05:09 +03:00
|
|
|
import {
|
|
|
|
postConfigUpdateToAPI,
|
|
|
|
API_S3_INFO,
|
|
|
|
RESET_TIMEOUT,
|
|
|
|
S3_TEXT_FIELDS_INFO,
|
2023-01-10 07:57:29 +03:00
|
|
|
} from '../../../../utils/config-constants';
|
2021-02-01 11:05:09 +03:00
|
|
|
import {
|
|
|
|
createInputStatus,
|
|
|
|
StatusState,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_PROCESSING,
|
|
|
|
STATUS_SUCCESS,
|
2023-01-10 07:57:29 +03:00
|
|
|
} from '../../../../utils/input-statuses';
|
|
|
|
import { TextField } from '../../TextField';
|
|
|
|
import { FormStatusIndicator } from '../../FormStatusIndicator';
|
|
|
|
import { isValidUrl } from '../../../../utils/urls';
|
|
|
|
import { ToggleSwitch } from '../../ToggleSwitch';
|
2021-02-01 11:05:09 +03:00
|
|
|
|
|
|
|
const { Panel } = Collapse;
|
|
|
|
|
|
|
|
// we could probably add more detailed checks here
|
|
|
|
// `currentValues` is what's currently in the global store and in the db
|
|
|
|
function checkSaveable(formValues: any, currentValues: any) {
|
2021-10-29 03:34:07 +03:00
|
|
|
const {
|
|
|
|
endpoint,
|
|
|
|
accessKey,
|
|
|
|
secret,
|
|
|
|
bucket,
|
|
|
|
region,
|
|
|
|
enabled,
|
|
|
|
servingEndpoint,
|
|
|
|
acl,
|
|
|
|
forcePathStyle,
|
|
|
|
} = formValues;
|
2021-02-01 11:05:09 +03:00
|
|
|
// if fields are filled out and different from what's in store, then return true
|
|
|
|
if (enabled) {
|
2021-02-02 02:20:26 +03:00
|
|
|
if (!!endpoint && isValidUrl(endpoint) && !!accessKey && !!secret && !!bucket && !!region) {
|
2021-02-01 11:05:09 +03:00
|
|
|
if (
|
2021-02-04 19:04:00 +03:00
|
|
|
enabled !== currentValues.enabled ||
|
2021-02-01 11:05:09 +03:00
|
|
|
endpoint !== currentValues.endpoint ||
|
|
|
|
accessKey !== currentValues.accessKey ||
|
|
|
|
secret !== currentValues.secret ||
|
2022-11-29 22:12:06 +03:00
|
|
|
bucket !== currentValues.bucket ||
|
2021-02-01 11:05:09 +03:00
|
|
|
region !== currentValues.region ||
|
2021-07-03 02:07:40 +03:00
|
|
|
(!currentValues.servingEndpoint && servingEndpoint !== '') ||
|
|
|
|
(!!currentValues.servingEndpoint && servingEndpoint !== currentValues.servingEndpoint) ||
|
|
|
|
(!currentValues.acl && acl !== '') ||
|
2021-10-29 03:33:32 +03:00
|
|
|
(!!currentValues.acl && acl !== currentValues.acl) ||
|
|
|
|
forcePathStyle !== currentValues.forcePathStyle
|
2021-02-01 11:05:09 +03:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (enabled !== currentValues.enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-10 07:57:29 +03:00
|
|
|
// eslint-disable-next-line react/function-component-definition
|
2022-12-28 05:48:21 +03:00
|
|
|
export default function EditStorage() {
|
2021-02-01 11:05:09 +03:00
|
|
|
const [formDataValues, setFormDataValues] = useState(null);
|
|
|
|
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
|
|
|
|
|
|
|
|
const [shouldDisplayForm, setShouldDisplayForm] = useState(false);
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
|
|
|
|
|
2021-02-04 19:04:00 +03:00
|
|
|
const { setMessage: setAlertMessage } = useContext(AlertMessageContext);
|
2021-02-04 10:24:12 +03:00
|
|
|
|
2021-02-01 11:05:09 +03:00
|
|
|
const { s3 } = serverConfig;
|
|
|
|
const {
|
|
|
|
accessKey = '',
|
|
|
|
acl = '',
|
|
|
|
bucket = '',
|
|
|
|
enabled = false,
|
|
|
|
endpoint = '',
|
|
|
|
region = '',
|
|
|
|
secret = '',
|
|
|
|
servingEndpoint = '',
|
2021-10-29 03:33:32 +03:00
|
|
|
forcePathStyle = false,
|
2021-02-01 11:05:09 +03:00
|
|
|
} = s3;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setFormDataValues({
|
|
|
|
accessKey,
|
|
|
|
acl,
|
|
|
|
bucket,
|
|
|
|
enabled,
|
|
|
|
endpoint,
|
|
|
|
region,
|
|
|
|
secret,
|
|
|
|
servingEndpoint,
|
2021-10-29 03:33:32 +03:00
|
|
|
forcePathStyle,
|
2021-02-01 11:05:09 +03:00
|
|
|
});
|
|
|
|
setShouldDisplayForm(enabled);
|
|
|
|
}, [s3]);
|
|
|
|
|
|
|
|
if (!formDataValues) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let resetTimer = null;
|
|
|
|
const resetStates = () => {
|
|
|
|
setSubmitStatus(null);
|
|
|
|
resetTimer = null;
|
|
|
|
clearTimeout(resetTimer);
|
|
|
|
};
|
|
|
|
|
|
|
|
// update individual values in state
|
|
|
|
const handleFieldChange = ({ fieldName, value }: UpdateArgs) => {
|
|
|
|
setFormDataValues({
|
|
|
|
...formDataValues,
|
|
|
|
[fieldName]: value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// posts the whole state
|
|
|
|
const handleSave = async () => {
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
|
|
|
|
const postValue = formDataValues;
|
|
|
|
|
|
|
|
await postConfigUpdateToAPI({
|
|
|
|
apiPath: API_S3_INFO,
|
|
|
|
data: { value: postValue },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({ fieldName: 's3', value: postValue, path: '' });
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_SUCCESS, 'Updated.'));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
2021-02-04 19:04:00 +03:00
|
|
|
setAlertMessage(
|
|
|
|
'Changing your storage configuration will take place the next time you start a new stream.',
|
|
|
|
);
|
2021-02-01 11:05:09 +03:00
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, message));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// toggle switch.
|
|
|
|
const handleSwitchChange = (storageEnabled: boolean) => {
|
|
|
|
setShouldDisplayForm(storageEnabled);
|
|
|
|
handleFieldChange({ fieldName: 'enabled', value: storageEnabled });
|
|
|
|
};
|
|
|
|
|
2021-10-29 03:33:32 +03:00
|
|
|
const handleForcePathStyleSwitchChange = (forcePathStyleEnabled: boolean) => {
|
|
|
|
handleFieldChange({ fieldName: 'forcePathStyle', value: forcePathStyleEnabled });
|
|
|
|
};
|
|
|
|
|
2021-02-01 11:05:09 +03:00
|
|
|
const containerClass = classNames({
|
|
|
|
'edit-storage-container': true,
|
2021-02-13 10:55:59 +03:00
|
|
|
'form-module': true,
|
2021-02-01 11:05:09 +03:00
|
|
|
enabled: shouldDisplayForm,
|
|
|
|
});
|
|
|
|
|
|
|
|
const isSaveable = checkSaveable(formDataValues, s3);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={containerClass}>
|
|
|
|
<div className="enable-switch">
|
2021-02-14 12:45:08 +03:00
|
|
|
<ToggleSwitch
|
|
|
|
apiPath=""
|
2021-02-13 10:55:59 +03:00
|
|
|
fieldName="enabled"
|
2021-11-15 23:22:45 +03:00
|
|
|
label="Use S3 Storage Provider"
|
2021-02-13 10:55:59 +03:00
|
|
|
checked={formDataValues.enabled}
|
|
|
|
onChange={handleSwitchChange}
|
2021-02-14 12:45:08 +03:00
|
|
|
/>
|
|
|
|
{/* <Switch
|
2021-02-01 11:05:09 +03:00
|
|
|
checked={formDataValues.enabled}
|
|
|
|
defaultChecked={formDataValues.enabled}
|
|
|
|
onChange={handleSwitchChange}
|
|
|
|
checkedChildren="ON"
|
|
|
|
unCheckedChildren="OFF"
|
|
|
|
/>{' '}
|
2021-02-14 12:45:08 +03:00
|
|
|
Enabled */}
|
2021-02-01 11:05:09 +03:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-fields">
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.endpoint}
|
|
|
|
value={formDataValues.endpoint}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.accessKey}
|
|
|
|
value={formDataValues.accessKey}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.secret}
|
|
|
|
value={formDataValues.secret}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.bucket}
|
|
|
|
value={formDataValues.bucket}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.region}
|
|
|
|
value={formDataValues.region}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Collapse className="advanced-section">
|
2021-02-02 02:20:26 +03:00
|
|
|
<Panel header="Optional Settings" key="1">
|
2021-02-01 11:05:09 +03:00
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.acl}
|
|
|
|
value={formDataValues.acl}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="field-container">
|
|
|
|
<TextField
|
|
|
|
{...S3_TEXT_FIELDS_INFO.servingEndpoint}
|
|
|
|
value={formDataValues.servingEndpoint}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-10-29 03:33:32 +03:00
|
|
|
<div className="enable-switch">
|
|
|
|
<ToggleSwitch
|
|
|
|
{...S3_TEXT_FIELDS_INFO.forcePathStyle}
|
|
|
|
fieldName="forcePathStyle"
|
|
|
|
checked={formDataValues.forcePathStyle}
|
|
|
|
onChange={handleForcePathStyleSwitchChange}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-02-01 11:05:09 +03:00
|
|
|
</Panel>
|
|
|
|
</Collapse>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="button-container">
|
|
|
|
<Button type="primary" onClick={handleSave} disabled={!isSaveable}>
|
|
|
|
Save
|
|
|
|
</Button>
|
2021-02-01 11:36:27 +03:00
|
|
|
<FormStatusIndicator status={submitStatus} />
|
2021-02-01 11:05:09 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-12-28 05:48:21 +03:00
|
|
|
}
|