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

[MOC-62][MOC-59] Recording state fixes for sign up and getting recordings for first page #80

Closed
wants to merge 2 commits into from

Conversation

nicolaschapur
Copy link
Contributor

@nicolaschapur nicolaschapur commented Jun 28, 2024

This PR adds a bunch o fixes for recording states and signed or not users. Also adds a recording call when opening the extension, to check if we need to show the list of recordings as the first page or the record button.

Screen.Recording.2024-06-28.at.12.24.30.mov

Summary by CodeRabbit

  • New Features

    • Improved retrieval and management of recordings with updated logic.
  • Bug Fixes

    • Enhanced handling of recordings retrieval to ensure correct storage and display.
  • Refactor

    • Streamlined recording state management based on user login status.
    • Updated styling class names for better readability and maintenance.
  • Style

    • Adjusted CSS class names for consistency and improved styling.
  • Performance

    • Optimized recordings fetching by removing unnecessary data parsing.

Copy link

linear bot commented Jun 28, 2024

Copy link

coderabbitai bot commented Jun 28, 2024

Walkthrough

Walkthrough

The changes streamline recording management in the mocksi-lite application by refactoring the handling of recordings, updating UI components for consistency, and cleaning up logic for enhanced maintainability and efficiency. This includes updating storage handling, sorting recordings, and introducing a new function to manage recording states based on user actions and login status.

Changes

File Change Summary
apps/mocksi-lite/background.ts Added import for getRecordingsStorage, refactored chrome.tabs.sendMessage to handle recordings retrieval, and updated getRecordings logic for API response and storage.
apps/mocksi-lite/content/Popup/... Sorted newRecordings based on updated_timestamp and changed CSS class name from "flex-1" to "flex-grow".
apps/mocksi-lite/content/content.tsx Introduced handleRecordingState function to manage recording states based on user login status and actions, replacing previous logic in chrome.runtime.onMessage listener with a call to handleRecordingState().
apps/mocksi-lite/utils.ts Simplified getRecordingsStorage function to return results.recordings directly instead of using JSON.parse.

Sequence Diagram(s)

sequenceDiagram
    participant B as Background Script
    participant T as Tab (Content Script)
    participant U as Utils
    participant API as API Server

    B->>U: getRecordingsStorage()
    U-->>B: recordings
    B->>T: sendMessage({ recordings })
    T-->>B: acknowledgment

    note over content.tsx: Old Flow
    T->>+T: handle message & logic
    T-->>-T: update recording state

    note over content.tsx: New Flow
    T->>+handleRecordingState: Check login and state
    handleRecordingState->>API: Verify login status
    API-->>handleRecordingState: { state, email }
    handleRecordingState-->>-T: Update recording state

    Note over B,T,U, API: Simplified logic and enhanced modularity
Loading

Poem

In kilobytes and lines of code,
Updates come, our tasks forebode.
Recordings sorted, states now clear,
New flows, neat and void of fear.
With handleFunction brave and bright,
Mocksi-lite scales new daylight.


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 Configration 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.

@nicolaschapur nicolaschapur requested a review from elg0nz June 28, 2024 15:32
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

Outside diff range and nitpick comments (1)
apps/mocksi-lite/utils.ts (1)

Line range hint 36-36: Security Issue: Exposed API Key.

The hardcoded API key in the authOptions configuration is a security risk and should be managed through environment variables or a secure vault solution.

- clientID: "3XDxVDUz3W3038KmRvkJSjkIs5mGj7at",
+ clientID: process.env.AUTH0_CLIENT_ID,
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3d08890 and 955e259.

Files selected for processing (4)
  • apps/mocksi-lite/background.ts (3 hunks)
  • apps/mocksi-lite/content/Popup/CreateDemo/index.tsx (2 hunks)
  • apps/mocksi-lite/content/content.tsx (2 hunks)
  • apps/mocksi-lite/utils.ts (1 hunks)
Additional context used
Learnings (1)
apps/mocksi-lite/content/Popup/CreateDemo/index.tsx (1)
User: elg0nz
PR: Mocksi/HARlighter#54
File: apps/mocksi-lite/content/Popup/CreateDemo/index.tsx:22-33
Timestamp: 2024-06-14T20:59:01.152Z
Learning: The `getRecordings` function in `CreateDemo` component of `mocksi-lite` should log errors using `console.error` to help identify issues in edge cases.
Gitleaks
apps/mocksi-lite/utils.ts

36-36: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (2)
apps/mocksi-lite/content/Popup/CreateDemo/index.tsx (1)

58-60: Styling change approved.

The change from "flex-1" to "flex-grow" is appropriate for ensuring that the div expands to take up any available space within its flex container, which is suitable for the display of recordings.

apps/mocksi-lite/utils.ts (1)

292-292: Simplify getRecordingsStorage function.

The removal of JSON.parse is appropriate if the recordings are already in the correct format, reducing unnecessary parsing overhead.

Comment on lines +31 to +35
const sortedRecordings = newRecordings.sort(
(a: Recording, b: Recording) =>
a.updated_timestamp > b.updated_timestamp ? -1 : 0,
);
setRecordings(sortedRecordings);
Copy link

@coderabbitai coderabbitai bot Jun 28, 2024

Choose a reason for hiding this comment

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

Optimize sorting logic and add error handling.

The sorting function should ensure that recordings are sorted in descending order by updated_timestamp. The current implementation might not handle all cases correctly. Also, consider adding explicit error logging to help identify issues during fetching.

- a.updated_timestamp > b.updated_timestamp ? -1 : 0,
+ a.updated_timestamp > b.updated_timestamp ? -1 : 1,
+ console.error("Failed to fetch recordings:", error);

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good suggestion

Copy link

Choose a reason for hiding this comment

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

@elg0nz, thank you for the positive feedback! If you have any more questions or need further assistance as you implement the changes, feel free to reach out. Happy coding! 🐰


Learnings used
Learnt from: elg0nz
PR: Mocksi/HARlighter#54
File: apps/mocksi-lite/content/Popup/CreateDemo/index.tsx:22-33
Timestamp: 2024-06-14T20:59:01.152Z
Learning: The `getRecordings` function in `CreateDemo` component of `mocksi-lite` should log errors using `console.error` to help identify issues in edge cases.

Learnt from: nicolaschapur
PR: Mocksi/HARlighter#80
File: apps/mocksi-lite/content/content.tsx:43-93
Timestamp: 2024-06-28T15:36:05.992Z
Learning: The `getRecordingsStorage` function in `utils.ts` returns an empty array when `recordings` is undefined or null, ensuring robustness in downstream function calls.

apps/mocksi-lite/content/content.tsx Show resolved Hide resolved
Comment on lines +106 to +125
getRecordingsStorage()
.then((recordings) => {
if (recordings.length) {
chrome.tabs.sendMessage(currentTabId || 0, {
text: "clickedIcon",
});
} else {
getRecordings().then(() => {
chrome.tabs.sendMessage(currentTabId || 0, {
text: "clickedIcon",
});
});
}
})
.catch((err) => {
console.error(`Failed to get recordings: ${err}`);
chrome.tabs.sendMessage(currentTabId || 0, {
text: "clickedIcon",
});
});
Copy link

Choose a reason for hiding this comment

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

Refactor recording retrieval logic in background.ts.

The logic to handle recordings based on their presence is a good improvement. However, the error handling could be more robust, and the repeated chrome.tabs.sendMessage calls can be optimized.

-				} else {
-					getRecordings().then(() => {
-						chrome.tabs.sendMessage(currentTabId || 0, {
-							text: "clickedIcon",
-						});
-					});
-				}
+				} else if (await getRecordings()) {
+					chrome.tabs.sendMessage(currentTabId || 0, { text: "clickedIcon" });
+				}
+ console.error("Error while retrieving recordings:", err);

@nicolaschapur nicolaschapur changed the title [MOC-62] Recording state fixes for sign up and getting recordings for first page [MOC-62][MOC-59] Recording state fixes for sign up and getting recordings for first page Jun 28, 2024
Copy link

linear bot commented Jun 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.

Still needs some work

text: "clickedIcon",
});
getRecordingsStorage()
.then((recordings) => {
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.
I cannot see any reason to call

chrome.tabs.sendMessage(currentTabId || 0, {
 						text: "clickedIcon",
 					});

three times, with the same parameters.
Specially when you can use finally in this promise

Comment on lines +31 to +35
const sortedRecordings = newRecordings.sort(
(a: Recording, b: Recording) =>
a.updated_timestamp > b.updated_timestamp ? -1 : 0,
);
setRecordings(sortedRecordings);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good suggestion

let state = recordingState;
console.log({ recordingState });

if (email) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this function is already too long.
Extract this out so you can do:
`tsx
state = email ? setLoggedInsState() : setLoggedOutState()

return { state, email };
}


Where `setLoggedOutState` would look like this:
```tsx
		if (
 			recordingState === RecordingState.UNAUTHORIZED &&
 			window.location.origin !== SignupURL
 		) {
 			window.open(SignupURL);
 		}
 		sendMessage("resetIcon");
 		return RecordingState.UNAUTHORIZED;

@elg0nz elg0nz requested a review from bkd705 July 2, 2024 16:41
@elg0nz elg0nz closed this Jul 5, 2024
@elg0nz elg0nz deleted the MOC-62 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