ChatCraft week 13: Fixing bugs
Tags: chatcraft, typescript, opensource, chatgpt
Published: 2024-04-13 13:03:01

---
series: Collaborating on ChatCraft
---

This week on [ChatCraft](https://chatcraft.org/), I fixed a few bugs I found in production that have arisen since the new custom provider feature was implemented. I was able to find these bugs because I reviewed many PRs this week and during testing those PRs I was able to see some abnormal behaviour.

[This](https://github.com/tarasglek/chatcraft.org/issues/583) is the issue I created which outlines how to recreate the 4 bugs I found.

## Bug 1

The first bug involved the Instructions page. This page is no longer in use for the most part, but will still come up when the user clears their current provider's api key.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pwnd0gmr5rhhw0314oa.png)

When the user uses a custom provider however, the dropdown list in the Instructions page did not include that custom provider, which leads to unexpected behaviour.

I fixed this by merging the supported providers list with the user's saved providers (which would include the custom provider).

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dndckh6irlm2icbk8w2.png)](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-9fcdb80f3814fd658a3bb7dcaf58478d27938a43cde9049d449210652e77434c)

## Bug 2

The second bug is also on the Instructions page. The previous behaviour was that when we toggle between the providers in the dropdown, if one of them had a saved api key (from localStorage), then we set that key to `settings.currentProvider`. This poses a problem, since we are bypassing the key validation code and taking the user away from the Instructions page (Instructions page only shows up for users when the `settings.currentProvider` api key is blank, and it's no longer blank so we leave). The stored api key could be a stored key that is invalid. Or, the stored key could have become invalid after time passed.

I fixed this by introducing a state variable `selectedProvider` in [`Instructions.tsx`](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-9fcdb80f3814fd658a3bb7dcaf58478d27938a43cde9049d449210652e77434c) so we can keep track of the dropdown's selected provider's api key. This allows us to delay the setting of the currentProvider's api key until after we click the Save button.

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fkcv1ksskv477wbspwr.png)](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-9fcdb80f3814fd658a3bb7dcaf58478d27938a43cde9049d449210652e77434c)

## Bug 3

The third bug involves the User Settings modal. When the user enters a blank or invalid key we get a form error message as expected. However, when we close the modal and reopen it, that message is still there. There is also strange behaviour where if the user input a correct key into that field, the form error message still persists.

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtdoefdbj0xbligmivqb.png)](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-1025748a16e04005898c20c05840616be48ca0eb4eb4c156248de41a2f3bdd7d)

I fixed this by adding one line of code to the use effect that runs when the modal is closed, clearing the field validation.

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ni5mrelj6xk8k2tf5idf.png)](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-1025748a16e04005898c20c05840616be48ca0eb4eb4c156248de41a2f3bdd7d)

## Bug 4

The last bug was brought to my attention while Yumei was testing my PR. I quickly found a fix so I just added it.

The bug involves browsers' password saving ability. When users enter an api key in Instructions page or when they create a custom provider, they were prompted by their browser whether they want to save the password. Users who have a key saved in their browser will experience a bug. The moment they clear their api key the browser will sense that the field is empty and repopulated it with their saved password. Also, when we create one or more providers with the same api url, it will indiscriminately populate the key for those providers.

I don't have a fix for users who have already saved an api key as their password, but I can stop ChatCraft from prompting users to save future keys as passwords. This took a bit of digging online, because many online solutions don't actually work. A lot of the time the browsers will not listen even if you set `autoComplete="off"`. I tried many online solutions one by one, and finally found one that worked.

I found that when [Chakra UI](https://v2.chakra-ui.com/)'s [`Input` component](https://v2.chakra-ui.com/docs/components/input) is set to `password` that is when the browsers will prompt the saving of the password. Therefore I changed it to always be `text`. Now I lost a feature of component, which used to use `type = "password"` to disguise the user key with asterix. However I was able to find a different way to hide the user key with asterix (see below!)

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3wg7itni11fxjffml0k.png)](https://github.com/tarasglek/chatcraft.org/pull/584/files#diff-489784f1d0127d723b28422bf45e60d2be2c40dadf51167c76db55e9977da062)

# PR

My [PR](https://github.com/tarasglek/chatcraft.org/pull/584) for these bug fixes was reviewed by three contributors and landed in release [v1.9.0](https://github.com/tarasglek/chatcraft.org/releases/tag/v1.9.0)!

# Reviews

I did reviews on the following PRs:

- [PR #577](https://github.com/tarasglek/chatcraft.org/pull/577)
- [PR #579](https://github.com/tarasglek/chatcraft.org/pull/579)
- [PR #575](https://github.com/tarasglek/chatcraft.org/pull/575)
- [PR #582](https://github.com/tarasglek/chatcraft.org/pull/582)

Most of these were bug fixes and there is minimal code additions, so I just did testing of the issue to make sure the bug is fixed and no new bugs were introduced.

Unfortunately I found a new bug after a PR had already been merged. PR #577 was a toast message width fix, but for some reason it affected the API Key validation toasts, making them appear when they should not, and in greater numbers. [Here](https://github.com/tarasglek/chatcraft.org/issues/586) is the issue I created which details how to reproduce the issue. It took me a while to track down which PR this bug originated from. At first I was worried this bug originated from one of my PRs. I went through each commit in ChatCraft in sequence until I found where the bug was no longer occurring. Although I managed to narrow down when the bug started occurring, I did not have time to investigate what part of the code from that PR was causing it. I contacted Roy since it was his PR and I believe he is looking into it now.