ChatCraft Adventures Week #11, Vite Precaching, Keyboard Shortcuts, Nomnoml Rendering
Tags: opensource, webdev
Published: 2024-03-31 05:17:21

[ChatCraft](https://chatcraft.org/) Release 1.7 is available [here](https://github.com/tarasglek/chatcraft.org/releases/tag/v1.7.0).

I had a pretty productive week. I made three Pull Requests, and added a couple cool features I'd like to share with you today.
## Pull Requests

### [Nomnoml Rendering Support](https://github.com/tarasglek/chatcraft.org/pull/526)

This is a Pull Request I started last week, but completed this week. This PR adds [skanaar/nomnoml](https://github.com/skanaar/nomnoml) to let ChatCraft render [nomnoml](https://nomnoml.com/) code in a preview.

You can try this feature yourself! On ChatCraft, just request a Nomnoml diagram from your selected LLM. You can also see the feature in action in [this chat](https://chatcraft.org/api/share/rjwignar/Qwt7SVDTKsfWFMFtcMKiu).

One thing I noticed is that depending on the LLM, you may receive nomnoml with syntax errors. When implementing this, I also noticed the nomnoml library doesn't provide options for a default theme. It would be really cool if I could change the default stylings for dark mode!

I'm proud of this feature. For a while I've been adding bug fixes and code enhancements, but until this PR I never added a feature. I can't take all the credit though. ChatCraft already renders Mermaid diagrams, so my strategy for this PR was to model my implementation off the already exiting Mermaid preview rendering feature. 
Viewing the history of the [MermaidPreview.tsx component](https://github.com/tarasglek/chatcraft.org/commits/main/src/components/MermaidPreview.tsx) took me to this [commit](https://github.com/tarasglek/chatcraft.org/commit/78069da57dbe4d20e6e2dddc3f2bc909e98666cb) that my class instructor, Dave, added last year. This commit helped inform how I should implement nomnoml preview rendering - what files to add, what files to make changes to and what changes to make.

### [/ Keyboard Shortcut to focus Chat Input](https://github.com/tarasglek/chatcraft.org/pull/527)

This is another cool feature I started last week and was able to complete this week. This feature adds a new keyboard shortcut that activates your current Chat's prompt input. If you want to quickly focus your prompt input, just press the forward slash key, `/`.

This feature is common on websites like YouTube and GitHub, but I never knew about it until this feature was [requested](https://github.com/tarasglek/chatcraft.org/issues/511) for ChatCraft.

When implementing this feature, I originally wanted to make use of this [handler for keyboard inputs](https://github.com/tarasglek/chatcraft.org/blob/main/src/hooks/use-key-down-handler.ts). However, my initial implementation didn't seem to work, so I added this keyboard shortcut as a [useEffect React Hook](https://react.dev/reference/react/useEffect). Where to place this useEffect was a challenge. I wanted to place it such that the keyboard shortcut would work anywhere on the page, so added it to what I believed was the entire chat window component, [ChatBase](https://github.com/tarasglek/chatcraft.org/blob/e8e55c53d39445b4096701fc5774961239d31183/src/Chat/ChatBase.tsx).

I'm pretty happy with this feature. I've never added keyboard shortcuts, so figuring out and learning how to do it was a fun experience.

### [Bug Fix: Non Precached Url](https://github.com/tarasglek/chatcraft.org/pull/529)

For a while, I noticed this error when opening my browser's developer tools on ChatCraft:

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

So I made an [Issue](https://github.com/tarasglek/chatcraft.org/issues/528) for it and began investigating.

I can't say exactly how long I've seen this error, but even with this error present ChatCraft works just fine.
My investigation involved searching the error message on Google:
```
Uncaught (in promise) non-precached-url: non-precached-url
```

My search led me to this [discussion](https://github.com/vite-pwa/vite-plugin-pwa/issues/120), and then to this [comment](https://github.com/vite-pwa/vite-plugin-pwa/issues/120#issuecomment-1202579983) that contained the changes I had to make to fix this issue.

#### Explanation
I discussed this PR with my classmates earlier this week, and Dave helped me understand what this error was about.

ChatCraft uses [vite-plugin-pwa](https://github.com/vite-pwa/vite-plugin-pwa) to make ChatCraft a [Progressive Web App](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Guides/What_is_a_progressive_web_app) (PWA). I don't exactly know what a PWA isFrom what I understand, making ChatCraft a PWA allows it to work and provide value even if the user's device isn't connected to the internet.

The error I fixed is related to workbox settings, located in a vite.config.ts. I'm still learning what workbox is, but for now I can say that in our case, the workbox configurations we have define what assets will be pre-cached.

The error was due to these settings expecting ChatCraft's index.html to be pre-cached. Though we precache some assets, we don't precache index.html. The changes I made tells the workbox not to expect index.html to be pre-cached, so the error no longer appears.


### Plans for April
Any new code added is more code that must be maintained, whether through refactoring or bug fixes. This is often referred to as [technical debt](https://en.wikipedia.org/wiki/Technical_debt ). There are three more weeks in my semester. In those remaining weeks I plan to help manage as much technical debt as I can.