How to Write Code with ChatGPT and Save Snippets Forever
Published: 2023-09-21 21:01:14

Of all the recent developments in AI tools, ChatGPT stands out as one of the most advanced chatbots, with wide-ranging applications in many fields. This revolutionary chatbot, developed by OpenAI, has attracted significant attention in the tech community due to its proficiency in [AI code generation](https://code.pieces.app/blog/9-best-ai-code-generation-tools). Powered by advanced natural language processing software and deep learning techniques, ChatGPT can assist developers in writing code snippets, suggesting solutions, and even offering programming guidance.

While [ChatGPT’s code generation](https://code.pieces.app/blog/pieces-developers-chatgpt) is already robust, combining it with [developer productivity tools](https://code.pieces.app/blog/best-developer-productivity-tools) can further enhance its potential. [Pieces](https://pieces.app/), also known as Pieces for Developers, is a productivity tool specifically designed for developers.

It leverages AI to save, generate, enrich, reuse, and share code. It also integrates seamlessly with your browser, IDE, and collaboration tools, so you can save code snippets from anywhere. Additionally, it uses on-device machine learning to add titles, descriptions, tags, related links, and other related contexts to your code snippets, making them easier to find and use.

This article explores how to write code with ChatGPT and use Pieces to save, organize, and reuse the generated code snippets.

## Using ChatGPT to Write Code and Saving Snippets in Pieces

This tutorial explains how to write code with ChatGPT and save snippets forever. You'll learn how to do this by building a blog app in Next.js solely based on code generated by ChatGPT and some code snippets from TinaCMS's official documentation.

### Prerequisites

To complete this tutorial, you'll need an [OpenAI account](https://chat.openai.com/auth/login) and [Node.js](https://nodejs.org/en) and [npm](https://www.npmjs.com/) installed on your local computer.

### Installing Pieces

You can install Pieces by following the instructions in the [documentation](https://docs.pieces.app/installation-getting-started/what-am-i-installing), which provides installation steps for Mac, Windows, and Linux operating systems.

Install the desktop application, browser extension, and IDE extension for Pieces. You need to install it across all three platforms. The desktop application will be used to create, retrieve, and transform code snippets; the browser extension will be used mostly to save, copy, and share code snippets; and the IDE extension will be used to save, reuse, and share code snippets.

You will also find steps for installing Pieces on different platforms, such as IDEs and browsers, in the left sidebar.

Follow the installation steps for your preferred IDE, browser, and operating system.

### Setting Up Your Next.js Application

The first step is to create a new Next.js application or use an existing Next.js application if you have one. As this tutorial shows how to generate code using ChatGPT, head over to [ChatGPT](https://chat.openai.com/) and type in the prompt below:

```
Show me how to initialize a Next.js application. The application should be named "tinablog" and the name of the folder should also be "tinablog".
```

The prompt above asks ChatGPT to show you how to initialize a Next.js application and provides some context to guide ChatGPT, including the application and directory names. You should get a response similar to the one in the following screenshot:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/h27866a_55b336377a4f5c4ab6530e01b9b2f1f6.jpg" alt="ChatGPT response on initializing a Next.js application."/></figure>

As a side note, with Pieces, you don't have to worry about losing your code snippets from ChatGPT or any other source. For example, you can save a code snippet from the ChatGPT response above forever. To do that, first enable the Pieces browser extension you installed earlier:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/w8onyuo_c794d02888f9e2a3c7a0b81470acad83.jpg" alt="The Pieces browser extension."/></figure>

Then, go back to the ChatGPT chat thread, reload the page, and hover over any of the code snippets. You'll see two Pieces buttons called **Copy and Save** and **Share**:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/oxy5t24_2e99ba8ca2856d36c4a221c321d010cd.jpg" alt="The Pieces buttons."/></figure>

Click the **Copy and Save** button to save the code snippet forever. To continue with the Next.js application, save all the code snippets in the ChatGPT response by clicking the **Copy and Save** button, as you will have to use those code snippets as you proceed with the tutorial.

Next, open the installed Pieces desktop application to see all the code snippets you've saved:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/usmmylw_08cbcb08044be21dd931889dc711b5c6.jpg" alt="Pieces desktop application."/></figure>

In the screenshot above, you can see that the Pieces desktop application has automatically enriched each snippet with titles, descriptions, tags, and related links.

Open your terminal or command line interface and change the directory to the preferred directory where you want your Next.js application to be initialized. Copy the code snippet titled "Next.js blog creation with npx" from Pieces and run it in your terminal.

You should also see some prompts. For this tutorial, you can select the default answers by punching the **Enter** key until the package installation starts. The final terminal result should look like the following:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/jbvvxj6_556d5be7ff1b85a363882b111f5a63e8.jpg" alt="Create Next.js app."/></figure>

### Setting Up TinaCMS

The next step in building this demo blog application is to set up [TinaCMS](https://tina.io/), an open source headless CMS that supports the Markdown format.

Go to TinaCMS's official documentation on [TinaCMS and Next.js](https://tina.io/docs/frameworks/nextjs/), which explains how to set up a TinaCMS blog on a Next.js application. You should see the same code snippets as in the following screenshot:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/x2jrsvo_116884e12ceb742eef2e49ca0082a2d4.png" alt="TinaCMS and Next.js guide."/></figure>

Save all the code snippets to Pieces, just as you did for the ChatGPT response earlier. Once you've saved the code snippets on the page, launch your Pieces desktop application. It should contain the code snippets you just saved:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/dwv5uns_417437723d2efb3df96c8aa720747f29.jpg" alt="Pieces dashboard showing saved code."/></figure>

Change the directory to the Next.js application with `cd tinablog` and run the "TinaCMS CLI Initialization" command saved in Pieces.

You'll be prompted with some questions. Use the following responses:

- When asked for your Tina Cloud Client ID, as the prompt says, you can hit **Enter** to set it later.
- Select npm as your package manager.
- Select Next.js as your framework.
- Select "No" when asked whether you'd like to use TypeScript for your Tina configuration.


After completing these prompts, the final result should look like the following screenshot:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/nutotyv_7c6c468e68e142a1177aa1a468385475.jpg" alt="TinaCMS CLI initialization result."/></figure>

### Using ChatGPT to Write Code for the Post Listing Page

In this section, you'll use ChatGPT to create a page in the application that lists all blog posts. Enter the following useful ChatGPT prompts:

```
Show me how to add a post listing page to a Next.js and TinaCMS application

Use the guides below
- Do not create separate components or pages.
- Ensure you add everything in a single file.
- Export const getStaticProps async function that gets and returns data, query, and variables from client.queries.postConnection()
- Export default PostList(prop) function that gets the prop data from the returned data, query, and variables
- Ensure you style the page.
```

The prompt above asks ChatGPT to describe how you can add a post listing page to your Next.js TinaCMS application and includes some context to guide how it should respond. You should receive a response similar to this:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/3dxjjbu_222c20f9a1043e303a09c6b1f56b1fd7.jpg" alt="ChatGPT post listing page first response. "/></figure>

You should note that ChatGPT may not always return the exact same response, but the prompt should produce a response with a similar intention. If you would like to continue with the exact code used in this tutorial, you can find it [here](https://cd8ccabe-04ad-4159-ba09-b4369deaca2f.pieces.cloud/?p=34f64aa9cd).

### Using ChatGPT to Debug Code for the Application

Before saving this code to Pieces, you should debug and demo it to ensure it is working as expected. Change the directory to the `src/pages` folder of your application, create a new file called `posts.js`, and paste the code snippet into the newly created file.

After that, change the directory back to your Next.js application root directory and run the `npm run dev` command to compile the application. The provided code snippet produces the following error:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/ijqj157_4e51c17a29ccc50c7e98a0cb3436befa.jpg" alt="Application compilation result."/></figure>

You can now use ChatGPT for debugging `posts.js` with the following prompt:

```
There are a few errors in the code snippet you provided above. You can fix them by doing the following:
- You should add await before client.queries.postConnection();
- I guess there should be a postConnection before data.posts.edges. It should be like data.posts.postConnection.edges
- The node child should be {node.node._sys.filename} not {node.sys.filename}
```

ChatGPT will then amend the code in response to your feedback. As before, remember that ChatGPT might not return the same response. Hence, here is the [link](https://cd8ccabe-04ad-4159-ba09-b4369deaca2f.pieces.cloud/?p=84b84cb060) to the code snippet from the response shown in the screenshot below:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/vnw7oks_7f30d4f87104d984cd49cfecfb69492e.jpg" alt="ChatGPT post listing page second response."/></figure>

Copy the code snippet from the response, then replace the contents of the `posts.js` file with the copied code. Once you save the changes, the code should compile successfully:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/cxpk4nc_4ff1615971a6fd10ebd560906036f0a5.jpg" alt="Application compilation result two."/></figure>

View the page by visiting `{{APPLICATION_URL}}/posts`, where `{{APPLICATION_URL}}` is your server URL. In this example, it's `http://localhost:3000`. You can find your server URL in the compile log. See the screenshot below for more details:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/7hcntds_db2bc7d7ef0e12c008c81afaa9fdb5eb.jpg" alt="Application URL."/></figure>

When you try to load the post page, you'll get a server error. This is because the `data.loading` variable has an undefined value, and an undefined value cannot be serialized as JSON:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/snvqcsk_4da67cdc78cd261be7e2fb76aa9538d8.jpg" alt="Browser error one."/></figure>

Head back to ChatGPT to debug `posts.js` with the following ChatGPT prompt examples:

```
There are a few errors in the corrected code snippet you provided above. You can fix them by doing the following:
- Remove everything that has to do with loading and error. (It should be const { data } = await client.queries.postConnection();)
- The node child should be {node.node._sys.filename} not {node.data.title}
```

ChatGPT will again rework the code in response to your feedback. Here is the [link](https://cd8ccabe-04ad-4159-ba09-b4369deaca2f.pieces.cloud/?p=08b94d83bd) to the code snippet shown in the screenshot below:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/rmziaoy_5db89f3445e0bd118279d65317d8f1df.jpg" alt="ChatGPT post listing page third response."/></figure>

Copy the code snippet in the response, go to your `posts.js` file, remove the existing code in the file, and paste the code snippet you copied into `posts.js`. Upon saving the changes, the code should compile successfully:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/yr6om7e_dc58457256c59fc4996d6ab2ac4495bb.jpg" alt="Application compilation result three."/></figure>

Proceed to view the page by visiting `{{APPLICATION_URL}}/posts`. Upon visiting the page, the example produces another error:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/resaaoq_7163b49f06961a260c1abc1eb375c356.jpg" alt="Browser error two."/></figure>

You will have to head back to ChatGPT to debug `posts.js` by providing the prompt below:

```
There are a few errors in the corrected code snippet you provided above. You can fix them by doing the following:
- Since you are no longer returning the "post" key, data.posts.postConnection.edges.map(({ node }) should be data.postConnection.edges.map(({ node })
- Remove the "link" tag and use "a" tag
- The node child should be {node._sys.filename} not {node.node._sys.filename}
```

The prompt above produces the following response, which you can also find ​​[here](https://cd8ccabe-04ad-4159-ba09-b4369deaca2f.pieces.cloud/?p=0ed24393e8):

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/osl1tsr_1ca73e99a07c1ec2eb0881182eeb9415.jpg" alt="ChatGPT post listing page fourth response."/></figure>

Once again, replace the contents of the `posts.js` file with this updated code. Save the changes, and the code will compile:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/wqd6lif_83bd54795fb55f8a124997eccbd2e2c0.jpg" alt="Application compilation result four."/></figure>

When you view the page by visiting `{{APPLICATION_URL}}/posts`, it should load successfully without any errors:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/cvowp8m_7ca49ca5348ae872726d48cfce319780.jpg" alt="Post listing page."/></figure>

Before you proceed to demo the application, save the final, corrected snippet using Pieces. To do that, head back to ChatGPT, ensure you have the Pieces browser extension enabled, and use the **Copy and Save** button.

Pieces also provides a transform code feature. This feature enables you to make your code snippets more readable by adding inline comments and optimizing the code for better performance.

To use the code transformation feature, open the Pieces desktop app, select the snippet starting with "React Post List Component," and click the **Edit** icon in the upper right:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/hof7bov_d13f45472217d642bda263ec6153e555.jpg" alt="Pieces Desktop edit button."/></figure>

You'll be presented with the transform code interface (edit mode):

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/xfgppyk_aad973919efb3024aa10ad7de0ba03e0.jpg" alt="Pieces desktop app edit mode interface."/></figure>

In edit mode, you can click the **Improve Readability** icon on the right side of the screen to make the code more readable. Clicking this icon will apply improvements such as adding comments and making the code more legible, as you can see in the following screenshot:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/onbfnhc_208f286b108d36baa25ec7741d747a40.jpg" alt="Pieces desktop improve readability."/></figure>

Feel free to play around with other code transformation features in edit mode.

Furthermore, the [Pieces Copilot](https://code.pieces.app/blog/introducing-pieces-copilot) within the desktop application has ChatGPT and proprietary AI models built in, enabling you to generate code snippets right within Pieces. This awesome feature not only enhances your productivity but also minimizes the tools required for code generation, writing, saving, and reuse. Visit the [official documentation](https://docs.pieces.app/features/pieces-copilot) to learn how to utilize this functionality.

Lastly, proceed to demo the application. Open your browser, paste in `{{APPLICATION_URL}}`, and visit the page:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/hlkrdcv_21dde97f5b8ab2c62938d7a272d41ef0.jpg" alt="Next.js default landing page."/></figure>

To view the TinaCMS admin page, visit `{{APPLICATION_URL}}/admin/index.html`:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/qvxeasu_9e6bff4feff43c09d1bdec7252fdc55a.jpg" alt="TinaCMS admin page."/></figure>

Click the **Enter Edit Mode** button to start managing your blog application. Feel free to add two or more blog posts. You can also view the post listing page to view all the blog posts by visiting `{{APPLICATION_URL}}/posts`. If you used the ChatGPT-generated code snippets, the page should look like the screenshot below:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/cvowp8m-1_05c3631cc867b71752aec308c16a8d9a.jpg" alt="Post listing page."/></figure>

Finally, you can view an individual blog post by clicking any of the listed posts. The default blog page design should look like the screenshot below:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/3tfmwrw_f3b12a83851da6d62a38daca1be4aea7.jpg" alt="Post page."/></figure>

## Conclusion

In this article, you learned how to write code with ChatGPT and save that code to [Pieces](https://pieces.app/), a free AI tool and robust developer productivity platform. You created a blog app in Next.js using  ChatGPT code generation and saved the generated code in Pieces. Pieces provides developers with persistent context and easy access to previously saved snippets, making the software development process more efficient and streamlined.

[Save your first piece today](https://docs.pieces.app/product-highlights-and-benefits/saving-useful-developer-materials)!