From da3e10cf04693505d81be20c89ea5afd8d5c9a74 Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Thu, 4 Jul 2024 16:48:06 +0800 Subject: [PATCH] feat: add new topic use keyboard shortcut --- .../pages/home/components/Chat/Inputbar.tsx | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/pages/home/components/Chat/Inputbar.tsx b/src/renderer/src/pages/home/components/Chat/Inputbar.tsx index 3b7bd155..42cdbda1 100644 --- a/src/renderer/src/pages/home/components/Chat/Inputbar.tsx +++ b/src/renderer/src/pages/home/components/Chat/Inputbar.tsx @@ -1,13 +1,14 @@ import { EVENT_NAMES, EventEmitter } from '@renderer/services/event' import { Assistant, Message, Topic } from '@renderer/types' import { uuid } from '@renderer/utils' -import { FC, useState } from 'react' +import { FC, useCallback, useEffect, useRef, useState } from 'react' import styled from 'styled-components' import { MoreOutlined } from '@ant-design/icons' import { Button, Popconfirm, Tooltip } from 'antd' import { useShowRightSidebar } from '@renderer/hooks/useStore' import { useAssistant } from '@renderer/hooks/useAssistant' import { ClearOutlined, HistoryOutlined, PlusCircleOutlined } from '@ant-design/icons' +import { TextAreaRef } from 'antd/es/input/TextArea' interface Props { assistant: Assistant @@ -18,6 +19,7 @@ const Inputbar: FC = ({ assistant, setActiveTopic }) => { const [text, setText] = useState('') const { setShowRightSidebar } = useShowRightSidebar() const { addTopic } = useAssistant(assistant.id) + const inputRef = useRef(null) const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { @@ -39,7 +41,7 @@ const Inputbar: FC = ({ assistant, setActiveTopic }) => { } } - const addNewConversation = () => { + const addNewTopic = useCallback(() => { const topic: Topic = { id: uuid(), name: 'Default Topic', @@ -47,18 +49,34 @@ const Inputbar: FC = ({ assistant, setActiveTopic }) => { } addTopic(topic) setActiveTopic(topic) - } + }, [addTopic, setActiveTopic]) const clearTopic = () => { EventEmitter.emit(EVENT_NAMES.CLEAR_CONVERSATION) } + // Command or Ctrl + N create new topic + useEffect(() => { + const onKeydown = (e) => { + if ((e.ctrlKey || e.metaKey) && e.key === 'n') { + addNewTopic() + inputRef.current?.focus() + } + } + document.addEventListener('keydown', onKeydown) + return () => document.removeEventListener('keydown', onKeydown) + }, [addNewTopic]) + + useEffect(() => { + inputRef.current?.focus() + }, [assistant]) + return ( - + @@ -97,6 +115,7 @@ const Inputbar: FC = ({ assistant, setActiveTopic }) => { placeholder="Type your message here..." autoFocus contextMenu="true" + ref={inputRef} /> )