From ca1780ad15f27f31ed959c867a7010ed76d56c16 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Date: Wed, 11 Sep 2024 16:25:55 +0530 Subject: [PATCH] Fix for Back to community (#864) --- .gitignore | 3 +- src/App.tsx | 75 ++++++++++--------- .../explorer/context/HistoryLength/index.tsx | 25 +++++++ .../explorer/pages/ProposalDetails/index.tsx | 14 +++- 4 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 src/modules/explorer/context/HistoryLength/index.tsx diff --git a/.gitignore b/.gitignore index 7c913880..c2727221 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ yarn-error.log* .idea .env.example -.cosine \ No newline at end of file +.cosine +bun.lockb \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 90d1c948..24e4004f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -26,6 +26,7 @@ import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider" import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs" import { CommunityCreator } from "modules/lite/creator" import { hexStringToBytes } from "services/utils/utils" +import { HistoryLengthProvider } from "modules/explorer/context/HistoryLength" const queryClient = new QueryClient({ defaultOptions: { @@ -101,47 +102,49 @@ const App: React.FC = () => { - - - - - - - - - {/* + + + + + + + + + {/* */} - - - - - - - - - - + + + + + + + + + + - {window.location.href.indexOf(HUMANITEZ_DAO) !== -1 ? ( - <> - {/* Special case for this DAO which was created before FA1.2 fix was created for the smart contract */} - - - ) : null} - - - - - - - + {window.location.href.indexOf(HUMANITEZ_DAO) !== -1 ? ( + <> + {/* Special case for this DAO which was created before FA1.2 fix was created for the smart contract */} + + + ) : null} + + + + + + + + diff --git a/src/modules/explorer/context/HistoryLength/index.tsx b/src/modules/explorer/context/HistoryLength/index.tsx new file mode 100644 index 00000000..68ba8083 --- /dev/null +++ b/src/modules/explorer/context/HistoryLength/index.tsx @@ -0,0 +1,25 @@ +import React, { createContext, useContext, useState, useEffect } from "react" +import { useHistory, useLocation } from "react-router-dom" + +const HistoryLengthContext = createContext(0) + +export const useHistoryLength = () => useContext(HistoryLengthContext) + +export const HistoryLengthProvider: React.FC = ({ children }) => { + const [historyLength, setHistoryLength] = useState(1) + const history = useHistory() + const location = useLocation() + + useEffect(() => { + console.log({ history }) + if (history && (history.action === "PUSH" || history.action === "REPLACE")) { + setHistoryLength(prevLength => prevLength + 1) + } + }, [history, location]) + + useEffect(() => { + console.log({ historyLength }) + }, [historyLength]) + + return {children} +} diff --git a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx index 26cc9644..9a21637c 100644 --- a/src/modules/lite/explorer/pages/ProposalDetails/index.tsx +++ b/src/modules/lite/explorer/pages/ProposalDetails/index.tsx @@ -21,6 +21,7 @@ import { useTokenVoteWeight } from "services/contracts/token/hooks/useTokenVoteW import BigNumber from "bignumber.js" import { ArrowBackIosOutlined } from "@material-ui/icons" import { useIsMember } from "../../hooks/useIsMember" +import { useHistoryLength } from "modules/explorer/context/HistoryLength" const PageContainer = styled("div")({ marginBottom: 50, @@ -54,8 +55,9 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { }>() const theme = useTheme() + const historyLength = useHistoryLength() const isMobileSmall = useMediaQuery(theme.breakpoints.down("sm")) - const { state } = useLocation<{ poll: Poll; choices: Choice[]; daoId: string }>() + const { state, pathname } = useLocation<{ poll: Poll; choices: Choice[]; daoId: string }>() const navigate = useHistory() const { data: dao } = useDAO(state?.daoId) const { account, wallet } = useTezos() @@ -73,6 +75,14 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { const isMember = useIsMember(network, community?.tokenAddress || "", account) const votingPower = poll?.isXTZ ? voteWeight?.votingXTZWeight : voteWeight?.votingWeight + const navigateToDao = () => { + if (historyLength > 1) { + navigate.goBack() + } else { + const daoUrl = pathname?.replace(`proposal/${proposalId}`, "") + navigate.push(daoUrl) + } + } useEffect(() => { // refetch() @@ -142,7 +152,7 @@ export const ProposalDetails: React.FC<{ id: string }> = ({ id }) => { navigate.goBack()} + onClick={() => navigateToDao()} alignItems="center" >