Fix incorrect index passing in ImageTimeline handleClick#107
Fix incorrect index passing in ImageTimeline handleClick#107Godzilla675 wants to merge 1 commit into
Conversation
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Summary of ChangesHello, 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 Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| const mediaFileIndices = useMemo(() => { | ||
| const indices = new Map<string, number>(); | ||
| mediaFiles.forEach((file, index) => { | ||
| indices.set(file.id, index); | ||
| }); | ||
| return indices; | ||
| }, [mediaFiles]); |
There was a problem hiding this comment.
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]
);
There was a problem hiding this comment.
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
handleClickparameter from a misusedindextoid, and updated selection to derive the correctmediaFilesindex by ID. - Added a memoized
mediaFileIndicesmap for constant-time ID → index lookups. - Switched from
react-redux’suseDispatchto the app-typeduseAppDispatch, 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); | |||
There was a problem hiding this comment.
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.
| 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); |
index(which was actually theid) withidinhandleClick.mediaFileIndicesviauseMemofor O(1) index lookups by ID, matching the pattern in other timeline elements.useDispatchfromreact-reduxwith strongly-typeduseAppDispatchfrom@/app/store.as anyandas unknown as stringcasts.PR created automatically by Jules for task 10598138742057226291 started by @Godzilla675