-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add automatical saving conversation in local storage #26
Open
mejsiejdev
wants to merge
7
commits into
main
Choose a base branch
from
13-add-automatical-saving-conversation-in-local-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
47ecb1e
feat(chatpage): Throw page not found error if chat is not in localSto…
mejsiejdev 4d257a9
feat(textarea): Save prompt to localStorage on submit
mejsiejdev 9432d30
feat(chatpage): Display saved prompt
mejsiejdev 28ee3ba
refactor(chatpage): remove comment
mejsiejdev e3e2715
fix(chatpage): Fix `lint` errors
mejsiejdev 9d0be30
feat(chatpage): Create `Chat` interface
mejsiejdev e3d9850
feat(chatpage): Store list of chats in localStorage
mejsiejdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
"use client"; | ||
//import { usePathname } from "next/navigation"; | ||
|
||
import { notFound, useParams } from "next/navigation"; | ||
|
||
interface Chat { | ||
prompt: string; | ||
} | ||
|
||
export default function ConversationPage() { | ||
/* | ||
const pathname = usePathname(); // pathname = /chat/{uuid} | ||
TODO load chat from local storage using uuid, if not found -> 404 | ||
*/ | ||
const parameters = useParams<{ uuid: string }>(); | ||
|
||
if (localStorage.getItem(parameters.uuid) === null) { | ||
notFound(); | ||
} | ||
|
||
const chat = JSON.parse(localStorage.getItem(parameters.uuid)!) as Chat; | ||
|
||
return <div></div>; | ||
return <div>{chat.prompt}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ export function AutoResizeTextArea({ | |
setIsSubmitting(true); | ||
const newUuid = v4(); | ||
//TODO adding conversation to localstorage there? | ||
localStorage.setItem(newUuid, JSON.stringify({ prompt: text })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need list of chats in local storage |
||
router.push(`/chat/${newUuid}`); | ||
}} | ||
disabled={isSubmitting} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The amount of code in the onClick callback can be reduced. Notice how this piece of code repeats two times:
I think it's always best to stick to the DRY principle (do not repeat yourself). Here's my take on the callback function - from 17 to 8 lines, and it works the same:
I'll try my best to explain the thought process behind my changes:
This line removes the need for the entire
if
block and utilizes a ternary operator instead. If a given local storage key doesn't exist then.getItem()
method returnsnull
, otherwise the json string, therefore we explicitly check whether.getItem("chats")
is null (this also makes eslint rule@typescript/strict-boolean-expressions
happy).If true ("chats" are null)
it means no chats were saved yet, so we set "chats" variable to an empty array, therefore no type assertions are needed
If false ("chats" are not null)
it means at least one chat is saved, so we set "chats" variable to the parsed json string. Here we still use non-null assertion since typescript still thinks that
.getItem("chats")
can be null + we do a type assertion sinceJSON.parse()
return type isany
And then we just set the "chats" local storage item to an array consisting of already existing chats and the newly created one. If "chats" variable is an empty array then we can still spread it without a problem.
This solution is compliant with our linter rules and passes all checks
EDIT: This is of course more of a nitpick and such refactor could be done later but since our project won't take that long and it isn't too complex we might as well tweak such things early on