use-set-chat-route.ts
Overview
The use-set-chat-route.ts file defines a custom React hook named useSetChatRouteParams that provides an interface for reading and updating specific chat-related query parameters in the URL. Its primary purpose is to manage the URL search parameter indicating whether a chat conversation is new (isNew), allowing components to easily get or set this state in the route.
This hook leverages React's hooks (useCallback, useMemo) for memoization and the useSearchParams hook from the umi routing library to read and write URL query parameters in a React-friendly manner.
Detailed Explanation
useSetChatRouteParams()
Description
A custom React hook that encapsulates logic to get and set the isNew chat conversation status in the browser's URL query parameters.
This hook abstracts away direct URL manipulation, providing two methods:
setConversationIsNew(value: string): Sets theisNewparameter in the URL to the given string value.getConversationIsNew(): string | null: Retrieves the current value of theisNewparameter from the URL. Returnsnullif not present.
This design ensures that components using this hook can remain declarative and focused on UI logic without managing URL intricacies.
Usage Example
import React from 'react';
import { useSetChatRouteParams } from './use-set-chat-route';
import { ChatSearchParams } from '@/constants/chat';
const ChatComponent = () => {
const { getConversationIsNew, setConversationIsNew } = useSetChatRouteParams();
React.useEffect(() => {
const isNew = getConversationIsNew();
if (isNew === 'true') {
// Handle new conversation logic here
console.log('This is a new conversation');
}
}, [getConversationIsNew]);
const startNewConversation = () => {
setConversationIsNew('true');
};
return (
<div>
<button onClick={startNewConversation}>Start New Conversation</button>
</div>
);
};
Returns
An object with the following methods:
Method | Parameters | Return Type | Description |
|---|---|---|---|
|
|
| Sets the |
| None | `string \ | null` |
Implementation Details
Dependency on
useSearchParamsfromumi:
This hook utilizesuseSearchParamsto access the current URL query parameters (currentQueryParameters) and a setter function to update them (setSearchParams).Immutability with
URLSearchParams:
To avoid mutating the original search parameters directly, the hook creates a newURLSearchParamsinstance (newQueryParameters) from the current query string. This ensures updates do not mutate the original object unexpectedly.Memoization:
newQueryParametersis memoized withuseMemoto recreate only whencurrentQueryParameterschanges.The setter and getter functions are memoized with
useCallbackto prevent unnecessary re-renders or redefinitions.
ChatSearchParams Constant:
The hook referencesChatSearchParams.isNew(imported from@/constants/chat) as the key for the query parameter representing whether the conversation is new. This promotes consistency and avoids hardcoding string literals.
Interaction with Other Parts of the System
Constants:
This file importsChatSearchParamsfrom@/constants/chat, which likely defines keys used across the chat module for query parameters, ensuring consistency in parameter naming.Routing Layer:
It depends onuseSearchParamsfrom theumiframework, which manages URL state in React apps. Changes made bysetSearchParamsupdate the browser URL, allowing other components or browser reloads to reflect the current chat state.React Components:
This hook is designed to be used within React functional components that need to control or react to theisNewURL parameter.
Mermaid Diagram
flowchart TD
A[useSetChatRouteParams Hook] --> B[useSearchParams]
A --> C[new URLSearchParams(currentQueryParameters)]
A --> D[setConversationIsNew(value)]
A --> E[getConversationIsNew()]
D -->|calls| F[newQueryParameters.set('isNew', value)]
D -->|calls| G[setSearchParams(newQueryParameters)]
E -->|calls| H[newQueryParameters.get('isNew')]
Summary
use-set-chat-route.ts provides a clean, reusable abstraction for managing the isNew chat conversation flag within URL query parameters. It encapsulates URL state management, leverages React and umi hooks for efficient updates, and interfaces with chat constants to maintain consistency. This hook simplifies route-based state management related to chat conversations, enhancing modularity and maintainability of the chat feature in the application.