Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add checks to draft auto save #5639

Merged
merged 8 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions app/containers/MessageComposer/hooks/useAutoSaveDraft.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { useRoute } from '@react-navigation/native';
import { useCallback, useEffect, useRef } from 'react';

import { saveDraftMessage } from '../../../lib/methods/draftMessage';
import { useRoomContext } from '../../../views/RoomView/context';
import { useFocused } from '../context';
import { saveDraftMessage } from '../../../lib/methods/draftMessage';

export const useAutoSaveDraft = (text = '') => {
const route = useRoute();
const { rid, tmid, action, selectedMessages } = useRoomContext();
const focused = useFocused();
const oldText = useRef('');
const intervalRef = useRef();

const mounted = useRef(true);

const saveMessageDraft = useCallback(() => {
if (route.name === 'ShareView') return;
if (action === 'edit') return;

const draftMessage = selectedMessages?.length ? JSON.stringify({ quotes: selectedMessages, msg: text }) : text;
if (oldText.current !== draftMessage) {
if (oldText.current !== draftMessage || (oldText.current === '' && draftMessage === '')) {
oldText.current = draftMessage;
saveDraftMessage({ rid, tmid, draftMessage });
}
}, [action, rid, tmid, text, selectedMessages?.length]);
}, [action, rid, tmid, text, selectedMessages?.length, route.name]);

useEffect(() => {
if (focused) {
Expand All @@ -29,7 +33,23 @@ export const useAutoSaveDraft = (text = '') => {

return () => {
clearInterval(intervalRef.current);
saveMessageDraft();
};
}, [focused, saveMessageDraft]);

// hack to call saveMessageDraft when component is unmounted
useEffect(() => {
() => {};
return () => {
mounted.current = false;
};
}, []);

useEffect(
() => () => {
if (!mounted.current) {
saveMessageDraft();
}
},
[saveMessageDraft]
);
};
3 changes: 2 additions & 1 deletion app/lib/methods/helpers/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type TKeyEmitterEvent = keyof TEmitterEvents;

export const emitter = mitt<TEmitterEvents>();

emitter.on('*', (type, e) => console.log(type, e));
// uncomment the line below to log all events
// emitter.on('*', (type, e) => console.log(type, e));
4 changes: 0 additions & 4 deletions app/lib/methods/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Encryption } from '../encryption';
import { E2EType, IMessage, IUser, TMessageModel } from '../../definitions';
import sdk from '../services/sdk';
import { E2E_MESSAGE_TYPE, E2E_STATUS, messagesStatus } from '../constants';
import { saveDraftMessage } from './draftMessage';

const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => {
const db = database.active;
Expand Down Expand Up @@ -232,9 +231,6 @@ export async function sendMessage(
}

await sendMessageCall(message);
// clear draft message when message is sent and app is in background or closed
// do not affect the user experience when the app is in the foreground because the hook useAutoSaveDraft will handle it
saveDraftMessage({ rid, tmid, draftMessage: '' });
} catch (e) {
log(e);
}
Expand Down
4 changes: 3 additions & 1 deletion app/views/RoomView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,9 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
const { rid } = this.state.room;
const { user } = this.props;
sendMessage(rid, message, this.tmid, user, tshow).then(() => {
this.setLastOpen(null);
if (this.mounted) {
this.setLastOpen(null);
}
Review.pushPositiveEvent();
});
this.resetAction();
Expand Down