Files
kandev/apps/web/hooks/use-quick-chat-launcher.ts
T
2026-08-06 11:16:29 +01:00

44 lines
1.6 KiB
TypeScript

import { useCallback } from "react";
import { useAppStore } from "@/components/state-provider";
import { captureQuickChatLauncherFocus } from "@/components/quick-chat/quick-chat-focus";
import type { QuickChatSessionKind } from "@/lib/state/slices/ui/types";
/**
* Hook to handle opening quick chat.
* Just opens the modal - the user will select an agent from the picker.
*/
export function useQuickChatLauncher(
workspaceId?: string | null,
kind: QuickChatSessionKind = "chat",
) {
const openQuickChat = useAppStore((state) => state.openQuickChat);
const quickChatSessions = useAppStore((state) => state.quickChat.sessions);
const activeSessionId = useAppStore((state) => state.quickChat.activeSessionId);
const handleOpenQuickChat = useCallback(() => {
if (!workspaceId) return;
captureQuickChatLauncherFocus();
// If there's an existing session, open it. Otherwise just open the modal with agent picker
const matchingSessions = quickChatSessions.filter(
(session) => session.workspaceId === workspaceId && (session.kind ?? "chat") === kind,
);
const existingSession =
matchingSessions.find((session) => session.sessionId === activeSessionId) ??
matchingSessions[0];
if (existingSession) {
openQuickChat(
existingSession.sessionId,
workspaceId,
undefined,
existingSession.kind ?? "chat",
);
} else {
// Open modal without a session - will show agent picker
openQuickChat("", workspaceId, undefined, kind);
}
}, [workspaceId, kind, quickChatSessions, activeSessionId, openQuickChat]);
return handleOpenQuickChat;
}