Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions apps/api-reference/src/components/EvmApi/run-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ export const RunButton = <ParameterName extends string>(
{status.type === StatusType.Results && (
<div>
<h3 className="mb-2 text-lg font-bold">Results</h3>
<Code language="json">{stringifyResponse(status.data)}</Code>
{props.type === EvmApiType.Write && status.data && typeof status.data === 'object' && 'hash' in status.data && typeof status.data.hash === 'string' ? (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these kinds of cases I suggest you use zod for this kind of thing and it's much easier to write and also easier to read and understand, e.g. make a Result component and use it here, something like this:

import { z } from "zod";
...
const Results = ({ data }) => {
    const parsed = hashSchema.safeParse(data);

    return parsed.success ? (
        <>
            <Code language="json">Tx Hash: {parsed.data.hash}</Code>
            ...
        </>
    ) : (
        <Code language="json">{stringifyResponse(status.data)}</Code>
    )
}

const hashSchema = z.strictObject({ hash: z.string() });

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much cleaner. I will keep it mind to use it now.

<>
<Code language="json">{`Tx Hash: ${status.data.hash}`}</Code>
{'link' in status.data && typeof status.data.link === 'string' && (
<a href={status.data.link} target="_blank" rel="noopener noreferrer" className="text-sm text-blue-500 hover:underline">
Open in explorer↗
</a>
)}
</>
) : (
<Code language="json">{stringifyResponse(status.data)}</Code>
)}
</div>
)}
{status.type === StatusType.Error && (
Expand Down Expand Up @@ -175,10 +186,15 @@ const useRunButton = <ParameterName extends string>({
})
.then(({ request }) => writeContract(config, request))
.then((result) => {
setStatus(Results(result));
const output: {hash: string, link?: string} = {hash: result};
if (config.chains[0].blockExplorers?.default !== undefined) {
const blockExplorerTxLink = `${config.chains[0].blockExplorers.default.url}/tx/${result}`;
output.link = blockExplorerTxLink;
}
setStatus(Results(output));
})
.catch((error: unknown) => {
setStatus(ErrorStatus(error));
setStatus(ErrorStatus(error))
});
}
return;
Expand Down
4 changes: 2 additions & 2 deletions apps/hermes/server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.