clean up config and remove unneccessary constants

This commit is contained in:
Sharon Kennedy
2022-01-17 15:44:05 -05:00
parent b82ba3da3f
commit eae9c737d7
12 changed files with 87 additions and 104 deletions

View File

@@ -1,16 +1,14 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import Chatbox from './chatbox';
import { DEFAULT_SETTINGS_ENDPOINT } from '../utils/constants';
const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest }) => {
const [settings, setSettings] = useState({});
const [shifts, setShifts] = useState();
const [isAvailable, setAvailability] = useState(false);
const [isAvailable, setAvailability] = useState(true);
const getSettings = async () => {
const endpoint = settingsEndpoint || DEFAULT_SETTINGS_ENDPOINT;
const url = `${endpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
const url = `${settingsEndpoint}?homeserver=${encodeURIComponent(matrixServerUrl)}`;
const res = await fetch(url);
const data = await res.json();
const { fields, schedule = [] } = data;
@@ -45,10 +43,6 @@ const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest
};
const checkSchedule = () => {
if (shifts.length === 0) {
setAvailability(true);
}
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const now = new Date();
@@ -71,11 +65,13 @@ const ChatboxWithSettings = ({ settingsEndpoint = null, matrixServerUrl, ...rest
};
useEffect(() => {
getSettings();
}, []);
if (settingsEndpoint) {
getSettings();
}
}, [settingsEndpoint]);
useEffect(() => {
if (shifts) {
if (shifts && shifts.length > 0) {
checkSchedule();
}
}, [shifts]);

View File

@@ -18,32 +18,13 @@ import Header from "./header";
import EmojiSelector from './emoji-selector';
import './styles.scss';
import defaultConfig from '../defaultConfig.js';
const ENCRYPTION_CONFIG = { "algorithm": "m.megolm.v1.aes-sha2" };
const ENCRYPTION_NOTICE = "Messages in this chat are secured with end-to-end encryption."
const UNENCRYPTION_NOTICE = "Messages in this chat are not encrypted."
const RESTARTING_UNENCRYPTED_CHAT_MESSAGE = "Restarting chat without encryption."
const CHAT_IS_OFFLINE_NOTICE = "CHAT_OFFLINE"
const DEFAULT_MATRIX_SERVER = "https://matrix.rhok.space/"
const DEFAULT_BOT_ID = "@help-bot:rhok.space"
const DEFAULT_TERMS_URL = "https://tosdr.org/"
const DEFAULT_ROOM_NAME = "Support Chat"
const DEFAULT_INTRO_MESSAGE = "This chat application does not collect any of your personal data or any data from your use of this service."
const DEFAULT_AGREEMENT_MESSAGE = "Do you want to continue?"
const DEFAULT_CONFIRMATION_MESSAGE = "Waiting for a facilitator to join the chat..."
const DEFAULT_EXIT_MESSAGE = "The chat is closed. You may close this window."
const DEFAULT_DISPLAY_NAME="Anonymous"
const DEFAULT_CHAT_UNAVAILABLE_MESSAGE = "The chat service is not available right now. Please try again later."
const DEFAULT_CHAT_OFFLINE_MESSAGE = "All of the chat facilitators are currently offline."
const DEFAULT_WAIT_MESSAGE = "Please be patient, our online facilitators are currently responding to other support requests."
const DEFAULT_ENCRYPTION_DISABLED = false
const DEFAULT_POSITION = 'bottom right'
const DEFAULT_SIZE = 'large'
const DEFAULT_WAIT_INTERVAL_MS = 120000 // 2 minutes
const DEFAULT_DOCK_LABEL = 'Start a new chat'
const DEFAULT_ENABLED = false
class ChatBox extends React.Component {
constructor(props) {
@@ -565,7 +546,7 @@ class ChatBox extends React.Component {
switch (signal) {
case 'END_CHAT':
this.displayBotMessage({ body: this.props.exitMessage })
return this.exitChat(false); // keep chat state
return this.exitChat(false); // keepg chat state
case 'CHAT_OFFLINE':
this.displayBotMessage({ body: this.props.chatOfflineMessage })
return this.exitChat(false); // keep chat state
@@ -659,6 +640,7 @@ class ChatBox extends React.Component {
}
render() {
console.log(this.props)
if (!this.props.enabled || !this.props.isAvailable) {
return null
}
@@ -791,29 +773,11 @@ ChatBox.propTypes = {
size: PropTypes.oneOf(['small', 'large']),
waitInterval: PropTypes.number,
dockLabel: PropTypes.string,
enabled: PropTypes.bool
enabled: PropTypes.bool,
isAvailable: PropTypes.bool
}
ChatBox.defaultProps = {
matrixServerUrl: DEFAULT_MATRIX_SERVER,
botId: DEFAULT_BOT_ID,
termsUrl: DEFAULT_TERMS_URL,
roomName: DEFAULT_ROOM_NAME,
introMessage: DEFAULT_INTRO_MESSAGE,
agreementMessage: DEFAULT_AGREEMENT_MESSAGE,
confirmationMessage: DEFAULT_CONFIRMATION_MESSAGE,
exitMessage: DEFAULT_EXIT_MESSAGE,
displayName: DEFAULT_DISPLAY_NAME,
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
waitMessage: DEFAULT_WAIT_MESSAGE,
chatOfflineMessage: DEFAULT_CHAT_OFFLINE_MESSAGE,
isEncryptionDisabled: DEFAULT_ENCRYPTION_DISABLED,
position: DEFAULT_POSITION,
size: DEFAULT_SIZE,
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
dockLabel: DEFAULT_DOCK_LABEL,
enabled: DEFAULT_ENABLED,
}
ChatBox.defaultProps = defaultConfig
export default ChatBox;

View File

@@ -27,8 +27,8 @@ import waitForExpect from 'wait-for-expect'
config.disabled = true
var testConfig = {
matrixServerUrl: 'https://matrix.rhok.space',
botId: '@help-bot:rhok.space',
matrixServerUrl: 'https://test.matrix.tld',
botId: '@help-bot:matrix.tld',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: 'This chat application does not collect any of your personal data or any data from your use of this service.',

39
src/defaultConfig.js Normal file
View File

@@ -0,0 +1,39 @@
const DEFAULT_TERMS_URL = 'https://tosdr.org/';
const DEFAULT_ROOM_NAME = 'Support Chat';
const DEFAULT_INTRO_MESSAGE = 'This chat application does not collect any of your personal data or any data from your use of this service.';
const DEFAULT_AGREEMENT_MESSAGE = 'Do you want to continue?';
const DEFAULT_CONFIRMATION_MESSAGE = 'Waiting for a facilitator to join the chat...';
const DEFAULT_EXIT_MESSAGE = 'The chat is closed. You may close this window.';
const DEFAULT_DISPLAY_NAME = 'Anonymous';
const DEFAULT_CHAT_UNAVAILABLE_MESSAGE = 'The chat service is not available right now. Please try again later.';
const DEFAULT_CHAT_OFFLINE_MESSAGE = 'All of the chat facilitators are currently offline.';
const DEFAULT_WAIT_MESSAGE = 'Please be patient, our online facilitators are currently responding to other support requests.';
const DEFAULT_ENCRYPTION_DISABLED = false;
const DEFAULT_POSITION = 'bottom right';
const DEFAULT_SIZE = 'large';
const DEFAULT_WAIT_INTERVAL_MS = 120000; // 2 minutes
const DEFAULT_DOCK_LABEL = 'Start a new chat';
const DEFAULT_ENABLED = true;
const DEFAULT_AVAILABLE = true;
const defaultConfig = {
termsUrl: DEFAULT_TERMS_URL,
roomName: DEFAULT_ROOM_NAME,
introMessage: DEFAULT_INTRO_MESSAGE,
agreementMessage: DEFAULT_AGREEMENT_MESSAGE,
confirmationMessage: DEFAULT_CONFIRMATION_MESSAGE,
exitMessage: DEFAULT_EXIT_MESSAGE,
displayName: DEFAULT_DISPLAY_NAME,
chatUnavailableMessage: DEFAULT_CHAT_UNAVAILABLE_MESSAGE,
waitMessage: DEFAULT_WAIT_MESSAGE,
chatOfflineMessage: DEFAULT_CHAT_OFFLINE_MESSAGE,
isEncryptionDisabled: DEFAULT_ENCRYPTION_DISABLED,
position: DEFAULT_POSITION,
size: DEFAULT_SIZE,
waitInterval: DEFAULT_WAIT_INTERVAL_MS,
dockLabel: DEFAULT_DOCK_LABEL,
enabled: DEFAULT_ENABLED,
isAvailable: DEFAULT_AVAILABLE,
};
export default defaultConfig;

View File

@@ -1,17 +1,7 @@
import EmbeddableChatbox from './embeddable-chatbox';
import defaultConfig from '../defaultConfig';
const config = {
matrixServerUrl: 'https://matrix.rhok.space',
botId: '@help-bot:rhok.space',
roomName: 'Support Chat',
termsUrl: 'https://tosdr.org/',
introMessage: 'This chat application does not collect any of your personal data or any data from your use of this service.',
agreementMessage: 'Do you want to continue?',
confirmationMessage: 'Waiting for a facilitator to join the chat...',
exitMessage: 'The chat is closed. You may close this window.',
chatUnavailableMessage: 'The chat service is not available right now. Please try again later.',
anonymousDisplayName: 'Anonymous',
};
const config = defaultConfig;
export default function bookmarklet() {
if (window.EmbeddableChatbox) {

View File

@@ -1 +0,0 @@
export const DEFAULT_SETTINGS_ENDPOINT = 'https://admin.safesupport.chat/api/get-settings';