Skip to content

Commit 0a50bb5

Browse files
author
Adam Tomaszczyk
committed
Allow Opening Governance Actions in New Tab from Live Voting Page
1 parent 2c55c4d commit 0a50bb5

File tree

5 files changed

+52
-66
lines changed

5 files changed

+52
-66
lines changed

govtool/frontend/src/App.tsx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,44 @@ export default () => {
6363
}, []);
6464

6565
const checkTheWalletIsActive = useCallback(() => {
66-
const hrefCondition =
67-
window.location.pathname === PATHS.home ||
68-
window.location.pathname === PATHS.governanceActions ||
69-
window.location.pathname === PATHS.governanceActionsAction;
66+
const isWalletAvailable = () =>
67+
window.cardano && walletName && Object.keys(window.cardano).includes(walletName);
7068

71-
const walletName = getItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
72-
if (window.cardano) {
73-
const walletExtensions = Object.keys(window.cardano);
74-
if (walletName && walletExtensions.includes(walletName)) {
75-
enable(walletName);
76-
return;
69+
const cleanUpWalletData = () => {
70+
removeItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
71+
removeItemFromLocalStorage(`${WALLET_LS_KEY}_stake_key`);
72+
};
73+
74+
const waitForWalletExtension = async () => {
75+
const timeout = 5000;
76+
const interval = 100;
77+
const startTime = Date.now();
78+
79+
while (Date.now() - startTime < timeout) {
80+
if (isWalletAvailable()) {
81+
enable(walletName);
82+
return;
83+
}
84+
await new Promise(resolve => setTimeout(resolve, interval));
7785
}
78-
}
79-
if (
80-
(!window.cardano && walletName) ||
81-
(walletName && !Object.keys(window.cardano).includes(walletName))
82-
) {
83-
if (!hrefCondition) {
86+
87+
if (!isOnAllowedPage) {
8488
navigate(PATHS.home);
8589
}
86-
removeItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
87-
removeItemFromLocalStorage(`${WALLET_LS_KEY}_stake_key`);
90+
cleanUpWalletData();
91+
};
92+
93+
const isOnAllowedPage = [PATHS.home, PATHS.governanceActions, PATHS.governanceActionsAction]
94+
.includes(window.location.pathname);
95+
96+
const walletName = getItemFromLocalStorage(`${WALLET_LS_KEY}_name`);
97+
98+
if (!walletName) return;
99+
100+
if (isWalletAvailable()) {
101+
enable(walletName);
102+
} else {
103+
waitForWalletExtension();
88104
}
89105
}, []);
90106

govtool/frontend/src/components/molecules/GovernanceActionCard.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FC } from "react";
22
import { Box, Skeleton } from "@mui/material";
3+
import { Link, useLocation } from "react-router-dom";
34

45
import { Button } from "@atoms";
56
import {
@@ -39,7 +40,6 @@ export const GovernanceActionCard: FC<ActionTypeProps> = ({
3940
inProgress = false,
4041
expiryDate,
4142
expiryEpochNo,
42-
onClick,
4343
createdDate,
4444
createdEpochNo,
4545
txHash,
@@ -58,6 +58,8 @@ export const GovernanceActionCard: FC<ActionTypeProps> = ({
5858
bech32Prefix: "gov_action",
5959
});
6060

61+
const location = useLocation();
62+
6163
return (
6264
<Box
6365
sx={{
@@ -150,7 +152,8 @@ export const GovernanceActionCard: FC<ActionTypeProps> = ({
150152
<Skeleton width="100%" height="40px" sx={{ borderRadius: "20px" }} />
151153
) : (
152154
<Button
153-
onClick={onClick}
155+
component={Link}
156+
to={`${location.pathname}/${govActionId}`}
154157
variant={inProgress ? "outlined" : "contained"}
155158
size="large"
156159
sx={{

govtool/frontend/src/components/organisms/GovernanceActionsToVote.tsx

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { useNavigate, generatePath } from "react-router-dom";
21
import { Box } from "@mui/material";
32

43
import { Typography } from "@atoms";
5-
import { PATHS } from "@consts";
64
import { useCardano } from "@context";
75
import { useScreenDimension, useTranslation } from "@hooks";
86
import { ProposalData } from "@models";
9-
import { getProposalTypeTitle, getFullGovActionId } from "@utils";
7+
import { getProposalTypeTitle } from "@utils";
108
import { Slider, ValidatedGovernanceActionCard } from "@organisms";
119

1210
type GovernanceActionsToVoteProps = {
@@ -25,7 +23,6 @@ export const GovernanceActionsToVote = ({
2523
sorting,
2624
}: GovernanceActionsToVoteProps) => {
2725
const { pendingTransaction } = useCardano();
28-
const navigate = useNavigate();
2926
const { isMobile, pagePadding } = useScreenDimension();
3027
const { t } = useTranslation();
3128

@@ -64,29 +61,6 @@ export const GovernanceActionsToVote = ({
6461
pendingTransaction.vote?.resourceId ===
6562
`${action.txHash ?? ""}${action.index ?? ""}`
6663
}
67-
onClick={() => {
68-
navigate(
69-
onDashboard
70-
? generatePath(
71-
PATHS.dashboardGovernanceActionsAction,
72-
{
73-
proposalId: getFullGovActionId(
74-
action.txHash,
75-
action.index,
76-
),
77-
},
78-
)
79-
: PATHS.governanceActionsAction.replace(
80-
":proposalId",
81-
getFullGovActionId(action.txHash, action.index),
82-
),
83-
{
84-
state: {
85-
proposal: action,
86-
},
87-
},
88-
);
89-
}}
9064
/>
9165
</div>
9266
))}

govtool/frontend/src/components/organisms/Slider.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useCallback, useEffect, useMemo, useState } from "react";
2-
import { generatePath, useNavigate } from "react-router-dom";
1+
import { useEffect, useMemo, useState } from "react";
2+
import { generatePath, Link } from "react-router-dom";
33
import { Box } from "@mui/material";
44
import { KeenSliderOptions } from "keen-slider";
55
import "keen-slider/keen-slider.min.css";
@@ -39,7 +39,6 @@ export const Slider = ({
3939
const [isSliderInitialized, setIsSliderInitialized] = useState(false);
4040

4141
const { isMobile, screenWidth } = useScreenDimension();
42-
const navigate = useNavigate();
4342
const { pendingTransaction } = useCardano();
4443
const { t } = useTranslation();
4544

@@ -77,19 +76,6 @@ export const Slider = ({
7776
instanceRef.current?.moveToIdx(0);
7877
};
7978

80-
const onClickShowAll = useCallback(() => {
81-
navigate(
82-
generatePath(
83-
onDashboard
84-
? PATHS.dashboardGovernanceActionsCategory
85-
: PATHS.governanceActionsCategory,
86-
{
87-
category: navigateKey,
88-
},
89-
),
90-
);
91-
}, [navigate, onDashboard]);
92-
9379
useEffect(() => {
9480
if (instanceRef.current) {
9581
setIsSliderInitialized(true);
@@ -129,6 +115,15 @@ export const Slider = ({
129115
<Typography variant="title2">{title}</Typography>
130116
{(notSlicedDataLength > 6 || (isMobile && isShowAll)) && (
131117
<Button
118+
component={Link}
119+
to={`${generatePath(
120+
onDashboard
121+
? PATHS.dashboardGovernanceActionsCategory
122+
: PATHS.governanceActionsCategory,
123+
{
124+
category: navigateKey,
125+
},
126+
)}`}
132127
variant="contained"
133128
size="medium"
134129
sx={{
@@ -139,7 +134,6 @@ export const Slider = ({
139134
minWidth: 93,
140135
"&:hover": { backgroundColor: arcticWhite },
141136
}}
142-
onClick={onClickShowAll}
143137
>
144138
{t("slider.showAll")}
145139
</Button>

govtool/frontend/src/components/organisms/ValidatedGovernanceActionCard.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
22

33
import { useValidateMutation } from "@/hooks/mutations";
44
import { MetadataStandard, ProposalData } from "@/models";
5-
import { GovernanceActionCard } from "../molecules";
5+
import { GovernanceActionCard } from "@molecules";
66

77
type ActionTypeProps = Omit<
88
ProposalData,
@@ -14,7 +14,6 @@ type ActionTypeProps = Omit<
1414
| "rationale"
1515
| "motivation"
1616
> & {
17-
onClick?: () => void;
1817
inProgress?: boolean;
1918
};
2019
export const ValidatedGovernanceActionCard = (props: ActionTypeProps) => {

0 commit comments

Comments
 (0)