2021-03-09 19:16:56 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, {
|
|
|
|
FormEventHandler,
|
|
|
|
FunctionComponent,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
|
|
|
|
|
|
|
import { LocalizerType } from '../../../types/Util';
|
2021-06-02 00:24:28 +00:00
|
|
|
import { Modal } from '../../Modal';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { AvatarEditor } from '../../AvatarEditor';
|
|
|
|
import { AvatarPreview } from '../../AvatarPreview';
|
2021-03-09 19:16:56 +00:00
|
|
|
import { Button, ButtonVariant } from '../../Button';
|
|
|
|
import { Spinner } from '../../Spinner';
|
2021-06-02 00:24:28 +00:00
|
|
|
import { GroupDescriptionInput } from '../../GroupDescriptionInput';
|
2021-03-09 19:16:56 +00:00
|
|
|
import { GroupTitleInput } from '../../GroupTitleInput';
|
2021-03-11 21:29:31 +00:00
|
|
|
import { RequestState } from './util';
|
2021-08-06 00:17:05 +00:00
|
|
|
import {
|
|
|
|
AvatarDataType,
|
|
|
|
DeleteAvatarFromDiskActionType,
|
|
|
|
ReplaceAvatarActionType,
|
|
|
|
SaveAvatarToDiskActionType,
|
|
|
|
} from '../../../types/Avatar';
|
|
|
|
import { AvatarColorType } from '../../../types/Colors';
|
2021-03-09 19:16:56 +00:00
|
|
|
|
|
|
|
type PropsType = {
|
2021-08-06 00:17:05 +00:00
|
|
|
avatarColor?: AvatarColorType;
|
2021-03-09 19:16:56 +00:00
|
|
|
avatarPath?: string;
|
2021-08-06 00:17:05 +00:00
|
|
|
conversationId: string;
|
2021-06-02 00:24:28 +00:00
|
|
|
groupDescription?: string;
|
2021-03-09 19:16:56 +00:00
|
|
|
i18n: LocalizerType;
|
2021-06-04 14:55:34 +00:00
|
|
|
initiallyFocusDescription: boolean;
|
2021-03-09 19:16:56 +00:00
|
|
|
makeRequest: (
|
|
|
|
_: Readonly<{
|
|
|
|
avatar?: undefined | ArrayBuffer;
|
2021-06-02 00:24:28 +00:00
|
|
|
description?: string;
|
2021-03-09 19:16:56 +00:00
|
|
|
title?: undefined | string;
|
|
|
|
}>
|
|
|
|
) => void;
|
|
|
|
onClose: () => void;
|
|
|
|
requestState: RequestState;
|
|
|
|
title: string;
|
2021-08-06 00:17:05 +00:00
|
|
|
deleteAvatarFromDisk: DeleteAvatarFromDiskActionType;
|
|
|
|
replaceAvatar: ReplaceAvatarActionType;
|
|
|
|
saveAvatarToDisk: SaveAvatarToDiskActionType;
|
|
|
|
userAvatarData: Array<AvatarDataType>;
|
2021-03-09 19:16:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const EditConversationAttributesModal: FunctionComponent<PropsType> = ({
|
2021-08-06 00:17:05 +00:00
|
|
|
avatarColor,
|
2021-03-09 19:16:56 +00:00
|
|
|
avatarPath: externalAvatarPath,
|
2021-08-06 00:17:05 +00:00
|
|
|
conversationId,
|
2021-06-02 00:24:28 +00:00
|
|
|
groupDescription: externalGroupDescription = '',
|
2021-03-09 19:16:56 +00:00
|
|
|
i18n,
|
2021-06-04 14:55:34 +00:00
|
|
|
initiallyFocusDescription,
|
2021-03-09 19:16:56 +00:00
|
|
|
makeRequest,
|
|
|
|
onClose,
|
|
|
|
requestState,
|
|
|
|
title: externalTitle,
|
2021-08-06 00:17:05 +00:00
|
|
|
deleteAvatarFromDisk,
|
|
|
|
replaceAvatar,
|
|
|
|
saveAvatarToDisk,
|
|
|
|
userAvatarData,
|
2021-03-09 19:16:56 +00:00
|
|
|
}) => {
|
2021-06-04 14:55:34 +00:00
|
|
|
const focusDescriptionRef = useRef<undefined | boolean>(
|
|
|
|
initiallyFocusDescription
|
|
|
|
);
|
|
|
|
const focusDescription = focusDescriptionRef.current;
|
|
|
|
|
2021-03-09 19:16:56 +00:00
|
|
|
const startingTitleRef = useRef<string>(externalTitle);
|
|
|
|
const startingAvatarPathRef = useRef<undefined | string>(externalAvatarPath);
|
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
const [editingAvatar, setEditingAvatar] = useState(false);
|
|
|
|
const [avatar, setAvatar] = useState<undefined | ArrayBuffer>();
|
2021-03-11 01:54:13 +00:00
|
|
|
const [rawTitle, setRawTitle] = useState(externalTitle);
|
2021-06-02 00:24:28 +00:00
|
|
|
const [rawGroupDescription, setRawGroupDescription] = useState(
|
|
|
|
externalGroupDescription
|
|
|
|
);
|
2021-03-09 19:16:56 +00:00
|
|
|
const [hasAvatarChanged, setHasAvatarChanged] = useState(false);
|
|
|
|
|
2021-03-11 01:54:13 +00:00
|
|
|
const trimmedTitle = rawTitle.trim();
|
2021-06-02 00:24:28 +00:00
|
|
|
const trimmedDescription = rawGroupDescription.trim();
|
2021-03-11 01:54:13 +00:00
|
|
|
|
2021-06-04 14:55:34 +00:00
|
|
|
const focusRef = (el: null | HTMLElement) => {
|
|
|
|
if (el) {
|
|
|
|
el.focus();
|
|
|
|
focusDescriptionRef.current = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-09 19:16:56 +00:00
|
|
|
const hasChangedExternally =
|
|
|
|
startingAvatarPathRef.current !== externalAvatarPath ||
|
|
|
|
startingTitleRef.current !== externalTitle;
|
2021-03-11 01:54:13 +00:00
|
|
|
const hasTitleChanged = trimmedTitle !== externalTitle.trim();
|
2021-06-02 00:24:28 +00:00
|
|
|
const hasGroupDescriptionChanged =
|
|
|
|
externalGroupDescription.trim() !== trimmedDescription;
|
2021-03-09 19:16:56 +00:00
|
|
|
|
|
|
|
const isRequestActive = requestState === RequestState.Active;
|
|
|
|
|
|
|
|
const canSubmit =
|
|
|
|
!isRequestActive &&
|
2021-06-02 00:24:28 +00:00
|
|
|
(hasChangedExternally ||
|
|
|
|
hasTitleChanged ||
|
|
|
|
hasAvatarChanged ||
|
|
|
|
hasGroupDescriptionChanged) &&
|
2021-03-11 01:54:13 +00:00
|
|
|
trimmedTitle.length > 0;
|
2021-03-09 19:16:56 +00:00
|
|
|
|
|
|
|
const onSubmit: FormEventHandler<HTMLFormElement> = event => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const request: {
|
|
|
|
avatar?: undefined | ArrayBuffer;
|
2021-06-02 00:24:28 +00:00
|
|
|
description?: string;
|
2021-03-09 19:16:56 +00:00
|
|
|
title?: string;
|
|
|
|
} = {};
|
|
|
|
if (hasAvatarChanged) {
|
|
|
|
request.avatar = avatar;
|
|
|
|
}
|
|
|
|
if (hasTitleChanged) {
|
2021-03-11 01:54:13 +00:00
|
|
|
request.title = trimmedTitle;
|
2021-03-09 19:16:56 +00:00
|
|
|
}
|
2021-06-02 00:24:28 +00:00
|
|
|
if (hasGroupDescriptionChanged) {
|
|
|
|
request.description = trimmedDescription;
|
|
|
|
}
|
2021-03-09 19:16:56 +00:00
|
|
|
makeRequest(request);
|
|
|
|
};
|
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
const avatarPathForPreview = hasAvatarChanged
|
|
|
|
? undefined
|
|
|
|
: externalAvatarPath;
|
|
|
|
|
|
|
|
let content: JSX.Element;
|
|
|
|
if (editingAvatar) {
|
|
|
|
content = (
|
|
|
|
<AvatarEditor
|
|
|
|
avatarColor={avatarColor}
|
|
|
|
avatarPath={avatarPathForPreview}
|
|
|
|
avatarValue={avatar}
|
|
|
|
conversationId={conversationId}
|
|
|
|
deleteAvatarFromDisk={deleteAvatarFromDisk}
|
|
|
|
i18n={i18n}
|
|
|
|
isGroup
|
|
|
|
onCancel={() => {
|
|
|
|
setHasAvatarChanged(false);
|
|
|
|
setEditingAvatar(false);
|
|
|
|
}}
|
|
|
|
onSave={newAvatar => {
|
|
|
|
setAvatar(newAvatar);
|
|
|
|
setHasAvatarChanged(true);
|
|
|
|
setEditingAvatar(false);
|
|
|
|
}}
|
|
|
|
userAvatarData={userAvatarData}
|
|
|
|
replaceAvatar={replaceAvatar}
|
|
|
|
saveAvatarToDisk={saveAvatarToDisk}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
content = (
|
2021-03-09 19:16:56 +00:00
|
|
|
<form
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
className="module-EditConversationAttributesModal"
|
|
|
|
>
|
2021-08-06 00:17:05 +00:00
|
|
|
<AvatarPreview
|
|
|
|
avatarColor={avatarColor}
|
|
|
|
avatarPath={avatarPathForPreview}
|
|
|
|
avatarValue={avatar}
|
2021-03-09 19:16:56 +00:00
|
|
|
i18n={i18n}
|
2021-08-06 00:17:05 +00:00
|
|
|
isEditable
|
|
|
|
isGroup
|
|
|
|
onClick={() => {
|
|
|
|
setEditingAvatar(true);
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
height: 96,
|
|
|
|
width: 96,
|
2021-03-09 19:16:56 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<GroupTitleInput
|
|
|
|
disabled={isRequestActive}
|
|
|
|
i18n={i18n}
|
2021-03-11 01:54:13 +00:00
|
|
|
onChangeValue={setRawTitle}
|
2021-06-04 14:55:34 +00:00
|
|
|
ref={focusDescription === false ? focusRef : undefined}
|
2021-03-11 01:54:13 +00:00
|
|
|
value={rawTitle}
|
2021-03-09 19:16:56 +00:00
|
|
|
/>
|
|
|
|
|
2021-06-02 00:24:28 +00:00
|
|
|
<GroupDescriptionInput
|
|
|
|
disabled={isRequestActive}
|
|
|
|
i18n={i18n}
|
|
|
|
onChangeValue={setRawGroupDescription}
|
2021-06-04 14:55:34 +00:00
|
|
|
ref={focusDescription === true ? focusRef : undefined}
|
2021-06-02 00:24:28 +00:00
|
|
|
value={rawGroupDescription}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="module-EditConversationAttributesModal__description-warning">
|
|
|
|
{i18n('EditConversationAttributesModal__description-warning')}
|
|
|
|
</div>
|
|
|
|
|
2021-03-09 19:16:56 +00:00
|
|
|
{requestState === RequestState.InactiveWithError && (
|
|
|
|
<div className="module-EditConversationAttributesModal__error-message">
|
|
|
|
{i18n('updateGroupAttributes__error-message')}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2021-06-02 00:24:28 +00:00
|
|
|
<Modal.ButtonFooter>
|
2021-03-09 19:16:56 +00:00
|
|
|
<Button
|
|
|
|
disabled={isRequestActive}
|
|
|
|
onClick={onClose}
|
|
|
|
variant={ButtonVariant.Secondary}
|
|
|
|
>
|
|
|
|
{i18n('cancel')}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
variant={ButtonVariant.Primary}
|
|
|
|
disabled={!canSubmit}
|
|
|
|
>
|
|
|
|
{isRequestActive ? (
|
|
|
|
<Spinner size="20px" svgSize="small" direction="on-avatar" />
|
|
|
|
) : (
|
|
|
|
i18n('save')
|
|
|
|
)}
|
|
|
|
</Button>
|
2021-06-02 00:24:28 +00:00
|
|
|
</Modal.ButtonFooter>
|
2021-03-09 19:16:56 +00:00
|
|
|
</form>
|
2021-08-06 00:17:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
hasStickyButtons
|
|
|
|
hasXButton
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={onClose}
|
|
|
|
title={i18n('updateGroupAttributes__title')}
|
|
|
|
>
|
|
|
|
{content}
|
2021-06-02 00:24:28 +00:00
|
|
|
</Modal>
|
2021-03-09 19:16:56 +00:00
|
|
|
);
|
|
|
|
};
|