Skip to content

Commit

Permalink
Add open new chat command
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on committed Nov 1, 2024
1 parent b3264e2 commit 7670084
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ export class ChatView extends ItemView {
)
}

addSelectionToChat(data: MentionableBlockData) {
this.chatRef.current?.addSelectionToChat(data)
openNewChat(selectedBlock?: MentionableBlockData) {
this.chatRef.current?.openNewChat(selectedBlock)
}

addSelectionToChat(selectedBlock: MentionableBlockData) {
this.chatRef.current?.addSelectionToChat(selectedBlock)
}

focusMessage() {
Expand Down
28 changes: 22 additions & 6 deletions src/components/chat-view/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const getNewInputMessage = (app: App): ChatUserMessage => {
}

export type ChatRef = {
addSelectionToChat: (data: MentionableBlockData) => void
openNewChat: (selectedBlock?: MentionableBlockData) => void
addSelectionToChat: (selectedBlock: MentionableBlockData) => void
focusMessage: () => void
}

Expand Down Expand Up @@ -196,10 +197,23 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
}
}

const handleNewChat = async () => {
const handleNewChat = (selectedBlock?: MentionableBlockData) => {
setCurrentConversationId(uuidv4())
setChatMessages([])
const newInputMessage = getNewInputMessage(app)
if (selectedBlock) {
const mentionableBlock: MentionableBlock = {
type: 'block',
...selectedBlock,
}
newInputMessage.mentionables = [
...newInputMessage.mentionables,
mentionableBlock,
]
setAddedBlockKey(
getMentionableKey(serializeMentionable(mentionableBlock)),
)
}
setInputMessage(newInputMessage)
setFocusedMessageId(newInputMessage.id)
setQueryProgress({
Expand Down Expand Up @@ -432,10 +446,12 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
}, [app.workspace, handleActiveLeafChange])

useImperativeHandle(ref, () => ({
addSelectionToChat(data: MentionableBlockData) {
openNewChat: (selectedBlock?: MentionableBlockData) =>
handleNewChat(selectedBlock),
addSelectionToChat: (selectedBlock: MentionableBlockData) => {
const mentionable: Omit<MentionableBlock, 'id'> = {
type: 'block',
...data,
...selectedBlock,
}

setAddedBlockKey(getMentionableKey(serializeMentionable(mentionable)))
Expand Down Expand Up @@ -470,7 +486,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
<h1 className="smtcmp-chat-header-title">Chat</h1>
<div className="smtcmp-chat-header-buttons">
<button
onClick={() => void handleNewChat()}
onClick={() => handleNewChat()}
className="smtcmp-chat-list-dropdown"
>
<Plus size={18} />
Expand All @@ -489,7 +505,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
if (nextConversation) {
void handleLoadConversation(nextConversation.id)
} else {
void handleNewChat()
handleNewChat()
}
}
}}
Expand Down
25 changes: 23 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export default class SmartCopilotPlugin extends Plugin {

// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-chat-view',
id: 'open-new-chat',
name: 'Open chat',
callback: () => this.openChatView(),
callback: () => this.openNewChat(),
})

this.addCommand({
Expand Down Expand Up @@ -158,6 +158,27 @@ export default class SmartCopilotPlugin extends Plugin {
})
}

async openNewChat() {
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
const editor = view?.editor
if (!view || !editor) {
this.activateChatView()
return
}

const selectedBlock = await getMentionableBlockData(editor, view)
const leaves = this.app.workspace.getLeavesOfType(CHAT_VIEW_TYPE)
if (leaves.length === 0) {
await this.activateChatView({
selectedBlock: selectedBlock ?? undefined,
})
} else {
const chatView = this.app.workspace.getLeavesOfType(CHAT_VIEW_TYPE)[0]
.view as ChatView
chatView.openNewChat(selectedBlock ?? undefined)
}
}

async activateChatView(chatProps?: ChatProps) {
// chatProps is consumed in ChatView.tsx
this.initialChatProps = chatProps
Expand Down

0 comments on commit 7670084

Please sign in to comment.