Skip to content
Open
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
22 changes: 18 additions & 4 deletions packages/app/src/features/MainScreen/TagsPanel/TagsTree.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, FC, useEffect, useMemo, useRef } from 'react';
import React, { createContext, FC, useMemo, useRef } from 'react';
import {
buildProxiedInstance,
FeatureImplementation,
Expand Down Expand Up @@ -64,6 +64,22 @@ export type ITagsListProps = {
contextMenu: TagContextMenuCallbacks;
};

/**
* Rebuild the tree while rendering, before rendering child components,
* otherwise they may use a stale tree that is out of sync with the latest tags list
*/
const useRebuildTreeOnTagsChange = (
tree: { rebuildTree: () => void },
tags: Record<string, TagNode>,
) => {
const prevTagsRef = useRef(tags);
if (prevTagsRef.current !== tags) {
prevTagsRef.current = tags;

tree.rebuildTree();
}
};

export const TagsTree: FC<ITagsListProps> = ({
tags,
activeTag,
Expand Down Expand Up @@ -118,9 +134,7 @@ export const TagsTree: FC<ITagsListProps> = ({
});

// Rebuild tree by changes
useEffect(() => {
tree.rebuildTree();
}, [tags, tree]);
useRebuildTreeOnTagsChange(tree, tags);

return (
<TagsListContext value={context}>
Expand Down