-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refresh layout when sidebar is updated
- Loading branch information
1 parent
b2be9d0
commit 177ce37
Showing
4 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/packages/core/src/components/Sidebar/useOnSidebarSelectionChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { activeLabelFields, labelsToggleTracker } from "@fiftyone/state"; | ||
import { useEffect, useRef } from "react"; | ||
import { useRecoilValue, useSetRecoilState } from "recoil"; | ||
import { gridPage } from "../Grid/recoil"; | ||
|
||
/** | ||
* This hook is used to update the sidebar tracker when the user changes the | ||
* selection of labels in the sidebar. We keep a map of grid page to the active | ||
* label fields for that page. | ||
*/ | ||
export const useOnSidebarSelectionChange = () => { | ||
const activeLabelFieldsValue = useRecoilValue( | ||
activeLabelFields({ modal: false }) | ||
); | ||
const gridPageValue = useRecoilValue(gridPage); | ||
|
||
const gridPageValueRef = useRef(gridPageValue); | ||
|
||
gridPageValueRef.current = gridPageValue; | ||
|
||
const setSidebarTracker = useSetRecoilState(labelsToggleTracker); | ||
|
||
const debugSidebarTracker = useRecoilValue(labelsToggleTracker); | ||
|
||
useEffect(() => { | ||
const thisPageActiveFields = debugSidebarTracker.get(gridPageValue); | ||
|
||
const currentActiveLabelFields = new Set(activeLabelFieldsValue); | ||
|
||
if (currentActiveLabelFields.size === 0) { | ||
return; | ||
} | ||
|
||
// diff the two sets, we only care about net new fields | ||
// if there are no new fields, we don't need to update the tracker | ||
let hasNewFields = false; | ||
if (thisPageActiveFields) { | ||
for (const field of currentActiveLabelFields) { | ||
if (!thisPageActiveFields.has(field)) { | ||
hasNewFields = true; | ||
break; | ||
} | ||
} | ||
} else { | ||
hasNewFields = true; | ||
} | ||
|
||
if (!hasNewFields) { | ||
return; | ||
} | ||
|
||
const newTracker = new Map([ | ||
...debugSidebarTracker, | ||
[gridPageValueRef.current, new Set(activeLabelFieldsValue)], | ||
]); | ||
|
||
if (newTracker.size === 0) { | ||
return; | ||
} | ||
|
||
setSidebarTracker(newTracker); | ||
}, [activeLabelFieldsValue, debugSidebarTracker]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters