hooks.ts


Overview

hooks.ts provides a comprehensive collection of custom React hooks designed to manage chat-related state, routing parameters, user interactions, and API interactions within a chat application. These hooks abstract complex logic such as fetching, updating, and manipulating conversations and dialogs, handling message lifecycle events, modal visibility, and UI state management around chat dialogs and conversations.

The file integrates multiple external hooks and utilities, including React Query for async state management, URL search parameters for routing, and SSE for streaming message responses. It acts as a central place for chat UI logic and state orchestration, promoting modularity and reusability across the chat system.


Detailed Documentation

Key Concepts and Entities


Exports and Their Functionality

1. useSetChatRouteParams

Purpose:
Manage chat-related URL search parameters, specifically the isNew flag indicating if a conversation is new.

API:

Usage Example:

const { setConversationIsNew, getConversationIsNew } = useSetChatRouteParams();
setConversationIsNew('true');
const isNew = getConversationIsNew();

2. useSetNewConversationRouteParams

Purpose:
Set multiple chat route parameters at once, specifically ConversationId and isNew.

API:


3. useSelectCurrentDialog

Purpose:
Select the current dialog from mutation state keyed by 'fetchDialog'.

Returns:


4. useSelectPromptConfigParameters

Purpose:
Extracts and maps prompt configuration parameters from the current dialog for UI display or editing.

Returns:

Important Details:


5. useDeleteDialog

Purpose:
Provides a handler to delete dialogs with a confirmation modal.

API:


6. useHandleItemHover

Purpose:
Manages hover state for UI items by tracking an activated ID.

API:


7. useEditDialog

Purpose:
Manages state and modal visibility for editing dialogs.

API:


8. useFindPrologueFromDialogList

Purpose:
Retrieves the prologue text from the currently selected dialog list.

Returns:


9. useSelectDerivedConversationList

Purpose:
Manages a list of conversations derived from the fetched conversation list, allowing temporary conversation insertion (e.g., for new chats).

API:


10. useSetConversation

Purpose:
Updates or creates a conversation via API, binding it to the current dialog.

API:


11. useSelectNextMessages

Purpose:
Manages message state for the current conversation, including adding, removing, and initializing messages (including prologue for new chats).

Returns:


12. useHandleMessageInputChange

Purpose:
Manages controlled input state for the chat message textarea.

Returns:


13. useSendNextMessage

Purpose:
Manages sending messages with Server-Sent Events (SSE) support, handling new or existing conversations, streaming answers, and message lifecycle.

Parameters:

Returns:

Important Implementation Details:


14. useGetFileIcon

Purpose:
Returns a function that maps a filename's extension to an SVG icon path.

API:


15. useDeleteConversation

Purpose:
Provides a handler for deleting conversations with confirmation modal.

API:


16. useRenameConversation

Purpose:
Manages state and modal visibility for renaming conversations.

API:


17. useGetSendButtonDisabled

Purpose:
Returns a boolean indicating if the send button should be disabled based on current route parameters.


18. useSendButtonDisabled

Purpose:
Returns a boolean indicating if the send button should be disabled based on current input value (empty or whitespace).


19. useCreateConversationBeforeUploadDocument

Purpose:
Creates a new conversation if needed before uploading a document, based on route parameters.

API:


Important Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

classDiagram
    class useSetChatRouteParams {
        +setConversationIsNew(value: string): void
        +getConversationIsNew(): string | null
    }
    class useSetNewConversationRouteParams {
        +setNewConversationRouteParams(conversationId: string, isNew: string): void
    }
    class useSelectCurrentDialog {
        +(): IDialog
    }
    class useSelectPromptConfigParameters {
        +(): VariableTableDataType[]
    }
    class useDeleteDialog {
        +onRemoveDialog(dialogIds: string[]): void
    }
    class useHandleItemHover {
        +activated: string
        +handleItemEnter(id: string): void
        +handleItemLeave(): void
    }
    class useEditDialog {
        +dialogSettingLoading: boolean
        +initialDialog: IDialog
        +onDialogEditOk(dialog: IDialog): Promise<void>
        +dialogEditVisible: boolean
        +hideDialogEditModal(): void
        +showDialogEditModal(dialogId?: string): Promise<void>
        +clearDialog(): void
    }
    class useSelectDerivedConversationList {
        +list: IConversation[]
        +addTemporaryConversation(): void
        +loading: boolean
    }
    class useSetConversation {
        +setConversation(message: string, isNew?: boolean, conversationId?: string): Promise<any>
    }
    class useSelectNextMessages {
        +scrollRef
        +messageContainerRef
        +derivedMessages: IMessage[]
        +loading: boolean
        +addNewestAnswer()
        +addNewestQuestion()
        +removeLatestMessage()
        +removeMessageById()
        +removeMessagesAfterCurrentMessage()
    }
    class useHandleMessageInputChange {
        +handleInputChange()
        +value: string
        +setValue(value: string): void
    }
    class useSendNextMessage {
        +handlePressEnter(documentIds: string[]): void
        +handleInputChange()
        +value: string
        +setValue(value: string): void
        +regenerateMessage()
        +sendLoading: boolean
        +loading: boolean
        +scrollRef
        +messageContainerRef
        +derivedMessages: IMessage[]
        +removeMessageById()
        +stopOutputMessage()
    }
    class useGetFileIcon {
        +getFileIcon(filename: string): string
    }
    class useDeleteConversation {
        +onRemoveConversation(conversationIds: string[]): void
    }
    class useRenameConversation {
        +conversationRenameLoading: boolean
        +initialConversationName: string
        +onConversationRenameOk(name: string): Promise<void>
        +conversationRenameVisible: boolean
        +hideConversationRenameModal(): void
        +showConversationRenameModal(conversationId: string): Promise<void>
    }
    class useGetSendButtonDisabled {
        +(): boolean
    }
    class useSendButtonDisabled {
        +value: string -> boolean
    }
    class useCreateConversationBeforeUploadDocument {
        +createConversationBeforeUploadDocument(message: string): Promise<any>
        +dialogId: string
    }

    useSendNextMessage --> useSetConversation : uses
    useSendNextMessage --> useSelectNextMessages : uses
    useSendNextMessage --> useHandleMessageInputChange : uses
    useSendNextMessage --> useSetChatRouteParams : uses

    useEditDialog --> useFetchManualDialog : fetchDialog
    useEditDialog --> useSetNextDialog : submitDialog
    useDeleteDialog --> useRemoveNextDialog : removeDialog
    useDeleteConversation --> useRemoveNextConversation : removeConversation
    useRenameConversation --> useFetchManualConversation : fetchConversation
    useRenameConversation --> useUpdateNextConversation : updateConversation

    useSelectDerivedConversationList --> useFetchNextConversationList : fetch conversation list
    useSelectDerivedConversationList --> useSetNewConversationRouteParams : setNewConversationRouteParams
    useSelectDerivedConversationList --> useFindPrologueFromDialogList : prologue

    useFindPrologueFromDialogList --> useFetchNextDialogList : fetch dialog list

Summary

This file is a crucial layer in the chat application that encapsulates the complex state management, UI interaction, and API communication logic related to chat dialogs and conversations. It decouples data fetching, mutation, and UI state concerns into reusable hooks, enabling clean, maintainable, and scalable chat features. The integration of URL parameters, SSE streaming, and modal controls provides a responsive and intuitive user experience.


End of Documentation for hooks.ts