feat(reply modal): greentext by selecting text

This commit is contained in:
Tom (plebeius.eth)
2024-06-26 18:20:25 +02:00
parent c8f62a754d
commit 972964deda
5 changed files with 38 additions and 10 deletions
+7 -2
View File
@@ -1,21 +1,26 @@
import { useState, useCallback } from 'react';
import useIsMobile from './use-is-mobile';
import useSelectedTextStore from '../stores/use-selected-text-store';
const useReplyModal = () => {
const [showReplyModal, setShowReplyModal] = useState(false);
const [activeCid, setActiveCid] = useState<string | null>(null);
const { resetSelectedText, setSelectedText } = useSelectedTextStore();
// on mobile, the position is absolute instead of fixed, so we need to calculate the top position
const isMobile = useIsMobile();
const [scrollY, setScrollY] = useState<number>(0);
const closeModal = useCallback(() => {
resetSelectedText();
setActiveCid(null);
setShowReplyModal(false);
}, []);
}, [resetSelectedText]);
const openReplyModal = useCallback(
(cid: string) => {
let text = document.getSelection()?.toString();
text && setSelectedText(`>${text}\n`);
if (isMobile) {
const currentScrollY = window.scrollY;
setScrollY(currentScrollY);
@@ -27,7 +32,7 @@ const useReplyModal = () => {
setShowReplyModal(true);
}
},
[activeCid, isMobile],
[activeCid, isMobile, setSelectedText],
);
return { activeCid, closeModal, openReplyModal, scrollY, showReplyModal };