logic-hooks.ts


Overview

logic-hooks.ts is a utility and state management file primarily designed for a React-based frontend application. It provides a collection of custom React hooks that encapsulate common logic related to:

This file serves as a centralized place to handle complex UI and interaction logic related to chat conversations and user settings, enhancing code reuse and maintainability.


Detailed Documentation

Custom Hooks


usePrevious<T>(value: T): T | undefined


useSetSelectedRecord<T = IKnowledgeFile>()


useChangeLanguage()


useGetPaginationWithRouter()


useHandleSearchChange()


useGetPagination()


useFetchAppConf()


useSetDoneRecord()


useSendMessageWithSse(url?: string)


useSpeechWithSse(url?: string)


useScrollToBottom(messages?, containerRef?)


useHandleMessageInputChange()


useSelectDerivedMessages()


useRemoveMessagesAfterCurrentMessage(setCurrentConversation)


useRegenerateMessage({ removeMessagesAfterCurrentMessage, sendMessage, messages })


useSelectItem(defaultId?: string)


useFetchModelId()


useHandleChunkMethodSelectChange(form: FormInstance)


useResetFormOnCloseModal({ form, visible })


Important Implementation Details


Interaction with Other System Parts


Visual Diagram: Class/Hook Structure Flowchart

flowchart TD
    A[usePrevious<T>] -->|Tracks| B[Previous value]
    C[useSetSelectedRecord] -->|Manages| D[Selected record]
    E[useChangeLanguage] -->|Changes & Saves| F[Language setting]
    G[useGetPaginationWithRouter] -->|Syncs| H[Pagination with Router]
    I[useGetPagination] -->|Local| J[Pagination state]
    K[useHandleSearchChange] -->|Updates| L[Search input & resets pagination]
    M[useFetchAppConf] -->|Fetches| N[App configuration]
    O[useSetDoneRecord] -->|Tracks| P[Async completion flags]
    Q[useSendMessageWithSse] -->|Handles| R[SSE streaming chat messages]
    S[useSpeechWithSse] -->|Sends| T[TTS requests]
    U[useScrollToBottom] -->|Manages| V[Scrolling behavior]
    W[useHandleMessageInputChange] -->|Handles| X[Message input changes]
    Y[useSelectDerivedMessages] -->|Manages| Z[Derived chat messages list]
    AA[useRemoveMessagesAfterCurrentMessage] -->|Removes| AB[Messages after given ID]
    AC[useRegenerateMessage] -->|Regenerates| AD[Message by resending]
    AE[useSelectItem] -->|Manages| AF[Selected item state]
    AG[useFetchModelId] -->|Fetches| AH[Tenant model ID]
    AI[useHandleChunkMethodSelectChange] -->|Updates| AJ[Form chunk token count]
    AK[useResetFormOnCloseModal] -->|Resets| AL[Form on modal close]

    style Q fill:#f9f,stroke:#333,stroke-width:1px
    style Y fill:#bbf,stroke:#333,stroke-width:1px

Summary

The logic-hooks.ts file is a comprehensive collection of React hooks that provide modular, reusable logic for managing chat conversations, pagination, language settings, SSE streaming, form handling, and UI behaviors. It integrates tightly with backend APIs and frontend state management, facilitating complex chat workflows with streaming responses, message regeneration, and user interaction management. This modular approach simplifies component code and enhances maintainability across the chat application.