Skip to content

Commit

Permalink
after load handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpkane committed Oct 4, 2024
1 parent d746a9f commit bd15f87
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/packages/core/src/components/Modal/ImaVidLooker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const ImaVidLookerReact = React.memo(

useEventHandler(looker, "clear", useClearSelectedLabels());

useKeyEvents(sample._id, looker);
useKeyEvents(initialRef, sample._id, looker);

const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
Expand Down
29 changes: 22 additions & 7 deletions app/packages/core/src/components/Modal/use-key-events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Lookers } from "@fiftyone/state";
import { hoveredSample } from "@fiftyone/state";
import { useEffect } from "react";
import type { MutableRefObject } from "react";
import { useEffect, useRef } from "react";
import { selector, useRecoilValue } from "recoil";

export const hoveredSampleId = selector<string>({
Expand All @@ -10,16 +11,30 @@ export const hoveredSampleId = selector<string>({
},
});

export default function (id: string, looker: Lookers) {
export default function (
ref: MutableRefObject<boolean>,
id: string,
looker: Lookers
) {
const hoveredId = useRecoilValue(hoveredSampleId);
const ready = useRef(false);

useEffect(() => {
const load = () => {
if (ref.current) {
// initial call should wait for load event
const update = () => {
looker.updateOptions({
shouldHandleKeyEvents: id === hoveredId,
});
ready.current = true;

looker.removeEventListener("load", update);
};
looker.addEventListener("load", update);
} else if (ready.current) {
looker.updateOptions({
shouldHandleKeyEvents: id === hoveredId,
});
looker.removeEventListener("load", load);
};
looker.addEventListener("load", load);
}, [hoveredId, id, looker]);
}
}, [hoveredId, id, looker, ref]);
}
2 changes: 1 addition & 1 deletion app/packages/core/src/components/Modal/use-looker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function useLooker<L extends fos.Lookers>({
}
);

useKeyEvents(sample.sample._id, looker);
useKeyEvents(initialRef, sample.sample._id, looker);

return { id, looker, ref, sample, updateLookerOptions };
}
Expand Down

0 comments on commit bd15f87

Please sign in to comment.