-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Simplified Collect][CLEAN UP][LOW] Standardize on Workspace wrapper #40598
Changes from 7 commits
4d15239
5829f66
6a18afb
a5d5822
26aed7a
02e1191
d9a2b22
ae7b35e
54cf1f5
572e0f6
e72d7bf
199c918
dd27831
8fc1426
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* eslint-disable rulesdir/no-negated-variables */ | ||
import React, {useEffect} from 'react'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; | ||
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; | ||
import * as Policy from '@userActions/Policy'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import type * as OnyxTypes from '@src/types/onyx'; | ||
import type {PolicyFeatureName} from '@src/types/onyx/Policy'; | ||
import callOrReturn from '@src/types/utils/callOrReturn'; | ||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
|
||
const POLICY_ACCESS_VARIANTS = { | ||
PAID: (policy: OnyxEntry<OnyxTypes.Policy>) => PolicyUtils.isPaidGroupPolicy(policy) && !!policy?.isPolicyExpenseChatEnabled, | ||
ADMIN: (policy: OnyxEntry<OnyxTypes.Policy>) => PolicyUtils.isPolicyAdmin(policy), | ||
} as const satisfies Record<string, (policy: OnyxTypes.Policy) => boolean>; | ||
|
||
type PolicyAccessVariant = keyof typeof POLICY_ACCESS_VARIANTS; | ||
type AccessOrNotFoundWrapperOnyxProps = { | ||
/** The report currently being looked at */ | ||
policy: OnyxEntry<OnyxTypes.Policy>; | ||
|
||
/** Indicated whether the report data is loading */ | ||
isLoadingReportData: OnyxEntry<boolean>; | ||
}; | ||
|
||
type AccessOrNotFoundWrapperProps = AccessOrNotFoundWrapperOnyxProps & { | ||
/** The children to render */ | ||
children: ((props: AccessOrNotFoundWrapperOnyxProps) => React.ReactNode) | React.ReactNode; | ||
|
||
/** The report currently being looked at */ | ||
policyID: string; | ||
|
||
/** Defines which types of access should be verified */ | ||
accessVariants?: PolicyAccessVariant[]; | ||
|
||
/** The current feature name that the user tries to get access to */ | ||
featureName?: PolicyFeatureName; | ||
}; | ||
|
||
type PageNotFoundFallackProps = Pick<AccessOrNotFoundWrapperProps, 'policyID'> & {shouldShowFullScreenFallback: boolean}; | ||
|
||
function PageNotFoundFallback({policyID, shouldShowFullScreenFallback}: PageNotFoundFallackProps) { | ||
return shouldShowFullScreenFallback ? ( | ||
<FullPageNotFoundView | ||
shouldShow | ||
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)} | ||
shouldForceFullScreen | ||
/> | ||
) : ( | ||
<NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_PROFILE.getRoute(policyID))} /> | ||
); | ||
} | ||
|
||
function AccessOrNotFoundWrapper({accessVariants = [], ...props}: AccessOrNotFoundWrapperProps) { | ||
const {policy, policyID, featureName, isLoadingReportData} = props; | ||
|
||
const isPolicyIDInRoute = !!policyID?.length; | ||
|
||
useEffect(() => { | ||
if (!isPolicyIDInRoute || !isEmptyObject(policy)) { | ||
// If the workspace is not required or is already loaded, we don't need to call the API | ||
return; | ||
} | ||
|
||
Policy.openWorkspace(policyID, []); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isPolicyIDInRoute, policyID]); | ||
|
||
const shouldShowFullScreenLoadingIndicator = isLoadingReportData !== false && (!Object.entries(policy ?? {}).length || !policy?.id); | ||
|
||
const isFeatureEnabled = featureName ? PolicyUtils.isPolicyFeatureEnabled(policy, featureName) : true; | ||
|
||
const isPageAccessible = accessVariants.reduce((acc, variant) => { | ||
const accessFunction = POLICY_ACCESS_VARIANTS[variant]; | ||
return acc && accessFunction(policy); | ||
}, true); | ||
|
||
const shouldShowNotFoundPage = isEmptyObject(policy) || (Object.keys(policy).length === 1 && !isEmptyObject(policy.errors)) || !policy?.id || !isPageAccessible || !isFeatureEnabled; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coming from #44206: |
||
|
||
if (shouldShowFullScreenLoadingIndicator) { | ||
return <FullscreenLoadingIndicator />; | ||
} | ||
|
||
if (shouldShowNotFoundPage) { | ||
return ( | ||
<PageNotFoundFallback | ||
policyID={policyID} | ||
shouldShowFullScreenFallback={!isFeatureEnabled} | ||
/> | ||
); | ||
} | ||
|
||
return callOrReturn(props.children, props); | ||
} | ||
|
||
export default withOnyx<AccessOrNotFoundWrapperProps, AccessOrNotFoundWrapperOnyxProps>({ | ||
policy: { | ||
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID ?? ''}`, | ||
}, | ||
isLoadingReportData: { | ||
key: ONYXKEYS.IS_LOADING_REPORT_DATA, | ||
}, | ||
})(AccessOrNotFoundWrapper); |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.