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

add timeline view #4965

Merged
merged 9 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ export default function FrameLoaderView(props: ViewPropsType) {
const setPanelState = useSetPanelStateById(true);
const localIdRef = React.useRef<string>();
const bufm = useRef(new BufferManager());
const frameDataRef = useRef();
ritch marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
localIdRef.current = Math.random().toString(36).substring(7);
if (data?.frames)
window.dispatchEvent(
new CustomEvent(`frames-loaded`, {
detail: { localId: localIdRef.current },
})
);
if (data?.frames) frameDataRef.current = data.frames;
window.dispatchEvent(
new CustomEvent(`frames-loaded`, {
ritch marked this conversation as resolved.
Show resolved Hide resolved
detail: { localId: localIdRef.current },
})
);
sashankaryal marked this conversation as resolved.
Show resolved Hide resolved
}, [data?.signature]);

const loadRange = React.useCallback(
Expand All @@ -41,15 +42,22 @@ export default function FrameLoaderView(props: ViewPropsType) {
}

return new Promise<void>((resolve) => {
window.addEventListener(`frames-loaded`, (e) => {
if (
e instanceof CustomEvent &&
e.detail.localId === localIdRef.current
) {
bufm.current.addNewRange(range);
resolve();
}
});
if (frameDataRef.current) {
bufm.current.addNewRange(range);
resolve();
} else {
window.addEventListener(`frames-loaded`, (e) => {
ritch marked this conversation as resolved.
Show resolved Hide resolved
if (
e instanceof CustomEvent &&
e.detail.localId === localIdRef.current
) {
try {
ritch marked this conversation as resolved.
Show resolved Hide resolved
bufm.current.addNewRange(range);
} catch (e) {}
resolve();
}
});
}
sashankaryal marked this conversation as resolved.
Show resolved Hide resolved
});
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ export default function PlotlyView(props: ViewPropsType) {
return (
<Box
{...getComponentProps(props, "container")}
useResizeHandler
sx={{ height: "100%", width: "100%" }}
>
<HeaderView {...props} nested />
Expand Down
47 changes: 47 additions & 0 deletions app/packages/core/src/plugins/SchemaIO/components/TimelineView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { Timeline, useCreateTimeline, useTimeline } from "@fiftyone/playback";
import { ViewPropsType } from "../utils/types";

export default function TimelineView(props: ViewPropsType) {
const { schema } = props;
const { view = {} } = schema;
const { timeline_name, loop, total_frames } = view;

const providedcConfig = {
loop,
totalFrames: total_frames,
};
ritch marked this conversation as resolved.
Show resolved Hide resolved

const defaultConfig = {
loop: false,
};
ritch marked this conversation as resolved.
Show resolved Hide resolved
const finalConfig = {
...defaultConfig,
...providedcConfig,
};
ritch marked this conversation as resolved.
Show resolved Hide resolved

if (!timeline_name) {
throw new Error("Timeline name is required");
}
if (!finalConfig.totalFrames) {
throw new Error("Total frames is required");
}

return <TimelineCreator timelineName={timeline_name} {...finalConfig} />;
}

export const TimelineCreator = ({ timelineName, totalFrames, loop }) => {
const { isTimelineInitialized } = useCreateTimeline({
name: timelineName,
config: {
totalFrames: totalFrames,
loop,
},
});
ritch marked this conversation as resolved.
Show resolved Hide resolved

if (!isTimelineInitialized) {
return null;
}
Comment on lines +38 to +40
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance loading state feedback

Returning null during initialization provides no feedback to users. Consider showing a loading indicator or message.

   if (!isTimelineInitialized) {
-    return null;
+    return <div aria-busy="true" role="status">Initializing timeline...</div>;
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!isTimelineInitialized) {
return null;
}
if (!isTimelineInitialized) {
return <div aria-busy="true" role="status">Initializing timeline...</div>;
}


return <Timeline name={timelineName} />;
};
1 change: 1 addition & 0 deletions app/packages/core/src/plugins/SchemaIO/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ export { default as TextFieldView } from "./TextFieldView";
export { default as TupleView } from "./TupleView";
export { default as UnsupportedView } from "./UnsupportedView";
export { default as FrameLoaderView } from "./FrameLoaderView";
export { default as TimelineView } from "./TimelineView";
13 changes: 13 additions & 0 deletions fiftyone/operators/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,19 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)


class TimelineView(View):
"""Represents a timeline for playing animations.

Args:
timeline_name (None): the name of the timeline
total_frames (None): the total number of frames in the timeline
loop (False): whether to loop the timeline
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)


ritch marked this conversation as resolved.
Show resolved Hide resolved
class Container(BaseType):
"""Represents a base container for a container types."""

Expand Down