moved some repeated code

This commit is contained in:
plebbitor
2023-03-20 17:08:05 +01:00
parent 1885a105e4
commit 90b53d49fd
14 changed files with 299 additions and 586 deletions
+32
View File
@@ -0,0 +1,32 @@
import { useNavigate, useLocation } from "react-router-dom";
import useAppStore from "../useAppStore";
const useClickForm = () => {
const navigate = useNavigate();
const location = useLocation();
const { setShowPostForm, setShowPostFormLink } = useAppStore.getState();
const handleClickForm = () => {
setShowPostForm(true);
setShowPostFormLink(false);
const subplebbitAddress = location.pathname.split("/")[1];
const threadCid = location.pathname.split("/")[3];
if (location.pathname === "/") {
navigate("/post");
} else if (location.pathname.endsWith("/catalog")) {
navigate(`/${subplebbitAddress}/catalog/post`);
} else if (location.pathname.endsWith("/thread")) {
navigate(`/${subplebbitAddress}/thread/post`);
} else if (threadCid) {
navigate(`/${subplebbitAddress}/thread/${threadCid}/post`);
} else {
navigate(`/${subplebbitAddress}/post`);
}
};
return handleClickForm;
};
export default useClickForm;