hooks.ts
Overview
The hooks.ts file provides custom React hooks and utility functions focused on selecting and managing document parsers based on file types and user interactions. It primarily facilitates:
Mapping document file extensions to appropriate parser types.
Fetching a filtered list of parsers applicable to a specific document extension.
Managing the currently selected parser state and handling parser selection changes.
Determining display logic for automatic keyword extraction based on the selected parser.
This file integrates tightly with user settings and form state management within the application, enabling dynamic and context-sensitive parser selection in document processing workflows.
Detailed Explanations
Constants and Utility Functions
ParserListMap
Type:
Map<string[], DocumentParserType[]>Description:
A mapping between arrays of document file extensions (e.g.,['pdf'],['doc', 'docx']) and arrays of supportedDocumentParserTypes for those extensions. This map defines which parser types are valid for each file type.Implementation Detail:
The keys are arrays of file extensions, and the values are arrays of parser types imported fromDocumentParserType. This map is used to dynamically filter parsers based on the document extension.
getParserList(values, parserList)
Parameters:
values: string[] — An array of parser type values (string) to filter by.
parserList: Array<{ value: string; label: string }>— The list of all available parsers with their labels.
Returns:
Array<{ value: string; label: string }>— Filtered list of parsers whose values match any in the values array.Description:
Filters the providedparserListto only include parsers whosevaluematches any string in the values array.Usage Example:
const availableParsers = [ { value: 'Naive', label: 'Naive Parser' }, { value: 'Resume', label: 'Resume Parser' }, { value: 'Book', label: 'Book Parser' }, ]; const selectedValues = ['Naive', 'Book']; const filtered = getParserList(selectedValues, availableParsers); // filtered will contain the Naive and Book parsers only
Hook: useFetchParserListOnMount
useFetchParserListOnMount(
documentId: string,
parserId: DocumentParserType,
documentExtension: string,
form: FormInstance,
)
Parameters:
documentId: string— Unique identifier for the current document.parserId: DocumentParserType— The default or currently selected parser type for the document.documentExtension: string— The file extension of the document (e.g., "pdf", "docx").form: FormInstance— Ant Design form instance for managing form state.
Returns:
An object containing:parserList: Array<{ value: string; label: string }>— Array of parsers applicable to the document extension.handleChange: (tag: string) => void— Function to handle parser selection changes.selectedTag: DocumentParserType | undefined— The currently selected parser type.
Description:
This hook performs the following:Determines the valid parser types for the provided document extension by consulting
ParserListMapand filtering with the user's available parser list (useSelectParserList).Sets and syncs the selected parser state (
selectedTag) based on the providedparserIdanddocumentId.Provides a handler (
handleChange) which updates the selected parser and triggers any related form state changes usinguseHandleChunkMethodSelectChange.
Implementation Details:
Uses
useMemoto efficiently compute the filtered parser list wheneverparserListordocumentExtensionchanges.Uses
useEffectto updateselectedTagwhenparserIdordocumentIdchanges, ensuring synchronization on document switch or external parser changes.The
handleChangefunction updates both form state and local hook state.
Usage Example:
const { parserList, handleChange, selectedTag } = useFetchParserListOnMount( 'doc123', DocumentParserType.Resume, 'pdf', formInstance, ); return ( <Select value={selectedTag} onChange={handleChange}> {parserList.map(parser => ( <Select.Option key={parser.value} value={parser.value}> {parser.label} </Select.Option> ))} </Select> );
Constant: hideAutoKeywords
Type:
DocumentParserType[]Description:
An array of parser types for which automatic keyword extraction UI elements should be hidden.
Hook: useShowAutoKeywords
useShowAutoKeywords()
Returns:
A function(selectedTag: DocumentParserType | undefined) => booleanthat determines whether to show automatic keywords UI based on the currently selected parser.Description:
Returns a memoized callback function that checks if the passedselectedTagis not in thehideAutoKeywordslist, indicating whether automatic keyword extraction UI should be displayed.Usage Example:
const showAutoKeywords = useShowAutoKeywords(); if (showAutoKeywords(selectedTag)) { // render auto keyword extraction components }
Important Implementation Details and Algorithms
The hook
useFetchParserListOnMountdynamically filters the available parsers on mount and when dependencies change, ensuring users only see valid parsers for the document type.The use of
Mapwith array keys inParserListMapis an unconventional but effective way to associate groups of file extensions with parser arrays, searched via.find()and.some().State synchronization between external props (
parserIdanddocumentId) and internal hook state (selectedTag) is handled viauseEffectto keep UI consistent on document changes.The
hideAutoKeywordsarray serves as a centralized configuration that influences UI rendering logic inuseShowAutoKeywords.
Interaction with Other Parts of the System
Imports:
DocumentParserTypefrom@/constants/knowledge: Defines all possible parser types used throughout the app.useHandleChunkMethodSelectChangefrom@/hooks/logic-hooks: A custom hook to handle form updates and side effects when the parser selection changes.useSelectParserListfrom@/hooks/user-setting-hooks: Provides the list of parsers available to the user, possibly from user settings or permissions.FormInstancefromantd: Integrates with Ant Design form components for UI state management.
Role in Application:
This file is central to the document parsing configuration UI, enabling dynamic parser selection based on file type and user permissions. It connects user settings, document metadata, and form state to deliver a responsive and coherent UX for parser selection.
Diagram: File Structure and Hook Relationships
flowchart TD
A[ParserListMap] -->|lookup by extension| B[getParserList]
B --> C[useFetchParserListOnMount]
C --> D[selectedTag state]
C --> E[handleChange function]
E --> F[useHandleChunkMethodSelectChange]
C --> G[useSelectParserList]
H[hideAutoKeywords] --> I[useShowAutoKeywords]
I --> J{Check selectedTag against hideAutoKeywords}
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#bbf,stroke:#333,stroke-width:1px
style C fill:#bfb,stroke:#333,stroke-width:1px
style I fill:#fbf,stroke:#333,stroke-width:1px
Explanation:
ParserListMapacts as the source mapping extensions to parsers.getParserListfilters parsers based on this mapping and user's available parsers.useFetchParserListOnMountorchestrates the selection logic, maintaining selected parser state and providing handlers.useShowAutoKeywordsuses a static list of parsers to control UI behavior for keyword extraction.
Summary
The hooks.ts file plays a critical role in managing parser selection logic in a document processing system. It encapsulates:
The mapping between document extensions and supported parsers.
Hooks that fetch and filter parser lists dynamically based on document metadata and user settings.
State management and event handling for parser selection with form integration.
UI logic for conditional display of automatic keyword extraction controls.
This modular and reactive design supports extensibility for new parsers or file types and maintains consistent UX aligned with user permissions and document context.