Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ const DYNAMIC_ROUTES = {
entryScreens: [SCREENS.REPORT, SCREENS.RIGHT_MODAL.SEARCH_REPORT, SCREENS.RIGHT_MODAL.EXPENSE_REPORT, SCREENS.RIGHT_MODAL.SEARCH_MONEY_REQUEST_REPORT],
getRoute: (reportID: string, reportActionID: string) => `flag/${reportID}/${reportActionID}`,
},
WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE: {
path: 'initial-list-value',
entryScreens: [SCREENS.WORKSPACE.REPORT_FIELDS_CREATE],
},
} as const satisfies DynamicRoutes;

const ROUTES = {
Expand Down
1 change: 1 addition & 0 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ const SCREENS = {
REPORT_FIELDS_VALUE_SETTINGS: 'Workspace_ReportFields_ValueSettings',
REPORT_FIELDS_EDIT_VALUE: 'Workspace_ReportFields_EditValue',
REPORT_FIELDS_EDIT_INITIAL_VALUE: 'Workspace_ReportFields_EditInitialValue',
DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE: 'Dynamic_Report_Fields_Initial_List_Value',
REPORT_FIELDS_TYPE_SELECTOR: 'Workspace_ReportFields_TypeSelector',
TAX_EDIT: 'Workspace_Tax_Edit',
TAX_NAME: 'Workspace_Tax_Name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,8 @@ const SettingsModalStackNavigator = createModalStackNavigator<SettingsNavigatorP
),
[SCREENS.SETTINGS.SUBSCRIPTION.ADD_PAYMENT_CARD]: withAgentAccessDenied(() => require<ReactComponentModule>('../../../../pages/settings/Subscription/PaymentCard').default),
[SCREENS.WORKSPACE.REPORT_FIELDS_CREATE]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/CreateReportFieldsPage').default,
[SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE]: () =>
require<ReactComponentModule>('../../../../pages/workspace/reports/InitialListValueSelector/DynamicReportFieldsInitialListValuePage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_SETTINGS]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsSettingsPage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_LIST_VALUES]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsListValuesPage').default,
[SCREENS.WORKSPACE.REPORT_FIELDS_ADD_VALUE]: () => require<ReactComponentModule>('../../../../pages/workspace/reports/ReportFieldsAddListValuePage').default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ const WORKSPACE_TO_RHP: Partial<Record<keyof WorkspaceSplitNavigatorParamList, s
SCREENS.WORKSPACE.REPORT_FIELDS_VALUE_SETTINGS,
SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_VALUE,
SCREENS.WORKSPACE.REPORT_FIELDS_EDIT_INITIAL_VALUE,
SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE,
SCREENS.WORKSPACE.REPORT_FIELDS_TYPE_SELECTOR,
],
[SCREENS.WORKSPACE.INVOICES]: [SCREENS.WORKSPACE.INVOICES_COMPANY_NAME, SCREENS.WORKSPACE.INVOICES_COMPANY_WEBSITE, SCREENS.WORKSPACE.INVOICES_VERIFY_ACCOUNT],
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/linkingConfig/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,7 @@ const config: LinkingOptions<RootNavigatorParamList>['config'] = {
[SCREENS.WORKSPACE.REPORT_FIELDS_CREATE]: {
path: ROUTES.WORKSPACE_CREATE_REPORT_FIELD.route,
},
[SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE]: DYNAMIC_ROUTES.WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE.path,
[SCREENS.WORKSPACE.REPORT_FIELDS_LIST_VALUES]: {
path: ROUTES.WORKSPACE_REPORT_FIELDS_LIST_VALUES.route,
},
Expand Down
3 changes: 3 additions & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,9 @@ type SettingsNavigatorParamList = {
[SCREENS.WORKSPACE.REPORT_FIELDS_CREATE]: {
policyID: string;
};
[SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE]: {
policyID: string;
};
[SCREENS.WORKSPACE.REPORT_FIELDS_LIST_VALUES]: {
policyID: string;
reportFieldID?: string;
Expand Down
1 change: 0 additions & 1 deletion src/pages/workspace/reports/CreateReportFieldsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ function WorkspaceCreateReportFieldsPage({
InputComponent={InitialListValueSelector}
inputID={INPUT_IDS.INITIAL_VALUE}
label={translate('common.initialValue')}
subtitle={translate('workspace.reportFields.listValuesInputSubtitle')}
shouldSaveDraft
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, {useEffect} from 'react';
import {View} from 'react-native';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import useDynamicBackPath from '@hooks/useDynamicBackPath';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useThemeStyles from '@hooks/useThemeStyles';
import {setDraftValues} from '@libs/actions/FormActions';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import {hasAccountingConnections} from '@libs/PolicyUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading';
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/WorkspaceReportFieldForm';
import ReportFieldsInitialListValuePicker from './ReportFieldsInitialListValuePicker';

type DynamicReportFieldsInitialListValuePageProps = WithPolicyAndFullscreenLoadingProps &
PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DYNAMIC_REPORT_FIELDS_INITIAL_LIST_VALUE>;

function DynamicReportFieldsInitialListValuePage({
policy,
route: {
params: {policyID},
},
}: DynamicReportFieldsInitialListValuePageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [formDraft, formDraftMetadata] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT);
const backPath = useDynamicBackPath(DYNAMIC_ROUTES.WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE.path);

const currentValue = formDraft?.[INPUT_IDS.INITIAL_VALUE] ?? '';
const isLoadingFormDraft = formDraftMetadata.status !== 'loaded';
// Mirror the guard used on the create page: the initial value selector is only relevant when there are
// available (non-disabled) list values. On a refresh or deeplink the form draft can be empty, in which case
// we send the user back to the Add field page instead of showing an empty picker.
const availableListValuesLength = (formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? []).filter((disabledListValue) => !disabledListValue).length;
const shouldRedirectToCreatePage = !isLoadingFormDraft && availableListValuesLength === 0;

useEffect(() => {
if (!shouldRedirectToCreatePage) {
return;
}
Navigation.goBack(backPath);
}, [shouldRedirectToCreatePage, backPath]);

const onValueSelected = (value: string) => {
setDraftValues(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM, {
[INPUT_IDS.INITIAL_VALUE]: currentValue === value ? '' : value,
});
Navigation.goBack(backPath);
};

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_REPORT_FIELDS_ENABLED}
shouldBeBlocked={hasAccountingConnections(policy)}
>
<ScreenWrapper
style={styles.pb0}
enableEdgeToEdgeBottomSafeAreaPadding
testID="DynamicReportFieldsInitialListValuePage"
>
<HeaderWithBackButton
title={translate('common.initialValue')}
onBackButtonPress={() => Navigation.goBack(backPath)}
/>
{isLoadingFormDraft || shouldRedirectToCreatePage ? (
<FullScreenLoadingIndicator reasonAttributes={{context: 'DynamicReportFieldsInitialListValuePage', isLoadingFormDraft: !!isLoadingFormDraft}} />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ UI-1 (docs)

FullScreenLoadingIndicator is rendered inside the same <ScreenWrapper> tree as the <HeaderWithBackButton> (lines 74-77). Because navigation (the back button) is visible alongside the loader, the user already has an escape route if loading hangs — so a full-screen loader is the wrong indicator here. This matches the UI-1 "Incorrect" example of FullscreenLoadingIndicator and HeaderWithBackButton living under the same JSX tree.

Use an ActivityIndicator centered in the content area instead, keeping the header rendered during loading:

{isLoadingFormDraft || shouldRedirectToCreatePage ? (
    <View style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter]}>
        <ActivityIndicator size={CONST.ACTIVITY_INDICATOR_SIZE.LARGE} />
    </View>
) : (
    // ...
)}

Reviewed at: 8ac54ef | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

) : (
<>
<View style={[styles.ph5, styles.pb4]}>
<Text style={[styles.sidebarLinkText, styles.optionAlternateText]}>{translate('workspace.reportFields.listValuesInputSubtitle')}</Text>
</View>
<ReportFieldsInitialListValuePicker
listValues={formDraft?.[INPUT_IDS.LIST_VALUES] ?? []}
disabledOptions={formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? []}
value={currentValue}
onValueChange={onValueSelected}
/>
</>
)}
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}

export default withPolicyAndFullscreenLoading(DynamicReportFieldsInitialListValuePage);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function ReportFieldsInitialListValuePicker({listValues, disabledOptions, value,
ListItem={SingleSelectListItem}
onSelectRow={(item) => onValueChange(item.value)}
initiallyFocusedItemKey={listValueOptions.find((listValue) => listValue.isSelected)?.keyForList}
shouldSingleExecuteRowSelect
addBottomSafeAreaPadding
/>
);
Expand Down
53 changes: 21 additions & 32 deletions src/pages/workspace/reports/InitialListValueSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
import type {ForwardedRef} from 'react';
import React, {useEffect, useState} from 'react';
import React, {useEffect, useRef} from 'react';
import {View} from 'react-native';
import type {MenuItemBaseProps} from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import useOnyx from '@hooks/useOnyx';
import blurActiveElement from '@libs/Accessibility/blurActiveElement';
import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import InitialListValueSelectorModal from './InitialListValueSelectorModal';
import {DYNAMIC_ROUTES} from '@src/ROUTES';

type InitialListValueSelectorProps = Pick<MenuItemBaseProps, 'label' | 'rightLabel' | 'errorText'> & {
/** Currently selected value */
value?: string;

/** Subtitle to display on field */
subtitle?: string;

/** Function to call when the user selects a value */
onInputChange?: (value: string) => void;

/** Reference to the outer element */
ref: ForwardedRef<View>;
};

function InitialListValueSelector({value = '', label = '', rightLabel, subtitle = '', errorText = '', onInputChange, ref}: InitialListValueSelectorProps) {
function InitialListValueSelector({value = '', label = '', rightLabel, errorText = '', onInputChange, ref}: InitialListValueSelectorProps) {
const [formDraft] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT);

const [isPickerVisible, setIsPickerVisible] = useState(false);

const showPickerModal = () => {
setIsPickerVisible(true);
};

const hidePickerModal = () => {
setIsPickerVisible(false);
blurActiveElement();
};

const updateValueInput = (initialValue: string) => {
onInputChange?.(value === initialValue ? '' : initialValue);
hidePickerModal();
};
const draftInitialValue = formDraft?.initialValue ?? '';

useEffect(() => {
const currentValueIndex = Object.values(formDraft?.listValues ?? {}).findIndex((listValue) => listValue === value);
Expand All @@ -51,6 +34,20 @@ function InitialListValueSelector({value = '', label = '', rightLabel, subtitle
}
}, [formDraft?.disabledListValues, formDraft?.listValues, onInputChange, value]);

// The value is selected on a separate dynamic route which writes directly to the form draft. FormProvider
// already syncs that draft write into the form's value, but it never re-runs validation, so the "Initial value"
// required error lingers. We can't detect the change by comparing the draft against `value` (they're already in
// sync by the time this runs), so we track the previous draft value and push it through onInputChange whenever it
// changes. onInputChange triggers the form's onValidate, which clears the stale error.
const previousDraftInitialValue = useRef(draftInitialValue);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ PERF-10 (docs)

This useEffect exists solely to push a value up to the parent form via the onInputChange callback whenever the Onyx draft's initialValue changes. Using an effect to mirror state into a parent callback is the parent-communication anti-pattern PERF-10 targets — it adds an extra render pass, requires the manual useRef previous-value bookkeeping, and makes the data flow indirect.

Prefer having the parent (the create page / FormProvider) read the draft initialValue from Onyx directly and feed it into the form value, so the value flows down as a prop rather than being echoed back up from the child through an effect. That removes the need for this effect and the previousDraftInitialValue ref entirely.


Reviewed at: 8ac54ef | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

useEffect(() => {
if (draftInitialValue === previousDraftInitialValue.current) {
return;
}
previousDraftInitialValue.current = draftInitialValue;
onInputChange?.(draftInitialValue);
}, [draftInitialValue, onInputChange]);

return (
<View>
<MenuItemWithTopDescription
Expand All @@ -61,15 +58,7 @@ function InitialListValueSelector({value = '', label = '', rightLabel, subtitle
rightLabel={rightLabel}
brickRoadIndicator={errorText ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
errorText={errorText}
onPress={showPickerModal}
/>
<InitialListValueSelectorModal
isVisible={isPickerVisible}
currentValue={value}
onClose={hidePickerModal}
onValueSelected={updateValueInput}
label={label}
subtitle={subtitle}
onPress={() => Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WORKSPACE_REPORT_FIELDS_INITIAL_LIST_VALUE.path))}
/>
</View>
);
Expand Down
Loading