perf: abstract reply modal logic into hook for post page and board page

This commit is contained in:
plebeius.eth
2024-04-29 12:35:37 +02:00
parent 84f55557c1
commit ff8882ce9c
3 changed files with 46 additions and 26 deletions
+29
View File
@@ -0,0 +1,29 @@
import { useState, useCallback } from 'react';
const useReplyModal = () => {
const [showReplyModal, setShowReplyModal] = useState(false);
const [activeCid, setActiveCid] = useState<string | null>(null);
const openReplyModal = useCallback(
(cid: string) => {
if (activeCid && activeCid !== cid) {
closeModal(); // close modal if a different CID is clicked
setActiveCid(cid);
setShowReplyModal(true);
} else if (!activeCid) {
setActiveCid(cid);
setShowReplyModal(true);
}
},
[activeCid],
);
const closeModal = useCallback(() => {
setActiveCid(null);
setShowReplyModal(false);
}, []);
return { showReplyModal, activeCid, openReplyModal, closeModal };
};
export default useReplyModal;