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 code duplication #5495

Merged
merged 2 commits into from
Sep 23, 2024
Merged

Conversation

mayfwl
Copy link
Contributor

@mayfwl mayfwl commented Sep 23, 2024

💻 变更类型 | Change Type

  • feat
  • fix
  • refactor
  • perf
  • style
  • test
  • docs
  • ci
  • chore
  • build

🔀 变更说明 | Description of Change

修复复制代码的时候,将‘展示全部’也复制

📝 补充信息 | Additional Information

Summary by CodeRabbit

  • New Features

    • Enhanced clipboard functionality to copy code snippets directly from the PreCode component.
  • Bug Fixes

    • Improved text retrieval logic to ensure accurate copying of code content, now targeting nested code elements.

Copy link

vercel bot commented Sep 23, 2024

@mayfwl is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes in this pull request focus on modifying the onClick event handler of the PreCode component in the app/components/markdown.tsx file. The update alters how the inner text is retrieved for copying to the clipboard by querying for a nested code element instead of directly accessing the inner text of the referenced element. This adjustment ensures that the correct text is copied when the event is triggered.

Changes

File Change Summary
app/components/markdown.tsx Modified onClick handler in PreCode to retrieve inner text from nested code element instead of the outer element.

Possibly related PRs

  • Modify View All Languages #5303: This PR also modifies the app/components/markdown.tsx file, indicating potential overlap in functionality or component behavior.

Suggested reviewers

  • lloydzhou

Poem

In the code where rabbits play,
A click now finds the right array.
Nested text, so neat and bright,
Copies smoothly, pure delight!
Hopping through the lines with glee,
Clipboard magic, come and see! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 35aa2c7 and d95d509.

Files selected for processing (1)
  • app/components/markdown.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/components/markdown.tsx

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.

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.

@mayfwl
Copy link
Contributor Author

mayfwl commented Sep 23, 2024

#5284

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


#5284

Copy link
Contributor

@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 23f2b62 and 35aa2c7.

Files selected for processing (1)
  • app/components/markdown.tsx (1 hunks)

Comment on lines 131 to 134
// const code = ref.current.innerText;
copyToClipboard(
ref.current.querySelector("code")?.innerText ?? "",
);
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 23, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Missing <code> element near copy-code-button.

The copy-code-button at line 128 in app/components/markdown.tsx is not within a pre element containing a code element. This may lead to incorrect copy functionality, such as copying an empty string instead of the intended code snippet.

  • File: app/components/markdown.tsx
    • Line: 128
Analysis chain

Verify the correctness of the code segment.

The updated implementation assumes that the text to be copied is contained within a code element nested inside the pre element. Please ensure that this assumption holds true for all cases where the copy functionality is used.

Consider the following:

  • If there are scenarios where the text to be copied is not within a code element, this change may lead to incorrect behavior.
  • If the code element is not found, an empty string will be copied, which may not be the desired outcome. Consider providing a more meaningful fallback value or handling the case differently.

To verify the correctness, you can run the following script:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the text to be copied is always within a `code` element.

# Test: Search for the `pre` elements that contain the copy button.
# Expect: Each `pre` element should have a nested `code` element.
rg --type tsx -A 5 $'className="copy-code-button"'

Length of output: 79


Script:

#!/bin/bash
# Description: Verify that the text to be copied is always within a `code` element.

# Test: Search for the `pre` elements that contain the copy button.
# Expect: Each `pre` element should have a nested `code` element.
rg --type typescriptreact -A 5 'className="copy-code-button"'

Length of output: 103


Script:

#!/bin/bash
# Description: Verify that the text to be copied is always within a `code` element.

# Test: Search for the `pre` elements that contain the copy button.
# Expect: Each `pre` element should have a nested `code` element.
rg --glob '*.tsx' -A 5 'className="copy-code-button"'

Length of output: 472


Script:

#!/bin/bash
# Description: Verify that each `copy-code-button` is within a `pre` element containing a `code` element.

# Search for `copy-code-button` and check if a `code` element exists within 5 lines before or after.
# This assumes that the `code` element is defined in close proximity to the `copy-code-button`.

rg --glob '*.tsx' -A 5 'className="copy-code-button"' | while IFS= read -r line
do
  file=$(echo "$line" | cut -d':' -f1)
  context=$(grep -A5 -B5 'className="copy-code-button"' "$file")
  if ! echo "$context" | grep -q '<code>'; then
    echo "Missing <code> element near `copy-code-button` in $file"
  fi
done

Length of output: 5063


Script:

#!/bin/bash
# Description: Verify that each `copy-code-button` is within a `pre` element containing a `code` element.

# List all .tsx files containing 'className="copy-code-button"'
files=$(rg --glob '*.tsx' -l 'className="copy-code-button"')

for file in $files
do
  # Find all line numbers with 'className="copy-code-button"'
  line_numbers=$(rg --glob '*.tsx' -n 'className="copy-code-button"' "$file" | cut -d':' -f1)
  
  for line in $line_numbers
  do
    # Check within 5 lines before and after for '<code>'
    if ! sed -n "$((line-5)),$((line+5))p" "$file" | grep -q '<code>'; then
      echo "Missing <code> element near line $line in $file"
    fi
  done
done

Length of output: 466

Copy link
Member

Choose a reason for hiding this comment

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

  1. 要么参照之前的代码重新给code赋值
  2. 要么把注释的这一行直接去掉

Copy link
Contributor

Choose a reason for hiding this comment

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

@lloydzhou,好,您可以直接删除被注释的那一行代码,以保持代码简洁。

Copy link
Contributor

Your build has completed!

Preview deployment

Comment on lines 131 to 134
// const code = ref.current.innerText;
copyToClipboard(
ref.current.querySelector("code")?.innerText ?? "",
);
Copy link
Member

Choose a reason for hiding this comment

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

  1. 要么参照之前的代码重新给code赋值
  2. 要么把注释的这一行直接去掉

@lloydzhou lloydzhou merged commit 4c3fd55 into ChatGPTNextWeb:main Sep 23, 2024
1 of 2 checks passed
@lloydzhou lloydzhou deleted the Fix-code-duplication branch September 26, 2024 13:35
@coderabbitai coderabbitai bot mentioned this pull request Nov 12, 2024
10 tasks
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.

3 participants