Skip to content

Commit 900112e

Browse files
committed
fix tag list diselected when editing name
1 parent f8a6b55 commit 900112e

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

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)