|
| 1 | +/* eslint-disable rulesdir/no-negated-variables */ |
| 2 | +import React, {ComponentType, ForwardedRef, RefAttributes} from 'react'; |
| 3 | +import {OnyxEntry, withOnyx} from 'react-native-onyx'; |
| 4 | +import {RouteProp} from '@react-navigation/native'; |
| 5 | +import getComponentDisplayName from '../../../libs/getComponentDisplayName'; |
| 6 | +import NotFoundPage from '../../ErrorPage/NotFoundPage'; |
| 7 | +import ONYXKEYS from '../../../ONYXKEYS'; |
| 8 | +import FullscreenLoadingIndicator from '../../../components/FullscreenLoadingIndicator'; |
| 9 | +import * as ReportUtils from '../../../libs/ReportUtils'; |
| 10 | +import * as OnyxTypes from '../../../types/onyx'; |
| 11 | + |
| 12 | +type OnyxProps = { |
| 13 | + /** The report currently being looked at */ |
| 14 | + report: OnyxEntry<OnyxTypes.Report>; |
| 15 | + /** The policies which the user has access to */ |
| 16 | + policies: OnyxEntry<OnyxTypes.Policy>; |
| 17 | + /** Beta features list */ |
| 18 | + betas: OnyxEntry<OnyxTypes.Beta[]>; |
| 19 | + /** Indicated whether the report data is loading */ |
| 20 | + isLoadingReportData: OnyxEntry<boolean>; |
| 21 | +}; |
| 22 | + |
| 23 | +type ComponentProps = OnyxProps & { |
| 24 | + route: RouteProp<{params: {reportID: string}}>; |
| 25 | +}; |
| 26 | + |
| 27 | +export default function ( |
| 28 | + shouldRequireReportID = true, |
| 29 | +): <TProps extends ComponentProps, TRef>( |
| 30 | + WrappedComponent: React.ComponentType<TProps & React.RefAttributes<TRef>>, |
| 31 | +) => React.ComponentType<Omit<TProps & React.RefAttributes<TRef>, keyof OnyxProps>> { |
| 32 | + return function <TProps extends ComponentProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) { |
| 33 | + function WithReportOrNotFound(props: TProps, ref: ForwardedRef<TRef>) { |
| 34 | + const contentShown = React.useRef(false); |
| 35 | + |
| 36 | + const isReportIdInRoute = props.route.params.reportID?.length; |
| 37 | + |
| 38 | + if (shouldRequireReportID || isReportIdInRoute) { |
| 39 | + const shouldShowFullScreenLoadingIndicator = props.isLoadingReportData && (!Object.entries(props.report ?? {}).length || !props.report?.reportID); |
| 40 | + |
| 41 | + const shouldShowNotFoundPage = |
| 42 | + !Object.entries(props.report ?? {}).length || !props.report?.reportID || !ReportUtils.canAccessReport(props.report, props.policies, props.betas, {}); |
| 43 | + |
| 44 | + // If the content was shown but it's not anymore that means the report was deleted and we are probably navigating out of this screen. |
| 45 | + // Return null for this case to avoid rendering FullScreenLoadingIndicator or NotFoundPage when animating transition. |
| 46 | + if (shouldShowNotFoundPage && contentShown.current) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (shouldShowFullScreenLoadingIndicator) { |
| 51 | + return <FullscreenLoadingIndicator />; |
| 52 | + } |
| 53 | + |
| 54 | + if (shouldShowNotFoundPage) { |
| 55 | + return <NotFoundPage />; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (!contentShown.current) { |
| 60 | + contentShown.current = true; |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <WrappedComponent |
| 65 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 66 | + {...props} |
| 67 | + ref={ref} |
| 68 | + /> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + WithReportOrNotFound.displayName = `withReportOrNotFound(${getComponentDisplayName(WrappedComponent)})`; |
| 73 | + |
| 74 | + return withOnyx<TProps & RefAttributes<TRef>, OnyxProps>({ |
| 75 | + report: { |
| 76 | + key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID}`, |
| 77 | + }, |
| 78 | + isLoadingReportData: { |
| 79 | + key: ONYXKEYS.IS_LOADING_REPORT_DATA, |
| 80 | + }, |
| 81 | + betas: { |
| 82 | + key: ONYXKEYS.BETAS, |
| 83 | + }, |
| 84 | + policies: { |
| 85 | + key: ONYXKEYS.COLLECTION.POLICY, |
| 86 | + }, |
| 87 | + })(React.forwardRef(WithReportOrNotFound)); |
| 88 | + }; |
| 89 | +} |
0 commit comments