Skip to content

Commit

Permalink
Delegate filtering logic to implementation
Browse files Browse the repository at this point in the history
Avoids having a special check for a specific type.

Once we move to a truly generic form for resources, we'll have to check extension data values, possibly adding metadata to control which items participate in search.
  • Loading branch information
drewnoakes committed Dec 7, 2023
1 parent 2b15a50 commit 4183ef1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/Aspire.Dashboard/Components/Pages/Resources.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ public partial class Resources : ComponentBase, IDisposable
[Inject]
public required TelemetryRepository TelemetryRepository { get; init; }
[Inject]
public required NavigationManager NavigationManager { get; set; }
public required NavigationManager NavigationManager { get; init; }

private IEnumerable<EnvironmentVariableViewModel>? SelectedEnvironmentVariables { get; set; }
private ResourceViewModel? SelectedResource { get; set; }

private bool Filter(ResourceViewModel resource)
=> _visibleResourceTypes.Contains(resource.ResourceType) &&
(resource.Name.Contains(_filter, StringComparison.CurrentCultureIgnoreCase) ||
(resource is ContainerViewModel containerViewModel &&
containerViewModel.Image.Contains(_filter, StringComparison.CurrentCultureIgnoreCase)));

private readonly CancellationTokenSource _watchTaskCancellationTokenSource = new();
private readonly Dictionary<string, ResourceViewModel> _resourcesMap = [];
// TODO populate resource types from server data
Expand All @@ -45,6 +39,8 @@ public Resources()
_visibleResourceTypes = new HashSet<string>(_allResourceTypes, StringComparers.ResourceType);
}

private bool Filter(ResourceViewModel resource) => _visibleResourceTypes.Contains(resource.ResourceType) && (_filter.Length == 0 || resource.MatchesFilter(_filter));

protected void OnResourceTypeVisibilityChanged(string resourceType, bool isVisible)
{
if (isVisible)
Expand Down
9 changes: 8 additions & 1 deletion src/Aspire.Dashboard/Extensions/StringComparers.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.Dashboard.Extensions;

internal static class StringComparers
{
public static StringComparer ResourceType => StringComparer.Ordinal;
public static StringComparer UserTextSearch => StringComparer.CurrentCultureIgnoreCase;
}

internal static class StringComparisons
{
public static StringComparison ResourceType => StringComparison.Ordinal;
public static StringComparison UserTextSearch => StringComparison.CurrentCultureIgnoreCase;
}
7 changes: 7 additions & 0 deletions src/Aspire.Dashboard/Model/ContainerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Extensions;

namespace Aspire.Dashboard.Model;

public class ContainerViewModel : ResourceViewModel
Expand All @@ -11,4 +13,9 @@ public class ContainerViewModel : ResourceViewModel
public List<int> Ports { get; } = new();
public string? Command { get; init; }
public List<string>? Args { get; init; }

internal override bool MatchesFilter(string filter)
{
return base.MatchesFilter(filter) || Image.Contains(filter, StringComparisons.UserTextSearch);
}
}
7 changes: 7 additions & 0 deletions src/Aspire.Dashboard/Model/ResourceViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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.Extensions;

namespace Aspire.Dashboard.Model;

public abstract class ResourceViewModel
Expand Down Expand Up @@ -34,6 +36,11 @@ public static string GetResourceName(ResourceViewModel resource, IEnumerable<Res

return resource.DisplayName;
}

internal virtual bool MatchesFilter(string filter)
{
return Name.Contains(filter, StringComparisons.UserTextSearch);
}
}

public sealed class ResourceService(string name, string? allocatedAddress, int? allocatedPort)
Expand Down

0 comments on commit 4183ef1

Please sign in to comment.