Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Simplify CodeBase to avoid stupid complexity overhead (#267)
Browse files Browse the repository at this point in the history
* Feat [Utility] [Storage Helper] "debounce"

- [+] feat(storageHelper.ts): add debouncedSave function
- [+] refactor(storageHelper.ts): import debounce from lodash
- [+] feat(storageHelper.ts): add check for input starting with ChatCommandPrefix

* Refactor [UI/UX] [Front End] [Chat List] "debouncedSave"

- [+] refactor(chat.tsx): move debouncedSave function to storageHelper module
  • Loading branch information
H0llyW00dzZ authored Feb 8, 2024
1 parent fe2f09e commit 868bbff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 1 addition & 9 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import { getClientConfig } from "../config/client";
import { useAllModels } from "../utils/hooks";
import { appWindow } from '@tauri-apps/api/window';
import { sendDesktopNotification } from "../utils/taurinotification";
import { debouncedSave } from "../utils/storageHelper";

const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
loading: () => <LoadingIcon />,
Expand Down Expand Up @@ -1290,15 +1291,6 @@ function _Chat() {
// Define the key for storing unfinished input based on the session ID outside of the useEffect.
const key = UNFINISHED_INPUT(session.id);

// Initialize the debounced function outside of the useCallback.
const debouncedSave = debounce((input, key) => {
if (input && !input.startsWith(ChatCommandPrefix)) {
localStorage.setItem(key, input);
} else {
localStorage.removeItem(key);
}
}, 500);

// Define a function that calls the debounced function, wrapped in useCallback.
const saveUnfinishedInput = useCallback((input: string) => {
debouncedSave(input, key);
Expand Down
11 changes: 11 additions & 0 deletions app/utils/storageHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// easy maintain unlike stupid complexity
import { debounce } from "lodash";
import { UNFINISHED_INPUT } from "../constant";
import { ChatCommandPrefix } from "../command";

// Function to clear unfinished input from local storage for a given session ID
export const clearUnfinishedInputForSession = (sessionId: string) => {
const key = UNFINISHED_INPUT(sessionId);
localStorage.removeItem(key);
};

// Initialize the debounced function outside of the useCallback.
export const debouncedSave = debounce((input, key) => {
if (input && !input.startsWith(ChatCommandPrefix)) {
localStorage.setItem(key, input);
} else {
localStorage.removeItem(key);
}
}, 500);

0 comments on commit 868bbff

Please sign in to comment.