LANCommander/LANCommander.Client/UI/Library/Components/LibraryList.razor
2024-07-10 17:39:23 -05:00

81 lines
No EOL
2.7 KiB
Text

@using LANCommander.Client.Models
@inject LibraryService LibraryService
@inject ImportService ImportService
<div class="library-list">
<Collapse>
@if (Groups.Count > 1)
{
foreach (var group in Groups.OrderByTitle(g => g))
{
<Panel Header="@group" Key="@group">
<AntList DataSource="@(LibraryService.LibraryItems.Where(i => i.Groups.Contains(group) || (group == "Uncategorized" && !i.Groups.Any())))" TItem="LibraryItem" Class="ant-list-clickable" Size="small">
<ListItem OnClick='() => SelectItem(context)' Class="@GetItemClasses(context)">
<div class="library-list-icon">
<MediaImage Id="@context.IconId" />
</div>
<span>@context.Name</span>
</ListItem>
</AntList>
</Panel>
}
}
else
{
<Panel Class="no-header">
<AntList DataSource="LibraryService.LibraryItems" TItem="LibraryItem" Class="ant-list-clickable" Size="small">
<ListItem OnClick='() => SelectItem(context)' Class="@GetItemClasses(context)">
<div class="library-list-icon">
<MediaImage Id="@context.IconId" />
</div>
<span>@context.Name</span>
</ListItem>
</AntList>
</Panel>
}
</Collapse>
</div>
@code {
[Parameter] public Guid? SelectedItem { get; set; }
[Parameter] public EventCallback<Guid> OnItemSelected { get; set; }
List<string> Groups = new List<string>();
protected override void OnInitialized()
{
LibraryService.OnLibraryChanged += UpdateGroups;
ImportService.OnImportComplete += UpdateGroups;
UpdateGroups();
}
string GetItemClasses(LibraryItem item)
{
HashSet<string> classes = new HashSet<string>();
if (item.State == LibraryItemState.Installed)
classes.Add("installed");
if (SelectedItem == item.Key)
classes.Add("selected");
return String.Join(" ", classes);
}
async Task SelectItem(LibraryItem item)
{
if (OnItemSelected.HasDelegate)
await OnItemSelected.InvokeAsync(item.Key);
}
void UpdateGroups()
{
Groups = LibraryService.LibraryItems.SelectMany(i => i.Groups).Distinct().ToList();
if (LibraryService.LibraryItems.Any(i => i.Groups == null || i.Groups.Length == 0))
Groups.Add("Uncategorized");
InvokeAsync(StateHasChanged);
}
}