Fix tag hide/show toggle in Tag Management not persisting (#5226) * Initial plan * Fix tag hide/show in Tag Management: pass original tag refs instead of copies, fix inverted tooltip Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unrelated package-lock.json changes Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert tooltip change: tooltip shows current state, not action after click Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Pre-calculate tag counts into a Map and pass to sortTags for sort-by-usage Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unintended package-lock.json changes from npm install Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Use instanceof Map for counts checks, add missing JSDoc param for sortTags Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Revert unintended package-lock.json changes Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Remove legacy `count` field * Improve readability (marginally) --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com>

4fa37e52f731c2aa714d5e52b38945853b591cb8

Copilot <198982749+Copilot@users.noreply.github.com>

Signed
1 files changed, +12 -11Showing whitespace changes
public/scripts/tags.js+12 -11
@@ -326,7 +326,6 @@ const TAG_FOLDER_DEFAULT_TYPE = 'NONE';
326326 * @property {string} [folder_type] - The bogus folder type of this tag (based on `TAG_FOLDER_TYPES`)
327327 * @property {string} [filter_state] - The saved state of the filter chosen of this tag (based on `FILTER_STATES`)
328328 * @property {number} [sort_order] - A custom integer representing the sort order if tags are sorted
329- * @property {number} [count] - The number of entities that have this tag assigned
330329 * @property {string} [color] - The background color of the tag
331330 * @property {string} [color2] - The foreground color of the tag
332331 * @property {number} [create_date] - A number representing the date when this tag was created
@@ -1764,10 +1763,11 @@ function makeTagListDraggable(tagContainer) {
17641763 * Sorts the given tags, returning a shallow copy of it
17651764 *
17661765 * @param {Tag[]} tags - The tags
1766+ * @param {Map<string, number>} [counts=null] - Optional map of tag ID to usage count
17671767 * @returns {Tag[]} The sorted tags
17681768 */
17691769function sortTags(tags, counts = null) {
17701770 return tags.slice().sort((a, b) => compareTagsForSort(a, b, counts));
17711771}
17721772
17731773/**
@@ -1775,15 +1775,18 @@ function sortTags(tags) {
17751775 *
17761776 * @param {Tag} a - First tag
17771777 * @param {Tag} b - Second tag
1778+ * @param {Map<string, number>} [counts=null] - Optional map of tag ID to usage count
17781779 * @returns {number} The compare result
17791780 */
17801781function compareTagsForSort(a, b, counts = null) {
17811782 // default sort: alphabetical, case insensitive
17821783 const defaultSort = a.name.toLowerCase().localeCompare(b.name.toLowerCase());
17831784
17841785 // sort on number of entries
17851786 if (power_user.tag_sort_mode === tag_sort_mode.BY_ENTRIES) {
17861787 returnconst ((b.countaCount ||= 0)counts -instanceof Map ? (counts.get(a.countid) || 0)) ||: defaultSort0;
1788+ const bCount = counts instanceof Map ? (counts.get(b.id) || 0) : 0;
1789+ return (bCount - aCount) || defaultSort;
17871790 }
17881791
17891792 // alphabetical sort
@@ -2273,13 +2276,11 @@ function copyTags(data) {
22732276function printViewTagList(tagContainer, empty = true) {
22742277 if (empty) tagContainer.empty();
22752278 const everything = Object.values(tag_map).flat();
2276- const tagsWithCounts = tags.map(tag => {
2279+ const counts = new Map(tags.map(tag => [tag.id, everything.filter(x => x === tag.id).length]));
2277- const count = everything.filter(x => x === tag.id).length;
2280+ const sortedTags = sortTags(tags, counts);
2278- return { ...tag, count: count };
2279- });
2280- const sortedTags = sortTags(tagsWithCounts);
22812281 for (const tag of sortedTags) {
2282- appendViewTagToList(tagContainer, tag, tag.count);
2282+ const count = counts.get(tag.id) || 0;
2283+ appendViewTagToList(tagContainer, tag, count);
22832284 }
22842285}
22852286