|
| 1 | +// eslint-disable-next-line no-restricted-imports |
| 2 | +import React, {createContext, useMemo} from 'react'; |
| 3 | +import useOnyx from '@hooks/useOnyx'; |
| 4 | +import ExpensiMark from '@libs/Parser'; |
| 5 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 6 | + |
| 7 | +type Extras = { |
| 8 | + reportIDToName?: Record<string, string>; |
| 9 | + accountIDToName?: Record<string, string>; |
| 10 | + cacheVideoAttributes?: (vidSource: string, attrs: string) => void; |
| 11 | + videoAttributeCache?: Record<string, string>; |
| 12 | +}; |
| 13 | +type ParserContextProps = { |
| 14 | + htmlToMarkdown: (htmlString: string, extras?: Extras) => string; |
| 15 | + htmlToText: (htmlString: string, extras?: Extras) => string; |
| 16 | +}; |
| 17 | +type ParserContextProviderProps = {children: React.ReactNode}; |
| 18 | +const ParserContext = createContext<ParserContextProps>({ |
| 19 | + htmlToMarkdown: () => '', |
| 20 | + htmlToText: () => '', |
| 21 | +}); |
| 22 | + |
| 23 | +function ParserContextProvider({children}: ParserContextProviderProps) { |
| 24 | + const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); |
| 25 | + const [personalDetailsList] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); |
| 26 | + |
| 27 | + const reportIDToNameMap = useMemo(() => { |
| 28 | + const map: Record<string, string> = {}; |
| 29 | + Object.values(reports ?? {}).forEach((report) => { |
| 30 | + if (!report) { |
| 31 | + return; |
| 32 | + } |
| 33 | + map[report.reportID] = report.reportName ?? report.reportID; |
| 34 | + }); |
| 35 | + return map; |
| 36 | + }, [reports]); |
| 37 | + |
| 38 | + const accountIDToNameMap = useMemo(() => { |
| 39 | + const map: Record<string, string> = {}; |
| 40 | + Object.values(personalDetailsList ?? {}).forEach((personalDetails) => { |
| 41 | + if (!personalDetails) { |
| 42 | + return; |
| 43 | + } |
| 44 | + map[personalDetails.accountID] = personalDetails.login ?? personalDetails.displayName ?? ''; |
| 45 | + }); |
| 46 | + return map; |
| 47 | + }, [personalDetailsList]); |
| 48 | + |
| 49 | + const Parser = React.useMemo<ParserContextProps>(() => { |
| 50 | + const parser = ExpensiMark as unknown as Record<string, unknown>; |
| 51 | + |
| 52 | + return { |
| 53 | + ...parser, |
| 54 | + htmlToMarkdown: (htmlString: string, extras?: Extras) => |
| 55 | + (parser.htmlToMarkdown as (htmlString: string, extras?: Extras) => string)(htmlString, { |
| 56 | + reportIDToName: extras?.reportIDToName ?? reportIDToNameMap, |
| 57 | + accountIDToName: extras?.accountIDToName ?? accountIDToNameMap, |
| 58 | + cacheVideoAttributes: extras?.cacheVideoAttributes, |
| 59 | + }), |
| 60 | + htmlToText: (htmlString: string, extras?: Extras) => |
| 61 | + (parser.htmlToText as (htmlString: string, extras?: Extras) => string)(htmlString, { |
| 62 | + reportIDToName: extras?.reportIDToName ?? reportIDToNameMap, |
| 63 | + accountIDToName: extras?.accountIDToName ?? accountIDToNameMap, |
| 64 | + cacheVideoAttributes: extras?.cacheVideoAttributes, |
| 65 | + }), |
| 66 | + }; |
| 67 | + }, [reportIDToNameMap, accountIDToNameMap]); |
| 68 | + |
| 69 | + const contextValue = useMemo<ParserContextProps>(() => Parser, [Parser]); |
| 70 | + |
| 71 | + return <ParserContext.Provider value={contextValue}>{children}</ParserContext.Provider>; |
| 72 | +} |
| 73 | + |
| 74 | +ParserContextProvider.displayName = 'ParserContextProvider'; |
| 75 | + |
| 76 | +export {ParserContext, ParserContextProvider}; |
0 commit comments