Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address dynamic groups PR comments #3000

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/packages/core/src/components/ImageContainerHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const ImageContainerHeader = () => {
const groupStats = useRecoilValue(groupStatistics(false));

const shouldShowSliceSelector = useMemo(
() => isGroup && groupSlices.length > 0,
() => isGroup && groupSlices.length > 1,
[isGroup, groupSlices]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as foq from "@fiftyone/relay";
import * as fos from "@fiftyone/state";
import React, {
MutableRefObject,
useCallback,
useEffect,
useId,
useLayoutEffect,
Expand Down Expand Up @@ -140,6 +141,24 @@ export const DynamicGroupsFlashlightWrapper = () => {
const flashlightRef = useRef<Flashlight<number>>();
selectSample.current = select;

const getScrollParams = useCallback(() => {
const flashlight = flashlightRef.current;

if (!flashlight) {
return;
}

const containerWidth = flashlight.element.clientWidth;
// elementWidth represents the width of the first element in the flashlight
const elementWidth =
flashlight.element.firstElementChild?.firstElementChild?.clientWidth ??
100;

const elementsCount = Math.ceil(containerWidth / elementWidth!);

return { elementWidth, elementsCount, containerWidth };
}, []);

const navigationCallback = useRecoilCallback(
({ snapshot }) =>
async (isPrevious) => {
Expand Down Expand Up @@ -167,18 +186,18 @@ export const DynamicGroupsFlashlightWrapper = () => {
const nextSample = store.samples.get(nextSampleId);
nextSample && setSample(nextSample);

// todo: very unstable, just ideating auto scroll based on quickstart groups params
// todo: implement better scrolling logic
if (flashlightRef.current) {
const { elementWidth } = getScrollParams()!;

const newLeft = isPrevious
? flashlightRef.current?.element.scrollLeft - 325 * 3
: flashlightRef.current?.element.scrollLeft + 325 * 3;

nextSampleIndex !== 0 &&
nextSampleIndex % 3 === 0 &&
flashlightRef.current?.element.scroll({
left: newLeft,
behavior: "smooth",
});
? flashlightRef.current?.element.scrollLeft - elementWidth
: flashlightRef.current?.element.scrollLeft + elementWidth;

flashlightRef.current?.element.scroll({
left: newLeft,
behavior: "smooth",
});
}
},
[setSample, store.indices, store.samples]
Expand All @@ -189,7 +208,7 @@ export const DynamicGroupsFlashlightWrapper = () => {
horizontal: true,
containerId: DYNAMIC_GROUPS_FLASHLIGHT_CONTAINER_ID,
elementId: DYNAMIC_GROUPS_FLASHLIGHT_ELEMENT_ID,
enableKeyNavigation: {
enableHorizontalKeyNavigation: {
navigationCallback,
previousKey: "<",
nextKey: ">",
Expand Down
2 changes: 1 addition & 1 deletion app/packages/core/src/components/Modal/Group/GroupView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const GroupView: React.FC<GroupViewProps> = ({ subBar }) => {

{!shouldSplitVertically && is3DVisible && <GroupSample3d />}
</div>
{subBar ?? subBar}
{subBar && subBar}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:

({ subBar = null })
// ...
{subBar}

</div>
);
};
13 changes: 6 additions & 7 deletions app/packages/flashlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface FlashlightConfig<K> {
options: FlashlightOptions;
elementId?: string;
containerId?: string;
enableKeyNavigation?: {
enableHorizontalKeyNavigation?: {
navigationCallback: (isPrev: boolean) => Promise<void>;
previousKey: string;
nextKey: string;
Expand Down Expand Up @@ -70,20 +70,19 @@ export default class Flashlight<K> {

document.addEventListener("visibilitychange", () => this.render());

if (config.enableKeyNavigation) {
if (config.enableHorizontalKeyNavigation && config.horizontal) {
const keyDownEventListener = (e) => {
if (!this.isAttached()) {
document.removeEventListener("keydown", keyDownEventListener);
return;
}

if (e.key === config.enableKeyNavigation.previousKey) {
if (e.key === config.enableHorizontalKeyNavigation.previousKey) {
e.preventDefault();
config.enableKeyNavigation.navigationCallback(true);
this.container.scrollLeft -= 316;
} else if (e.key === config.enableKeyNavigation.nextKey) {
config.enableHorizontalKeyNavigation.navigationCallback(true);
} else if (e.key === config.enableHorizontalKeyNavigation.nextKey) {
e.preventDefault();
config.enableKeyNavigation.navigationCallback(false);
config.enableHorizontalKeyNavigation.navigationCallback(false);
}
};

Expand Down
17 changes: 12 additions & 5 deletions app/packages/state/src/recoil/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import { RelayEnvironmentKey } from "./relay";
import { fieldSchema } from "./schema";
import { datasetName } from "./selectors";
import { dynamicGroupViewQuery, view } from "./view";
import {
FLOAT_FIELD,
FRAME_NUMBER_FIELD,
INT_FIELD,
OBJECT_ID_FIELD,
STRING_FIELD,
} from "@fiftyone/utilities";

export type SliceName = string | undefined | null;

Expand Down Expand Up @@ -240,11 +247,11 @@ export const dynamicGroupCandidateFields = selector<string[]>({
([_, { name, ftype }]) =>
name !== "filepath" &&
name !== "id" &&
(ftype === "fiftyone.core.fields.IntField" ||
ftype === "fiftyone.core.fields.FloatField" ||
ftype === "fiftyone.core.fields.StringField" ||
ftype === "fiftyone.core.fields.FrameNumberField" ||
ftype === "fiftyone.core.fields.ObjectIdField")
(ftype === INT_FIELD ||
ftype === FLOAT_FIELD ||
ftype === STRING_FIELD ||
ftype === FRAME_NUMBER_FIELD ||
ftype === OBJECT_ID_FIELD)
)
.map(([_, { name }]) => name);
},
Expand Down