Skip to content

Commit

Permalink
Refresh BookmarksMenu when it is opened. (#1378)
Browse files Browse the repository at this point in the history
Without adding this logic, the BookmarksMenu will not reflect any new
bookmarks that are added unless you refresh the browser. The reason is
because the menu is always mounted, as it lives at the `App` layer,
though hidden off screen. This means that the `useEffect()` call only
happens once, when the app is first loaded.

By capturing the `isOpen` variable in the `useEffect()`'s dependency
list, we can retrigger the `useEffect()` call whenever the menu is
opened, causing it to be updated with the latest state of the user's
bookmarks.
  • Loading branch information
richardxia authored Jun 28, 2024
1 parent 9791283 commit 306fb8a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/components/ui/BookmarksMenu/BookmarksMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,24 @@ export const BookmarksMenu = ({
itemListElement="div"
width={450}
>
<BookmarksInnerMenu toggleMenu={toggleBookmarksMenu} />
<BookmarksInnerMenu toggleMenu={toggleBookmarksMenu} isOpen={isOpen} />
</Menu>
);

const BookmarksInnerMenu = ({
toggleMenu,
isOpen,
}: {
toggleMenu: (open: boolean) => void;
isOpen: boolean;
}) => {
const [activeFolder, setActiveFolder] = useState<number | null>(null);
const [bookmarkFolders, setBookmarkFolders] = useState<Folder[]>([]);
const { authState } = useAppContext();

useEffect(() => {
if (!authState) return;
if (!isOpen) return;
const authToken = authState.accessTokenObject.token;
getCurrentUser(authToken).then((user) =>
Promise.all([
Expand All @@ -97,7 +100,11 @@ const BookmarksInnerMenu = ({
setBookmarkFolders(transformedFolders);
})
);
}, [authState]);
// We capture isOpen because we want to refresh this menu every time this
// modal is reopened. Otherwise, if you add a bookmark, navigate to the
// dashboard, and then open this BookmarkMenu, it will not reflect the newly
// added bookmarks.
}, [authState, isOpen]);

const showFolders = () => {
if (activeFolder !== null) {
Expand Down

0 comments on commit 306fb8a

Please sign in to comment.