search-home.tsx
Overview
search-home.tsx defines a React functional component named SearchPage which renders a visually appealing and interactive search input interface. The component is designed for initiating and managing search operations within the application, providing user feedback and conditional controls based on user permissions and search states.
Key functionalities include:
Displaying a styled search input box with placeholder and icon.
Handling user input with immediate validation and feedback.
Conditionally showing welcome messages and a spotlight animation when not searching.
Managing search state toggling via keyboard or button interactions.
Integrating internationalization for dynamic UI text.
This component acts as the primary user interface for search interactions and is expected to be integrated wherever search capabilities are needed in the front-end.
Detailed Component Documentation
SearchPage Component
Description
A React component for rendering a search input section with associated UI elements such as a title, welcome messages, a spotlight animation, and a search button. It manages search states and user interaction events, enforcing search permission constraints.
Props
Prop | Type | Required | Description |
|---|---|---|---|
|
| Yes | Indicates whether a search operation is currently active. |
|
| Yes | React state setter function to update the |
|
| Yes | The current value of the search input text. |
|
| Yes | React state setter function to update the |
|
| No | Optional user information object, used to personalize the welcome message. |
|
| No | Flag indicating if the user is allowed to perform searches (e.g., dataset selected). |
Usage Example
import React, { useState } from 'react';
import SearchPage from './search-home';
import { IUserInfo } from '@/interfaces/database/user-setting';
const App = () => {
const [isSearching, setIsSearching] = useState(false);
const [searchText, setSearchText] = useState('');
const userInfo: IUserInfo = { nickname: 'Alice' }; // example userInfo
const canSearch = true; // example permission flag
return (
<SearchPage
isSearching={isSearching}
setIsSearching={setIsSearching}
searchText={searchText}
setSearchText={setSearchText}
userInfo={userInfo}
canSearch={canSearch}
/>
);
};
Rendered Structure
Section Container: Centered flex container with margin top to position the search UI.
Title (
h1): Displays "RAGFlow" with a gradient text effect.Search Box Container: A bordered box which conditionally shows:
Spotlightanimation whenisSearchingis false.Welcome messages personalized with
userInfo.nickname.An input field for entering search queries.
A search button with a search icon.
Methods & Event Handlers
Input
onChangehandler:Updates
searchTextstate with current input value ifcanSearchis true.Shows a warning message ("chooseDataset") if
canSearchis false.
Input
onKeyUphandler:On
Enterkey press, togglesisSearchingstate ifcanSearchis true.Otherwise, shows a warning message.
Search Button
onClickhandler:Toggles
isSearchingstate ifcanSearchis true.Otherwise, shows a warning message.
Return Value
Returns a React JSX element representing the search UI component.
Important Implementation Details
Internationalization (i18n):
The component uses theuseTranslationhook fromreact-i18nextto support multiple languages by localizing placeholder texts, welcome messages, and warnings.Conditional UI Rendering:
The component distinguishes between the searching and idle states (isSearching) to change the UI. For example, the spotlight animation and welcome messages only appear when not searching.User Permission Enforcement (
canSearch):
Before allowing search input or search initiation, the component checks if the user can perform searches. If not, it triggers a warning message via themessageUI component.Styling:
Styling is handled with a combination of Tailwind CSS utility classes and a local stylesheetindex.less. The component usescnutility to conditionally concatenate class names.Accessibility & UX:
The search button is keyboard accessible.
The input supports triggering search on pressing Enter.
Visual feedback (e.g., warning messages) informs the user about missing prerequisites like dataset selection.
Interaction with Other System Components
InputComponent (@/components/originui/input):
Custom input field component used for search text entry.SpotlightComponent (@/components/spotlight):
Visual animation shown when not searching, enhancing UI appeal.messageComponent (@/components/ui/message):
Provides feedback to users, such as warnings when search cannot proceed.IUserInfoInterface (@/interfaces/database/user-setting):
Defines user information structure, used here to personalize greetings.cnUtility (@/lib/utils):
Utility for conditional class name concatenation.SearchIcon (lucide-react):
SVG icon used within the search button.react-i18next:
Provides internationalization support for translations.
The component is expected to be used as part of a page or layout that manages search state and feeds necessary props like userInfo and canSearch.
Visual Diagram
componentDiagram
component SearchPage {
+isSearching: boolean
+setIsSearching: Dispatch<boolean>
+searchText: string
+setSearchText: Dispatch<string>
+userInfo?: IUserInfo
+canSearch?: boolean
+render()
}
component Input {
+placeholder: string
+value: string
+onChange(event)
+onKeyUp(event)
}
component Spotlight
component message
component SearchIcon
SearchPage --> Input : renders
SearchPage --> Spotlight : conditionally renders (if !isSearching)
SearchPage --> message : invokes for warnings
SearchPage --> SearchIcon : used inside search button
Summary
search-home.tsx is a self-contained React component providing a localized, styled, and interactive search input UI with user permission validation and dynamic state management. It integrates with various UI components and utilities to deliver a smooth and user-friendly search experience within the application.