Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented May 16, 2025

Link issues

fixes #6036

Summary By Copilot

This pull request introduces several updates to the Toast notification system in the BootstrapBlazor project, focusing on improving functionality, usability, and maintainability. Key changes include adding support for asynchronous notifications, refactoring code for consistency and clarity, and enhancing the JavaScript logic for dynamic updates.

Enhancements to Toast functionality:

  • Added an asynchronous notification demo with step-by-step updates (OnAsyncClick method) in Toasts.razor.cs, showcasing how notifications can display sequential messages with delays.
  • Introduced a new update function in Toast.razor.js to dynamically update Toast configurations and progress bars without reinitializing the component.
  • Updated the Show method in ToastContainer.razor.cs to support updating existing Toast content dynamically.

Code refactoring for maintainability:

  • Replaced _delayTs with DelayTs as a private property for better naming consistency in Toasts.razor.cs.
  • Changed several protected properties to private in Toast.razor.cs to encapsulate implementation details.

Localization updates:

  • Added new localization strings for asynchronous notifications in both en-US.json and zh-CN.json, ensuring multi-language support for the new feature. [1] [2]

Minor fixes and improvements:

  • Fixed a missing semicolon in Toast.razor.js for code consistency.
  • Updated the default value for ValueList in Toasts.razor.cs to improve clarity in attribute definitions.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Enable asynchronous updates of Toast notifications and dynamic content reconfiguration for existing toasts without reinitialization.

New Features:

  • Add an async toast demo (OnAsyncClick) that updates a single toast instance in steps.
  • Allow existing toast content and options to be updated by reusing the same ToastOption in ToastContainer.Show.

Enhancements:

  • Introduce an update function in the toast JavaScript to adjust delay and progress bar on re-render.
  • Refactor naming and encapsulation by renaming _delayTs to DelayTs and changing protected members to private.
  • Standardize default value formatting for attribute lists in sample code.

Documentation:

  • Add new localization strings for the async toast demo in both English and Chinese.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 16, 2025

Reviewer's Guide

This PR enhances the Toast system by enabling asynchronous content updates using the same ToastOption instance, introducing a dynamic JavaScript update logic, refactoring naming and access levels for consistency, updating the sample UI to showcase async notifications, and adding corresponding localization strings.

File-Level Changes

Change Details Files
Add async toast update demo
  • Introduce a private readonly ToastOption _option
  • Implement OnAsyncClick to sequentially update and show the same toast
  • Replace multiple Show calls to reuse _option for async steps
src/BootstrapBlazor.Server/Components/Samples/Toasts.razor.cs
Enable dynamic toast reconfiguration at runtime
  • Add update(id) in Toast.razor.js to adjust delay and progress bar
  • Override OnAfterRenderAsync in Toast.razor.cs to invoke JS update on rerenders
  • Modify ToastContainer.Show to check for existing option and update instead of always adding
src/BootstrapBlazor/Components/Toast/Toast.razor.js
src/BootstrapBlazor/Components/Toast/Toast.razor.cs
src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs
Refactor naming and encapsulation
  • Rename _delayTs to DelayTs and update interpolations
  • Change protected properties (DelayString, AnimationString, ToastContainer) to private
  • Adjust ValueList defaults formatting for attribute items
src/BootstrapBlazor.Server/Components/Samples/Toasts.razor.cs
src/BootstrapBlazor/Components/Toast/Toast.razor.cs
Update sample UI to include async demo
  • Add a DemoBlock for async toast in Toasts.razor
  • Remove unused IJSRuntime injection
src/BootstrapBlazor.Server/Components/Samples/Toasts.razor
Add localization for async notifications
  • Append new keys for async demo title and step texts in en-US.json
  • Append corresponding keys in zh-CN.json
src/BootstrapBlazor.Server/Locales/en-US.json
src/BootstrapBlazor.Server/Locales/zh-CN.json

Assessment against linked issues

Issue Objective Addressed Explanation

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto bot added the enhancement New feature or request label May 16, 2025
@bb-auto bb-auto bot added this to the v9.6.0 milestone May 16, 2025
@codecov
Copy link

codecov bot commented May 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (2fec2d0) to head (9951283).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #6037   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          701       701           
  Lines        30945     30955   +10     
  Branches      4375      4377    +2     
=========================================
+ Hits         30945     30955   +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ArgoZhang ArgoZhang merged commit bd404f7 into main May 16, 2025
6 checks passed
@ArgoZhang ArgoZhang deleted the feat-notify branch May 16, 2025 13:24
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • Rename the private field _option (used in async updates) to follow your PascalCase/private naming convention (e.g. option or _option) consistently across the class.
  • In the JS update function, add a guard for missing toast-progress elements to avoid runtime errors when updating toasts that don’t include a progress bar.
  • The ToastContainer.Show method uses Toasts.Contains(option) (reference equality) to detect existing toasts—consider using a unique identifier or value-based comparison to handle reused option instances more reliably.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

toast: new bootstrap.Toast(el),
showProgress: () => {
return toast.toast._config.autohide
}
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Event handler for 'transitionend' may be attached multiple times.

Attaching a new listener on each update can cause duplicate handlers, triggering toast.hide() repeatedly and leaking memory. Consider removing existing handlers or using a one-time listener.

private int _delayTs => Options.Value.ToastDelay / 1000;
private int DelayTs => Options.Value.ToastDelay / 1000;

private readonly ToastOption _option = new();
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider creating a new ToastOption instance for each toast step instead of reusing and mutating a shared field.

Consider avoiding the shared, mutable _option and instead build a fresh ToastOption for each step (or extract a small factory helper). This keeps each toast’s state isolated and makes the flow easier to follow.

For example, you can extract a helper:

private ToastOption CreateStep(string content, ToastCategory category,
                               bool isAutoHide = true, int delay = 3000, bool forceDelay = false)
{
  return new ToastOption {
    Title       = Localizer["ToastsAsyncDemoTitle"],
    Content     = content,
    Category    = category,
    IsAutoHide  = isAutoHide,
    Delay       = delay,
    ForceDelay  = forceDelay
  };
}

Then rewrite OnAsyncClick without mutating a field:

private async Task OnAsyncClick()
{
  // Step 1
  var first = CreateStep(Localizer["ToastsAsyncDemoStep1Text"],
                         ToastCategory.Information,
                         isAutoHide: false,
                         delay: 3000,
                         forceDelay: true);
  await ToastService.Show(first);
  await Task.Delay(first.Delay);

  // Step 2
  var second = CreateStep(Localizer["ToastsAsyncDemoStep2Text"],
                          ToastCategory.Information,
                          isAutoHide: true);
  await ToastService.Show(second);
  await Task.Delay(2000);

  // Step 3
  var third = CreateStep(Localizer["ToastsAsyncDemoStep3Text"],
                         ToastCategory.Success);
  await ToastService.Show(third);
}

This removes the mutable _option and makes each toast instantiation self-contained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(Toast): support async update content use same option

2 participants