2024-02-07 18:57:22 +00:00
|
|
|
import { useCallback, useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
import { useRoomContext } from '../../../views/RoomView/context';
|
|
|
|
import { useFocused } from '../context';
|
2024-02-28 11:49:14 +00:00
|
|
|
import { saveDraftMessage } from '../../../lib/methods/draftMessage';
|
2024-02-07 18:57:22 +00:00
|
|
|
|
|
|
|
export const useAutoSaveDraft = (text = '') => {
|
|
|
|
const { rid, tmid, action, selectedMessages } = useRoomContext();
|
|
|
|
const focused = useFocused();
|
|
|
|
const oldText = useRef('');
|
|
|
|
const intervalRef = useRef();
|
|
|
|
|
|
|
|
const saveMessageDraft = useCallback(() => {
|
|
|
|
if (action === 'edit') return;
|
|
|
|
|
|
|
|
const draftMessage = selectedMessages?.length ? JSON.stringify({ quotes: selectedMessages, msg: text }) : text;
|
|
|
|
if (oldText.current !== draftMessage) {
|
|
|
|
oldText.current = draftMessage;
|
|
|
|
saveDraftMessage({ rid, tmid, draftMessage });
|
|
|
|
}
|
|
|
|
}, [action, rid, tmid, text, selectedMessages?.length]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (focused) {
|
|
|
|
intervalRef.current = setInterval(saveMessageDraft, 3000) as any;
|
|
|
|
} else {
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
saveMessageDraft();
|
|
|
|
};
|
|
|
|
}, [focused, saveMessageDraft]);
|
|
|
|
};
|