This repository was archived by the owner on Sep 15, 2024. It is now read-only.
forked from ChatGPTNextWeb/NextChat
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BOT] Reorganizing Codebase Without Disrupting Functionality (#293)
- [+] refactor(chat.tsx): extract usePinApp and useDebouncedEffect functions to separate files
- Loading branch information
1 parent
d56af54
commit 0a5488e
Showing
3 changed files
with
85 additions
and
76 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useCallback } from "react"; | ||
import { debounce } from "lodash"; | ||
|
||
// Custom hook for debouncing a function | ||
export function useDebouncedEffect(effect: () => void, deps: any[], delay: number) { | ||
// Include `effect` in the dependency array for `useCallback` | ||
const callback = useCallback(effect, [effect, ...deps]); | ||
|
||
useEffect(() => { | ||
const handler = debounce(callback, delay); | ||
|
||
handler(); | ||
|
||
// Cleanup function to cancel the debounced call if the component unmounts | ||
return () => handler.cancel(); | ||
}, [callback, delay]); // `callback` already includes `effect` in its dependencies, so no need to add it here again. | ||
} |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
import { useChatStore, useAppConfig } from "../store"; | ||
import Locale from "../locales"; | ||
import { showToast } from "./ui-lib"; | ||
import { getClientConfig } from "../config/client"; | ||
import { appWindow } from '@tauri-apps/api/window'; | ||
import { sendDesktopNotification } from "../utils/taurinotification"; | ||
|
||
export function usePinApp(sessionId: string) { | ||
const [pinApp, setPinApp] = useState(false); | ||
const isApp = getClientConfig()?.isApp; | ||
const config = useAppConfig(); | ||
const TauriShortcut = config.desktopShortcut; | ||
const chatStore = useChatStore(); | ||
const session = chatStore.currentSession(); | ||
|
||
const togglePinApp = useCallback(async () => { | ||
if (!isApp) { | ||
return; | ||
} | ||
|
||
if (pinApp) { | ||
await appWindow.setAlwaysOnTop(false); | ||
sendDesktopNotification(Locale.Chat.Actions.PinAppContent.UnPinned); | ||
showToast(Locale.Chat.Actions.PinAppContent.UnPinned); | ||
} else { | ||
await appWindow.setAlwaysOnTop(true); | ||
sendDesktopNotification(Locale.Chat.Actions.PinAppContent.Pinned); | ||
showToast(Locale.Chat.Actions.PinAppContent.Pinned); | ||
} | ||
setPinApp((prevPinApp) => !prevPinApp); | ||
}, [isApp, pinApp]); | ||
|
||
useEffect(() => { | ||
const handleKeyPress = (event: KeyboardEvent) => { | ||
if (event.key === TauriShortcut) { | ||
togglePinApp(); | ||
} | ||
}; | ||
// Usage : Mouse+5,Mouse+4,Mouse+1(Middle Click) | ||
// You need to copy-paste (e.g., Mouse+5 paste in settings) instead of typing manually in settings | ||
const handleMouseClick = (event: MouseEvent) => { | ||
if (event.button === 1 || event.button === 4 || event.button === 5) { | ||
togglePinApp(); | ||
} | ||
}; | ||
|
||
document.addEventListener("keydown", handleKeyPress); | ||
document.addEventListener("mousedown", handleMouseClick); | ||
|
||
return () => { | ||
document.removeEventListener("keydown", handleKeyPress); | ||
document.removeEventListener("mousedown", handleMouseClick); | ||
}; | ||
}, [TauriShortcut, togglePinApp]); | ||
// Reset pinApp when the session changes | ||
useEffect(() => { | ||
setPinApp(false); | ||
}, [sessionId]); // Listen for changes to sessionId | ||
|
||
return { | ||
pinApp: isApp ? pinApp : false, | ||
togglePinApp: isApp ? togglePinApp : () => { }, | ||
}; | ||
} |