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

PCH Smart Linking: Fix bug causing ‘No smart links’ error despite valid API response #2662

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

vaurdan
Copy link
Contributor

@vaurdan vaurdan commented Jul 23, 2024

Description

The #2654 PR introduced a bug where, even though the API would return valid Smart Links, the UI would throw a "No smart links were generated." error.

This was due to how React handles store variables. Even though processSmartLinks updates the smartLinks variable in the Smart Linking store, the smartLinks variable would be empty/outdated when we check its length (if (smartLinks.length === 0)).

This PR addresses this issue by returning the processed smart links from the processSmartLinks function and using that returned value to determine if an error should be outputted.

Motivation and context

Fix bug that prevents the proper usage of the Smart Linking feature

How has this been tested?

Tested manually with different mocked-up API responses.

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for smart link processing, improving clarity and user experience.
    • Streamlined flow for displaying processed smart links in the review modal.
  • Improvements

    • Improved type safety with explicit return types for smart link processing functions.

@vaurdan vaurdan added this to the 3.16.2 milestone Jul 23, 2024
@vaurdan vaurdan self-assigned this Jul 23, 2024
@vaurdan vaurdan requested a review from a team as a code owner July 23, 2024 09:49
Copy link
Contributor

coderabbitai bot commented Jul 23, 2024

Walkthrough

Walkthrough

The recent changes to the SmartLinkingPanel component enhance type safety and error handling by explicitly defining the return type of the processSmartLinks function as Promise<SmartLink[]>. This adjustment allows the function to return processed links, improving the management of these links in the calling context. Additionally, the refined error reporting mechanism provides a more user-friendly experience when no links are found.

Changes

Files Change Summary
src/content-helper/editor-sidebar/smart-linking/component.tsx - Enhanced processSmartLinks to explicitly return Promise<SmartLink[]>, improving type safety.
- Adjusted handling of smart links to use processedSmartLinks for better error handling and control flow.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SmartLinkingPanel
    participant LinkProcessor

    User->>SmartLinkingPanel: Request to process links
    SmartLinkingPanel->>LinkProcessor: processSmartLinks(links)
    LinkProcessor-->>SmartLinkingPanel: return processedSmartLinks
    SmartLinkingPanel->>SmartLinkingPanel: Check processedSmartLinks.length
    alt Links found
        SmartLinkingPanel->>User: Open review modal
    else No links found
        SmartLinkingPanel->>User: Throw error (ContentHelperErrorCode.ParselySuggestionsApiNoData)
    end
Loading

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7936658 and 64f67ee.

Files ignored due to path filters (2)
  • build/content-helper/editor-sidebar.asset.php is excluded by !build/**
  • build/content-helper/editor-sidebar.js is excluded by !build/**
Files selected for processing (1)
  • src/content-helper/editor-sidebar/smart-linking/component.tsx (3 hunks)
Additional context used
Path-based instructions (1)
src/content-helper/editor-sidebar/smart-linking/component.tsx (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
Additional comments not posted (4)
src/content-helper/editor-sidebar/smart-linking/component.tsx (4)

354-355: LGTM! Approve the type change.

The change to explicitly define the return type as Promise<SmartLink[]> improves type safety and clarity.


488-498: LGTM! Approve the changes.

The function now stores the processed smart links in a new variable processedSmartLinks and checks its length to determine error handling. This change improves the accuracy of error handling.


Line range hint 501-539:
LGTM! No changes observed.

The generateSmartLinksWithRetry function handles retries and error handling for generating smart links. No changes are observed in this function.


Line range hint 357-433:
Ensure all function calls to processSmartLinks match the new signature.

The function now returns a Promise<SmartLink[]>. Verify that all calls to processSmartLinks handle the returned promise correctly.

Verification successful

All function calls to processSmartLinks handle the returned promise correctly.

  • The function call to processSmartLinks is awaited, ensuring the promise is handled properly.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `processSmartLinks` handle the returned promise correctly.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type tsx -A 5 $'processSmartLinks'

Length of output: 66


Script:

#!/bin/bash
# Description: Verify all function calls to `processSmartLinks` handle the returned promise correctly.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --glob '**/*.tsx' -A 5 'processSmartLinks'

Length of output: 1349

@vaurdan vaurdan force-pushed the fix/smart-linking-not-returning-links branch from 64f67ee to 75280f5 Compare July 23, 2024 10:01
@vaurdan vaurdan changed the base branch from develop to prepare/3.16.2 July 23, 2024 10:01
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: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 64f67ee and 75280f5.

Files ignored due to path filters (2)
  • build/content-helper/editor-sidebar.asset.php is excluded by !build/**
  • build/content-helper/editor-sidebar.js is excluded by !build/**
Files selected for processing (1)
  • src/content-helper/editor-sidebar/smart-linking/component.tsx (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/content-helper/editor-sidebar/smart-linking/component.tsx

Copy link
Collaborator

@acicovic acicovic left a comment

Choose a reason for hiding this comment

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

Thanks!

@vaurdan vaurdan merged commit be3bfd8 into prepare/3.16.2 Jul 24, 2024
35 checks passed
@vaurdan vaurdan deleted the fix/smart-linking-not-returning-links branch July 24, 2024 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants