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

Storing DOM changes #27

Merged
merged 12 commits into from
Jun 5, 2024
Merged

Storing DOM changes #27

merged 12 commits into from
Jun 5, 2024

Conversation

NicoMorenoSirius
Copy link
Contributor

@NicoMorenoSirius NicoMorenoSirius commented Jun 4, 2024

Screen.Recording.2024-06-04.at.12.31.33.mov

Summary by CodeRabbit

  • New Features

    • Introduced handling of modifications on double-click events in the application.
  • Enhancements

    • Improved the DemoItem component to load modifications on button click.
    • Enhanced the elementWithBorder function to save modifications automatically.

@elg0nz
Copy link
Contributor

elg0nz commented Jun 4, 2024

Copy link

coderabbitai bot commented Jun 4, 2024

Walkthrough

Walkthrough

The recent updates to the mocksi-lite app introduce a new constant MOCKSI_MODIFICATIONS and enhance the application's functionality by adding loadModifications and saveModification functions. These functions are invoked in various components to manage modifications efficiently, particularly on double-click events.

Changes

File Path Change Summary
apps/mocksi-lite/consts.ts Added new constant MOCKSI_MODIFICATIONS.
apps/mocksi-lite/content/content.tsx Imported and invoked loadModifications and saveModification functions to handle modifications.
apps/mocksi-lite/content/Popup/CreateDemo/DemoItem.tsx Changed onClick handler to call loadModifications() instead of setEditorMode(false).

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant UI as User Interface
    participant Utils as Utility Functions
    User->>UI: Double-click element
    UI->>Utils: loadModifications()
    Utils-->>UI: Modifications data
    UI->>UI: setEditorMode()
    User->>UI: Double-click element
    UI->>Utils: saveModification(element, content)
    Utils-->>UI: Save confirmation
Loading

Poem

In the land of Mocksi's light,
New constants shine so bright,
With mods that load and save,
On double-click, they're brave.
Our app now stands so tall,
Thanks to changes, big and small! 🐇✨


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

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2dbe99a and f1b4ae8.

Files selected for processing (3)
  • apps/mocksi-lite/consts.ts (1 hunks)
  • apps/mocksi-lite/content/content.tsx (3 hunks)
  • apps/mocksi-lite/utils.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • apps/mocksi-lite/consts.ts
Additional comments not posted (2)
apps/mocksi-lite/utils.ts (1)

22-53: Consider avoiding direct manipulation of innerHTML due to potential XSS vulnerabilities.

- elemToModify.innerHTML = value;
+ elemToModify.textContent = value;

Likely invalid or redundant comment.

apps/mocksi-lite/content/content.tsx (1)

186-186: The integration of loadModifications in setEditorMode aligns well with the PR objectives.

Comment on lines 55 to 76
export const loadModifications = () => {
const modifications = JSON.parse(
localStorage.getItem(MOCKSI_MODIFICATIONS) || "{}",
);
for (const modification of Object.entries(modifications)) {
const [querySelector, value] = modification;
const hasIndex = querySelector.match(/\[+[0-9]\]/);
if (hasIndex) {
const index: number = +hasIndex[0].replace("[", "").replace("]", "");
const elemToModify = document.querySelectorAll(
querySelector.replace(hasIndex[0], ""),
)[index];
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.innerHTML = value;
} else {
const [elemToModify] = document.querySelectorAll(querySelector);
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.innerHTML = value;
}
}
console.log("loadedModifications!", modifications);
};
Copy link

Choose a reason for hiding this comment

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

Consider replacing innerHTML with textContent to mitigate XSS risks.

- elemToModify.innerHTML = value;
+ elemToModify.textContent = value;
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.

Suggested change
export const loadModifications = () => {
const modifications = JSON.parse(
localStorage.getItem(MOCKSI_MODIFICATIONS) || "{}",
);
for (const modification of Object.entries(modifications)) {
const [querySelector, value] = modification;
const hasIndex = querySelector.match(/\[+[0-9]\]/);
if (hasIndex) {
const index: number = +hasIndex[0].replace("[", "").replace("]", "");
const elemToModify = document.querySelectorAll(
querySelector.replace(hasIndex[0], ""),
)[index];
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.innerHTML = value;
} else {
const [elemToModify] = document.querySelectorAll(querySelector);
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.innerHTML = value;
}
}
console.log("loadedModifications!", modifications);
};
export const loadModifications = () => {
const modifications = JSON.parse(
localStorage.getItem(MOCKSI_MODIFICATIONS) || "{}",
);
for (const modification of Object.entries(modifications)) {
const [querySelector, value] = modification;
const hasIndex = querySelector.match(/\[+[0-9]\]/);
if (hasIndex) {
const index: number = +hasIndex[0].replace("[", "").replace("]", "");
const elemToModify = document.querySelectorAll(
querySelector.replace(hasIndex[0], ""),
)[index];
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.textContent = value;
} else {
const [elemToModify] = document.querySelectorAll(querySelector);
//@ts-ignore EXTREMELY INSECURE!!!!!!!!
elemToModify.textContent = value;
}
}
console.log("loadedModifications!", modifications);
};

Comment on lines +136 to +139
saveModification(
parentElement as HTMLElement,
parentElement?.innerHTML || parentElement?.innerText || "",
);
Copy link

Choose a reason for hiding this comment

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

Ensure consistent handling of DOM content. Consider using textContent for better security and consistency.

- parentElement?.innerHTML || parentElement?.innerText || "",
+ parentElement?.textContent || "",
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.

Suggested change
saveModification(
parentElement as HTMLElement,
parentElement?.innerHTML || parentElement?.innerText || "",
);
saveModification(
parentElement as HTMLElement,
parentElement?.textContent || "",
);

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 f1b4ae8 and b12c3a4.

Files selected for processing (1)
  • apps/mocksi-lite/content/content.tsx (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/mocksi-lite/content/content.tsx

@NicoMorenoSirius NicoMorenoSirius marked this pull request as ready for review June 4, 2024 16:10
Copy link
Contributor

ellipsis-dev bot commented Jun 4, 2024

Your free trial has expired. To keep using Ellipsis, sign up at https://app.ellipsis.dev for $20/seat/month or reach us at [email protected]

@NicoMorenoSirius
Copy link
Contributor Author

I'm aware of the security risks of manipulating innerHTML the way is doing in this branch.
I've tested some payloads to confirm the risk, but had no luck.
When applying the DOM changes and saving/loading dom contents we're encoding the content, but still need to test more payloads to ensure there's no security risk here.

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 b12c3a4 and ada2b64.

Files selected for processing (1)
  • apps/mocksi-lite/utils.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • apps/mocksi-lite/utils.ts

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ada2b64 and 79701ba.

Files selected for processing (3)
  • apps/mocksi-lite/content/Popup/CreateDemo/DemoItem.tsx (2 hunks)
  • apps/mocksi-lite/content/content.tsx (3 hunks)
  • apps/mocksi-lite/utils.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • apps/mocksi-lite/content/content.tsx
  • apps/mocksi-lite/utils.ts
Additional comments not posted (1)
apps/mocksi-lite/content/Popup/CreateDemo/DemoItem.tsx (1)

6-6: Import statement added for loadModifications.

This import is necessary for the new functionality as per the PR description.

@@ -16,7 +17,7 @@ const DemoItem = ({ name, customer }: Demo) => {
<Button variant={Variant.icon} onClick={() => setEditorMode(true)}>
<img src={editIcon} alt={"editIcon"} />
</Button>
<Button variant={Variant.icon} onClick={() => setEditorMode(false)}>
<Button variant={Variant.icon} onClick={() => loadModifications()}>
Copy link

@coderabbitai coderabbitai bot Jun 4, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The loadModifications function retrieves modifications from localStorage and applies them to the DOM elements. This functionality is different from setEditorMode(false), which likely had a different purpose related to editor state management.

  • loadModifications focuses on applying stored modifications to the DOM.
  • setEditorMode(false) likely managed the editor's state, such as toggling the editor mode off.

These functions serve different purposes, and replacing setEditorMode(false) with loadModifications might lead to unintended behavior if the editor state management is still required.

  • Ensure that the editor state management is handled appropriately elsewhere if setEditorMode(false) is removed.
Analysis chain

Change in onClick handler to use loadModifications.

Ensure that loadModifications is properly handling all expected scenarios since it replaces setEditorMode(false), which likely had a different purpose.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all scenarios where `loadModifications` is used.

# Test: Search for the function usage. Expect: Proper error handling and functionality coverage.
rg --type typescript $'loadModifications'

Length of output: 75



Script:

#!/bin/bash
# Description: Verify all scenarios where `loadModifications` is used.

# Test: Search for the function usage. Expect: Proper error handling and functionality coverage.
rg --type ts 'loadModifications'

Length of output: 434



Script:

#!/bin/bash
# Description: Extract the implementation of `loadModifications` to verify its functionality.

# Test: Retrieve the function definition. Expect: Proper handling of scenarios and functionality coverage.
ast-grep --lang ts --pattern 'const loadModifications = () => { $$$ }'

Length of output: 1525

@NicoMorenoSirius NicoMorenoSirius requested a review from elg0nz June 5, 2024 13:17
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 79701ba and 19ebd56.

Files selected for processing (3)
  • apps/mocksi-lite/consts.ts (1 hunks)
  • apps/mocksi-lite/content/Popup/CreateDemo/DemoItem.tsx (2 hunks)
  • apps/mocksi-lite/content/content.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (3)
  • apps/mocksi-lite/consts.ts
  • apps/mocksi-lite/content/Popup/CreateDemo/DemoItem.tsx
  • apps/mocksi-lite/content/content.tsx

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 19ebd56 and 54f795f.

Files selected for processing (1)
  • apps/mocksi-lite/content/content.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • apps/mocksi-lite/content/content.tsx

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.

Looking great! 🚢

FYI: in a future PR, we'll have to make saveModification a little more complex so we can add things like undo/redo.

This is more of less what I'm thinking of: https://dev.to/samuelkollat/beyond-basics-transform-your-typescript-codebase-with-command-design-pattern-3f4b

@NicoMorenoSirius NicoMorenoSirius merged commit cafee3b into main Jun 5, 2024
2 checks passed
@NicoMorenoSirius NicoMorenoSirius deleted the CU-86b0mugf1-storing-changes branch June 5, 2024 15:43
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