Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public partial class NotificationEntryComponent : ComponentBase
[Parameter]
public EventCallback OnDismiss { get; set; }

[Inject]
public required IServiceProvider Services { get; init; }

private string IntentClass => Entry.Intent switch
{
MessageIntent.Success => "intent-success",
Expand Down Expand Up @@ -49,7 +52,7 @@ private async Task HandlePrimaryAction()
{
if (Entry.PrimaryAction is { } primaryAction)
{
await primaryAction.OnClick();
await primaryAction.OnClick(Services);
}
}
}
24 changes: 15 additions & 9 deletions src/Aspire.Dashboard/Model/DashboardCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,20 @@ public async Task ExecuteAsyncCore(ResourceViewModel resource, CommandViewModel
if (response.Result is not null)
{
toastParameters.PrimaryAction = loc[nameof(Dashboard.Resources.Resources.ResourceCommandViewResponse)];
toastParameters.OnPrimaryAction = EventCallback.Factory.Create<ToastResult>(this, () => OpenViewResponseDialogAsync(command, response));
toastParameters.OnPrimaryAction = EventCallback.Factory.Create<ToastResult>(this, () => OpenViewResponseDialogAsync(dialogService, command, response));
}

notificationService.ReplaceNotification(progressNotificationId, new NotificationEntry
{
Title = successTitle,
Body = response.Message,
Intent = MessageIntent.Success,
PrimaryAction = response.Result is not null ? CreateViewResponseNotificationAction(command, response) : null
PrimaryAction = response.Result is not null ? CreateViewResponseNotificationAction(loc, command, response) : null
});

if (response.Result?.DisplayImmediately == true)
{
await OpenViewResponseDialogAsync(command, response).ConfigureAwait(false);
await OpenViewResponseDialogAsync(dialogService, command, response).ConfigureAwait(false);
}
}
else if (response.Kind == ResourceCommandResponseKind.Cancelled)
Expand Down Expand Up @@ -206,20 +206,20 @@ public async Task ExecuteAsyncCore(ResourceViewModel resource, CommandViewModel
if (response.Result is not null)
{
toastParameters.SecondaryAction = loc[nameof(Dashboard.Resources.Resources.ResourceCommandViewResponse)];
toastParameters.OnSecondaryAction = EventCallback.Factory.Create<ToastResult>(this, () => OpenViewResponseDialogAsync(command, response));
toastParameters.OnSecondaryAction = EventCallback.Factory.Create<ToastResult>(this, () => OpenViewResponseDialogAsync(dialogService, command, response));
}

notificationService.ReplaceNotification(progressNotificationId, new NotificationEntry
{
Title = failedTitle,
Body = response.Message,
Intent = MessageIntent.Error,
PrimaryAction = response.Result is not null ? CreateViewResponseNotificationAction(command, response) : null
PrimaryAction = response.Result is not null ? CreateViewResponseNotificationAction(loc, command, response) : null
});

if (response.Result?.DisplayImmediately == true)
{
await OpenViewResponseDialogAsync(command, response).ConfigureAwait(false);
await OpenViewResponseDialogAsync(dialogService, command, response).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -261,16 +261,22 @@ private static (Icon Icon, Color Color)? GetIntentIcon(ToastIntent intent)
};
}

private NotificationAction CreateViewResponseNotificationAction(CommandViewModel command, ResourceCommandResponseViewModel response)
private static NotificationAction CreateViewResponseNotificationAction(IStringLocalizer<Dashboard.Resources.Resources> loc, CommandViewModel command, ResourceCommandResponseViewModel response)
{
return new NotificationAction
{
Text = loc[nameof(Dashboard.Resources.Resources.ResourceCommandViewResponse)],
OnClick = () => OpenViewResponseDialogAsync(command, response)
OnClick = (services) =>
{
// Get dialog service from passed in services since this data is long lived.
// Using the dialog service from executor could cause closure over scoped services.
var dialogService = services.GetRequiredService<DashboardDialogService>();
Comment thread
JamesNK marked this conversation as resolved.
return OpenViewResponseDialogAsync(dialogService, command, response);
}
};
}

private async Task OpenViewResponseDialogAsync(CommandViewModel command, ResourceCommandResponseViewModel response)
private static async Task OpenViewResponseDialogAsync(DashboardDialogService dialogService, CommandViewModel command, ResourceCommandResponseViewModel response)
{
var fixedFormat = response.Result!.Format switch
{
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.Dashboard/Model/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public sealed class NotificationEntry
public sealed class NotificationAction
{
public required string Text { get; init; }
public required Func<Task> OnClick { get; init; }
public required Func<IServiceProvider, Task> OnClick { get; init; }
}

/// <summary>
Expand Down
Loading