Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

feat: add CodeSegment component with enhanced code snippets in ContractFunction #2673

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/components/contract-functions/contract-function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import { camelToTitle } from "contract-ui/components/solidity-inputs/helpers";
import { useRouter } from "next/router";
import { Dispatch, SetStateAction, useMemo, useState } from "react";
import { Badge, Button, Card, Heading, Text } from "tw-components";
import { CodeSegment } from "../contract-tabs/code/CodeSegment";
import {
COMMANDS,
formatSnippet,
} from "../../contract-ui/tabs/code/components/code-overview";
import { CodeEnvironment } from "../contract-tabs/code/types";

interface ContractFunctionProps {
fn?: AbiFunction | AbiEvent;
Expand All @@ -43,12 +49,18 @@ const ContractFunction: React.FC<ContractFunctionProps> = ({
fn,
contract,
}) => {
const [environment, setEnvironment] = useState<CodeEnvironment>("javascript");

if (!fn) {
return null;
}

const isFunction = "stateMutability" in fn;

const isRead =
isFunction &&
(fn.stateMutability === "view" || fn.stateMutability === "pure");

return (
<Flex direction="column" gap={1.5}>
<Flex
Expand Down Expand Up @@ -144,6 +156,24 @@ const ContractFunction: React.FC<ContractFunctionProps> = ({
abiFunction={fn}
/>
)}

<Heading size="subtitle.md" mt={6}>
Use this function in your app
</Heading>
<Divider mb={2} />
<CodeSegment
environment={environment}
setEnvironment={setEnvironment}
snippet={formatSnippet(
COMMANDS[isFunction ? (isRead ? "read" : "write") : "events"] as any,
{
contractAddress: contract?.getAddress(),
fn,
args: fn.inputs?.map((i) => i.name),
chainId: contract?.chainId,
},
)}
/>
</Flex>
);
};
Expand Down
18 changes: 9 additions & 9 deletions src/contract-ui/tabs/code/components/code-overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,35 +177,35 @@ const transaction = await prepareContractCall({
const { transactionHash } = await sendTransaction({
transaction,
account
})`,
});`,
react: `import { prepareContractCall } from "thirdweb"
import { useSendTransaction } from "thirdweb/react";

export default function Component() {
const { mutate: sendTransaction, isLoading, isError } = useSendTransaction();
const { mutate: sendTransaction } = useSendTransaction();

const call = async () => {
const transaction = await prepareContractCall({
const onClick = () => {
const transaction = prepareContractCall({
contract,
method: "{{function}}",
params: [{{args}}]
});
const { transactionHash } = await sendTransaction(transaction);
sendTransaction(transaction);
}
}`,
"react-native": `import { prepareContractCall } from "thirdweb"
import { useSendTransaction } from "thirdweb/react";

export default function Component() {
const { mutate: sendTransaction, isLoading, isError } = useSendTransaction();
const { mutate: sendTransaction } = useSendTransaction();

const call = async () => {
const transaction = await prepareContractCall({
const onClick = () => {
const transaction = prepareContractCall({
contract,
method: "{{function}}",
params: [{{args}}]
});
const { transactionHash } = await sendTransaction(transaction);
sendTransaction(transaction);
}
}`,
},
Expand Down
Loading