// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Dashboard.Utils;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.FluentUI.AspNetCore.Components;
using Icons = Microsoft.FluentUI.AspNetCore.Components.Icons;
namespace Aspire.Dashboard.Components.Layout;
/// <summary>
/// The top-level navigation displayed across the top of the desktop layout. With a resource
/// service the dashboard is organized around the resource selection, which includes telemetry-only services, and
/// telemetry is reached from the pages of a resource selection. Standalone, the navigation has a single telemetry
/// section.
/// </summary>
public partial class DesktopNavMenu : ComponentBase, IDisposable
{
internal static Icon TelemetryIcon(bool active = false) =>
active ? new Icons.Filled.Size24.DataUsage() : new Icons.Regular.Size24.DataUsage();
internal static Icon HomeIcon(bool active = false) =>
active ? new Icons.Filled.Size24.Home()
: new Icons.Regular.Size24.Home();
internal static Icon ConsoleLogsIcon(bool active = false) =>
active ? new Icons.Filled.Size24.SlideText()
: new Icons.Regular.Size24.SlideText();
internal static Icon ResourcesIcon(bool active = false) =>
active ? new Icons.Filled.Size24.AppFolder()
: new Icons.Regular.Size24.AppFolder();
// WindowConsole only ships at Size20 in this Fluent version, so callers scale it to 24px. The Size24 alternatives
// (WindowDevTools, Code) read as "developer tools" rather than "terminal".
internal static Icon TerminalsIcon(bool active = false) =>
active ? new Icons.Filled.Size20.WindowConsole()
: new Icons.Regular.Size20.WindowConsole();
internal static Icon ExtensionsIcon(bool active = false) =>
active ? new Icons.Filled.Size24.PuzzlePiece()
: new Icons.Regular.Size24.PuzzlePiece();
internal static Icon ParametersIcon(bool active = false) =>
active ? new Icons.Filled.Size24.Key()
: new Icons.Regular.Size24.Key();
internal static Icon GraphIcon(bool active = false) =>
active ? new Icons.Filled.Size24.ShareAndroid()
: new Icons.Regular.Size24.ShareAndroid();
internal static Icon StructuredLogsIcon(bool active = false) =>
active ? new Icons.Filled.Size24.SlideTextSparkle()
: new Icons.Regular.Size24.SlideTextSparkle();
internal static Icon TracesIcon(bool active = false) =>
active ? new Icons.Filled.Size24.GanttChart()
: new Icons.Regular.Size24.GanttChart();
internal static Icon MetricsIcon(bool active = false) =>
active ? new Icons.Filled.Size24.ChartMultiple()
: new Icons.Regular.Size24.ChartMultiple();
[Inject]
public required NavigationManager NavigationManager { get; init; }
private NavSection _activeSection;
protected override void OnInitialized()
{
NavigationManager.LocationChanged += OnLocationChanged;
_activeSection = GetSection(NavigationManager.ToBaseRelativePath(NavigationManager.Uri), DashboardClient.IsEnabled);
}
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
// Always re-render: the links to a selection's pages follow the selection in the URL.
_activeSection = GetSection(NavigationManager.ToBaseRelativePath(e.Location), DashboardClient.IsEnabled);
_ = InvokeAsync(StateHasChanged);
}
/// <summary>
/// Gets the URL of a page of the current resource selection, such as its console logs. The selection is read from
/// the current URL; pages that aren't about resources, like the parameters, select every resource.
/// </summary>
internal static string GetSelectionPageUrl(NavigationManager navigationManager, ResourceTab tab)
{
var path = navigationManager.ToBaseRelativePath(navigationManager.Uri);
IReadOnlyList<string> resourceNames = [];
string? routeResourceName = null;
if (GetSection(path, hasResourceService: true) is NavSection.Resources)
{
var location = ResourcesLayout.ParseLocation(path);
resourceNames = location.SelectedResourceNames;
routeResourceName = location.RouteResourceName;
}
// Without a selection, the overview restores the last selected resources, like the Resources entry always did.
if (tab == ResourceTab.Overview && resourceNames.Count == 0)
{
return DashboardUrls.ResourceListUrl();
}
return ResourcesLayout.GetTabUrl(tab, resourceNames, routeResourceName);
}
private IEnumerable<NavItem> GetItems()
{
if (DashboardClient.IsEnabled)
{
// The pages of a selection (overview, console logs, structured logs, traces and metrics) are icons at the
// top of the resources view, so the app navigation has a single entry for them. Telemetry sources are
// listed with the app's resources.
yield return new NavItem(NavSection.Resources, GetSelectionPageUrl(NavigationManager, ResourceTab.Overview), Loc[nameof(Resources.Layout.NavMenuResourcesTab)], ResourcesIcon(), ResourcesIcon(active: true));
}
else
{
yield return new NavItem(NavSection.Telemetry, DashboardUrls.StructuredLogsUrl(), Loc[nameof(Resources.Layout.NavMenuTelemetrySourcesTab)], TelemetryIcon(), TelemetryIcon(active: true));
}
}
/// <summary>
/// Maps a base-relative path (e.g. <c>traces/resource/api?type=http</c>) to the top-level section it belongs to.
/// </summary>
internal static NavSection GetSection(string baseRelativePath, bool hasResourceService)
{
var path = baseRelativePath;
var queryIndex = path.IndexOfAny(['?', '#']);
if (queryIndex >= 0)
{
path = path[..queryIndex];
}
var firstSegment = path.Split('/', 2)[0];
if (!hasResourceService)
{
return firstSegment switch
{
// The root URL redirects to the overview, which summarizes the telemetry sources.
"" or
DashboardUrls.ResourceOverviewBasePath or
DashboardUrls.StructuredLogsBasePath or
DashboardUrls.TracesBasePath or
DashboardUrls.MetricsBasePath => NavSection.Telemetry,
_ => NavSection.None
};
}
return firstSegment switch
{
// The root URL redirects to the resources.
"" => NavSection.Resources,
// The pages of a selection are part of the resources view.
DashboardUrls.ConsoleLogBasePath or
DashboardUrls.StructuredLogsBasePath or
DashboardUrls.TracesBasePath or
DashboardUrls.MetricsBasePath => NavSection.Resources,
DashboardUrls.ParametersBasePath => NavSection.Parameters,
DashboardUrls.GraphBasePath => NavSection.Graph,
DashboardUrls.TerminalsBasePath => path.StartsWith($"{DashboardUrls.TerminalsBasePath}/resource/", StringComparisons.UrlPath)
? NavSection.Resources
: NavSection.Terminals,
DashboardUrls.ExtensionsBasePath => NavSection.Extensions,
DashboardUrls.ResourceOverviewBasePath => NavSection.Resources,
_ => NavSection.None
};
}
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
}
internal enum NavSection
{
None,
Resources,
Telemetry,
Parameters,
Graph,
Terminals,
Extensions
}
private sealed record NavItem(NavSection Section, string Href, string Text, Icon Icon, Icon ActiveIcon);
}