LANCommander/LANCommander.Client/UI/Library/Components/LibraryList.razor
2024-07-13 12:55:42 -05:00

93 lines
No EOL
3.1 KiB
Text

@using LANCommander.Client.Models
@inject LibraryService LibraryService
@inject ImportService ImportService
@inject MessageBusService MessageBusService
<div class="library-list">
<Collapse>
@if (Groups.Count > 1)
{
foreach (var group in Groups.OrderByTitle(g => g))
{
<Panel Header="@group" Key="@group">
<AntList DataSource="@(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>();
IEnumerable<LibraryItem> LibraryItems;
protected override void OnInitialized()
{
LibraryService.OnLibraryChanged += UpdateGroups;
LibraryService.OnLibraryItemsFiltered += UpdateGroups;
ImportService.OnImportComplete += LibraryService.LibraryChanged;
UpdateGroups(LibraryService.LibraryItems);
}
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);
}
async Task UpdateGroups(IEnumerable<LibraryItem> items)
{
LibraryItems = items;
Groups = LibraryItems.SelectMany(i => i.Groups).Distinct().ToList();
if (LibraryItems.Any(i => i.Groups == null || i.Groups.Length == 0))
Groups.Add("Uncategorized");
await InvokeAsync(StateHasChanged);
}
public void Dispose()
{
LibraryService.OnLibraryChanged -= UpdateGroups;
LibraryService.OnLibraryItemsFiltered -= UpdateGroups;
ImportService.OnImportComplete -= LibraryService.LibraryChanged;
}
}