Skip to content

Commit 698585e

Browse files
authored
Merge pull request Expensify#67981 from huult/67608-category-tag-list-deselected-when-editing-name
fix category and tag list gets deselected when editing list item name
2 parents 3f91630 + 1e762d0 commit 698585e

2 files changed

Lines changed: 80 additions & 10 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),

src/pages/workspace/tags/WorkspaceTagsPage.tsx

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import Text from '@components/Text';
2424
import TextLink from '@components/TextLink';
2525
import useCleanupSelectedOptions from '@hooks/useCleanupSelectedOptions';
2626
import useEnvironment from '@hooks/useEnvironment';
27-
import useFilteredSelection from '@hooks/useFilteredSelection';
2827
import useLocalize from '@hooks/useLocalize';
2928
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
3029
import useNetwork from '@hooks/useNetwork';
@@ -122,9 +121,7 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
122121
return policyTagLists?.at(0)?.tags;
123122
}, [isMultiLevelTags, policyTagLists]);
124123

125-
const filterTags = useCallback((tag?: PolicyTag | PolicyTagList) => !!tag && tag.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, []);
126-
127-
const [selectedTags, setSelectedTags] = useFilteredSelection(tagsList, filterTags);
124+
const [selectedTags, setSelectedTags] = useState<string[]>([]);
128125

129126
const isTagSelected = useCallback((tag: TagListItem) => selectedTags.includes(tag.value), [selectedTags]);
130127

@@ -136,7 +133,55 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
136133
// eslint-disable-next-line react-hooks/exhaustive-deps
137134
}, []);
138135

139-
const cleanupSelectedOption = useCallback(() => setSelectedTags([]), [setSelectedTags]);
136+
useEffect(() => {
137+
if (selectedTags.length === 0 || !canSelectMultiple) {
138+
return;
139+
}
140+
141+
setSelectedTags((prevSelectedTags) => {
142+
const newSelectedTags = [];
143+
144+
for (const tagName of prevSelectedTags) {
145+
if (isMultiLevelTags) {
146+
const tagListExists = tagsList?.[tagName];
147+
if (!tagListExists) {
148+
const renamedTagList = Object.entries(tagsList ?? {}).find(([, tagList]) => {
149+
const typedTagList = tagList as {previousTagName?: string};
150+
return typedTagList.previousTagName === tagName;
151+
});
152+
if (renamedTagList) {
153+
newSelectedTags.push(renamedTagList[0]);
154+
continue;
155+
}
156+
}
157+
158+
if (tagListExists && tagListExists.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
159+
newSelectedTags.push(tagName);
160+
}
161+
} else {
162+
const tagExists = tagsList?.[tagName];
163+
if (!tagExists) {
164+
const renamedTag = Object.entries(tagsList ?? {}).find(([, tag]) => {
165+
const typedTag = tag as {previousTagName?: string};
166+
return typedTag.previousTagName === tagName;
167+
});
168+
if (renamedTag) {
169+
newSelectedTags.push(renamedTag[0]);
170+
continue;
171+
}
172+
}
173+
174+
if (tagExists && tagExists.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
175+
newSelectedTags.push(tagName);
176+
}
177+
}
178+
}
179+
180+
return newSelectedTags;
181+
});
182+
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
183+
}, [tagsList]);
184+
const cleanupSelectedOption = useCallback(() => setSelectedTags([]), []);
140185
useCleanupSelectedOptions(cleanupSelectedOption);
141186

142187
useSearchBackPress({

0 commit comments

Comments
 (0)