Page MenuHomePhabricator

D7828.diff
No OneTemporary

D7828.diff

diff --git a/web/chat/edit-message-provider.js b/web/chat/edit-message-provider.js
--- a/web/chat/edit-message-provider.js
+++ b/web/chat/edit-message-provider.js
@@ -19,6 +19,7 @@
+clearEditModal: () => void,
+editState: ?EditState,
+setDraft: string => void,
+ +setError: boolean => void,
};
const EditModalContext: React.Context<EditModalContextType> =
@@ -27,6 +28,7 @@
clearEditModal: () => {},
editState: null,
setDraft: () => {},
+ setError: () => {},
});
type Props = {
@@ -63,14 +65,25 @@
[editState, setEditState],
);
+ const setError = React.useCallback(
+ (isError: boolean) => {
+ invariant(editState, 'editState should be set in setError');
+ setEditState({
+ ...editState,
+ isError,
+ });
+ },
+ [editState, setEditState],
+ );
const value = React.useMemo(
() => ({
renderEditModal,
clearEditModal: clearEditModal,
editState,
setDraft,
+ setError,
}),
- [renderEditModal, clearEditModal, editState, setDraft],
+ [renderEditModal, clearEditModal, editState, setDraft, setError],
);
const modalOverlay = React.useMemo(() => {
diff --git a/web/chat/edit-text-message.react.js b/web/chat/edit-text-message.react.js
--- a/web/chat/edit-text-message.react.js
+++ b/web/chat/edit-text-message.react.js
@@ -5,6 +5,8 @@
import { XCircle as XCircleIcon } from 'react-feather';
import type { ChatMessageInfoItem } from 'lib/selectors/chat-selectors.js';
+import { useEditMessage } from 'lib/shared/edit-messages-utils.js';
+import { trimMessage } from 'lib/shared/message-utils.js';
import { type ThreadInfo } from 'lib/types/thread-types.js';
import cssInputBar from './chat-input-bar.css';
@@ -26,8 +28,10 @@
};
function EditTextMessage(props: Props): React.Node {
- const { background, threadInfo } = props;
- const { editState, clearEditModal, setDraft } = useEditModalContext();
+ const { background, threadInfo, item } = props;
+ const { editState, clearEditModal, setDraft, setError } =
+ useEditModalContext();
+ const editMessage = useEditMessage();
const editedMessageDraft = editState?.editedMessageDraft ?? '';
const threadColor = threadInfo.color;
@@ -37,6 +41,36 @@
}),
[threadColor],
);
+
+ const isMessageEdited = React.useMemo(() => {
+ const { messageInfo } = item;
+ if (!messageInfo || !messageInfo.text || !editState) {
+ return false;
+ }
+ if (!editedMessageDraft) {
+ return false;
+ }
+ const trimmedDraft = trimMessage(editedMessageDraft);
+ return trimmedDraft !== messageInfo.text;
+ }, [editState, editedMessageDraft, item]);
+
+ const checkAndEdit = async () => {
+ const { id: messageInfoID } = item.messageInfo;
+ if (!isMessageEdited) {
+ clearEditModal();
+ return;
+ }
+ if (!messageInfoID || !editState?.editedMessageDraft) {
+ return;
+ }
+ try {
+ await editMessage(messageInfoID, editState.editedMessageDraft);
+ clearEditModal();
+ } catch (e) {
+ setError(true);
+ }
+ };
+
let editFailed;
if (editState?.isError) {
editFailed = (
@@ -59,6 +93,7 @@
focus={!background}
currentText={editedMessageDraft}
setCurrentText={setDraft}
+ send={checkAndEdit}
/>
</div>
<div className={css.bottomRow}>
@@ -68,6 +103,7 @@
className={[css.saveButton, css.smallButton]}
variant="filled"
buttonColor={saveButtonColor}
+ onClick={checkAndEdit}
>
Save (enter)
</Button>

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 4:47 AM (20 h, 55 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2573993
Default Alt Text
D7828.diff (3 KB)

Event Timeline