2020-10-20 22:38:38 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Chatbox from './chatbox';
|
|
|
|
|
|
|
|
|
2020-11-13 03:00:06 +00:00
|
|
|
const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => {
|
2020-10-20 22:38:38 +00:00
|
|
|
const [settings, setSettings] = useState({});
|
|
|
|
const settingsObj = {};
|
|
|
|
|
|
|
|
const getSettings = async () => {
|
|
|
|
if (!settingsEndpoint) {
|
2020-10-20 23:29:12 +00:00
|
|
|
const props = {
|
|
|
|
...rest,
|
2020-11-13 03:00:06 +00:00
|
|
|
enabled: true,
|
|
|
|
};
|
2020-10-20 23:29:12 +00:00
|
|
|
|
|
|
|
return setSettings(props);
|
2020-10-20 22:38:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 03:00:06 +00:00
|
|
|
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
|
|
|
|
const res = await fetch(url);
|
2020-10-20 22:38:38 +00:00
|
|
|
const data = await res.json();
|
|
|
|
const { fields } = data;
|
|
|
|
|
|
|
|
Object.entries(fields).forEach(([k, v]) => {
|
|
|
|
const [scope, key] = k.split('_');
|
|
|
|
|
|
|
|
if (scope === 'web') {
|
|
|
|
settingsObj[key] = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-20 23:29:12 +00:00
|
|
|
if (!settingsObj.enabled) {
|
|
|
|
settingsObj.enabled = false;
|
|
|
|
}
|
2020-10-20 22:38:38 +00:00
|
|
|
|
|
|
|
return setSettings(settingsObj);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getSettings();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-11-13 03:00:06 +00:00
|
|
|
<Chatbox matrixServerUrl={matrixServerUrl} {...settings} {...rest} />
|
2020-10-20 22:38:38 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ChatboxWithSettings.propTypes = {
|
2020-11-13 03:00:06 +00:00
|
|
|
matrixServerUrl: PropTypes.string.isRequired,
|
2020-10-20 22:38:38 +00:00
|
|
|
settingsEndpoint: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2020-11-13 03:00:06 +00:00
|
|
|
ChatboxWithSettings.defaultProps = {
|
|
|
|
settingsEndpoint: null,
|
|
|
|
};
|
|
|
|
|
2020-10-20 22:38:38 +00:00
|
|
|
export default ChatboxWithSettings;
|