Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3352147
D7828.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D7828.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D7828: [web] Added message editing
Attached
Detach File
Event Timeline
Log In to Comment