2024-04-29 12:35:37 +02:00
|
|
|
import { useState, useCallback } from 'react';
|
2024-05-29 16:17:37 +02:00
|
|
|
import useIsMobile from './use-is-mobile';
|
2024-04-29 12:35:37 +02:00
|
|
|
|
|
|
|
|
const useReplyModal = () => {
|
|
|
|
|
const [showReplyModal, setShowReplyModal] = useState(false);
|
|
|
|
|
const [activeCid, setActiveCid] = useState<string | null>(null);
|
|
|
|
|
|
2024-04-30 11:08:01 +02:00
|
|
|
// on mobile, the 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-04-29 12:35:37 +02:00
|
|
|
const closeModal = useCallback(() => {
|
|
|
|
|
setActiveCid(null);
|
|
|
|
|
setShowReplyModal(false);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-04-29 13:34:46 +02:00
|
|
|
const openReplyModal = useCallback(
|
|
|
|
|
(cid: string) => {
|
2024-04-30 11:08:01 +02:00
|
|
|
if (isMobile) {
|
2024-04-30 11:35:47 +02:00
|
|
|
const currentScrollY = window.scrollY;
|
|
|
|
|
setScrollY(currentScrollY);
|
2024-04-30 11:08:01 +02:00
|
|
|
}
|
2024-04-29 13:34:46 +02:00
|
|
|
if (activeCid && activeCid !== cid) {
|
2024-05-08 17:13:31 +02:00
|
|
|
return;
|
2024-04-29 13:34:46 +02:00
|
|
|
} else if (!activeCid) {
|
|
|
|
|
setActiveCid(cid);
|
|
|
|
|
setShowReplyModal(true);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-05-09 16:04:24 +02:00
|
|
|
[activeCid, isMobile],
|
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;
|