Skip to content

Commit

Permalink
refactor: centralise error modal config
Browse files Browse the repository at this point in the history
  • Loading branch information
sohrab- committed Feb 23, 2023
1 parent 30723c0 commit 4ba0abb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 26 deletions.
5 changes: 2 additions & 3 deletions src/components/client/Instruction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import { mapIInstructionPreviewToIInstruction } from "mappers/preview-to-internal";
import React, { useMemo } from "react";
import { FaAnchor, FaEject, FaExchangeAlt, FaRocket } from "react-icons/fa";
import { DEFAULT_ERROR_MODAL } from "utils/ui-constants";

export const Instruction: React.FC<{ index: number }> = ({ index }) => {
const rpcEndpoint = useSessionStoreWithUndo((state) => state.rpcEndpoint);
Expand Down Expand Up @@ -78,11 +79,9 @@ export const Instruction: React.FC<{ index: number }> = ({ index }) => {
set(mapped);
} catch (e: unknown) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Cannot map instruction to Anchor",
description: (e as Error).message,
status: "error",
duration: 8000,
isClosable: true,
});
}
};
Expand Down
5 changes: 2 additions & 3 deletions src/components/client/accounts/AirdropButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useWeb3Connection } from "hooks/useWeb3Connection";
import React, { useRef, useState } from "react";
import { FaParachuteBox } from "react-icons/fa";
import { IPubKey } from "types/internal";
import { DEFAULT_ERROR_MODAL } from "utils/ui-constants";
import { toLamports } from "utils/web3js";

export const AirdropButton: React.FC<{ accountPubkey: IPubKey }> = ({
Expand All @@ -57,12 +58,10 @@ export const AirdropButton: React.FC<{ accountPubkey: IPubKey }> = ({
})
.catch((e) => {
toast({
...DEFAULT_ERROR_MODAL,
position: "top-right",
status: "error",
title: `Air Drop has failed.`,
description: `Error: ${e}`,
duration: 10_000,
isClosable: true,
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/client/data/RawData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Description } from "components/common/Description";
import { useInstruction } from "hooks/useInstruction";
import React from "react";
import { RawEncoding } from "types/internal";
import { DEFAULT_ERROR_MODAL } from "utils/ui-constants";

export const RawData: React.FC = () => {
const { useShallowGet, update } = useInstruction();
Expand Down Expand Up @@ -55,10 +56,9 @@ export const RawData: React.FC = () => {
});
} catch (e) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Invalid raw data",
description: `This is not a valid ${targetEncoding} string.`,
status: "error",
isClosable: true,
});
return;
}
Expand Down
13 changes: 4 additions & 9 deletions src/hooks/useAccountType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAccount } from "hooks/useAccount";
import { useInstruction } from "hooks/useInstruction";
import { useSessionStoreWithUndo } from "hooks/useSessionStore";
import { AccountType, IAccountMetadata } from "types/internal";
import { DEFAULT_ERROR_MODAL } from "utils/ui-constants";
import { isValidPublicKey } from "utils/web3js";

/**
Expand Down Expand Up @@ -35,11 +36,9 @@ export const useAccountType = (): {
const wallet = () => {
if (!walletPublicKey) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Wallet not connected!",
description: "Please use the top-right button to connect one.",
status: "error",
duration: 8000,
isClosable: true,
});
return;
}
Expand Down Expand Up @@ -80,11 +79,9 @@ export const useAccountType = (): {

if (!isValidPublicKey(programId)) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Cannot create PDA",
description: "The instruction needs a program ID",
status: "error",
duration: 8000,
isClosable: true,
});
return;
}
Expand All @@ -109,11 +106,9 @@ export const useAccountType = (): {

if (!walletPublicKey) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Wallet not connected!",
description: "Please use the top-right button to connect one.",
status: "error",
duration: 8000,
isClosable: true,
});
return;
}
Expand Down
13 changes: 4 additions & 9 deletions src/hooks/useImportFromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mapProtobufToITransactionExt } from "mappers/protobuf-to-external";
import { mapTransactionResponseToIPreview } from "mappers/web3js-to-preview";
import { useEffect, useMemo, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { DEFAULT_ERROR_MODAL } from "utils/ui-constants";
import { short } from "utils/web3js";

const DEFAULT_STATUS = { isLoading: false, status: "" };
Expand Down Expand Up @@ -67,11 +68,9 @@ export const useImportFromUrl = (): {
setSearchParams({});
} catch (e) {
toast({
...DEFAULT_ERROR_MODAL,
title: "Transaction import failed",
description: `Could not decode the provided transcation`,
status: "error",
duration: 10000,
isClosable: true,
});
}
});
Expand Down Expand Up @@ -99,11 +98,9 @@ export const useImportFromUrl = (): {
onError: (error) => {
setStatus(DEFAULT_STATUS);
toast({
...DEFAULT_ERROR_MODAL,
title: "Transaction import failed",
description: `Failed to fetch the transcation: ${error.message}`,
status: "error",
duration: 15000,
isClosable: true,
});
},
});
Expand Down Expand Up @@ -154,11 +151,9 @@ export const useImportFromUrl = (): {
.catch((err) => {
setStatus(DEFAULT_STATUS);
toast({
...DEFAULT_ERROR_MODAL,
title: "Transaction import failed",
description: `Cannot fetch transaction from the URL: ${err}`,
status: "error",
duration: 10000,
isClosable: true,
});
});

Expand Down
7 changes: 7 additions & 0 deletions src/utils/ui-constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { UseToastOptions } from "@chakra-ui/react";
import { Commitment, Finality, TransactionVersion } from "@solana/web3.js";
import { DataFormat, InstructionDataFieldType } from "types/internal";
import { Explorer, RunType } from "types/state";

export const SIMULATED_SIGNATURE = "<simulated>";

export const DEFAULT_ERROR_MODAL: UseToastOptions = {
status: "error",
duration: 8000,
isClosable: true,
};

export const RUN_TYPES: { id: RunType; name: string }[] = [
{ id: "send", name: "Send" },
{ id: "simulate", name: "Simulate" },
Expand Down

0 comments on commit 4ba0abb

Please sign in to comment.