refactor(react/launch_bar): port ai chat button

This commit is contained in:
Elian Doran 2025-12-04 15:47:42 +02:00
parent 8efb849391
commit 991f07e148
No known key found for this signature in database
3 changed files with 20 additions and 28 deletions

View File

@ -1,26 +0,0 @@
import type { EventData } from "../../components/app_context.js";
import type FNote from "../../entities/fnote.js";
import options from "../../services/options.js";
import CommandButtonWidget from "./command_button.js";
export default class AiChatButton extends CommandButtonWidget {
constructor(note: FNote) {
super();
this.command("createAiChat")
.title(() => note.title)
.icon(() => note.getIcon())
.class("launcher-button");
}
isEnabled() {
return options.get("aiEnabled") === "true";
}
entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (loadResults.isOptionReloaded("aiEnabled")) {
this.refresh();
}
}
}

View File

@ -10,10 +10,10 @@ import TodayLauncher from "../buttons/launcher/today_launcher.js";
import QuickSearchLauncherWidget from "../quick_search_launcher.js"; import QuickSearchLauncherWidget from "../quick_search_launcher.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import type { CommandNames } from "../../components/app_context.js"; import type { CommandNames } from "../../components/app_context.js";
import AiChatButton from "../buttons/ai_chat_button.js";
import BookmarkButtons from "../launch_bar/BookmarkButtons.jsx"; import BookmarkButtons from "../launch_bar/BookmarkButtons.jsx";
import SpacerWidget from "../launch_bar/SpacerWidget.jsx"; import SpacerWidget from "../launch_bar/SpacerWidget.jsx";
import HistoryNavigationButton from "../launch_bar/HistoryNavigation.jsx"; import HistoryNavigationButton from "../launch_bar/HistoryNavigation.jsx";
import AiChatButton from "../launch_bar/AiChatButton.jsx";
interface InnerWidget extends BasicWidget { interface InnerWidget extends BasicWidget {
settings?: { settings?: {
@ -125,7 +125,7 @@ export default class LauncherWidget extends BasicWidget {
case "quickSearch": case "quickSearch":
return new QuickSearchLauncherWidget(this.isHorizontalLayout); return new QuickSearchLauncherWidget(this.isHorizontalLayout);
case "aiChatLauncher": case "aiChatLauncher":
return new AiChatButton(note); return <AiChatButton launcherNote={note} />
default: default:
throw new Error(`Unrecognized builtin widget ${builtinWidget} for launcher ${note.noteId} "${note.title}"`); throw new Error(`Unrecognized builtin widget ${builtinWidget} for launcher ${note.noteId} "${note.title}"`);
} }

View File

@ -0,0 +1,18 @@
import FNote from "../../entities/fnote";
import { escapeHtml } from "../../services/utils";
import { useNoteLabel, useNoteProperty, useTriliumOptionBool } from "../react/hooks";
import { LaunchBarActionButton } from "./launch_bar_widgets";
export default function AiChatButton({ launcherNote }: { launcherNote: FNote }) {
const [ aiEnabled ] = useTriliumOptionBool("aiEnabled");
const [ iconClass ] = useNoteLabel(launcherNote, "iconClass");
const title = useNoteProperty(launcherNote, "title");
return aiEnabled && iconClass && title && (
<LaunchBarActionButton
icon={iconClass}
text={escapeHtml(title)}
triggerCommand="createAiChat"
/>
)
}