Skip to content

Commit

Permalink
Solving the alert race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Jør∂¡ committed Mar 15, 2024
1 parent 2a7a144 commit e96fec6
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 42 deletions.
4 changes: 2 additions & 2 deletions components/alert/alert-container.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { FC } from "react";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";
import { AlertCard, AlertVariant, Icon, IconType } from "@aragon/ods";
import { IAlert } from "@/utils/types";

const AlertContainer: FC = () => {
const { alerts } = useAlertContext();
const { alerts } = useAlerts();

return (
<div className="fixed top-0 right-0 w-72 md:w-96 m-4 z-50">
Expand Down
4 changes: 2 additions & 2 deletions components/input/function-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, InputText } from "@aragon/ods";
import { AbiFunction } from "abitype";
import { Else, If, Then } from "@/components/if";
import { decodeCamelCase } from "@/utils/case";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";
import { InputParameter } from "./input-parameter";
import { InputValue } from "@/utils/input-values";

Expand All @@ -16,7 +16,7 @@ export const FunctionSelector = ({
abi,
actionEntered,
}: IFunctionSelectorProps) => {
const { addAlert } = useAlertContext();
const { addAlert } = useAlerts();
const [selectedAbiItem, setSelectedAbiItem] = useState<
AbiFunction | undefined
>();
Expand Down
27 changes: 14 additions & 13 deletions context/AlertContext.tsx → context/Alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export const AlertProvider: React.FC<{ children: React.ReactNode }> = ({

// Add a new alert to the list
const addAlert = (message: string, alertOptions?: AlertOptions) => {
const newAlertList = ([] as IAlert[]).concat(alerts);

// Clean duplicates
const idx = alerts.findIndex((a) => {
if (a.message !== message) return false;
Expand All @@ -38,14 +36,17 @@ export const AlertProvider: React.FC<{ children: React.ReactNode }> = ({
return true;
});
if (idx >= 0) {
const [prevAlert] = newAlertList.splice(idx, 1);
clearTimeout(prevAlert.dismissTimeout);
const timeout = alertOptions?.timeout ?? DEFAULT_ALERT_TIMEOUT;
prevAlert.dismissTimeout = setTimeout(
() => removeAlert(prevAlert.id),
timeout
);
setAlerts(newAlertList.concat(prevAlert));
// Update the existing one
setAlerts((curAlerts) => {
const [prevAlert] = curAlerts.splice(idx, 1);
clearTimeout(prevAlert.dismissTimeout);
const timeout = alertOptions?.timeout ?? DEFAULT_ALERT_TIMEOUT;
prevAlert.dismissTimeout = setTimeout(
() => removeAlert(prevAlert.id),
timeout
);
return curAlerts.concat(prevAlert);
});
return;
}

Expand All @@ -64,12 +65,12 @@ export const AlertProvider: React.FC<{ children: React.ReactNode }> = ({
() => removeAlert(newAlert.id),
timeout
);
setAlerts(newAlertList.concat(newAlert));
setAlerts((curAlerts) => curAlerts.concat(newAlert));
};

// Function to remove an alert
const removeAlert = (id: number) => {
setAlerts(alerts.filter((alert) => alert.id !== id));
setAlerts((prevAlerts) => prevAlerts.filter((alert) => alert.id !== id));
};

return (
Expand All @@ -79,7 +80,7 @@ export const AlertProvider: React.FC<{ children: React.ReactNode }> = ({
);
};

export const useAlertContext = () => {
export const useAlerts = () => {
const context = useContext(AlertContext);

if (!context) {
Expand Down
2 changes: 1 addition & 1 deletion context/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AlertProvider } from "./AlertContext";
import { AlertProvider } from "./Alerts";
import { ReactNode } from "react";
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { config } from "@/context/Web3Modal";
Expand Down
4 changes: 2 additions & 2 deletions hooks/useAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { AbiFunction } from "abitype";
import { useQuery } from "@tanstack/react-query";
import { isAddress } from "@/utils/evm";
import { PUB_CHAIN, PUB_ETHERSCAN_API_KEY } from "@/constants";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";

export const useAbi = (contractAddress: Address) => {
const { addAlert } = useAlertContext();
const { addAlert } = useAlerts();
const publicClient = usePublicClient({ chainId: PUB_CHAIN.id });

const {
Expand Down
4 changes: 2 additions & 2 deletions hooks/usePermit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
} from "wagmi";
import { hexToSignature, Address } from "viem";
import { ERC20PermitAbi } from "@/artifacts/ERC20Permit.sol";
import { useAlertContext, AlertContextProps } from "@/context/AlertContext";
import { useAlerts, AlertContextProps } from "@/context/Alerts";
import { PUB_CHAIN, PUB_TOKEN_ADDRESS } from "@/constants";

export function usePermit() {
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;

const account_address = useAccount().address!;
const erc20Contract = {
Expand Down
4 changes: 2 additions & 2 deletions plugins/dualGovernance/hooks/useProposalExecute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
useWriteContract,
} from "wagmi";
import { OptimisticTokenVotingPluginAbi } from "../artifacts/OptimisticTokenVotingPlugin.sol";
import { AlertContextProps, useAlertContext } from "@/context/AlertContext";
import { AlertContextProps, useAlerts } from "@/context/Alerts";
import { useRouter } from "next/router";
import { PUB_CHAIN, PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS } from "@/constants";

export function useProposalExecute(proposalId: string) {
const { reload } = useRouter();
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;

const {
data: canExecute,
Expand Down
4 changes: 2 additions & 2 deletions plugins/dualGovernance/hooks/useProposalVeto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useProposal } from "./useProposal";
import { useProposalVetoes } from "@/plugins/dualGovernance/hooks/useProposalVetoes";
import { useUserCanVeto } from "@/plugins/dualGovernance/hooks/useUserCanVeto";
import { OptimisticTokenVotingPluginAbi } from "@/plugins/dualGovernance/artifacts/OptimisticTokenVotingPlugin.sol";
import { useAlertContext, AlertContextProps } from "@/context/AlertContext";
import { useAlerts, AlertContextProps } from "@/context/Alerts";
import { PUB_CHAIN, PUB_DUAL_GOVERNANCE_PLUGIN_ADDRESS } from "@/constants";

export function useProposalVeto(proposalId: string) {
Expand All @@ -26,7 +26,7 @@ export function useProposalVeto(proposalId: string) {
proposal
);

const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;
const {
writeContract: vetoWrite,
data: vetoTxHash,
Expand Down
4 changes: 2 additions & 2 deletions plugins/dualGovernance/pages/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { uploadToIPFS } from "@/utils/ipfs";
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { toHex } from "viem";
import { OptimisticTokenVotingPluginAbi } from "@/plugins/dualGovernance/artifacts/OptimisticTokenVotingPlugin.sol";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";
import WithdrawalInput from "@/components/input/withdrawal";
import { FunctionCallForm } from "@/components/input/function-call-form";
import { Action } from "@/utils/types";
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function Create() {
const [title, setTitle] = useState<string>("");
const [summary, setSummary] = useState<string>("");
const [actions, setActions] = useState<Action[]>([]);
const { addAlert } = useAlertContext();
const { addAlert } = useAlerts();
const {
writeContract: createProposalWrite,
data: createTxHash,
Expand Down
4 changes: 2 additions & 2 deletions plugins/lockToVote/hooks/useProposalClaimLock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
useWaitForTransactionReceipt,
useWriteContract,
} from "wagmi";
import { AlertContextProps, useAlertContext } from "@/context/AlertContext";
import { AlertContextProps, useAlerts } from "@/context/Alerts";
import { useRouter } from "next/router";
import { PUB_CHAIN, PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS } from "@/constants";
import { LockToVetoPluginAbi } from "../artifacts/LockToVetoPlugin.sol";

export function useProposalClaimLock(proposalId: string) {
const { reload } = useRouter();
const account = useAccount();
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;

const {
data: hasClaimed,
Expand Down
4 changes: 2 additions & 2 deletions plugins/lockToVote/hooks/useProposalExecute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
useWriteContract,
} from "wagmi";
import { OptimisticTokenVotingPluginAbi } from "../artifacts/OptimisticTokenVotingPlugin.sol";
import { AlertContextProps, useAlertContext } from "@/context/AlertContext";
import { AlertContextProps, useAlerts } from "@/context/Alerts";
import { useRouter } from "next/router";
import { PUB_CHAIN, PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS } from "@/constants";

export function useProposalExecute(proposalId: string) {
const { reload } = useRouter();
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;

const {
data: canExecute,
Expand Down
4 changes: 2 additions & 2 deletions plugins/lockToVote/hooks/useProposalVeto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useProposalVetoes } from "@/plugins/lockToVote/hooks/useProposalVetoes"
import { useUserCanVeto } from "@/plugins/lockToVote/hooks/useUserCanVeto";
import { LockToVetoPluginAbi } from "@/plugins/lockToVote/artifacts/LockToVetoPlugin.sol";
import { usePermit } from "@/hooks/usePermit";
import { useAlertContext, AlertContextProps } from "@/context/AlertContext";
import { useAlerts, AlertContextProps } from "@/context/Alerts";
import { PUB_CHAIN, PUB_TOKEN_ADDRESS, PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS } from "@/constants";

export function useProposalVeto(proposalId: string) {
Expand All @@ -32,7 +32,7 @@ export function useProposalVeto(proposalId: string) {
);
const { signPermit, refetchPermitData } = usePermit();

const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;
const account_address = useAccount().address!;

const { data: balanceData } = useReadContract({
Expand Down
4 changes: 2 additions & 2 deletions plugins/lockToVote/pages/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { uploadToIPFS } from "@/utils/ipfs";
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { toHex } from "viem";
import { OptimisticTokenVotingPluginAbi } from "@/plugins/dualGovernance/artifacts/OptimisticTokenVotingPlugin.sol";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";
import WithdrawalInput from "@/components/input/withdrawal";
import { FunctionCallForm } from "@/components/input/function-call-form";
import { Action } from "@/utils/types";
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function Create() {
const [title, setTitle] = useState<string>("");
const [summary, setSummary] = useState<string>("");
const [actions, setActions] = useState<Action[]>([]);
const { addAlert } = useAlertContext();
const { addAlert } = useAlerts();
const {
writeContract: createProposalWrite,
data: createTxHash,
Expand Down
4 changes: 2 additions & 2 deletions plugins/tokenVoting/hooks/useProposalExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
useReadContract,
} from "wagmi";
import { TokenVotingAbi } from "../artifacts/TokenVoting.sol";
import { AlertContextProps, useAlertContext } from "@/context/AlertContext";
import { AlertContextProps, useAlerts } from "@/context/Alerts";
import { useRouter } from "next/router";
import { PUB_CHAIN, PUB_TOKEN_VOTING_PLUGIN_ADDRESS } from "@/constants";

export function useProposalExecute(proposalId: string) {
const { reload } = useRouter();
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;

const {
data: canExecute,
Expand Down
4 changes: 2 additions & 2 deletions plugins/tokenVoting/hooks/useProposalVoting.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect } from "react";
import { useWriteContract, useWaitForTransactionReceipt } from "wagmi";
import { TokenVotingAbi } from "@/plugins/tokenVoting/artifacts/TokenVoting.sol";
import { AlertContextProps, useAlertContext } from "@/context/AlertContext";
import { AlertContextProps, useAlerts } from "@/context/Alerts";
import { useRouter } from "next/router";
import { PUB_TOKEN_VOTING_PLUGIN_ADDRESS } from "@/constants";

export function useProposalVoting(proposalId: string) {
const { reload } = useRouter();
const { addAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlerts() as AlertContextProps;
const {
writeContract: voteWrite,
data: votingTxHash,
Expand Down
4 changes: 2 additions & 2 deletions plugins/tokenVoting/pages/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { uploadToIPFS } from "@/utils/ipfs";
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { toHex } from "viem";
import { TokenVotingAbi } from "@/plugins/tokenVoting/artifacts/TokenVoting.sol";
import { useAlertContext } from "@/context/AlertContext";
import { useAlerts } from "@/context/Alerts";
import WithdrawalInput from "@/components/input/withdrawal";
import { FunctionCallForm } from "@/components/input/function-call-form";
import { Action } from "@/utils/types";
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function Create() {
const [title, setTitle] = useState<string>("");
const [summary, setSummary] = useState<string>("");
const [actions, setActions] = useState<Action[]>([]);
const { addAlert } = useAlertContext();
const { addAlert } = useAlerts();
const {
writeContract: createProposalWrite,
data: createTxHash,
Expand Down

0 comments on commit e96fec6

Please sign in to comment.