4 Commits

Author SHA1 Message Date
Sharon Kennedy
4491b681c5 2.3.0 2020-11-28 15:03:17 -05:00
Sharon Kennedy
dcd7718f8f check schedule if using admin settings 2020-11-28 15:02:55 -05:00
Sharon Kennedy
c8c42e9086 2.2.2 2020-11-12 22:02:26 -05:00
Sharon Kennedy
004f88d5cd pass homeserver query param to get chatbox with settings 2020-11-12 22:01:51 -05:00
7 changed files with 63 additions and 18 deletions

2
dist/bookmarklet.js vendored

File diff suppressed because one or more lines are too long

2
dist/chatbox.js vendored

File diff suppressed because one or more lines are too long

3
dist/index.html vendored
View File

@@ -13,7 +13,8 @@
<script src="./chatbox.js"></script> <script src="./chatbox.js"></script>
<script> <script>
var config = { var config = {
settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings' settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings',
matrixServerUrl: 'https://matrix.safesupport.chat'
} }
EmbeddableChatbox.mount(config); EmbeddableChatbox.mount(config);

View File

@@ -1,6 +1,6 @@
{ {
"name": "private-safesupport-chatbox", "name": "private-safesupport-chatbox",
"version": "2.2.1", "version": "2.3.0",
"description": "A secure and private embeddable chatbox that connects to Riot", "description": "A secure and private embeddable chatbox that connects to Riot",
"main": "dist/chatbox.js", "main": "dist/chatbox.js",
"scripts": { "scripts": {

View File

@@ -13,7 +13,8 @@
<script src="./chatbox.js"></script> <script src="./chatbox.js"></script>
<script> <script>
var config = { var config = {
settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings' settingsEndpoint: 'https://safesupport-admin.herokuapp.com/api/get-settings',
matrixServerUrl: 'https://matrix.safesupport.chat'
} }
EmbeddableChatbox.mount(config); EmbeddableChatbox.mount(config);

View File

@@ -3,24 +3,29 @@ import PropTypes from 'prop-types';
import Chatbox from './chatbox'; import Chatbox from './chatbox';
const ChatboxWithSettings = ({ settingsEndpoint, ...rest }) => { const ChatboxWithSettings = ({ settingsEndpoint, matrixServerUrl, ...rest }) => {
const [settings, setSettings] = useState({}); const [settings, setSettings] = useState({});
const [shifts, setShifts] = useState();
const [isAvailable, setAvailability] = useState(false);
const settingsObj = {}; const settingsObj = {};
const getSettings = async () => { const getSettings = async () => {
if (!settingsEndpoint) { if (!settingsEndpoint) {
const props = { const props = {
...rest, ...rest,
enabled: true enabled: true,
} isAvailable: true,
};
return setSettings(props); return setSettings(props);
} }
const res = await fetch(settingsEndpoint); const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
const res = await fetch(url);
const data = await res.json(); const data = await res.json();
const { fields } = data; const { fields, schedule = [] } = data;
setShifts(schedule);
Object.entries(fields).forEach(([k, v]) => { Object.entries(fields).forEach(([k, v]) => {
const [scope, key] = k.split('_'); const [scope, key] = k.split('_');
@@ -37,17 +42,55 @@ const ChatboxWithSettings = ({ settingsEndpoint, ...rest }) => {
return setSettings(settingsObj); return setSettings(settingsObj);
}; };
const checkSchedule = () => {
if (shifts.length === 0) {
setAvailability(true);
}
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const now = new Date();
const weekday = weekdays[now.getDay()];
const hours = now.getHours();
const minutes = now.getMinutes();
shifts.map(async (shift) => {
const [startHours, startMinutes] = shift.start.split(':').map((t) => Number(t));
const [endHours, endMinutes] = shift.end.split(':').map((t) => Number(t));
if (
shift.service === 'web'
&& shift.weekday === weekday
&& ((startHours < hours) || (startHours === hours && startMinutes <= minutes))
&& ((endHours > hours) || (endHours === hours && endMinutes > minutes))
) {
setAvailability(true);
}
});
};
useEffect(() => { useEffect(() => {
getSettings(); getSettings();
}, []); }, []);
useEffect(() => {
if (shifts) {
checkSchedule();
}
}, [shifts]);
return ( return (
<Chatbox {...settings} {...rest} /> /* eslint-disable react/jsx-props-no-spreading */
<Chatbox matrixServerUrl={matrixServerUrl} isAvailable={isAvailable} {...settings} {...rest} />
); );
}; };
ChatboxWithSettings.propTypes = { ChatboxWithSettings.propTypes = {
matrixServerUrl: PropTypes.string.isRequired,
settingsEndpoint: PropTypes.string, settingsEndpoint: PropTypes.string,
}; };
ChatboxWithSettings.defaultProps = {
settingsEndpoint: null,
};
export default ChatboxWithSettings; export default ChatboxWithSettings;

View File

@@ -69,7 +69,7 @@ class ChatBox extends React.Component {
isMobile: true, isMobile: true,
isSlowConnection: true, isSlowConnection: true,
decryptionErrors: {}, decryptionErrors: {},
messagesInFlight: [], messagesInFlight: []
} }
this.state = this.initialState this.state = this.initialState
this.chatboxInput = React.createRef(); this.chatboxInput = React.createRef();
@@ -587,14 +587,14 @@ class ChatBox extends React.Component {
} }
componentDidMount() { componentDidMount() {
document.addEventListener("keydown", this.handleKeyDown, false); document.addEventListener("keydown", this.handleKeyDown, false)
window.addEventListener('beforeunload', this.exitChat) window.addEventListener('beforeunload', this.exitChat)
} }
componentWillUnmount() { componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyDown, false); document.removeEventListener("keydown", this.handleKeyDown, false)
window.removeEventListener('beforeunload', this.exitChat) window.removeEventListener('beforeunload', this.exitChat)
this.exitChat(); this.exitChat()
} }
handleInputChange = e => { handleInputChange = e => {
@@ -655,7 +655,7 @@ class ChatBox extends React.Component {
} }
render() { render() {
if (!this.props.enabled) { if (!this.props.enabled || !this.props.isAvailable) {
return null return null
} }
@@ -788,7 +788,7 @@ ChatBox.propTypes = {
maxWaitTime: PropTypes.number, maxWaitTime: PropTypes.number,
waitInterval: PropTypes.number, waitInterval: PropTypes.number,
dockLabel: PropTypes.string, dockLabel: PropTypes.string,
enabled: PropTypes.bool, enabled: PropTypes.bool
} }
ChatBox.defaultProps = { ChatBox.defaultProps = {