Skip to content

Commit

Permalink
Adapt to useExplorer from starknet react (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura authored Dec 31, 2024
1 parent 6473a51 commit 88a5f2f
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions packages/keychain/src/components/ErrorAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import {
Divider,
} from "@chakra-ui/react";
import { motion } from "framer-motion";
import React, { ReactElement, useEffect, useState } from "react";
import React, { ReactElement, useCallback, useEffect, useState } from "react";
import { ErrorCode } from "@cartridge/account-wasm/controller";
import { ControllerError } from "@/utils/connection";
import { useConnection } from "@/hooks/connection";
import { constants } from "starknet";
import { parseExecutionError, parseValidationError } from "@/utils/errors";
import { formatAddress } from "@cartridge/utils";
import { Link } from "react-router-dom";
import { useExplorer } from "@starknet-react/core";

export function ErrorAlert({
title,
Expand Down Expand Up @@ -325,23 +324,18 @@ function StackTraceDisplay({
}: {
stackTrace: ReturnType<typeof parseExecutionError>["stack"];
}) {
const { chainId } = useConnection();

const getExplorerUrl = (type: "contract" | "class", value: string) => {
if (!chainId) return;

const baseUrl = {
[constants.StarknetChainId.SN_SEPOLIA]: "https://sepolia.starkscan.co",
[constants.StarknetChainId.SN_MAIN]: "https://starkscan.co",
}[chainId];

return baseUrl ? `${baseUrl}/${type}/${value}` : undefined;
};

const isExternalLink = [
constants.StarknetChainId.SN_SEPOLIA,
constants.StarknetChainId.SN_MAIN,
].includes(chainId as constants.StarknetChainId);
const explorer = useExplorer();
const getExplorerUrl = useCallback(
(key: "address" | "class", value: string) => {
switch (key) {
case "address":
return explorer.contract(value);
case "class":
return explorer.class(value);
}
},
[explorer],
);

return (
<VStack align="start" spacing={2} w="full">
Expand All @@ -367,13 +361,8 @@ function StackTraceDisplay({
</Text>
{key === "address" || key === "class" ? (
<Link
to={
getExplorerUrl(
key === "address" ? "contract" : "class",
value as string,
) as string
}
target={isExternalLink ? "_blank" : undefined}
to={getExplorerUrl(key, value as string)}
target="_blank"
className="break-all text-left hover:underline"
>
{formatAddress(value as string, {
Expand Down

0 comments on commit 88a5f2f

Please sign in to comment.