Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import BaseListItem from '@components/SelectionListWithSections/BaseListItem';
import type {BaseListItemProps, ListItem} from '@components/SelectionListWithSections/types';
import useThemeStyles from '@hooks/useThemeStyles';
import BaseListItem from './BaseListItem';
import type {ListItem, SpendCategorySelectorListItemProps} from './types';

function SpendCategorySelectorListItem<TItem extends ListItem>({item, onSelectRow, isFocused}: BaseListItemProps<TItem>) {
function SpendCategorySelectorListItem<TItem extends ListItem>({item, onSelectRow, isFocused}: SpendCategorySelectorListItemProps<TItem>) {
const styles = useThemeStyles();
const {groupID, categoryID} = item;

Expand Down
9 changes: 8 additions & 1 deletion src/components/SelectionList/ListItem/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
import type CursorStyles from '@styles/utils/cursor/types';
import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {ReceiptErrors} from '@src/types/onyx/Transaction';
import type BaseListItem from './BaseListItem';
import type MultiSelectListItem from './MultiSelectListItem';
import type RadioListItem from './RadioListItem';
import type SingleSelectListItem from './SingleSelectListItem';
import type SpendCategorySelectorListItem from './SpendCategorySelectorListItem';

type ListItem<K extends string | number = string> = {
/** Text to display */
Expand Down Expand Up @@ -230,7 +234,7 @@ type ListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {
shouldUseDefaultRightHandSideCheckmark?: boolean;
};

type ValidListItem = typeof RadioListItem;
type ValidListItem = typeof RadioListItem | typeof BaseListItem | typeof MultiSelectListItem | typeof SingleSelectListItem | typeof SpendCategorySelectorListItem;

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.

Good catch 😌


type BaseListItemProps<TItem extends ListItem> = CommonListItemProps<TItem> & {
item: TItem;
Expand Down Expand Up @@ -259,6 +263,8 @@ type SingleSelectListItemProps<TItem extends ListItem> = ListItemProps<TItem>;

type MultiSelectListItemProps<TItem extends ListItem> = ListItemProps<TItem>;

type SpendCategorySelectorListItemProps<TItem extends ListItem> = ListItemProps<TItem>;

type UserListItemProps<TItem extends ListItem> = ListItemProps<TItem> & ForwardedFSClassProps;

export type {
Expand All @@ -271,5 +277,6 @@ export type {
ValidListItem,
SingleSelectListItemProps,
MultiSelectListItemProps,
SpendCategorySelectorListItemProps,
UserListItemProps,
};
4 changes: 1 addition & 3 deletions src/components/SelectionListWithSections/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {SearchColumnType, SearchGroupBy, SearchQueryJSON} from '@components
import type {ForwardedFSClassProps} from '@libs/Fullstory/types';
import type {BrickRoad} from '@libs/WorkspacesSettingsUtils';
import type UnreportedExpenseListItem from '@pages/UnreportedExpenseListItem';
import type SpendCategorySelectorListItem from '@pages/workspace/categories/SpendCategorySelectorListItem';
// eslint-disable-next-line no-restricted-imports
import type CursorStyles from '@styles/utils/cursor/types';
import type {TransactionPreviewData} from '@userActions/Search';
Expand Down Expand Up @@ -581,8 +580,7 @@ type ValidListItem =
| typeof SearchQueryListItem
| typeof SearchRouterItem
| typeof TravelDomainListItem
| typeof UnreportedExpenseListItem
| typeof SpendCategorySelectorListItem;
| typeof UnreportedExpenseListItem;

type Section<TItem extends ListItem> = {
/** Title of the section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import CategorySelectorModal from '@components/CategorySelector/CategorySelector
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import SelectionList from '@components/SelectionListWithSections';
import type {ListItem} from '@components/SelectionListWithSections/types';
import SelectionList from '@components/SelectionList';
import SpendCategorySelectorListItem from '@components/SelectionList/ListItem/SpendCategorySelectorListItem';
import type {ListItem} from '@components/SelectionList/ListItem/types';
import type {ListItem as SelectionListWithSectionsListItem} from '@components/SelectionListWithSections/types';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
Expand All @@ -25,7 +27,6 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import SpendCategorySelectorListItem from './SpendCategorySelectorListItem';

type WorkspaceCategoriesSettingsPageProps = WithPolicyConnectionsProps &
(
Expand Down Expand Up @@ -55,38 +56,31 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet
setWorkspaceRequiresCategory(policyID, value, policyTagLists, policyCategories, allTransactionViolations);
};

const {sections} = useMemo(() => {
const data = useMemo(() => {
if (!(currentPolicy && currentPolicy.mccGroup)) {
return {sections: [{data: []}]};
return [];
}

return {
sections: [
{
data: Object.entries(currentPolicy.mccGroup).map(
([mccKey, mccGroup]) =>
({
categoryID: mccGroup.category,
keyForList: mccKey,
groupID: mccKey,
tabIndex: -1,
pendingAction: mccGroup?.pendingAction,
}) as ListItem,
),
},
],
};
return Object.entries(currentPolicy.mccGroup).map(
([mccKey, mccGroup]): ListItem => ({
categoryID: mccGroup.category,
keyForList: mccKey,
groupID: mccKey,
tabIndex: -1,
pendingAction: mccGroup?.pendingAction,
}),
);
}, [currentPolicy]);

const hasEnabledCategories = hasEnabledOptions(policyCategories ?? {});
const isToggleDisabled = !policy?.areCategoriesEnabled || !hasEnabledCategories || isConnectedToAccounting;

const setNewCategory = (selectedCategory: ListItem) => {
if (!selectedCategory.keyForList || !groupID) {

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.

Why this check is not needed anymore? What happen if groupID is not set initially? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Instead I added currentGroupID prop to the setNewCategory function, because looking at the logic, the only usage of this function will have groupID defined:

{!!categoryID && !!groupID && (
                    <CategorySelectorModal
                      ...
                        onCategorySelected={(selectedCategory) => setNewCategory(selectedCategory, groupID)}
                      ...
                      />

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.

okay

const setNewCategory = (selectedCategory: SelectionListWithSectionsListItem, currentGroupID: string) => {
if (!selectedCategory.keyForList) {
return;
}
if (categoryID !== selectedCategory.keyForList) {
setWorkspaceDefaultSpendCategory(policyID, groupID, selectedCategory.keyForList);
setWorkspaceDefaultSpendCategory(policyID, currentGroupID, selectedCategory.keyForList);
}

Keyboard.dismiss();
Expand All @@ -96,6 +90,23 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet
});
};

const onSelectItem = (item: ListItem) => {

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.

let's wrap it into useCallback

if (!item.groupID || !item.categoryID) {
return;
}

setIsSelectorModalVisible(true);
setCategoryID(item.categoryID);
setGroupID(item.groupID);
};

const selectionListHeaderContent = (

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.

let's wrap it into useMemo

@GCyganek GCyganek Oct 22, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it really necessary to wrap these into useMemo and useCallback? I played a bit with the Profiler and it doesn't show any rerenders of SelectionList caused by onSelectRow and customListHeaderContent when I'm using the list. It looks like React Compiler takes care of the memoization here to prevent unnecessary re-renders. What do you think?

<View style={[styles.mh5, styles.mt2, styles.mb1]}>
<Text style={[styles.headerText]}>{translate('workspace.categories.defaultSpendCategories')}</Text>
<Text style={[styles.mt1, styles.lh20]}>{translate('workspace.categories.spendCategoriesDescription')}</Text>
</View>
);

return (
<AccessOrNotFoundWrapper
policyID={policyID}
Expand Down Expand Up @@ -127,25 +138,13 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet
/>
<View style={[styles.sectionDividerLine, styles.mh5, styles.mv6]} />
<View style={[styles.containerWithSpaceBetween]}>
{!!currentPolicy && (sections.at(0)?.data?.length ?? 0) > 0 && (
{!!currentPolicy && data.length > 0 && (
<SelectionList
addBottomSafeAreaPadding
headerContent={
<View style={[styles.mh5, styles.mt2, styles.mb1]}>
<Text style={[styles.headerText]}>{translate('workspace.categories.defaultSpendCategories')}</Text>
<Text style={[styles.mt1, styles.lh20]}>{translate('workspace.categories.spendCategoriesDescription')}</Text>
</View>
}
sections={sections}
customListHeaderContent={selectionListHeaderContent}
data={data}
ListItem={SpendCategorySelectorListItem}
onSelectRow={(item) => {
if (!item.groupID || !item.categoryID) {
return;
}
setIsSelectorModalVisible(true);
setCategoryID(item.categoryID);
setGroupID(item.groupID);
}}
onSelectRow={onSelectItem}
/>
)}
</View>
Expand All @@ -156,7 +155,7 @@ function WorkspaceCategoriesSettingsPage({policy, route}: WorkspaceCategoriesSet
isVisible={isSelectorModalVisible}
currentCategory={categoryID}
onClose={() => setIsSelectorModalVisible(false)}
onCategorySelected={setNewCategory}
onCategorySelected={(selectedCategory) => setNewCategory(selectedCategory, groupID)}
label={groupID[0].toUpperCase() + groupID.slice(1)}
/>
)}
Expand Down