File size: 1,365 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useCallback } from 'react';
import { useNotebookStore } from '@/store/notebookStore';
import { useAIChatStore } from '@/store/aiChatStore';

// Hook to open the Notebook panel with the AI tab and optionally load a specific conversation

export function useOpenAIInNotebook() {
  const { setNotebookVisible, setNotebookActiveTab } = useNotebookStore();
  const { setActiveConversation, createConversation } = useAIChatStore();

  const openAIInNotebook = useCallback(
    async (options?: {
      conversationId?: string;
      bookHash?: string;
      newConversationTitle?: string;
    }) => {
      // Open notebook and switch to AI tab
      setNotebookVisible(true);
      setNotebookActiveTab('ai');

      if (options?.conversationId) {
        // Load existing conversation
        await setActiveConversation(options.conversationId);
      } else if (options?.bookHash && options?.newConversationTitle) {
        // Create new conversation
        await createConversation(options.bookHash, options.newConversationTitle);
      }
    },
    [setNotebookVisible, setNotebookActiveTab, setActiveConversation, createConversation],
  );

  const closeAIInNotebook = useCallback(() => {
    setNotebookActiveTab('notes');
  }, [setNotebookActiveTab]);

  return {
    openAIInNotebook,
    closeAIInNotebook,
  };
}

export default useOpenAIInNotebook;