Skip to content

Commit

Permalink
Removing all alert shorthands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jør∂¡ committed Feb 23, 2024
1 parent 228c5bb commit 47c3fa2
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 278 deletions.
49 changes: 18 additions & 31 deletions context/AlertContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import { usePublicClient } from "wagmi";

const DEFAULT_ALERT_TIMEOUT = 7 * 1000;

export type NewAlert = {
type: "success" | "info" | "error";
message: string;
export type AlertOptions = {
type?: "success" | "info" | "error";
description?: string;
txHash?: string;
timeout?: number;
};

export interface AlertContextProps {
alerts: IAlert[];
addAlert: (newAlert: NewAlert) => void;
addSuccessAlert: (message: string) => void;
addInfoAlert: (message: string) => void;
addErrorAlert: (message: string) => void;
addAlert: (message: string, alertOptions?: AlertOptions) => void;
}

export const AlertContext = createContext<AlertContextProps | undefined>(
Expand All @@ -31,52 +27,43 @@ export const AlertProvider: React.FC<{ children: React.ReactNode }> = ({
const client = usePublicClient();

// Add a new alert to the list
const addAlert = (alert: NewAlert) => {
const addAlert = (message: string, alertOptions?: AlertOptions) => {
// Clean duplicates
const idx = alerts.findIndex(
(a) => a.message === alert.message && a.description === alert.description
);
const idx = alerts.findIndex((a) => {
if (a.message !== message) return false;
else if (a.description !== alertOptions?.description) return false;
else if (a.type !== alertOptions?.type) return false;

return true;
});
if (idx >= 0) removeAlert(idx);

const newAlert: IAlert = {
id: Date.now(),
message: alert.message,
description: alert.description,
type: alert.type,
message,
description: alertOptions?.description,
type: alertOptions?.type ?? "info",
};
if (alert.txHash && client) {
if (alertOptions?.txHash && client) {
newAlert.explorerLink =
client.chain.blockExplorers?.default.url + "/tx/" + alert.txHash;
client.chain.blockExplorers?.default.url + "/tx/" + alertOptions.txHash;
}
setAlerts(alerts.concat(newAlert));

// Schedule the clean-up
const timeout = alert.timeout ?? DEFAULT_ALERT_TIMEOUT;
const timeout = alertOptions?.timeout ?? DEFAULT_ALERT_TIMEOUT;
setTimeout(() => {
removeAlert(newAlert.id);
}, timeout);
};

// Convenience aliases
const addSuccessAlert = (message: string) => {
addAlert({ message, type: "success" });
};
const addInfoAlert = (message: string) => {
addAlert({ message, type: "info" });
};
const addErrorAlert = (message: string) => {
addAlert({ message, type: "error" });
};

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

return (
<AlertContext.Provider
value={{ alerts, addAlert, addSuccessAlert, addInfoAlert, addErrorAlert }}
>
<AlertContext.Provider value={{ alerts, addAlert }}>
{children}
</AlertContext.Provider>
);
Expand Down
14 changes: 5 additions & 9 deletions plugins/dualGovernance/components/proposal/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ProposalHeader: React.FC<ProposalHeaderProps> = ({
onVetoPressed,
}) => {
const { reload } = useRouter();
const { addAlert, addErrorAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlertContext() as AlertContextProps;
const proposalVariant = useProposalVariantStatus(proposal);

const {
Expand All @@ -56,32 +56,28 @@ const ProposalHeader: React.FC<ProposalHeaderProps> = ({
if (status === "idle" || status === "pending") return;
else if (status === "error") {
if (error?.message?.startsWith("User rejected the request")) {
addAlert({
message: "Transaction rejected by the user",
type: "error",
addAlert("Transaction rejected by the user", {
timeout: 4 * 1000,
});
} else {
console.error(error);
addErrorAlert("Could not execute the proposal");
addAlert("Could not execute the proposal", { type: "error" });
}
return;
}

// success
if (!executeTxHash) return;
else if (isConfirming) {
addAlert({
message: "Proposal submitted",
addAlert("Proposal submitted", {
description: "Waiting for the transaction to be validated",
type: "info",
txHash: executeTxHash,
});
return;
} else if (!isConfirmed) return;

addAlert({
message: "Proposal executed",
addAlert("Proposal executed", {
description: "The transaction has been validated",
type: "success",
txHash: executeTxHash,
Expand Down
34 changes: 16 additions & 18 deletions plugins/dualGovernance/pages/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function Create() {
const [title, setTitle] = useState<string>("");
const [summary, setSummary] = useState<string>("");
const [actions, setActions] = useState<Action[]>([]);
const { addAlert, addErrorAlert } = useAlertContext();
const { addAlert } = useAlertContext();
const {
writeContract: createProposalWrite,
data: createTxHash,
Expand All @@ -64,31 +64,26 @@ export default function Create() {
if (status === "idle" || status === "pending") return;
else if (status === "error") {
if (error?.message?.startsWith("User rejected the request")) {
addAlert({
message: "Transaction rejected by the user",
type: "error",
addAlert("Transaction rejected by the user", {
timeout: 4 * 1000,
});
} else {
addErrorAlert("Could not create the proposal");
addAlert("Could not create the proposal", { type: "error" });
}
return;
}

// success
if (!createTxHash) return;
else if (isConfirming) {
addAlert({
message: "Proposal submitted",
addAlert("Proposal submitted", {
description: "Waiting for the transaction to be validated",
type: "info",
txHash: createTxHash,
});
return;
} else if (!isConfirmed) return;

addAlert({
message: "Proposal created",
addAlert("Proposal created", {
description: "The transaction has been validated",
type: "success",
txHash: createTxHash,
Expand All @@ -100,29 +95,32 @@ export default function Create() {

const submitProposal = async () => {
// Check metadata
if (!title.trim()) return addErrorAlert("Please, enter a title");
if (!title.trim())
return addAlert("Please, enter a title", { type: "error" });

const plainSummary = getPlainText(summary).trim();
if (!plainSummary.trim())
return addErrorAlert(
"Please, enter a summary of what the proposal is about"
);
return addAlert("Please, enter a summary of what the proposal is about", {
type: "error",
});

// Check the action
switch (actionType) {
case ActionType.Signaling:
break;
case ActionType.Withdrawal:
if (!actions.length) {
return addErrorAlert(
"Please ensure that the withdrawal address and the amount to transfer are valid"
return addAlert(
"Please ensure that the withdrawal address and the amount to transfer are valid",
{ type: "error" }
);
}
break;
default:
if (!actions.length || !actions[0].data || actions[0].data === "0x") {
return addErrorAlert(
"Please ensure that the values of the action to execute are correct"
return addAlert(
"Please ensure that the values of the action to execute are correct",
{ type: "error" }
);
}
}
Expand Down
15 changes: 5 additions & 10 deletions plugins/dualGovernance/pages/proposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function ProposalDetail({ id: proposalId }: { id: string }) {

const [bottomSection, setBottomSection] =
useState<BottomSection>("description");
const { addAlert, addErrorAlert } = useAlertContext() as AlertContextProps;
const { addAlert } = useAlertContext() as AlertContextProps;
const {
writeContract: vetoWrite,
data: vetoTxHash,
Expand All @@ -58,31 +58,26 @@ export default function ProposalDetail({ id: proposalId }: { id: string }) {
if (status === "idle" || status === "pending") return;
else if (status === "error") {
if (error?.message?.startsWith("User rejected the request")) {
addAlert({
message: "Transaction rejected by the user",
type: "error",
addAlert("Transaction rejected by the user", {
timeout: 4 * 1000,
});
} else {
addErrorAlert("Could not create the proposal");
addAlert("Could not create the proposal", { type: "error" });
}
return;
}

// success
if (!vetoTxHash) return;
else if (isConfirming) {
addAlert({
message: "Veto submitted",
addAlert("Veto submitted", {
description: "Waiting for the transaction to be validated",
type: "info",
txHash: vetoTxHash,
});
return;
} else if (!isConfirmed) return;

addAlert({
message: "Veto registered",
addAlert("Veto registered", {
description: "The transaction has been validated",
type: "success",
txHash: vetoTxHash,
Expand Down
Loading

0 comments on commit 47c3fa2

Please sign in to comment.