Skip to content

feat(system_logs): Fixed auto scroll #630

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

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

Kamlesh72
Copy link
Contributor

@Kamlesh72 Kamlesh72 commented Mar 2, 2025

Linked Issue(s)

Closes #607

Acceptance Criteria fulfillment

  • Auto scroll disabled If scrollbar is out of range
  • Scroll down button

Proposed changes (including videos or screenshots)

Screen.Recording.2025-03-03.at.1.32.29.AM.mov

Summary by CodeRabbit

  • New Features
    • Introduced a new button that appears when the log section is out of view, allowing for quick navigation.
  • Refactor
    • Adjusted the scrolling behavior by removing automatic scroll on load and now triggering the scroll based on updated log content or user action.

Copy link

coderabbitai bot commented Mar 2, 2025

Walkthrough

The changes add a button to the logs section that appears when the logs container is out of view. An IntersectionObserver monitors the container's visibility, toggling a state variable (showScrollDownButton) to show or hide the newly added button. Clicking the button triggers the scrollDown function, which scrolls the container into view. Additionally, a new effect checks for log updates to trigger scrolling if necessary, while previous logic that automatically scrolled on mount has been removed.

Changes

File Change Summary
web-server/src/.../SystemLogs.tsx Added a new scroll-down button using ExpandCircleDown icon. Introduced showScrollDownButton via useBoolState, an IntersectionObserver to toggle its visibility, and a scrollDown function to handle scrolling. Removed previous auto-scroll-on-mount logic.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant SL as SystemLogs Component
    participant IO as IntersectionObserver
    participant LC as Logs Container

    U->>SL: Load component with logs
    IO-->>SL: Detect visibility of LC
    alt LC out of view
        SL->>U: Display scroll down button
        U->>SL: Click scroll down button
        SL->>LC: Trigger scrollDown (scroll into view)
    else LC visible
        SL->>U: Keep button hidden
    end
Loading

Poem

I'm a bunny hopping through the code,
Found a scroll button on a winding road.
With each click, logs come into view,
A playful hop makes tasks feel new.
Celebrate changes with a joyful "woo-hoo!"

✨ Finishing Touches
  • 📝 Generate Docstrings

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 generate docstrings to generate docstrings for this 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

@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: 1

🧹 Nitpick comments (2)
web-server/src/content/Service/SystemLogs.tsx (2)

92-113: Scroll button implementation needs performance optimization.

The button implementation works correctly, but there's a potential performance issue with reading DOM properties during render.

Reading containerRef.current.clientWidth during render (lines 105-107) can cause layout thrashing. Consider moving this calculation to a useEffect or a state variable:

export const SystemLogs = ({ serviceName }: { serviceName?: ServiceNames }) => {
  const { services, loading, logs } = useSystemLogs({ serviceName });
  const showScrollDownButton = useBoolState(false);
  const containerRef = useRef<HTMLDivElement>(null);
+ const [buttonMarginLeft, setButtonMarginLeft] = useState(0);

  const scrollDown = () => {
    containerRef.current.scrollIntoView({ behavior: 'smooth' });
  };

+ useEffect(() => {
+   if (containerRef.current) {
+     setButtonMarginLeft(containerRef.current.clientWidth / 2 - 67);
+   }
+ }, []);

  // Rest of component...

  return (
    // Rest of JSX...
    {showScrollDownButton.value && (
      <Button
        onClick={scrollDown}
        component={MotionBox}
        initial={{ opacity: 0, scale: 0.7 }}
        animate={{ opacity: 1, scale: 1 }}
        transition={{
          ease: 'easeOut'
        }}
        bottom={20}
        sx={{
          position: 'fixed',
-         marginLeft: `calc(${
-           containerRef.current
-             ? containerRef.current.clientWidth / 2 - 67
-             : 0
-         }px)`
+         marginLeft: `${buttonMarginLeft}px`
        }}
      >
        <ExpandCircleDown fontSize="large" color="secondary" />
      </Button>
    )}
  );
};

Additionally, consider adding a resize observer to update the button position if the container width changes.


45-48: Variable declaration can be optimized.

There's a minor code duplication issue with containerElement.

- const containerElement = containerRef.current;
-
  if (containerRef.current) {
-   observer.observe(containerElement);
+   const containerElement = containerRef.current;
+   observer.observe(containerElement);

This makes the variable scoped only where it's needed and avoids declaring it when it might be null.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 35db939 and 3455fa7.

📒 Files selected for processing (1)
  • web-server/src/content/Service/SystemLogs.tsx (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: All file linting
🔇 Additional comments (3)
web-server/src/content/Service/SystemLogs.tsx (3)

1-2: Import additions look good.

The necessary imports have been added for the new scroll button functionality including MUI components, animation components, and state management hooks.

Also applies to: 13-13, 17-18


21-21: State and scroll function implementation is clean.

Good use of the useBoolState hook for managing button visibility, and the scrollDown function is implemented correctly with smooth scrolling behavior.

Also applies to: 24-26


57-61: Auto-scroll effect implementation is robust.

This effect correctly handles automatic scrolling when:

  1. New logs are added (logs dependency)
  2. The scroll button is not visible (user hasn't manually scrolled up)

This satisfies the requirement to disable auto-scroll when the user has manually scrolled away from the bottom.

Comment on lines 28 to +55
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
showScrollDownButton.false();
} else {
showScrollDownButton.true();
}
});
},
{
threshold: 0
}
);

const containerElement = containerRef.current;

if (containerRef.current) {
containerRef.current.scrollIntoView({ behavior: 'smooth' });
observer.observe(containerElement);
}
}, [logs]);

return () => {
if (containerElement) {
observer.unobserve(containerElement);
}
};
}, [showScrollDownButton]);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

IntersectionObserver implementation needs dependency fix.

The implementation correctly observes when the logs container is in/out of view, but there's an issue with the effect dependency array.

The dependency array includes showScrollDownButton which is an object (the state handler), not a primitive. This could cause unnecessary re-renders and observer recreation. Change it to use the primitive value:

-  }, [showScrollDownButton]);
+  }, []);

The effect doesn't need any dependencies since it's only setting up and cleaning up the observer, and the state updaters (showScrollDownButton.true() and showScrollDownButton.false()) are stable references that won't change.

📝 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
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
showScrollDownButton.false();
} else {
showScrollDownButton.true();
}
});
},
{
threshold: 0
}
);
const containerElement = containerRef.current;
if (containerRef.current) {
containerRef.current.scrollIntoView({ behavior: 'smooth' });
observer.observe(containerElement);
}
}, [logs]);
return () => {
if (containerElement) {
observer.unobserve(containerElement);
}
};
}, [showScrollDownButton]);
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
showScrollDownButton.false();
} else {
showScrollDownButton.true();
}
});
},
{
threshold: 0
}
);
const containerElement = containerRef.current;
if (containerRef.current) {
observer.observe(containerElement);
}
return () => {
if (containerElement) {
observer.unobserve(containerElement);
}
};
}, []);

Copy link

@dreamerchandra dreamerchandra left a comment

Choose a reason for hiding this comment

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

Thanks @Kamlesh72

@jayantbh jayantbh merged commit b715c16 into middlewarehq:main Mar 6, 2025
4 checks passed
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.

Fix: Auto-Scroll for Logs
3 participants