diff --git a/main.ts b/main.ts index c1a24bb..5cb0564 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ import { App, Plugin, PluginSettingTab, Setting } from 'obsidian'; -import { AppView, VIEW_TYPE } from './src/ui/AppView'; +import { AppView, OBSIDIAN_INTELLIGENCE_VIEW_TYPE } from './src/ui/AppView'; import OpenAI from 'openai'; import { IThread } from './src/ui/types'; @@ -25,7 +25,7 @@ export default class ObsidianIntelligence extends Plugin { async onload() { await this.loadSettings(); - this.registerView(VIEW_TYPE, (leaf) => new AppView(leaf, this)); + this.registerView(OBSIDIAN_INTELLIGENCE_VIEW_TYPE, (leaf) => new AppView(leaf, this)); const ribbonIconEl = this.addRibbonIcon( 'bot', @@ -68,15 +68,18 @@ export default class ObsidianIntelligence extends Plugin { } async activateView() { - this.app.workspace.detachLeavesOfType(VIEW_TYPE); + this.app.workspace.detachLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE); await this.app.workspace.getRightLeaf(false).setViewState({ - type: VIEW_TYPE, + type: OBSIDIAN_INTELLIGENCE_VIEW_TYPE, active: true, }); + this.revealView(); + } + async revealView() { this.app.workspace.revealLeaf( - this.app.workspace.getLeavesOfType(VIEW_TYPE)[0], + this.app.workspace.getLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE)[0], ); } } diff --git a/src/ui/AppView.tsx b/src/ui/AppView.tsx index 4a5d9bf..b13c858 100644 --- a/src/ui/AppView.tsx +++ b/src/ui/AppView.tsx @@ -6,7 +6,9 @@ import { App } from 'obsidian'; import ObsidianIntelligence from '../../main'; import OpenAI from 'openai'; -export const VIEW_TYPE = 'example-view'; +export const OBSIDIAN_INTELLIGENCE_VIEW_TYPE = 'obsidian-intelligence-view'; + + export const AppContext = React.createContext(undefined); @@ -53,7 +55,7 @@ export class AppView extends ItemView { } getViewType() { - return VIEW_TYPE; + return OBSIDIAN_INTELLIGENCE_VIEW_TYPE; } getDisplayText() { diff --git a/src/ui/components/AssistantManager.tsx b/src/ui/components/AssistantManager.tsx index 905e444..891dcc2 100644 --- a/src/ui/components/AssistantManager.tsx +++ b/src/ui/components/AssistantManager.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useMemo } from 'react'; import OpenAI from 'openai'; -import { useApp, useOpenAI, usePlugin } from '../AppView'; +import { OBSIDIAN_INTELLIGENCE_VIEW_TYPE, useApp, useOpenAI, usePlugin } from '../AppView'; import { IThread } from '../types'; import DropdownSelect from './DropdownSelect'; import { MarkdownView } from 'obsidian'; @@ -96,9 +96,27 @@ const AssistantManager = ({ } }, }); + + plugin.addCommand({ + id: 'create-assistant-from-active-note', + name: 'Create Thread', + callback: async () => { + const isViewOpen = app.workspace.getLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE).some((leaf) => { + return leaf.view; + }); + if (!isViewOpen) { + plugin.activateView(); + } + + plugin.revealView(); + createThread(); + } + }); }, [plugin]); - useEffect(() => {}, [plugin]); + useEffect(() => { + console.log('threads update', threads); + }, [threads]); const createThread = async () => { if (!openaiInstance || !plugin) { @@ -120,9 +138,8 @@ const AssistantManager = ({ name: newThreadName, }, }; - updateThreads([...threads, newThread]); + updateThreads([...plugin.settings.threads, newThread]); updateActiveThread(newThread); - plugin.saveSettings(); }); };