hooks.ts
Overview
The hooks.ts file contains custom React hooks and utility logic related to selecting and filtering parser options based on file extensions. It primarily serves to map document/file extensions to appropriate parser types and expose hooks to retrieve filtered parser lists and control UI behavior around certain parser "keywords".
Key functionalities include:
Mapping file extensions to available parsers.
Filtering parsers based on file extension and user settings.
Providing a hook to fetch the filtered parser list on component mount.
Providing a hook to determine whether certain "auto keywords" should be shown or hidden based on selected tags.
This file is designed to be used within a React application that handles document parsing and user settings related to parser selection.
Detailed Explanations
Constants
ParserListMap: Map<string[], string[]>
A Map associating arrays of file extensions (keys) to arrays of parser identifiers (values).
Purpose: Defines which parsers are relevant for each file extension group.
Example Entry:
[
['pdf'],
['naive', 'resume', 'manual', 'paper', 'book', 'laws', 'presentation', 'one', 'qa', 'knowledge_graph'],
]
Usage: Used internally to find the parser list for a given file extension.
Function: getParserList
const getParserList = (
values: string[],
parserList: Array<{ value: string; label: string }>
) => Array<{ value: string; label: string }>;
Description: Filters a list of parser options to include only those whose
valueis present in the providedvaluesarray.Parameters:
values: An array of strings representing parser values to keep.parserList: An array of parser option objects withvalueandlabelproperties.
Returns: A filtered array of parser options matching the
values.Example Usage:
const parsers = [
{ value: 'naive', label: 'Naive Parser' },
{ value: 'resume', label: 'Resume Parser' },
{ value: 'manual', label: 'Manual Parser' },
];
const filtered = getParserList(['naive', 'manual'], parsers);
// filtered will contain only the naive and manual parser objects
Hook: useFetchParserListOnMount
export const useFetchParserListOnMount = (
documentExtension: string
) => { parserList: Array<{ value: string; label: string }> };
Description: React hook that returns a filtered list of parsers appropriate for the given file extension. It uses user settings from the
useSelectParserListhook, applies the mapping defined inParserListMap, and memoizes the result.Parameters:
documentExtension: The file extension (e.g.,"pdf","docx") to find parsers for.
Returns:
An object containing a single property
parserList, which is the filtered list of parser options.
Implementation Details:
Uses
useMemoto recompute only whenparserListordocumentExtensionchanges.Looks up the extension in
ParserListMap.If no specific mapping exists, returns a default parser list.
Usage Example:
const { parserList } = useFetchParserListOnMount('pdf');
// parserList contains parsers suitable for PDF files according to user settings
Constant: hideAutoKeywords
const hideAutoKeywords = ['qa', 'table', 'resume', 'knowledge_graph', 'tag'];
Description: An array of parser keywords that should be hidden or suppressed in certain UI contexts.
Hook: useShowAutoKeywords
export const useShowAutoKeywords = () => (selectedTag: string) => boolean;
Description: React hook that returns a function (
showAutoKeywords) to determine whether a given tag should show auto keywords.Behavior: The returned function returns
trueif theselectedTagis not in thehideAutoKeywordslist.Parameters: None at the hook level; the returned callback accepts one string parameter:
selectedTag: The tag to check.
Returns: Boolean indicating whether to show auto keywords.
Usage Example:
const showAutoKeywords = useShowAutoKeywords();
if (showAutoKeywords('qa')) {
// This will be false because 'qa' is in hideAutoKeywords
}
if (showAutoKeywords('manual')) {
// This will be true
}
Important Implementation Details
Mapping with Arrays as Keys:
TheParserListMapuses arrays as keys, which is unusual since in JavaScript/TypeScript, object keys are usually strings or symbols. However, the code never directly usesParserListMap.get(documentExtension)but searches for a key array that contains the extension and then uses that key to get the value. This is done via:const key = [...ParserListMap.keys()].find((x) => x.some((y) => y === documentExtension) );This workaround is necessary because Map keys are reference-based, and arrays are objects.
Use of
useMemoanduseCallback:
Both hooks optimize performance by memoizing computations and callbacks so that components do not re-render unnecessarily.Dependency on
useSelectParserList:
This hook is imported from an external user settings hook module (@/hooks/user-setting-hooks). It presumably provides the full list of available parsers based on user preferences or application state.
Interaction with Other Parts of the System
useSelectParserListHook:
This file depends onuseSelectParserListto retrieve the user's configured parser options, which it then filters based on document extension.UI Components:
The hooks returned by this file are likely consumed by UI components responsible for displaying parser options in dropdowns or settings panels.Parsing Logic:
The parser strings (e.g.,'naive','resume','qa') correspond to different parser implementations elsewhere in the system. This file does not implement parsers but manages their selection.
Visual Diagram
flowchart TD
A[useFetchParserListOnMount(documentExtension)] --> B{Find key in ParserListMap with extension}
B -->|Found| C[Get parser values from ParserListMap]
B -->|Not found| D[Use default parser values]
C --> E[getParserList(values, parserList)]
D --> E[getParserList(defaultValues, parserList)]
E --> F[Return filtered parserList]
G[useShowAutoKeywords()] --> H[Returns showAutoKeywords(selectedTag)]
H --> I{selectedTag in hideAutoKeywords?}
I -->|Yes| J[Return false]
I -->|No| K[Return true]
Summary
The hooks.ts file is a utility and hook provider that enables React components to:
Retrieve a filtered list of parsers suitable for a given document extension.
Determine UI behavior regarding the visibility of certain parser-related keywords.
It leverages React hooks (useMemo, useCallback) to optimize performance and relies on a centralized mapping of file extensions to parser options. This file is a key part of the parser selection and configuration subsystem in the application.