index.tsx
Overview
This file defines a React functional component named ChunkPage. It serves as a page layout that provides navigation and UI controls related to a data chunk or dataset view within a larger application. The component integrates breadcrumb navigation, segmented controls for switching views, and action buttons, while rendering nested routes using React Router's <Outlet />.
The primary purpose of this file is to create a user interface that allows users to navigate between different result views (Parsed results, Chunk result, Result view) related to a dataset or chunk and to provide contextual breadcrumbs for easy navigation back to higher-level pages.
Component: ChunkPage
Description
ChunkPage is a React component that renders:
A page header with breadcrumb navigation.
A segmented control for switching between different result views.
Action buttons for additional options and saving.
An outlet for rendering nested child routes.
It uses hooks to manage navigation, reads the current URL location to determine the active segment, and dynamically generates UI options.
Imports and Dependencies
UI Components:
PageHeader,Breadcrumband its subcomponents,Button,Segmentedfrom internal UI libraries.Navigation Hooks:
useNavigatePagefor programmatic navigation and query string management.Routing:
useLocationfrom UmiJS for accessing current location, and<Outlet />for nested route rendering.Icons:
EllipsisVertical,Savefrom Lucide icon library.React:
useMemofor memoizing options and path values.
Functions and Hooks Used
useNavigatePage()
Custom hook imported from
@/hooks/logic-hooks/navigate-hooks.Provides navigation functions:
navigateToDataset: navigates to a dataset page given an ID.getQueryString: reads query parameters from the URL.navigateToChunk: navigates to a different chunk result view.
useLocation()
Provided by
umi(React Router wrapper).Returns the current location object, including pathname, which is used to determine the current active route segment.
Component Implementation Details
export default function ChunkPage() {
const { navigateToDataset, getQueryString, navigateToChunk } = useNavigatePage();
const location = useLocation();
// Memoized options for the segmented control (tabs)
const options = useMemo(() => {
return [
{ label: 'Parsed results', value: Routes.ParsedResult },
{ label: 'Chunk result', value: Routes.ChunkResult },
{ label: 'Result view', value: Routes.ResultView },
];
}, []);
// Extract base path from current location for segmented control value
const path = useMemo(() => {
return location.pathname.split('/').slice(0, 3).join('/');
}, [location.pathname]);
return (
<section>
<PageHeader>
{/* Breadcrumb navigation */}
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink
onClick={navigateToDataset(getQueryString(QueryStringMap.KnowledgeId) as string)}
>
Agent
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage>xxx</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
{/* Segmented control for switching views */}
<div>
<Segmented
options={options}
value={path}
onChange={navigateToChunk as (val: SegmentedValue) => void}
/>
</div>
{/* Action buttons */}
<div className="flex items-center gap-2">
<Button variant={'icon'} size={'icon'}>
<EllipsisVertical />
</Button>
<Button size={'sm'}>
<Save />
Save
</Button>
</div>
</PageHeader>
{/* Render nested routes */}
<Outlet />
</section>
);
}
Parameters
This component does not accept any props; it relies entirely on hooks for data and navigation.
Return Value
Returns JSX representing the page layout described above.
Usage Example
This component is typically used as a page route in the application's routing configuration:
{
path: '/chunk',
component: ChunkPage,
routes: [
{ path: 'parsed-result', component: ParsedResultComponent },
{ path: 'chunk-result', component: ChunkResultComponent },
{ path: 'result-view', component: ResultViewComponent },
],
}
Rendering ChunkPage will display the header and segmented controls, and the nested child route component will be rendered inside <Outlet /> based on the current path.
Important Implementation Details
Dynamic Breadcrumb Link:
The first breadcrumb item "Agent" is clickable and navigates to a dataset page using an ID obtained from the query string (KnowledgeId). This shows integration with URL query parameters and navigation hooks.Segmented Control State:
The segmented control's selected value is derived from the current pathname by extracting the first three segments. This ensures the correct tab is active based on the URL.Separation of Concerns:
Navigation logic is abstracted intouseNavigatePagehook, keeping the component clean and focused on rendering.Icons and Buttons:
The buttons include an icon-only button (likely for more options) and a save button with an icon and label, suggesting common UI patterns.
Interaction with Other Parts of the System
Route System:
Uses<Outlet />to render nested routes; relies on UmiJS routing.Navigation Hook (
useNavigatePage):
This hook likely interacts with the application's routing and query management systems.UI Components:
Leverages shared UI components (PageHeader,Breadcrumb,Button,Segmented) from the app's component library, ensuring consistent styling and behavior.Constants (
RoutesandQueryStringMap):
Uses predefined route constants and query string keys for navigation and URL parsing.
Visual Diagram: Component Structure and Interaction
componentDiagram
ChunkPage <|-- PageHeader
PageHeader <|-- Breadcrumb
Breadcrumb *-- BreadcrumbList
BreadcrumbList *-- BreadcrumbItem
BreadcrumbItem *-- BreadcrumbLink
BreadcrumbItem *-- BreadcrumbPage
BreadcrumbList *-- BreadcrumbSeparator
PageHeader *-- Segmented
PageHeader *-- Button
ChunkPage *-- Outlet
note right of ChunkPage
- Uses useNavigatePage hook
- Uses useLocation hook
- Controls navigation and UI state
end note
Summary
index.tsx exports a single
ChunkPagecomponent that provides a structured UI for navigating between different chunk-related views.It features breadcrumb navigation, segmented tabs for switching result views, and action buttons.
The component depends on routing and navigation hooks to manage URL-driven state.
Nested child routes are rendered via the
<Outlet />component, enabling modular UI composition.The file fits within a larger React/UmiJS application ecosystem, integrating with shared UI components and navigation utilities.
This documentation provides a thorough understanding of the file's purpose, components, and interactions, enabling easy maintenance and enhancement by developers.