Skip to content

Commit

Permalink
build fix in convo saving code
Browse files Browse the repository at this point in the history
  • Loading branch information
KastanDay committed Jun 24, 2024
1 parent 7a2c667 commit 2d731bf
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/utils/app/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ export const saveConversation = (conversation: Conversation) => {
)
posthog.capture('local_storage_full', {
course_name:
conversation.messages.length > 0 &&
conversation.messages[0].contexts.length > 0
? conversation.messages[0].contexts[0].course_name
: 'Unknown Course',
conversation.messages?.[0]?.contexts?.[0]?.course_name ||
'Unknown Course',
user_email: conversation.user_email,
inSaveConversation: true,
})
Expand All @@ -55,22 +53,18 @@ export const saveConversation = (conversation: Conversation) => {
const clearSingleOldestConversation = () => {
console.debug('CLEARING OLDEST CONVERSATIONS to free space in local storage.')

let existingConversations = JSON.parse(
const existingConversations = JSON.parse(
localStorage.getItem('conversationHistory') || '[]',
)

// let existingConversations = JSON.parse(localStorage.getItem('conversationHistory') || '[]');
while (existingConversations.length > 0) {
console.debug('INSIDE WHILE LOOP')
existingConversations.shift() // Remove the oldest conversation
try {
localStorage.setItem(
'conversationHistory',
JSON.stringify(existingConversations),
)
console.debug(
'SUCCESSFULLY SAVED CONVERSATION HISTORY AFTER FREEING SPACE',
)
break // Exit loop if setItem succeeds
} catch (error) {
continue // Try removing another conversation
Expand All @@ -89,35 +83,27 @@ export const saveConversations = (conversations: Conversation[]) => {
} catch (e) {
posthog.capture('local_storage_full', {
course_name:
conversations.length > 0 &&
conversations[conversations.length - 1].messages.length > 0 &&
conversations[conversations.length - 1].messages[0].contexts.length > 0
? conversations[conversations.length - 1].messages[0].contexts[0]
.course_name
: 'Unknown Course',
user_email: conversations[conversations.length - 1].user_email,
conversations?.slice(-1)[0]?.messages?.[0]?.contexts?.[0]
?.course_name || 'Unknown Course',
user_email: conversations?.slice(-1)[0]?.user_email || 'Unknown Email',
inSaveConversations: true,
})

let existingConversations = JSON.parse(
const existingConversations = JSON.parse(
localStorage.getItem('conversationHistory') || '[]',
)
while (
existingConversations.length > 0 &&
e instanceof DOMException &&
e.code === 22
) {
console.debug('INSIDE WHILE LOOP')
existingConversations.shift() // Remove the oldest conversation
try {
localStorage.setItem(
'conversationHistory',
JSON.stringify(existingConversations),
)
e = null // Clear the error since space has been freed
console.debug(
'SUCCESSFULLY SAVED CONVERSATION HISTORY AFTER FREEING SPACE',
)
} catch (error) {
e = error // Update the error if it fails again
continue // Try removing another conversation
Expand Down

0 comments on commit 2d731bf

Please sign in to comment.