Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

MOC-226: play mode icon #164

Merged
merged 8 commits into from
Aug 28, 2024
Merged

MOC-226: play mode icon #164

merged 8 commits into from
Aug 28, 2024

Conversation

fitzk
Copy link
Contributor

@fitzk fitzk commented Aug 28, 2024

https://github.com/Mocksi/nest/pull/32

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced state management for the Chrome extension, allowing better communication of the last known request to the content script.
    • Improved iframe responsiveness and visibility based on various message types, adapting dynamically to user interactions.
  • Bug Fixes

    • Refined handling of incoming messages to ensure correct icon changes and state preservation.
  • Performance Improvements

    • Added lazy loading for the iframe, improving performance by deferring loading until it is in the viewport.

Kayla Fitzsimmons added 2 commits August 28, 2024 12:56
Copy link

coderabbitai bot commented Aug 28, 2024

Walkthrough

Walkthrough

The changes involve updates to a Chrome extension's background script and a React application's iframe component. The background script now tracks the last request state and communicates it to the content script. The iframe's styling and behavior have been refined for improved responsiveness and visibility management based on various message types, enhancing overall performance.

Changes

Files Change Summary
apps/mocksi-lite-next/src/pages/background/index.ts Introduced prevRequest variable to manage the last request state. Updated chrome.action.onClicked to send prevRequest to the active tab. Modified message handling logic and icon updates based on request messages.
apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx Altered iframe styling with flexible properties for visibility and positioning based on message types. Added loading="lazy" attribute for performance improvements. Removed unused import.

Poem

🐇 In the code where rabbits play,
Changes hop in a bright array.
Requests and states now dance in line,
Iframes shimmer, all is fine!
With every click, a joyful cheer,
Our extension's magic draws near! ✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ff3303 and a8a77e3.

Files ignored due to path filters (1)
  • apps/mocksi-lite-next/public/play-icon.png is excluded by !**/*.png
Files selected for processing (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts (5 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (4 hunks)
Additional comments not posted (5)
apps/mocksi-lite-next/src/pages/background/index.ts (3)

6-9: LGTM!

The initialization of prevRequest is correct and straightforward.

The code changes are approved.


88-99: LGTM!

The updates to the chrome.action.onClicked event listener improve the continuity of user interactions by maintaining the state of the last request and updating the extension icon accordingly.

The code changes are approved.


Line range hint 118-186: LGTM!

The updates to the chrome.runtime.onMessageExternal event listener provide better debugging information and ensure that the last state is preserved for future interactions.

The code changes are approved.

apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (2)

Line range hint 94-131: LGTM!

The updates to the iframe's visibility and positioning based on different message types enhance the responsiveness and visibility management of the iframe.

The code changes are approved.


156-163: LGTM!

The addition of the loading="lazy" attribute to the iframe improves performance by deferring loading until the iframe is in the viewport.

The code changes are approved.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a8a77e3 and db7324e.

Files selected for processing (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts (5 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx

@fitzk fitzk requested a review from elg0nz August 28, 2024 21:15
@fitzk fitzk marked this pull request as ready for review August 28, 2024 21:16
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between db7324e and 59e22cf.

Files selected for processing (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts (5 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx

@fitzk fitzk changed the title Moc 226 play mode MOC-226: play mode Aug 28, 2024
Copy link

linear bot commented Aug 28, 2024

@fitzk fitzk changed the title MOC-226: play mode MOC-226: play mode icon Aug 28, 2024
Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

Hey, great work on this! Just a couple of tiny tweaks to consider 😃

@@ -80,6 +85,18 @@ addEventListener("install", () => {
chrome.action.onClicked.addListener((tab) => {
if (tab.id) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets get rid of the tab.id nesting by refactoring this to:

if (!tab?.id) {
  console.log("No tab found, could not mount extension");
  return;
}

chrome.tabs.sendMessage(tab.id, { message: "mount-extension" });

if (prevRequest.message) {
  chrome.tabs.sendMessage(tab.id, {
    data: prevRequest.data,
    message: prevRequest.message,
  });
}

if (prevRequest.message === "PLAY") {
  chrome.action.setIcon({
    path: "play-icon.png",
    tabId: tab.id,
  });
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice

@@ -98,7 +115,10 @@ chrome.runtime.onMessage.addListener(

chrome.runtime.onMessageExternal.addListener(
(request, _sender, sendResponse) => {
console.log("Received message from external:", request);
// This logging is useful and only shows up in the service worker
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -132,6 +152,19 @@ chrome.runtime.onMessageExternal.addListener(
console.log("No active tab found, could not send message");
return true;
}

if (request.message === "PLAY") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's refactor this code to use a switch statement for better readability and scalability:

switch (request.message) {
  case "PLAY":
    await chrome.action.setIcon({
      path: "play-icon.png",
      tabId: tab.id,
    });
    break;
    
  case "MINIMIZED":
    // No action needed for "MINIMIZED"
    break;

  default:
    chrome.action.setIcon({
      path: "mocksi-icon.png",
      tabId: tab.id,
    });
    break;
}

This refactor handles the different cases of request.message more cleanly. If there are more cases to handle in the future, adding them to the switch statement will be much easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for sure

height: "150px",
top: "0px",
/* top right bottom left */
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Remove the comment /* top right bottom left */

Tip: To remember the order of these CSS properties, think of it as moving clockwise like on a clock face: starting at 12 (top), then 3 (right), 6 (bottom), and finally 9 (left).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for sure removed

@@ -91,6 +91,7 @@ chrome.runtime.onMessage.addListener((request) => {
) {
reactor.detach();
}

// resize iframe
if (iframeRef.current) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, awesome job on this! I wanted to suggest a small tweak that could make the code a bit cleaner. I’ve found that when an if statement goes over five lines, it’s usually a sign that it could benefit from some refactoring. It just keeps things more readable and easier to maintain.

For the part where you’re setting styles based on request.message, we could simplify things by moving that logic into a separate function. This way, the main code stays focused, and it’ll be easier to update or debug if we need to adjust the styles down the line.

Here’s a quick refactor idea:

// Function to get styles based on the message
function getIframeStyles(message: string): Partial<CSSStyleDeclaration> {
  switch (message) {
    case "ANALYZING":
    case "PLAY":
    case "RECORDING":
      return {
        display: "block",
        height: "150px",
        inset: "0px 10px auto auto",
        width: "400px",
      };

    case "MINIMIZED":
      return {
        display: "none",
        inset: "0px 0px auto auto",
      };

    case "EDITING":
    case "INIT":
    case "LIST":
    case "READYTORECORD":
    case "SETTINGS":
    case "STOP_EDITING":
    case "STOP_PLAYING":
    case "UNAUTHORIZED":
      return {
        display: "block",
        height: "600px",
        inset: "auto 10px 10px auto",
        width: "500px",
      };

    default:
      return {};
  }
}

// Resize iframe with the new styles
if (iframeRef.current) {
  const styles = getIframeStyles(request.message);
  Object.assign(iframeRef.current.style, styles);
}

This should help keep things more modular and easier to manage. Let me know what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for sure changed, I have plans to clean this up

Copy link
Contributor

@elg0nz elg0nz left a comment

Choose a reason for hiding this comment

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

🤩 nice!

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 59e22cf and 4c4fc22.

Files selected for processing (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts (5 hunks)
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • apps/mocksi-lite-next/src/pages/background/index.ts
  • apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx

@fitzk fitzk merged commit b002319 into main Aug 28, 2024
3 checks passed
@elg0nz elg0nz deleted the MOC-226_play-mode branch September 12, 2024 21:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants