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

Commit

Permalink
feat: add CodeSegment component with enhanced code snippets in Contra…
Browse files Browse the repository at this point in the history
…ctFunction (#2673)

### TL;DR

Enhances the `ContractFunction` component by adding interactive code snippets for various environments, helping users utilize contract functions in their applications.

### What changed?

- Added code snippets under `use this function in your app` section.
- Introduced `CodeEnvironment` state to toggle between environments (e.g., JavaScript).
- Added import for `CodeSegment`, `COMMANDS`, `formatSnippet`, and `CodeEnvironment`.
- Removed console log statements and unused imports.

### How to test?

- Verify that code snippets appear as expected for different contract functions.
- Toggle between environments and ensure the correct code is displayed.

### Why make this change?

Provides developers with ready-to-use code snippets, streamlining the integration process for contract functions into applications.

---

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to refactor code related to contract function execution and UI components in the contract overview tab.

### Detailed summary
- Refactored code execution logic for contract functions
- Added `CodeSegment` component for displaying code snippets
- Updated function usage display in UI based on function type and state mutability

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
jnsdls committed Jun 15, 2024
1 parent 4faf636 commit 091ecf8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
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

0 comments on commit 091ecf8

Please sign in to comment.