refactor: extract and reuse focusInput logic across components

Move focus logic to exported method and trigger it after conversation
resets to maintain input focus consistently throughout the UI flow.
This commit is contained in:
Andrew Beal 2025-10-09 21:34:40 +01:00
parent 9a34634008
commit 9cbedb6b53
4 changed files with 18 additions and 6 deletions

View file

@ -40,6 +40,12 @@
let currentThought: string | null = null;
export function focusInput() {
tick().then(() => {
textareaElement?.focus();
});
}
onMount(() => {
if (chatContainer) {
plugin.registerDomEvent(chatContainer, 'click', handleLinkClick);
@ -76,6 +82,7 @@
}
async function handleSubmit() {
focusInput();
if (!await semaphore.wait()) {
return;
}
@ -121,9 +128,6 @@
currentThought = null;
isSubmitting = false;
semaphore.release();
tick().then(() => {
textareaElement?.focus();
});
}
}

View file

@ -9,6 +9,7 @@
import { openPluginSettings } from 'Helpers/Helpers';
export let leaf: WorkspaceLeaf;
export let onNewConversation: (() => void) | undefined = undefined;
const plugin = Resolve<AIAgentPlugin>(Services.AIAgentPlugin);
const conversationService = Resolve<ConversationFileSystemService>(Services.ConversationFileSystemService);
@ -16,15 +17,18 @@
function startNewConversation() {
conversationService.resetCurrentConversation();
conversationStore.reset();
onNewConversation?.();
}
async function deleteCurrentConversation() {
await conversationService.deleteCurrentConversation();
conversationStore.reset();
onNewConversation?.();
}
function openConversationHistory() {
const modal = Resolve<ConversationHistoryModal>(Services.ConversationHistoryModal);
modal.onModalClose = onNewConversation;
modal.open();
}

View file

@ -25,6 +25,7 @@ export class ConversationHistoryModal extends Modal {
private component: Record<string, any> | null = null;
private items: ListItem[];
private conversations: Conversation[];
public onModalClose?: () => void;
constructor(app: App) {
super(app);
@ -107,5 +108,7 @@ export class ConversationHistoryModal extends Modal {
const { contentEl } = this;
contentEl.empty();
this.onModalClose?.();
}
}

View file

@ -25,15 +25,16 @@ export class MainView extends ItemView {
const container = this.contentEl;
container.empty();
// Mount TopBar
// Mount TopBar with reference to ChatWindow's focus function
this.topBar = mount(TopBar, {
target: container,
props: {
leaf: this.leaf
leaf: this.leaf,
onNewConversation: () => this.input?.focusInput()
}
});
// Mount ChatWindow
// Mount ChatWindow first
this.input = mount(ChatWindow, {
target: container,
props: {}