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
61 changes: 34 additions & 27 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
<PageTitle><ApplicationName ResourceName="@nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsPageTitle)" Loc="@Loc" /></PageTitle>

<div class="page-content-container">
<AspirePageContentLayout
AddNewlineOnToolbar="true"
@ref="@_contentLayout"
MainContentStyle="margin-top: 10px;"
MobileToolbarButtonText="@Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsSelectResourceToolbar)]">
<AspirePageContentLayout AddNewlineOnToolbar="true"
@ref="@_contentLayout"
MainContentStyle="margin-top: 10px;"
MobileToolbarButtonText="@Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsSelectResourceToolbar)]">
<PageTitleSection>
<h1 class="page-header">@Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsHeader)]</h1>
</PageTitleSection>
Expand All @@ -21,34 +20,42 @@
AriaLabel="@ControlsStringsLoc[nameof(ControlsStrings.ResourceLabel)]"
@bind-SelectedResource="PageViewModel.SelectedOption"
@bind-SelectedResource:after="HandleSelectedOptionChangedAsync" />
@if (ViewportInformation.IsDesktop)
{
// This takes up too much horizontal space on mobile, so show on a new line on mobile
<FluentLabel Typo="Typography.Body" aria-live="polite" aria-label="@Loc[nameof(Dashboard.Resources.ConsoleLogs.LogStatusLabel)]">@PageViewModel.Status</FluentLabel>
}

@{
var menuItems = new List<MenuButtonItem>
{
new()
@foreach (var command in _highlightedCommands)
{
<FluentButton Appearance="Appearance.Lightweight"
Title="@(!string.IsNullOrEmpty(command.DisplayDescription) ? command.DisplayDescription : command.DisplayName)"
Disabled="@(command.State == CommandViewModelState.Disabled)"
OnClick="@(() => ExecuteResourceCommandAsync(command))">
@if (!string.IsNullOrEmpty(command.IconName) && CommandViewModel.ResolveIconName(command.IconName, command.IconVariant) is { } icon)
{
<FluentIcon Value="@icon" Width="16px" />
}
else
{
IsDisabled = PageViewModel.SelectedResource is null,
OnClick = DownloadLogsAsync,
AdditionalAttributes = new Dictionary<string, object>
{
{ "data-action", "download" },
{ "data-resource", PageViewModel.SelectedResource?.Name ?? string.Empty }
},
Comment on lines -37 to -41
Copy link
Member Author

Choose a reason for hiding this comment

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

@adamint I believe these were left over from earlier iterations of logs download. I removed them and everything still seems to work

Text = Loc[nameof(Dashboard.Resources.ConsoleLogs.DownloadLogs)],
Icon = new Icons.Regular.Size16.ArrowDownload()
@command.DisplayName
}
};
</FluentButton>
}

@if (_resourceMenuItems.Count > 0)
{
<AspireMenuButton ButtonAppearance="Appearance.Lightweight"
Icon="@(new Icons.Regular.Size20.MoreHorizontal())"
Items="@_resourceMenuItems"
Title="@Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsResourceCommands)]" />
}

@if (ViewportInformation.IsDesktop)
{
// This takes up too much horizontal space on mobile, so show on a new line on mobile
<FluentLabel Typo="Typography.Body" aria-live="polite" aria-label="@Loc[nameof(Dashboard.Resources.ConsoleLogs.LogStatusLabel)]" slot="end">@PageViewModel.Status</FluentLabel>
}

<AspireMenuButton ButtonAppearance="Appearance.Lightweight"
Icon="@(new Icons.Regular.Size20.MoreHorizontal())"
Items="@menuItems"
Title="@ControlsStringsLoc[nameof(ControlsStrings.ActionsButtonText)]"
Icon="@(new Icons.Regular.Size20.Settings())"
Items="@_logsMenuItems"
Title="@Loc[nameof(Dashboard.Resources.ConsoleLogs.ConsoleLogsSettings)]"
slot="end" />
</ToolbarSection>

Expand Down
58 changes: 58 additions & 0 deletions src/Aspire.Dashboard/Components/Pages/ConsoleLogs.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.JSInterop;

namespace Aspire.Dashboard.Components.Pages;
Expand Down Expand Up @@ -63,6 +64,9 @@ private sealed class ConsoleLogsSubscription
[Inject]
public required IJSRuntime JS { get; init; }

[Inject]
public required DashboardCommandExecutor DashboardCommandExecutor { get; init; }

[CascadingParameter]
public required ViewportInformation ViewportInformation { get; init; }

Expand All @@ -80,6 +84,9 @@ private sealed class ConsoleLogsSubscription
// UI
private SelectViewModel<ResourceTypeDetails> _noSelection = null!;
private AspirePageContentLayout? _contentLayout;
private readonly List<CommandViewModel> _highlightedCommands = new();
private readonly List<MenuButtonItem> _logsMenuItems = new();
private readonly List<MenuButtonItem> _resourceMenuItems = new();

// State
public ConsoleLogsViewModel PageViewModel { get; set; } = null!;
Expand Down Expand Up @@ -166,6 +173,7 @@ async Task TrackResourceSnapshotsAsync()
}
}

UpdateMenuButtons();
await InvokeAsync(StateHasChanged);
}
});
Expand All @@ -191,6 +199,8 @@ protected override async Task OnParametersSetAsync()
return;
}

UpdateMenuButtons();

var selectedResourceName = PageViewModel.SelectedResource?.Name;
if (!string.Equals(selectedResourceName, _consoleLogsSubscription?.Name, StringComparisons.ResourceName))
{
Expand Down Expand Up @@ -234,6 +244,54 @@ protected override async Task OnParametersSetAsync()
}
}

private void UpdateMenuButtons()
{
_highlightedCommands.Clear();
_logsMenuItems.Clear();
_resourceMenuItems.Clear();

_logsMenuItems.Add(new()
{
IsDisabled = PageViewModel.SelectedResource is null,
OnClick = DownloadLogsAsync,
Text = Loc[nameof(Dashboard.Resources.ConsoleLogs.DownloadLogs)],
Icon = new Icons.Regular.Size16.ArrowDownload()
});

if (PageViewModel.SelectedResource != null)
{
if (ViewportInformation.IsDesktop)
{
_highlightedCommands.AddRange(PageViewModel.SelectedResource.Commands.Where(c => c.IsHighlighted && c.State != CommandViewModelState.Hidden).Take(DashboardUIHelpers.MaxHighlightedCommands));
}

var menuCommands = PageViewModel.SelectedResource.Commands.Where(c => !_highlightedCommands.Contains(c) && c.State != CommandViewModelState.Hidden).ToList();
if (menuCommands.Count > 0)
{
foreach (var command in menuCommands)
{
var icon = (!string.IsNullOrEmpty(command.IconName) && CommandViewModel.ResolveIconName(command.IconName, command.IconVariant) is { } i) ? i : null;

_resourceMenuItems.Add(new MenuButtonItem
{
Text = command.DisplayName,
Tooltip = command.DisplayDescription,
Icon = icon,
OnClick = () => ExecuteResourceCommandAsync(command),
IsDisabled = command.State == CommandViewModelState.Disabled
});
}
}
}
}

private async Task ExecuteResourceCommandAsync(CommandViewModel command)
{
await DashboardCommandExecutor.ExecuteAsync(PageViewModel.SelectedResource!, command, GetResourceName);
}

private string GetResourceName(ResourceViewModel resource) => ResourceViewModel.GetResourceName(resource, _resourceByName);

internal static ImmutableList<SelectViewModel<ResourceTypeDetails>> GetConsoleLogResourceSelectViewModels(
ConcurrentDictionary<string, ResourceViewModel> resourcesByName,
SelectViewModel<ResourceTypeDetails> noSelectionViewModel,
Expand Down
69 changes: 3 additions & 66 deletions src/Aspire.Dashboard/Components/Pages/Resources.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public partial class Resources : ComponentBase, IAsyncDisposable
[Inject]
public required NavigationManager NavigationManager { get; init; }
[Inject]
public required IDialogService DialogService { get; init; }
[Inject]
public required IToastService ToastService { get; init; }
public required DashboardCommandExecutor DashboardCommandExecutor { get; init; }
[Inject]
public required BrowserTimeProvider TimeProvider { get; init; }
[Inject]
Expand Down Expand Up @@ -283,7 +281,7 @@ private void UpdateMaxHighlightedCount()

// Don't attempt to display more than 2 highlighted commands. Many commands will take up too much space.
// Extra highlighted commands are still available in the menu.
_maxHighlightedCount = Math.Min(maxHighlightedCount, 2);
_maxHighlightedCount = Math.Min(maxHighlightedCount, DashboardUIHelpers.MaxHighlightedCommands);
}

protected override async Task OnParametersSetAsync()
Expand Down Expand Up @@ -394,68 +392,7 @@ private string GetRowClass(ResourceViewModel resource)

private async Task ExecuteResourceCommandAsync(ResourceViewModel resource, CommandViewModel command)
{
if (!string.IsNullOrWhiteSpace(command.ConfirmationMessage))
{
var dialogReference = await DialogService.ShowConfirmationAsync(command.ConfirmationMessage);
var result = await dialogReference.Result;
if (result.Cancelled)
{
return;
}
}

var messageResourceName = GetResourceName(resource);

var toastParameters = new ToastParameters<CommunicationToastContent>()
{
Id = Guid.NewGuid().ToString(),
Intent = ToastIntent.Progress,
Title = string.Format(CultureInfo.InvariantCulture, Loc[nameof(Dashboard.Resources.Resources.ResourceCommandStarting)], messageResourceName, command.DisplayName),
Content = new CommunicationToastContent()
};

// Show a toast immediately to indicate the command is starting.
ToastService.ShowCommunicationToast(toastParameters);

var response = await DashboardClient.ExecuteResourceCommandAsync(resource.Name, resource.ResourceType, command, CancellationToken.None);

// Update toast with the result;
if (response.Kind == ResourceCommandResponseKind.Succeeded)
{
toastParameters.Title = string.Format(CultureInfo.InvariantCulture, Loc[nameof(Dashboard.Resources.Resources.ResourceCommandSuccess)], messageResourceName, command.DisplayName);
toastParameters.Intent = ToastIntent.Success;
toastParameters.Icon = GetIntentIcon(ToastIntent.Success);
}
else
{
toastParameters.Title = string.Format(CultureInfo.InvariantCulture, Loc[nameof(Dashboard.Resources.Resources.ResourceCommandFailed)], messageResourceName, command.DisplayName);
toastParameters.Intent = ToastIntent.Error;
toastParameters.Icon = GetIntentIcon(ToastIntent.Error);
toastParameters.Content.Details = response.ErrorMessage;
toastParameters.PrimaryAction = Loc[nameof(Dashboard.Resources.Resources.ResourceCommandToastViewLogs)];
toastParameters.OnPrimaryAction = EventCallback.Factory.Create<ToastResult>(this, () => NavigationManager.NavigateTo(DashboardUrls.ConsoleLogsUrl(resource: resource.Name)));
}

ToastService.UpdateToast(toastParameters.Id, toastParameters);
}

// Copied from FluentUI.
private static (Icon Icon, Color Color)? GetIntentIcon(ToastIntent intent)
{
return intent switch
{
ToastIntent.Success => (new Icons.Filled.Size24.CheckmarkCircle(), Color.Success),
ToastIntent.Warning => (new Icons.Filled.Size24.Warning(), Color.Warning),
ToastIntent.Error => (new Icons.Filled.Size24.DismissCircle(), Color.Error),
ToastIntent.Info => (new Icons.Filled.Size24.Info(), Color.Info),
ToastIntent.Progress => (new Icons.Regular.Size24.Flash(), Color.Neutral),
ToastIntent.Upload => (new Icons.Regular.Size24.ArrowUpload(), Color.Neutral),
ToastIntent.Download => (new Icons.Regular.Size24.ArrowDownload(), Color.Neutral),
ToastIntent.Event => (new Icons.Regular.Size24.CalendarLtr(), Color.Neutral),
ToastIntent.Mention => (new Icons.Regular.Size24.Person(), Color.Neutral),
ToastIntent.Custom => null,
_ => throw new InvalidOperationException()
};
await DashboardCommandExecutor.ExecuteAsync(resource, command, GetResourceName);
}

private static (string Value, string? ContentAfterValue, string ValueToCopy, string Tooltip)? GetSourceColumnValueAndTooltip(ResourceViewModel resource)
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public DashboardWebApplication(
// Data from the server.
builder.Services.TryAddScoped<IDashboardClient, DashboardClient>();
builder.Services.TryAddSingleton<IDashboardClientStatus, DashboardClientStatus>();
builder.Services.TryAddScoped<DashboardCommandExecutor>();

// OTLP services.
builder.Services.AddGrpc();
Expand Down
84 changes: 84 additions & 0 deletions src/Aspire.Dashboard/Model/DashboardCommandExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Aspire.Dashboard.Utils;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.FluentUI.AspNetCore.Components;

namespace Aspire.Dashboard.Model;

public sealed class DashboardCommandExecutor(
IDashboardClient dashboardClient,
IDialogService dialogService,
IToastService toastService,
IStringLocalizer<Dashboard.Resources.Resources> loc,
NavigationManager navigationManager)
{
public async Task ExecuteAsync(ResourceViewModel resource, CommandViewModel command, Func<ResourceViewModel, string> getResourceName)
{
if (!string.IsNullOrWhiteSpace(command.ConfirmationMessage))
{
var dialogReference = await dialogService.ShowConfirmationAsync(command.ConfirmationMessage).ConfigureAwait(false);
var result = await dialogReference.Result.ConfigureAwait(false);
if (result.Cancelled)
{
return;
}
}

var messageResourceName = getResourceName(resource);

var toastParameters = new ToastParameters<CommunicationToastContent>()
{
Id = Guid.NewGuid().ToString(),
Intent = ToastIntent.Progress,
Title = string.Format(CultureInfo.InvariantCulture, loc[nameof(Dashboard.Resources.Resources.ResourceCommandStarting)], messageResourceName, command.DisplayName),
Content = new CommunicationToastContent()
};

// Show a toast immediately to indicate the command is starting.
toastService.ShowCommunicationToast(toastParameters);

var response = await dashboardClient.ExecuteResourceCommandAsync(resource.Name, resource.ResourceType, command, CancellationToken.None).ConfigureAwait(false);

// Update toast with the result;
if (response.Kind == ResourceCommandResponseKind.Succeeded)
{
toastParameters.Title = string.Format(CultureInfo.InvariantCulture, loc[nameof(Dashboard.Resources.Resources.ResourceCommandSuccess)], messageResourceName, command.DisplayName);
toastParameters.Intent = ToastIntent.Success;
toastParameters.Icon = GetIntentIcon(ToastIntent.Success);
}
else
{
toastParameters.Title = string.Format(CultureInfo.InvariantCulture, loc[nameof(Dashboard.Resources.Resources.ResourceCommandFailed)], messageResourceName, command.DisplayName);
toastParameters.Intent = ToastIntent.Error;
toastParameters.Icon = GetIntentIcon(ToastIntent.Error);
toastParameters.Content.Details = response.ErrorMessage;
toastParameters.PrimaryAction = loc[nameof(Dashboard.Resources.Resources.ResourceCommandToastViewLogs)];
toastParameters.OnPrimaryAction = EventCallback.Factory.Create<ToastResult>(this, () => navigationManager.NavigateTo(DashboardUrls.ConsoleLogsUrl(resource: resource.Name)));
}

toastService.UpdateToast(toastParameters.Id, toastParameters);
}

// Copied from FluentUI.
private static (Icon Icon, Color Color)? GetIntentIcon(ToastIntent intent)
{
return intent switch
{
ToastIntent.Success => (new Icons.Filled.Size24.CheckmarkCircle(), Color.Success),
ToastIntent.Warning => (new Icons.Filled.Size24.Warning(), Color.Warning),
ToastIntent.Error => (new Icons.Filled.Size24.DismissCircle(), Color.Error),
ToastIntent.Info => (new Icons.Filled.Size24.Info(), Color.Info),
ToastIntent.Progress => (new Icons.Regular.Size24.Flash(), Color.Neutral),
ToastIntent.Upload => (new Icons.Regular.Size24.ArrowUpload(), Color.Neutral),
ToastIntent.Download => (new Icons.Regular.Size24.ArrowDownload(), Color.Neutral),
ToastIntent.Event => (new Icons.Regular.Size24.CalendarLtr(), Color.Neutral),
ToastIntent.Mention => (new Icons.Regular.Size24.Person(), Color.Neutral),
ToastIntent.Custom => null,
_ => throw new InvalidOperationException()
};
}
}
Loading