mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 02:10:15 +01:00
🏄♂️ fix: Handle SSE Stream Edge Case (#8556)
* refactor: Move draft-related utilities to a new `drafts.ts` file * refactor: auto-save draft logic to use new get/set functions * fix: Ensure `getDraft` properly decodes stored draft values * fix: Handle edge case where stream is cancelled before any response, which creates a blank page
This commit is contained in:
parent
f70e0cf849
commit
4c754c1190
4 changed files with 87 additions and 56 deletions
39
client/src/utils/drafts.ts
Normal file
39
client/src/utils/drafts.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import debounce from 'lodash/debounce';
|
||||
import { LocalStorageKeys } from 'librechat-data-provider';
|
||||
|
||||
export const clearDraft = debounce((id?: string | null) => {
|
||||
localStorage.removeItem(`${LocalStorageKeys.TEXT_DRAFT}${id ?? ''}`);
|
||||
}, 2500);
|
||||
|
||||
export const encodeBase64 = (plainText: string): string => {
|
||||
try {
|
||||
const textBytes = new TextEncoder().encode(plainText);
|
||||
return btoa(String.fromCharCode(...textBytes));
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
export const decodeBase64 = (base64String: string): string => {
|
||||
try {
|
||||
const bytes = atob(base64String);
|
||||
const uint8Array = new Uint8Array(bytes.length);
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
uint8Array[i] = bytes.charCodeAt(i);
|
||||
}
|
||||
return new TextDecoder().decode(uint8Array);
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
export const setDraft = ({ id, value }: { id: string; value?: string }) => {
|
||||
if (value && value.length > 1) {
|
||||
localStorage.setItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`, encodeBase64(value));
|
||||
return;
|
||||
}
|
||||
localStorage.removeItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`);
|
||||
};
|
||||
|
||||
export const getDraft = (id?: string): string | null =>
|
||||
decodeBase64((localStorage.getItem(`${LocalStorageKeys.TEXT_DRAFT}${id ?? ''}`) ?? '') || '');
|
||||
Loading…
Add table
Add a link
Reference in a new issue