2021-06-22 06:19:00 +03:00
|
|
|
import { Typography } from 'antd';
|
2023-01-16 10:11:44 +03:00
|
|
|
import React, { ReactElement, useContext, useEffect, useState } from 'react';
|
2023-01-10 07:57:29 +03:00
|
|
|
import { TEXTFIELD_TYPE_TEXTAREA } from '../../components/admin/TextField';
|
|
|
|
import { TextFieldWithSubmit } from '../../components/admin/TextFieldWithSubmit';
|
|
|
|
import { ToggleSwitch } from '../../components/admin/ToggleSwitch';
|
|
|
|
import { EditValueArray } from '../../components/admin/EditValueArray';
|
2022-01-12 21:17:14 +03:00
|
|
|
import {
|
|
|
|
createInputStatus,
|
|
|
|
StatusState,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_SUCCESS,
|
2022-04-20 05:57:27 +03:00
|
|
|
} from '../../utils/input-statuses';
|
2021-07-26 08:12:50 +03:00
|
|
|
|
2022-04-20 05:57:27 +03:00
|
|
|
import { UpdateArgs } from '../../types/config-section';
|
2021-06-22 06:19:00 +03:00
|
|
|
import {
|
2021-07-26 08:12:50 +03:00
|
|
|
API_CHAT_FORBIDDEN_USERNAMES,
|
2022-01-12 21:17:14 +03:00
|
|
|
API_CHAT_SUGGESTED_USERNAMES,
|
2022-03-06 09:36:38 +03:00
|
|
|
FIELD_PROPS_CHAT_JOIN_MESSAGES_ENABLED,
|
2022-03-07 11:06:07 +03:00
|
|
|
CHAT_ESTABLISHED_USER_MODE,
|
2022-01-12 21:17:14 +03:00
|
|
|
FIELD_PROPS_DISABLE_CHAT,
|
2021-07-26 08:12:50 +03:00
|
|
|
postConfigUpdateToAPI,
|
|
|
|
RESET_TIMEOUT,
|
2022-01-12 21:17:14 +03:00
|
|
|
TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES,
|
|
|
|
TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES,
|
|
|
|
TEXTFIELD_PROPS_SERVER_WELCOME_MESSAGE,
|
2022-04-20 05:57:27 +03:00
|
|
|
} from '../../utils/config-constants';
|
|
|
|
import { ServerStatusContext } from '../../utils/server-status-context';
|
2021-06-22 06:19:00 +03:00
|
|
|
|
2023-01-16 10:11:44 +03:00
|
|
|
import { AdminLayout } from '../../components/layouts/AdminLayout';
|
|
|
|
|
2021-06-22 06:19:00 +03:00
|
|
|
export default function ConfigChat() {
|
|
|
|
const { Title } = Typography;
|
|
|
|
const [formDataValues, setFormDataValues] = useState(null);
|
2022-04-23 23:52:04 +03:00
|
|
|
const [forbiddenUsernameSaveState, setForbiddenUsernameSaveState] = useState<StatusState>(null);
|
|
|
|
const [suggestedUsernameSaveState, setSuggestedUsernameSaveState] = useState<StatusState>(null);
|
2021-06-22 06:19:00 +03:00
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
2021-07-26 08:12:50 +03:00
|
|
|
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
|
2021-06-22 06:19:00 +03:00
|
|
|
|
2022-03-06 09:36:38 +03:00
|
|
|
const {
|
|
|
|
chatDisabled,
|
|
|
|
chatJoinMessagesEnabled,
|
|
|
|
forbiddenUsernames,
|
|
|
|
instanceDetails,
|
|
|
|
suggestedUsernames,
|
2022-03-07 11:06:07 +03:00
|
|
|
chatEstablishedUserMode,
|
2022-03-06 09:36:38 +03:00
|
|
|
} = serverConfig;
|
2021-07-20 08:02:02 +03:00
|
|
|
const { welcomeMessage } = instanceDetails;
|
2021-06-22 06:19:00 +03:00
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
const handleFieldChange = ({ fieldName, value }: UpdateArgs) => {
|
|
|
|
setFormDataValues({
|
|
|
|
...formDataValues,
|
|
|
|
[fieldName]: value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-22 06:19:00 +03:00
|
|
|
function handleChatDisableChange(disabled: boolean) {
|
2021-09-11 01:08:15 +03:00
|
|
|
handleFieldChange({ fieldName: 'chatDisabled', value: !disabled });
|
2021-06-22 06:19:00 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 09:36:38 +03:00
|
|
|
function handleChatJoinMessagesEnabledChange(enabled: boolean) {
|
|
|
|
handleFieldChange({ fieldName: 'chatJoinMessagesEnabled', value: enabled });
|
|
|
|
}
|
|
|
|
|
2022-03-07 11:06:07 +03:00
|
|
|
function handleEstablishedUserModeChange(enabled: boolean) {
|
|
|
|
handleFieldChange({ fieldName: 'chatEstablishedUserMode', value: enabled });
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:12:50 +03:00
|
|
|
function resetForbiddenUsernameState() {
|
2022-01-12 21:17:14 +03:00
|
|
|
setForbiddenUsernameSaveState(null);
|
2021-07-26 08:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveForbiddenUsernames() {
|
|
|
|
postConfigUpdateToAPI({
|
|
|
|
apiPath: API_CHAT_FORBIDDEN_USERNAMES,
|
|
|
|
data: { value: formDataValues.forbiddenUsernames },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({
|
|
|
|
fieldName: 'forbiddenUsernames',
|
|
|
|
value: formDataValues.forbiddenUsernames,
|
|
|
|
});
|
2022-04-23 23:52:04 +03:00
|
|
|
setForbiddenUsernameSaveState(createInputStatus(STATUS_SUCCESS));
|
2021-07-26 08:12:50 +03:00
|
|
|
setTimeout(resetForbiddenUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
2022-01-12 21:17:14 +03:00
|
|
|
setForbiddenUsernameSaveState(createInputStatus(STATUS_ERROR, message));
|
2021-07-26 08:12:50 +03:00
|
|
|
setTimeout(resetForbiddenUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-12 21:17:14 +03:00
|
|
|
function handleDeleteForbiddenUsernameIndex(index: number) {
|
2021-07-26 08:12:50 +03:00
|
|
|
formDataValues.forbiddenUsernames.splice(index, 1);
|
|
|
|
saveForbiddenUsernames();
|
|
|
|
}
|
|
|
|
|
2022-01-12 21:17:14 +03:00
|
|
|
function handleCreateForbiddenUsername(tag: string) {
|
2021-07-26 08:12:50 +03:00
|
|
|
formDataValues.forbiddenUsernames.push(tag);
|
|
|
|
handleFieldChange({
|
|
|
|
fieldName: 'forbiddenUsernames',
|
|
|
|
value: formDataValues.forbiddenUsernames,
|
|
|
|
});
|
|
|
|
saveForbiddenUsernames();
|
2021-06-22 06:19:00 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 21:17:14 +03:00
|
|
|
function resetSuggestedUsernameState() {
|
|
|
|
setSuggestedUsernameSaveState(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveSuggestedUsernames() {
|
|
|
|
postConfigUpdateToAPI({
|
|
|
|
apiPath: API_CHAT_SUGGESTED_USERNAMES,
|
|
|
|
data: { value: formDataValues.suggestedUsernames },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({
|
|
|
|
fieldName: 'suggestedUsernames',
|
|
|
|
value: formDataValues.suggestedUsernames,
|
|
|
|
});
|
2022-04-23 23:52:04 +03:00
|
|
|
setSuggestedUsernameSaveState(createInputStatus(STATUS_SUCCESS));
|
2022-01-12 21:17:14 +03:00
|
|
|
setTimeout(resetSuggestedUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
|
|
|
setForbiddenUsernameSaveState(createInputStatus(STATUS_ERROR, message));
|
|
|
|
setTimeout(resetSuggestedUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDeleteSuggestedUsernameIndex(index: number) {
|
|
|
|
formDataValues.suggestedUsernames.splice(index, 1);
|
|
|
|
saveSuggestedUsernames();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCreateSuggestedUsername(tag: string) {
|
|
|
|
formDataValues.suggestedUsernames.push(tag);
|
|
|
|
handleFieldChange({
|
|
|
|
fieldName: 'suggestedUsernames',
|
|
|
|
value: formDataValues.suggestedUsernames,
|
|
|
|
});
|
|
|
|
saveSuggestedUsernames();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSuggestedUsernamesLimitWarning(length: number): StatusState | null {
|
|
|
|
if (length === 0)
|
|
|
|
return createInputStatus('success', TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES.no_entries);
|
|
|
|
if (length > 0 && length < 10)
|
|
|
|
return createInputStatus('warning', TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES.min_not_reached);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-22 06:19:00 +03:00
|
|
|
useEffect(() => {
|
|
|
|
setFormDataValues({
|
|
|
|
chatDisabled,
|
2022-03-06 09:36:38 +03:00
|
|
|
chatJoinMessagesEnabled,
|
2021-07-20 08:02:02 +03:00
|
|
|
forbiddenUsernames,
|
2022-01-12 21:17:14 +03:00
|
|
|
suggestedUsernames,
|
2021-07-20 08:02:02 +03:00
|
|
|
welcomeMessage,
|
2022-03-07 11:06:07 +03:00
|
|
|
chatEstablishedUserMode,
|
2021-06-22 06:19:00 +03:00
|
|
|
});
|
|
|
|
}, [serverConfig]);
|
|
|
|
|
|
|
|
if (!formDataValues) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="config-server-details-form">
|
|
|
|
<Title>Chat Settings</Title>
|
|
|
|
<div className="form-module config-server-details-container">
|
|
|
|
<ToggleSwitch
|
|
|
|
fieldName="chatDisabled"
|
|
|
|
{...FIELD_PROPS_DISABLE_CHAT}
|
2021-09-11 01:08:15 +03:00
|
|
|
checked={!formDataValues.chatDisabled}
|
|
|
|
reversed
|
2021-06-22 06:19:00 +03:00
|
|
|
onChange={handleChatDisableChange}
|
|
|
|
/>
|
2022-03-06 09:36:38 +03:00
|
|
|
<ToggleSwitch
|
|
|
|
fieldName="chatJoinMessagesEnabled"
|
|
|
|
{...FIELD_PROPS_CHAT_JOIN_MESSAGES_ENABLED}
|
|
|
|
checked={formDataValues.chatJoinMessagesEnabled}
|
|
|
|
onChange={handleChatJoinMessagesEnabledChange}
|
|
|
|
/>
|
2022-03-07 11:06:07 +03:00
|
|
|
<ToggleSwitch
|
|
|
|
fieldName="establishedUserMode"
|
|
|
|
{...CHAT_ESTABLISHED_USER_MODE}
|
|
|
|
checked={formDataValues.chatEstablishedUserMode}
|
|
|
|
onChange={handleEstablishedUserModeChange}
|
|
|
|
/>
|
2021-07-20 08:02:02 +03:00
|
|
|
<TextFieldWithSubmit
|
|
|
|
fieldName="welcomeMessage"
|
|
|
|
{...TEXTFIELD_PROPS_SERVER_WELCOME_MESSAGE}
|
|
|
|
type={TEXTFIELD_TYPE_TEXTAREA}
|
|
|
|
value={formDataValues.welcomeMessage}
|
2021-07-21 21:26:19 +03:00
|
|
|
initialValue={welcomeMessage}
|
2021-07-20 08:02:02 +03:00
|
|
|
onChange={handleFieldChange}
|
2021-06-22 06:19:00 +03:00
|
|
|
/>
|
2021-07-26 08:12:50 +03:00
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<EditValueArray
|
|
|
|
title={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.label}
|
|
|
|
placeholder={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.placeholder}
|
|
|
|
description={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.tip}
|
|
|
|
values={formDataValues.forbiddenUsernames}
|
2022-01-12 21:17:14 +03:00
|
|
|
handleDeleteIndex={handleDeleteForbiddenUsernameIndex}
|
|
|
|
handleCreateString={handleCreateForbiddenUsername}
|
2022-04-23 23:52:04 +03:00
|
|
|
submitStatus={forbiddenUsernameSaveState}
|
2021-07-26 08:12:50 +03:00
|
|
|
/>
|
2022-01-12 21:17:14 +03:00
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<EditValueArray
|
|
|
|
title={TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES.label}
|
|
|
|
placeholder={TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES.placeholder}
|
|
|
|
description={TEXTFIELD_PROPS_CHAT_SUGGESTED_USERNAMES.tip}
|
|
|
|
values={formDataValues.suggestedUsernames}
|
|
|
|
handleDeleteIndex={handleDeleteSuggestedUsernameIndex}
|
|
|
|
handleCreateString={handleCreateSuggestedUsername}
|
2022-04-23 23:52:04 +03:00
|
|
|
submitStatus={suggestedUsernameSaveState}
|
2022-01-12 21:17:14 +03:00
|
|
|
continuousStatusMessage={getSuggestedUsernamesLimitWarning(
|
|
|
|
formDataValues.suggestedUsernames.length,
|
|
|
|
)}
|
|
|
|
/>
|
2021-06-22 06:19:00 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-01-16 10:11:44 +03:00
|
|
|
|
|
|
|
ConfigChat.getLayout = function getLayout(page: ReactElement) {
|
|
|
|
return <AdminLayout page={page} />;
|
|
|
|
};
|