File size: 916 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useCallback, useState } from "react";
import { listChatSessions, type ChatSessionOut } from "@/lib/api/chatHistory";
import { listLocalChatSessions } from "./local-chat-history";

export function useChatSessions() {
  const [chatSessions, setChatSessions] = useState<ChatSessionOut[]>([]);
  const [isHistoryLoading, setIsHistoryLoading] = useState(false);
  const [activeSessionId, setActiveSessionId] = useState<string | null>(null);

  const refreshChatHistory = useCallback(async () => {
    setIsHistoryLoading(true);
    try {
      const sessions = await listChatSessions();
      setChatSessions(sessions);
    } catch {
      setChatSessions(listLocalChatSessions());
    } finally {
      setIsHistoryLoading(false);
    }
  }, []);

  return {
    chatSessions,
    setChatSessions,
    isHistoryLoading,
    activeSessionId,
    setActiveSessionId,
    refreshChatHistory,
  };
}