Skip to content

Commit 5bd94f9

Browse files
committed
fix category and tag list gets deselected when editing list item name
1 parent 028bd0f commit 5bd94f9

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/pages/workspace/categories/WorkspaceCategoriesPage.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import TextLink from '@components/TextLink';
2525
import useAutoTurnSelectionModeOffWhenHasNoActiveOption from '@hooks/useAutoTurnSelectionModeOffWhenHasNoActiveOption';
2626
import useCleanupSelectedOptions from '@hooks/useCleanupSelectedOptions';
2727
import useEnvironment from '@hooks/useEnvironment';
28-
import useFilteredSelection from '@hooks/useFilteredSelection';
2928
import useLocalize from '@hooks/useLocalize';
3029
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
3130
import useNetwork from '@hooks/useNetwork';
@@ -52,7 +51,6 @@ import CONST from '@src/CONST';
5251
import ONYXKEYS from '@src/ONYXKEYS';
5352
import ROUTES from '@src/ROUTES';
5453
import SCREENS from '@src/SCREENS';
55-
import type {PolicyCategory} from '@src/types/onyx';
5654
import type DeepValueOf from '@src/types/utils/DeepValueOf';
5755

5856
type PolicyOption = ListItem & {
@@ -90,9 +88,8 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
9088
const isConnectionVerified = connectedIntegration && !isConnectionUnverified(policy, connectedIntegration);
9189
const currentConnectionName = getCurrentConnectionName(policy);
9290
const isQuickSettingsFlow = route.name === SCREENS.SETTINGS_CATEGORIES.SETTINGS_CATEGORIES_ROOT;
93-
const filterCategories = useCallback((category: PolicyCategory | undefined) => !!category && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, []);
9491

95-
const [selectedCategories, setSelectedCategories] = useFilteredSelection(policyCategories, filterCategories);
92+
const [selectedCategories, setSelectedCategories] = useState<string[]>([]);
9693
const canSelectMultiple = isSmallScreenWidth ? isMobileSelectionModeEnabled : true;
9794

9895
const fetchCategories = useCallback(() => {
@@ -107,9 +104,37 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
107104
// eslint-disable-next-line react-hooks/exhaustive-deps
108105
}, []);
109106

110-
const cleanupSelectedOption = useCallback(() => setSelectedCategories([]), [setSelectedCategories]);
107+
const cleanupSelectedOption = useCallback(() => setSelectedCategories([]), []);
111108
useCleanupSelectedOptions(cleanupSelectedOption);
112109

110+
useEffect(() => {
111+
if (selectedCategories.length === 0 || !canSelectMultiple) {
112+
return;
113+
}
114+
115+
setSelectedCategories((prevSelectedCategories) => {
116+
const newSelectedCategories = [];
117+
118+
for (const categoryName of prevSelectedCategories) {
119+
const categoryExists = policyCategories?.[categoryName];
120+
if (!categoryExists) {
121+
const renamedCategory = Object.entries(policyCategories ?? {}).find(([, category]) => category.previousCategoryName === categoryName);
122+
if (renamedCategory) {
123+
newSelectedCategories.push(renamedCategory[0]);
124+
continue;
125+
}
126+
}
127+
128+
if (categoryExists && categoryExists.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
129+
newSelectedCategories.push(categoryName);
130+
}
131+
}
132+
133+
return newSelectedCategories;
134+
});
135+
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
136+
}, [policyCategories]);
137+
113138
useSearchBackPress({
114139
onClearSelection: () => setSelectedCategories([]),
115140
onNavigationCallBack: () => Navigation.goBack(backTo),

0 commit comments

Comments
 (0)