2024-06-26 21:27:28 +02:00
|
|
|
import { useState } from 'react';
|
2024-05-29 16:17:37 +02:00
|
|
|
import useIsMobile from './use-is-mobile';
|
2024-06-26 18:20:25 +02:00
|
|
|
import useSelectedTextStore from '../stores/use-selected-text-store';
|
2024-04-29 12:35:37 +02:00
|
|
|
|
|
|
|
|
const useReplyModal = () => {
|
|
|
|
|
const [showReplyModal, setShowReplyModal] = useState(false);
|
|
|
|
|
const [activeCid, setActiveCid] = useState<string | null>(null);
|
2024-06-26 18:20:25 +02:00
|
|
|
const { resetSelectedText, setSelectedText } = useSelectedTextStore();
|
2024-04-29 12:35:37 +02:00
|
|
|
|
2024-06-26 21:27:28 +02:00
|
|
|
// on mobile, the css position is absolute instead of fixed, so we need to calculate the top position
|
2024-05-29 16:17:37 +02:00
|
|
|
const isMobile = useIsMobile();
|
2024-04-30 11:08:01 +02:00
|
|
|
const [scrollY, setScrollY] = useState<number>(0);
|
|
|
|
|
|
2024-06-26 21:27:28 +02:00
|
|
|
const closeModal = () => {
|
2024-06-26 18:20:25 +02:00
|
|
|
resetSelectedText();
|
2024-04-29 12:35:37 +02:00
|
|
|
setActiveCid(null);
|
|
|
|
|
setShowReplyModal(false);
|
2024-06-26 21:27:28 +02:00
|
|
|
};
|
2024-04-29 12:35:37 +02:00
|
|
|
|
2024-06-26 21:27:28 +02:00
|
|
|
const getSelectedText = () => {
|
|
|
|
|
let text = document.getSelection()?.toString();
|
|
|
|
|
text && setSelectedText(`>${text}\n`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openReplyModal = (cid: string) => {
|
|
|
|
|
getSelectedText();
|
|
|
|
|
|
|
|
|
|
if (isMobile) {
|
|
|
|
|
const currentScrollY = window.scrollY;
|
|
|
|
|
setScrollY(currentScrollY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (activeCid && activeCid !== cid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setActiveCid(cid);
|
|
|
|
|
setShowReplyModal(true);
|
|
|
|
|
};
|
2024-04-29 13:34:46 +02:00
|
|
|
|
2024-04-30 11:08:01 +02:00
|
|
|
return { activeCid, closeModal, openReplyModal, scrollY, showReplyModal };
|
2024-04-29 12:35:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useReplyModal;
|