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

fix(web): timeline block speed #1233

Merged
merged 12 commits into from
Nov 13, 2024
Merged

fix(web): timeline block speed #1233

merged 12 commits into from
Nov 13, 2024

Conversation

mkumbobeaty
Copy link
Contributor

@mkumbobeaty mkumbobeaty commented Nov 8, 2024

Overview

  • Added default Not set option with the speed from current visualizer speed.

What I've done

What I haven't done

How I tested

Which point I want you to review particularly

Memo

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Removed unnecessary functions for time conversion to streamline functionality.
    • Updated speed change handling to improve error management during playback speed adjustments.
    • Minor formatting adjustments made to ensure code adheres to best practices.
  • Chores

    • Enhanced readability of configuration files without altering functionality.

Copy link

coderabbitai bot commented Nov 8, 2024

Walkthrough

This pull request involves the removal of specific functions related to time conversion from the utils.ts file and updates to the useTimelineBlock.ts file to reflect these removals. The handleSubmit functions in both the CZML and KML hooks are modified for minor formatting changes, while the isValidUrl function in the utility file is updated for code clarity. Additionally, the vite.config.ts file is reformatted for improved readability. Overall, the changes focus on code maintenance and structural adjustments without altering core functionalities.

Changes

File Path Change Summary
web/src/beta/features/Visualizer/Crust/StoryPanel/utils.ts Removed functions: timeStringToSeconds and convertOptionToSeconds.
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts Removed convertOptionToSeconds, updated playSpeedOptions to an array of objects, added TRANSITION_SPEED, and modified handleOnSpeedChange for promise-based speed change.
web/src/beta/features/Editor/Map/DataSourceLayerCreator/CZML/hooks.tsx Adjusted handleSubmit to explicitly set type to "czml" in the data object.
web/src/beta/features/Editor/Map/DataSourceLayerCreator/KML/hooks.tsx Removed a trailing comma in the config object within handleSubmit.
web/src/beta/features/Editor/Map/DataSourceLayerCreator/util.ts Updated isValidUrl for code formatting; no functional changes.
web/vite.config.ts Reformatted __REEARTH_COMMIT_HASH__ definition from single-line to multi-line for readability; no functional changes.

Possibly related PRs

Suggested reviewers

  • airslice
  • m-abe-dev

Poem

🐇 In the code where time once flowed,
Functions gone, a lighter load.
With a hop and a skip, we clean,
Formatting bright, our code's a sheen.
So let’s cheer for the tidy space,
In our project, we find our grace! ✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Nov 8, 2024

Deploy Preview for reearth-web ready!

Name Link
🔨 Latest commit 96dbcaa
🔍 Latest deploy log https://app.netlify.com/sites/reearth-web/deploys/67346a052cc89700085e3f47
😎 Deploy Preview https://deploy-preview-1233--reearth-web.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mkumbobeaty mkumbobeaty marked this pull request as ready for review November 8, 2024 10:25
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2)

47-61: Optimize playSpeedOptions implementation and add validation.

The current implementation has several potential improvements:

  1. Static options are recreated on every render
  2. No validation for multiplier value
  3. Potential confusion if multiplier matches a predefined speed

Consider this optimization:

+ const STATIC_SPEED_OPTIONS = [
+   { timeString: "1sec/sec", seconds: 1 },
+   { timeString: "0.5min/sec", seconds: 30 },
+   { timeString: "1min/sec", seconds: 60 },
+   { timeString: "0.1hr/sec", seconds: 360 },
+   { timeString: "0.5hr/sec", seconds: 1800 },
+   { timeString: "1hr/sec", seconds: 3600 }
+ ];

  const playSpeedOptions = useMemo(() => {
+   // Validate multiplier is within reasonable bounds
+   const validMultiplier = multiplier > 0 && multiplier <= 7200 ? multiplier : 1;
+   
    return [
      {
        timeString: "Not set",
-       seconds: multiplier
+       seconds: validMultiplier
      },
-     { timeString: "1sec/sec", seconds: 1 },
-     { timeString: "0.5min/sec", seconds: 30 },
-     { timeString: "1min/sec", seconds: 60 },
-     { timeString: "0.1hr/sec", seconds: 360 },
-     { timeString: "0.5hr/sec", seconds: 1800 },
-     { timeString: "1hr/sec", seconds: 3600 }
+     ...STATIC_SPEED_OPTIONS
    ];
  }, [multiplier]);

43-61: Consider enhancing timeline speed management architecture.

While the implementation achieves the goal of adding a "Not set" speed option, consider these architectural improvements:

  1. Create a dedicated TimelineSpeed type/interface to encapsulate speed-related logic
  2. Add validation rules for speed values
  3. Consider implementing a speed conversion utility for better maintainability

Example structure:

interface TimelineSpeed {
  timeString: string;
  seconds: number;
  isCustom?: boolean;
}

class TimelineSpeedManager {
  private static validateSpeed(speed: number): boolean {
    return speed > 0 && speed <= 7200; // 2 hours/second max
  }
  
  static createCustomSpeed(multiplier: number): TimelineSpeed {
    const validMultiplier = this.validateSpeed(multiplier) ? multiplier : 1;
    return {
      timeString: "Not set",
      seconds: validMultiplier,
      isCustom: true
    };
  }
}
web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts (1)

94-94: Add type safety for speed options and improve validation.

The speed selection logic could benefit from:

  1. A type-safe enum or const object for speed options
  2. Validation in handleOnSelect to ensure selected values are valid

Consider adding:

// Speed options type
type SpeedOption = {
  label: string;
  value: number;
};

const SPEED_OPTIONS: readonly SpeedOption[] = [
  { label: "Not set", value: 0 }, // or appropriate value
  // ... other options
] as const;

// Update state type
const [selected, setSelected] = useState<SpeedOption["label"]>("Not set");

// Update handler with validation
const handleOnSelect = useCallback(
  (value: SpeedOption["label"], second: number) => {
    if (!inEditor) {
      setIsOpen(false);
      if (value !== selected && SPEED_OPTIONS.some(opt => opt.label === value)) {
        setSelected(value);
        const speedValue = isPlayingReversed ? second * -1 : second;
        onSpeedChange?.(speedValue, committer.id);
      }
    }
  },
  [committer.id, inEditor, isPlayingReversed, onSpeedChange, selected]
);

Also applies to: 146-165

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3d0f736 and 06b95ce.

📒 Files selected for processing (3)
  • web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts (1 hunks)
  • web/src/beta/features/Visualizer/Crust/StoryPanel/utils.ts (0 hunks)
  • web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • web/src/beta/features/Visualizer/Crust/StoryPanel/utils.ts
🔇 Additional comments (2)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1)

63-63: Verify speed state updates and timeline behavior.

The speed state initialization and update logic looks correct, but we should verify:

  1. Speed state updates correctly when the multiplier changes
  2. Timeline behaves correctly with the "Not set" speed option

Also applies to: 156-162

✅ Verification successful

Let me gather more specific information about the speed state implementation and tests.


Let me gather one final piece of information about the playSpeedOptions implementation to understand the "Not set" speed behavior.


Based on the gathered information, I can now provide a final response:

Speed state implementation is correct and handles all cases properly

The implementation correctly handles both requirements:

  1. Speed state updates properly with multiplier changes:
    • Initial speed uses playSpeedOptions[0].seconds which is set to the multiplier value for "Not set" option
    • handleOnSpeedChange correctly updates the speed state and propagates changes via onSpeedChange callback
  2. "Not set" speed option is properly implemented:
    • It's the first option in playSpeedOptions array
    • Its value is dynamically set to the current multiplier
    • Timeline's speed state is initialized with this value
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for timeline speed-related tests and implementations
echo "Searching for timeline speed tests..."
rg -l "timeline.*speed|speed.*timeline" --type ts

echo "Searching for multiplier usage in tests..."
rg "multiplier" --type test

echo "Checking handleOnSpeedChange usage..."
ast-grep --pattern 'handleOnSpeedChange($$$)'

Length of output: 491


Script:

#!/bin/bash
# Search for speed-related state and handlers in the codebase
echo "Searching for speed state and handlers..."
rg -A 5 "const \[speed, setSpeed\]" --type ts

echo "Searching for playSpeedOptions..."
rg -A 3 "playSpeedOptions" --type ts

echo "Searching for speed-related test files..."
fd ".*\.test\.(ts|tsx)$" | xargs rg -l "speed"

echo "Looking for timeline speed change handlers..."
ast-grep --pattern 'const handleOnSpeedChange = $_'

Length of output: 6069


Script:

#!/bin/bash
# Check the complete implementation of playSpeedOptions
echo "Checking playSpeedOptions implementation..."
rg -A 10 "const playSpeedOptions = useMemo" --type ts

echo "Looking for speed option types..."
rg "interface.*Speed" --type ts

Length of output: 1372

web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts (1)

94-94: Ensure synchronization between "Not set" state and current visualizer speed.

The initial state change to "Not set" aligns with the PR objective. However, there should be a mechanism to ensure this state stays synchronized with visualizerContext?.current?.timeline?.current?.options.multiplier.

Let's verify the speed options handling:

✅ Verification successful

This will help us understand:

  1. How the hook is implemented and how it manages speed state
  2. How the Timeline component uses the hook
  3. The relationship between selected state and speed changes

This will help us understand:

  1. The complete implementation of the Timeline hook and how it manages the selected state
  2. The shared timeline hook implementation that defines the speed options and their synchronization

The initial state change to "Not set" is correct and properly synchronized with the visualizer speed

The implementation shows that:

  1. The playSpeedOptions in useTimelineBlock hook correctly initializes with "Not set" option that uses the current visualizer multiplier value
  2. The selected state in the Timeline hook is properly synchronized through handleOnSelect which triggers onSpeedChange to update the visualizer
  3. The speed state is properly managed between the Timeline component and the visualizer context
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for speed options definition and usage
echo "Searching for speed options definition..."
rg -A 5 "playSpeedOptions|speedOptions" 

echo "Searching for multiplier usage..."
rg -A 5 "options.multiplier"

Length of output: 3299


Script:

#!/bin/bash
# Let's check the hook implementation and its usage
echo "Checking hook implementation..."
ast-grep --pattern 'const useTimelineBlock = $_' -A 30

echo "Checking Timeline component usage..."
rg -A 10 "useTimelineBlock|handleOnSelect.*timeString" 

echo "Checking speed state management..."
rg -A 5 "setSelected.*timeString|handleOnSelect.*timeString"

Length of output: 6032


Script:

#!/bin/bash
# Let's check the implementation of the hook file and speed handling
echo "Checking Timeline hook implementation..."
cat web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts

echo "Checking shared timeline hook..."
cat web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts

Length of output: 18534

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 06b95ce and 82e882a.

📒 Files selected for processing (1)
  • web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (3 hunks)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1)

27-38: Consider adding type safety to playSpeedOptions.

The implementation correctly follows the requirement of using 0 for the "Not set" option. Consider these type-safety improvements:

+interface SpeedOption {
+  readonly timeString: string;
+  readonly seconds: number;
+}

-const playSpeedOptions = [
+const playSpeedOptions: readonly SpeedOption[] = [
   {
     timeString: "Not set",
     seconds: 0
   },
   // ... rest of the options
 ] as const;
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1482229 and 1fde1c4.

📒 Files selected for processing (6)
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/CZML/hooks.tsx (1 hunks)
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/KML/hooks.tsx (1 hunks)
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/util.ts (1 hunks)
  • web/src/beta/features/Visualizer/Crust/StoryPanel/utils.ts (0 hunks)
  • web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2 hunks)
  • web/vite.config.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • web/src/beta/features/Visualizer/Crust/StoryPanel/utils.ts
✅ Files skipped from review due to trivial changes (4)
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/CZML/hooks.tsx
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/KML/hooks.tsx
  • web/src/beta/features/Editor/Map/DataSourceLayerCreator/util.ts
  • web/vite.config.ts
🧰 Additional context used
📓 Learnings (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2)
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts:47-59
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In the `useTimelineBlock.ts` file, for the "Not set" option in `playSpeedOptions`, we should set `seconds` to `0` and not use the current multiplier from `visualizerContext?.current?.timeline?.current?.options.multiplier`.
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts:94-94
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In `web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts`, when the speed option is set to "Not set", we intentionally do not set any speed because we don't want to override the existing speed.
🔇 Additional comments (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1)

157-170: ⚠️ Potential issue

Improve speed change sequencing and error handling.

The current implementation has potential issues:

  1. Using setTimeout(fn, 0) is an anti-pattern for sequencing operations
  2. Race conditions could occur if multiple speed changes are triggered rapidly
  3. Error state might be inconsistent if the second speed change fails

Consider this more robust implementation:

 const handleOnSpeedChange = useCallback(
   (speed: number, committerId?: string) => {
     try {
-      onSpeedChange(TRANSITION_SPEED, committerId);
-      setTimeout(() => {
+      // Use requestAnimationFrame for better sequencing
+      onSpeedChange(TRANSITION_SPEED, committerId);
+      requestAnimationFrame(() => {
         onSpeedChange(speed, committerId);
         setSpeed(speed);
-      }, 0);
+      });
     } catch (error) {
       console.error("Error during speed change:", error);
-      setSpeed(playSpeedOptions[0].seconds);
-      throw error;
+      // Ensure consistent state
+      const defaultSpeed = playSpeedOptions[0].seconds;
+      onSpeedChange(defaultSpeed, committerId);
+      setSpeed(defaultSpeed);
     }
   },
-  [onSpeedChange]
+  [onSpeedChange, playSpeedOptions]
 );

Let's verify the impact of rapid speed changes:

✅ Verification successful

Speed change implementation is acceptable.

No additional conflicting speed change handlers or potential race conditions were detected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other speed change handlers that might conflict
rg -A 5 "onSpeedChange.*committerId" 

# Look for potential race conditions in timeline commits
ast-grep --pattern 'commit({
  cmd: "SET_OPTIONS",
  $$$
})'

Length of output: 1733

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1fde1c4 and 223fb9e.

📒 Files selected for processing (1)
  • web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2 hunks)
🧰 Additional context used
📓 Learnings (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2)
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts:47-59
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In the `useTimelineBlock.ts` file, for the "Not set" option in `playSpeedOptions`, we should set `seconds` to `0` and not use the current multiplier from `visualizerContext?.current?.timeline?.current?.options.multiplier`.
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts:94-94
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In `web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts`, when the speed option is set to "Not set", we intentionally do not set any speed because we don't want to override the existing speed.
🔇 Additional comments (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1)

36-36: ⚠️ Potential issue

Potential conflict between TRANSITION_SPEED and "Not set" speed

Setting TRANSITION_SPEED to 0 might conflict with the "Not set" option which should also use 0. Consider using a different transition value to avoid confusion.

-const TRANSITION_SPEED = 0;
+// Use a distinctive value to avoid confusion with "Not set" (0)
+const TRANSITION_SPEED = -1;

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 223fb9e and 96dbcaa.

📒 Files selected for processing (1)
  • web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2 hunks)
🧰 Additional context used
📓 Learnings (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (2)
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts:47-59
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In the `useTimelineBlock.ts` file, for the "Not set" option in `playSpeedOptions`, we should set `seconds` to `0` and not use the current multiplier from `visualizerContext?.current?.timeline?.current?.options.multiplier`.
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts:94-94
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In `web/src/beta/features/Visualizer/Crust/StoryPanel/Block/builtin/Timeline/hook.ts`, when the speed option is set to "Not set", we intentionally do not set any speed because we don't want to override the existing speed.
🔇 Additional comments (1)
web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts (1)

27-34: 🛠️ Refactor suggestion

Move constant outside module and add "Not set" option.

The playSpeedOptions array should be:

  1. Moved outside the module scope as it's a static constant
  2. Include the "Not set" option as per PR objectives
+export const playSpeedOptions = [
+  { timeString: "Not set", seconds: 0 },
+  { timeString: "1sec/sec", seconds: 1 },
+  { timeString: "0.5min/sec", seconds: 30 },
+  { timeString: "1min/sec", seconds: 60 },
+  { timeString: "0.1hr/sec", seconds: 360 },
+  { timeString: "0.5hr/sec", seconds: 1800 },
+  { timeString: "1hr/sec", seconds: 3600 }
+];
⛔ Skipped due to learnings
Learnt from: mkumbobeaty
PR: reearth/reearth-visualizer#1233
File: web/src/beta/features/Visualizer/shared/hooks/useTimelineBlock.ts:47-59
Timestamp: 2024-11-12T15:21:04.151Z
Learning: In the `useTimelineBlock.ts` file, for the "Not set" option in `playSpeedOptions`, we should set `seconds` to `0` and not use the current multiplier from `visualizerContext?.current?.timeline?.current?.options.multiplier`.

{ timeString: "1hr/sec", seconds: 3600 }
];

const TRANSITION_SPEED = 0;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Revise transition speed value and name.

The TRANSITION_SPEED constant:

  1. Should not be 0 as it could conflict with "Not set" option
  2. Name should reflect its purpose as an intermediate value
-const TRANSITION_SPEED = 0;
+// Intermediate speed value used to ensure speed changes are registered
+const INTERMEDIATE_SPEED = 100;
📝 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
const TRANSITION_SPEED = 0;
// Intermediate speed value used to ensure speed changes are registered
const INTERMEDIATE_SPEED = 100;

Comment on lines 155 to +165
(speed: number, committerId?: string) => {
onSpeedChange?.(speed, committerId);
setSpeed(speed);
try {
onSpeedChange(TRANSITION_SPEED, committerId);
setTimeout(() => {
onSpeedChange(speed, committerId);
setSpeed(speed);
}, 0);
} catch (error) {
setSpeed(playSpeedOptions[0].seconds);
throw error;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Simplify speed change handling and respect "Not set" option.

The current implementation has several issues:

  1. Uses unnecessary setTimeout
  2. Error handling resets speed inappropriately
  3. Doesn't handle "Not set" option correctly
 const handleOnSpeedChange = useCallback(
   (speed: number, committerId?: string) => {
     try {
+      // Don't override speed when "Not set" (0) is selected
+      if (speed === 0) return;
+
-      onSpeedChange(TRANSITION_SPEED, committerId);
-      setTimeout(() => {
-        onSpeedChange(speed, committerId);
-        setSpeed(speed);
-      }, 0);
+      onSpeedChange(INTERMEDIATE_SPEED, committerId);
+      onSpeedChange(speed, committerId);
+      setSpeed(speed);
     } catch (error) {
       console.error("Error during speed change:", error);
-      setSpeed(playSpeedOptions[0].seconds);
       throw error;
     }
   },
   [onSpeedChange]
 );
📝 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
(speed: number, committerId?: string) => {
onSpeedChange?.(speed, committerId);
setSpeed(speed);
try {
onSpeedChange(TRANSITION_SPEED, committerId);
setTimeout(() => {
onSpeedChange(speed, committerId);
setSpeed(speed);
}, 0);
} catch (error) {
setSpeed(playSpeedOptions[0].seconds);
throw error;
}
(speed: number, committerId?: string) => {
try {
// Don't override speed when "Not set" (0) is selected
if (speed === 0) return;
onSpeedChange(INTERMEDIATE_SPEED, committerId);
onSpeedChange(speed, committerId);
setSpeed(speed);
} catch (error) {
console.error("Error during speed change:", error);
throw error;
}

@mkumbobeaty mkumbobeaty merged commit f333ed0 into main Nov 13, 2024
13 checks passed
@mkumbobeaty mkumbobeaty deleted the fix/timeline-block-speed branch November 13, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants