Download src/hooks/useChatPage.ts from IOE-IFAD/test: direct link, hf CLI and curl.
- Browser
- Download file 2.32 kB
-
https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/hooks/useChatPage.ts
- Command line
-
hf download hf://spaces/IOE-IFAD/test/src/hooks/useChatPage.ts
-
curl -L -o useChatPage.ts https://huggingface.co/spaces/IOE-IFAD/test/resolve/main/src/hooks/useChatPage.ts
2.32 kB
| import { useState, useEffect } from "react"; | |
| import { useChatStore } from "@/stores/chatStore"; | |
| import { getTranscripts } from "@/lib/api"; | |
| import { useScrollToBottom } from "@/hooks/useScrollToBottom"; | |
| import { CHAT_UPDATED_EVENT, chatEventEmitter } from "@/lib/chatEventEmitter"; | |
| import type { Transcript } from "@/lib/types"; | |
| export function useChatPage() { | |
| const [selectedTranscripts, setSelectedTranscripts] = useState<Transcript[]>( | |
| [] | |
| ); | |
| const [selectedTranscript, setSelectedTranscript] = | |
| useState<Transcript | null>(null); | |
| const [isLoading, setIsLoading] = useState<boolean>(false); | |
| const [evaluatingMessageIndex, setEvaluatingMessageIndex] = useState< | |
| number | null | |
| >(null); | |
| const [hasScrolledToBottom, setHasScrolledToBottom] = useState(false); | |
| const [isTranscriptsOpen, setIsTranscriptsOpen] = useState<boolean>(false); | |
| const { selectedChat, selectedChatId } = useChatStore(); | |
| const { scrollRef, scrollToBottom } = useScrollToBottom(); | |
| const handleTranscriptClick = async (transcript: Transcript) => { | |
| if (!transcript) return; | |
| setSelectedTranscript(transcript); | |
| if (transcript.filename !== selectedTranscript?.filename) { | |
| try { | |
| const data = await getTranscripts(transcript.filename); | |
| setSelectedTranscripts(data); | |
| setIsTranscriptsOpen(true); | |
| } catch (error) { | |
| console.error("Error loading transcripts:", error); | |
| } | |
| } | |
| }; | |
| const handleEvaluate = (messageIndex: number) => { | |
| setEvaluatingMessageIndex(messageIndex); | |
| }; | |
| useEffect(() => { | |
| if (!hasScrolledToBottom) { | |
| scrollToBottom(); | |
| setHasScrolledToBottom(true); | |
| } | |
| const handleChatUpdate = () => { | |
| if (!hasScrolledToBottom) { | |
| scrollToBottom(); | |
| setHasScrolledToBottom(true); | |
| } | |
| }; | |
| chatEventEmitter.on(CHAT_UPDATED_EVENT, handleChatUpdate); | |
| return () => { | |
| chatEventEmitter.off(CHAT_UPDATED_EVENT, handleChatUpdate); | |
| }; | |
| }, [scrollToBottom, hasScrolledToBottom]); | |
| return { | |
| selectedTranscripts, | |
| selectedTranscript, | |
| isLoading, | |
| evaluatingMessageIndex, | |
| setEvaluatingMessageIndex, | |
| selectedChat, | |
| selectedChatId, | |
| scrollRef, | |
| scrollToBottom, | |
| handleTranscriptClick, | |
| handleEvaluate, | |
| isTranscriptsOpen, | |
| setIsTranscriptsOpen, | |
| }; | |
| } | |