Refactored project to module structure
This commit is contained in:
126
Moonlight.Frontend/Infrastructure/Partials/AppSidebar.razor
Normal file
126
Moonlight.Frontend/Infrastructure/Partials/AppSidebar.razor
Normal file
@@ -0,0 +1,126 @@
|
||||
@inject NavigationManager Navigation
|
||||
@inject IAuthorizationService AuthorizationService
|
||||
@inject FrontendService FrontendService
|
||||
@inject IEnumerable<ISidebarProvider> Providers
|
||||
|
||||
@using System.Text.Json
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Moonlight.Frontend.Infrastructure.Hooks
|
||||
@using Moonlight.Frontend.Infrastructure.Models
|
||||
@using Moonlight.Frontend.Shared.Frontend
|
||||
@using ShadcnBlazor.Sidebars
|
||||
@implements IDisposable
|
||||
|
||||
@{
|
||||
var url = new Uri(Navigation.Uri);
|
||||
}
|
||||
|
||||
<Sidebar Variant="SidebarVariant.Sidebar" Collapsible="SidebarCollapsible.Icon">
|
||||
<SidebarHeader>
|
||||
<SidebarMenu>
|
||||
<SidebarMenuItem>
|
||||
<SidebarMenuButton>
|
||||
<a href="/" class="flex flex-row items-center">
|
||||
<img alt="Logo" src="/_content/Moonlight.Frontend/logo.svg" class="size-6"/>
|
||||
<span class="ms-2.5 text-lg font-semibold">@FrontendConfiguration?.Name</span>
|
||||
</a>
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
</SidebarMenu>
|
||||
</SidebarHeader>
|
||||
|
||||
<SidebarContent>
|
||||
@foreach (var group in Items.GroupBy(x => x.Group))
|
||||
{
|
||||
<SidebarGroup>
|
||||
@if (!string.IsNullOrWhiteSpace(group.Key))
|
||||
{
|
||||
<SidebarGroupLabel>
|
||||
@group.Key
|
||||
</SidebarGroupLabel>
|
||||
}
|
||||
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
@foreach (var item in group.OrderBy(x => x.Order))
|
||||
{
|
||||
var isActive = item.IsExactPath
|
||||
? item.Path == url.LocalPath
|
||||
: url.LocalPath.StartsWith(item.Path, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
<SidebarMenuItem @key="item">
|
||||
<SidebarMenuButton IsActive="@isActive">
|
||||
<Slot>
|
||||
<a href="@item.Path" @attributes="context">
|
||||
<DynamicComponent Type="item.IconType"/>
|
||||
<span>@item.Name</span>
|
||||
</a>
|
||||
</Slot>
|
||||
</SidebarMenuButton>
|
||||
</SidebarMenuItem>
|
||||
}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
}
|
||||
</SidebarContent>
|
||||
|
||||
<SidebarFooter>
|
||||
<NavUser/>
|
||||
</SidebarFooter>
|
||||
</Sidebar>
|
||||
|
||||
@code
|
||||
{
|
||||
[CascadingParameter] public Task<AuthenticationState> AuthState { get; set; }
|
||||
|
||||
private readonly List<SidebarItem> Items = new();
|
||||
private FrontendConfiguration? FrontendConfiguration;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var authState = await AuthState;
|
||||
|
||||
foreach (var provider in Providers)
|
||||
{
|
||||
var items = await provider.GetItemsAsync();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(item.Policy))
|
||||
{
|
||||
var result = await AuthorizationService.AuthorizeAsync(authState.User, item.Policy);
|
||||
|
||||
if (!result.Succeeded)
|
||||
continue;
|
||||
}
|
||||
|
||||
Items.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
Navigation.LocationChanged += OnLocationChanged;
|
||||
|
||||
FrontendConfiguration = await FrontendService.GetConfigurationAsync();
|
||||
|
||||
Console.WriteLine(JsonSerializer.Serialize(FrontendConfiguration));
|
||||
}
|
||||
|
||||
private async void OnLocationChanged(object? sender, LocationChangedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Navigation.LocationChanged -= OnLocationChanged;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user