Skip to content

Fix incorrect index passing in ImageTimeline handleClick#107

Open
Godzilla675 wants to merge 1 commit into
mainfrom
jules-10598138742057226291-3718f656
Open

Fix incorrect index passing in ImageTimeline handleClick#107
Godzilla675 wants to merge 1 commit into
mainfrom
jules-10598138742057226291-3718f656

Conversation

@Godzilla675

Copy link
Copy Markdown
Owner
  • Replaced parameter index (which was actually the id) with id in handleClick.
  • Implemented mediaFileIndices via useMemo for O(1) index lookups by ID, matching the pattern in other timeline elements.
  • Replaced useDispatch from react-redux with strongly-typed useAppDispatch from @/app/store.
  • Removed unnecessary as any and as unknown as string casts.
  • Added conditional checks before dispatching Redux state updates to prevent unnecessary re-renders.

PR created automatically by Jules for task 10598138742057226291 started by @Godzilla675

Copilot AI review requested due to automatic review settings March 3, 2026 11:56
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue in the ImageTimeline component where media file indexing was handled incorrectly, leading to potential bugs. The changes streamline the process of identifying and activating media elements by leveraging direct ID lookups and optimizing state updates. This improves the component's reliability, performance, and adherence to best practices for Redux and React hooks.

Highlights

  • Refactored handleClick logic: The handleClick function in ImageTimeline was updated to correctly use id instead of an incorrectly passed index parameter, resolving a bug where the index was actually the id.
  • Optimized media file index lookups: A mediaFileIndices map was introduced using useMemo to provide O(1) lookup times for media file indices by their ID, improving performance and aligning with patterns in other timeline elements.
  • Improved Redux dispatching: The component now uses the strongly-typed useAppDispatch from @/app/store instead of useDispatch from react-redux. Additionally, conditional checks were added before dispatching Redux state updates to prevent unnecessary re-renders.
  • Removed unnecessary type assertions: Redundant as any and as unknown as string type casts were removed, enhancing type safety and code clarity.
Changelog
  • apps/web/app/components/editor/timeline/elements-timeline/ImageTimeline.tsx
    • Updated Redux dispatch hook from useDispatch to useAppDispatch.
    • Refactored handleClick to accept id directly and use mediaFileIndices for efficient lookups.
    • Implemented mediaFileIndices using useMemo for O(1) index retrieval.
    • Added conditional checks to Redux dispatch calls for setActiveElement and setActiveElementIndex to avoid redundant updates.
    • Removed unnecessary as any and as unknown as string type assertions.
Activity
  • PR created automatically by Jules for task 10598138742057226291, initiated by @Godzilla675.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several valuable improvements to the ImageTimeline component. The change from useDispatch to the strongly-typed useAppDispatch enhances type safety. The introduction of mediaFileIndices with useMemo is a great performance optimization for looking up media file indices, and the refactoring of handleClick to use it, along with removing unsafe type casts and adding conditional dispatches, significantly improves the code's correctness, performance, and maintainability. I have one suggestion to make the creation of the mediaFileIndices map more concise.

Comment on lines +26 to +32
const mediaFileIndices = useMemo(() => {
const indices = new Map<string, number>();
mediaFiles.forEach((file, index) => {
indices.set(file.id, index);
});
return indices;
}, [mediaFiles]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

For better conciseness and to use a more declarative style, you can construct the Map directly from the result of mediaFiles.map(). This is a more idiomatic way to create a map from an array of items in modern JavaScript/TypeScript.

    const mediaFileIndices = useMemo(
        () => new Map(mediaFiles.map((file, index) => [file.id, index])),
        [mediaFiles]
    );

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes media element selection in ImageTimeline by ensuring clicks/drags identify the active media clip via its id (not a misnamed “index”), aligning the selection approach with other timeline components and reducing unnecessary Redux dispatches.

Changes:

  • Replaced the handleClick parameter from a misused index to id, and updated selection to derive the correct mediaFiles index by ID.
  • Added a memoized mediaFileIndices map for constant-time ID → index lookups.
  • Switched from react-redux’s useDispatch to the app-typed useAppDispatch, removed unsafe casts, and added conditional dispatch guards.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -12,7 +11,7 @@ import { debounce, throttle } from "lodash";
export default function ImageTimeline() {
const targetRefs = useRef<Record<string, HTMLDivElement | null>>({});
const { mediaFiles, textElements, activeElement, activeElementIndex, timelineZoom } = useAppSelector((state) => state.projectState);

Copilot AI Mar 3, 2026

Copy link

Choose a reason for hiding this comment

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

useAppSelector((state) => state.projectState) subscribes this component to all projectState changes (even unrelated ones like currentTime), which can cause frequent re-renders. Since this PR is already optimizing selection/dispatch, consider switching to separate selectors for mediaFiles, activeElement, activeElementIndex, and timelineZoom (as done in VideoTimeline) and drop the unused textElements destructure to reduce renders.

Suggested change
const { mediaFiles, textElements, activeElement, activeElementIndex, timelineZoom } = useAppSelector((state) => state.projectState);
const mediaFiles = useAppSelector((state) => state.projectState.mediaFiles);
const activeElement = useAppSelector((state) => state.projectState.activeElement);
const activeElementIndex = useAppSelector((state) => state.projectState.activeElementIndex);
const timelineZoom = useAppSelector((state) => state.projectState.timelineZoom);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants