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

[BOT] Reorganizing Codebase Without Disrupting Functionality #293

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 3 additions & 76 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import RobotIcon from "../icons/robot.svg";
import ChatGptIcon from "../icons/chatgpt.png";
import EyeOnIcon from "../icons/eye.svg";
import EyeOffIcon from "../icons/eye-off.svg";
import { debounce, escapeRegExp } from "lodash";
import { escapeRegExp } from "lodash";
import CloseIcon from "../icons/close.svg";

import {
Expand Down Expand Up @@ -104,9 +104,9 @@ import { prettyObject } from "../utils/format";
import { ExportMessageModal } from "./exporter";
import { getClientConfig } from "../config/client";
import { useAllModels } from "../utils/hooks";
import { appWindow } from '@tauri-apps/api/window';
import { sendDesktopNotification } from "../utils/taurinotification";
import { clearUnfinishedInputForSession, debouncedSave } from "../utils/storageHelper";
import { usePinApp } from "./usePinApp";
import { useDebouncedEffect } from "./useDebouncedEffect";
import { MultimodalContent } from "../client/api";
import Image from 'next/image';

Expand Down Expand Up @@ -764,79 +764,6 @@ export function EditMessageModal(props: { onClose: () => void }) {
);
}

function usePinApp(sessionId: string) { // Accept sessionId as a parameter
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 : () => { },
};
}

// Custom hook for debouncing a function
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.
}

export function DeleteImageButton(props: { deleteImage: () => void }) {
return (
<div
Expand Down
17 changes: 17 additions & 0 deletions app/components/useDebouncedEffect.tsx
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.
}
65 changes: 65 additions & 0 deletions app/components/usePinApp.tsx
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 : () => { },
};
}
Loading