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:

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).

[
  ['pdf'],
  ['naive', 'resume', 'manual', 'paper', 'book', 'laws', 'presentation', 'one', 'qa', 'knowledge_graph'],
]

Function: getParserList

const getParserList = (
  values: string[],
  parserList: Array<{ value: string; label: string }>
) => Array<{ value: string; label: string }>;
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 }> };
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'];

Hook: useShowAutoKeywords

export const useShowAutoKeywords = () => (selectedTag: string) => boolean;
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


Interaction with Other Parts of the System


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:

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.