| | | 1 | | @page "/{*Path}" |
| | | 2 | | @layout PassthroughLayout |
| | | 3 | | @using Microsoft.AspNetCore.Components.Authorization |
| | | 4 | | @using Microsoft.AspNetCore.Components.Rendering |
| | | 5 | | @using Chronicis.Client.Pages |
| | | 6 | | @using Chronicis.Client.Pages.Maps |
| | | 7 | | @using Chronicis.Client.Services.Routing |
| | | 8 | | @using Chronicis.Shared.Routing |
| | | 9 | | @inject IPathApiService PathApi |
| | | 10 | | @inject IClientReservedSlugProvider ReservedSlugs |
| | | 11 | | @inject NavigationManager Navigation |
| | | 12 | | @inject AuthenticationStateProvider AuthStateProvider |
| | | 13 | | |
| | | 14 | | <LayoutView Layout="@_layoutType"> |
| | | 15 | | @if (_isLoading) |
| | | 16 | | { |
| | | 17 | | <LoadingSkeleton /> |
| | | 18 | | } |
| | | 19 | | else if (_resolution == null && _hasResolved) |
| | | 20 | | { |
| | | 21 | | <NotFoundAlert Message="Page not found" /> |
| | | 22 | | } |
| | | 23 | | else if (_resolution != null) |
| | | 24 | | { |
| | | 25 | | @RenderResolution(_resolution) |
| | | 26 | | } |
| | | 27 | | </LayoutView> |
| | | 28 | | |
| | | 29 | | @code { |
| | | 30 | | [Parameter] |
| | | 31 | | public string? Path { get; set; } |
| | | 32 | | |
| | 18 | 33 | | private Type _layoutType = typeof(PublicLayout); |
| | | 34 | | private bool _isLoading; |
| | | 35 | | private bool _hasResolved; |
| | | 36 | | private SlugPathResolution? _resolution; |
| | | 37 | | |
| | | 38 | | internal static Type SelectLayout(bool isAuthenticated) => |
| | 20 | 39 | | isAuthenticated ? typeof(AuthenticatedLayout) : typeof(PublicLayout); |
| | | 40 | | |
| | 21 | 41 | | internal static Type? GetDetailComponentType(ResolvedEntityKind kind) => kind switch |
| | 21 | 42 | | { |
| | 2 | 43 | | ResolvedEntityKind.World => typeof(WorldDetail), |
| | 2 | 44 | | ResolvedEntityKind.Campaign => typeof(CampaignDetail), |
| | 2 | 45 | | ResolvedEntityKind.Arc => typeof(ArcDetail), |
| | 2 | 46 | | ResolvedEntityKind.Session => typeof(SessionDetail), |
| | 3 | 47 | | ResolvedEntityKind.SessionNote => typeof(ArticleDetail), |
| | 3 | 48 | | ResolvedEntityKind.WikiArticle => typeof(ArticleDetail), |
| | 1 | 49 | | ResolvedEntityKind.Tutorial => typeof(ArticleDetail), |
| | 2 | 50 | | ResolvedEntityKind.MapListing => typeof(MapListing), |
| | 2 | 51 | | ResolvedEntityKind.Map => typeof(MapDetail), |
| | 2 | 52 | | _ => null |
| | 21 | 53 | | }; |
| | | 54 | | |
| | | 55 | | protected override async Task OnParametersSetAsync() |
| | | 56 | | { |
| | | 57 | | var authState = await AuthStateProvider.GetAuthenticationStateAsync(); |
| | | 58 | | _layoutType = SelectLayout(authState.User.Identity?.IsAuthenticated == true); |
| | | 59 | | |
| | | 60 | | var segments = Path?.Split('/', StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>(); |
| | | 61 | | |
| | | 62 | | if (segments.Length == 0) |
| | | 63 | | return; |
| | | 64 | | |
| | | 65 | | // Tutorials are handled by PathApi (tutorials is also reserved to block world name collisions) |
| | | 66 | | if (!segments[0].Equals("tutorials", StringComparison.OrdinalIgnoreCase) |
| | | 67 | | && ReservedSlugs.IsReserved(segments[0])) |
| | | 68 | | { |
| | | 69 | | Navigation.NavigateTo("/dashboard", replace: true); |
| | | 70 | | return; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | _isLoading = true; |
| | | 74 | | _hasResolved = false; |
| | | 75 | | StateHasChanged(); |
| | | 76 | | |
| | | 77 | | _resolution = await PathApi.ResolveAsync(Path!, CancellationToken.None); |
| | | 78 | | _isLoading = false; |
| | | 79 | | _hasResolved = true; |
| | | 80 | | } |
| | | 81 | | |
| | 12 | 82 | | private RenderFragment RenderResolution(SlugPathResolution resolution) => builder => |
| | 12 | 83 | | { |
| | 12 | 84 | | var componentType = GetDetailComponentType(resolution.Kind); |
| | 12 | 85 | | if (componentType == null) |
| | 12 | 86 | | return; |
| | 12 | 87 | | |
| | 12 | 88 | | builder.OpenComponent(0, componentType); |
| | 12 | 89 | | AddComponentAttributes(builder, resolution); |
| | 12 | 90 | | builder.CloseComponent(); |
| | 12 | 91 | | }; |
| | | 92 | | |
| | | 93 | | private static void AddComponentAttributes(RenderTreeBuilder builder, SlugPathResolution resolution) |
| | | 94 | | { |
| | 11 | 95 | | switch (resolution.Kind) |
| | | 96 | | { |
| | | 97 | | case ResolvedEntityKind.World: |
| | | 98 | | case ResolvedEntityKind.MapListing: |
| | 2 | 99 | | if (resolution.WorldId.HasValue) |
| | 2 | 100 | | builder.AddAttribute(1, "WorldId", resolution.WorldId.Value); |
| | 2 | 101 | | break; |
| | | 102 | | case ResolvedEntityKind.Campaign: |
| | 1 | 103 | | if (resolution.CampaignId.HasValue) |
| | 1 | 104 | | builder.AddAttribute(1, "CampaignId", resolution.CampaignId.Value); |
| | 1 | 105 | | break; |
| | | 106 | | case ResolvedEntityKind.Arc: |
| | 1 | 107 | | if (resolution.ArcId.HasValue) |
| | 1 | 108 | | builder.AddAttribute(1, "ArcId", resolution.ArcId.Value); |
| | 1 | 109 | | break; |
| | | 110 | | case ResolvedEntityKind.Session: |
| | 1 | 111 | | if (resolution.SessionId.HasValue) |
| | 1 | 112 | | builder.AddAttribute(1, "SessionId", resolution.SessionId.Value); |
| | 1 | 113 | | break; |
| | | 114 | | case ResolvedEntityKind.Map: |
| | 1 | 115 | | if (resolution.WorldId.HasValue) |
| | 1 | 116 | | builder.AddAttribute(1, "WorldId", resolution.WorldId.Value); |
| | 1 | 117 | | if (resolution.MapId.HasValue) |
| | 1 | 118 | | builder.AddAttribute(2, "MapId", resolution.MapId.Value); |
| | 1 | 119 | | break; |
| | | 120 | | case ResolvedEntityKind.SessionNote: |
| | | 121 | | case ResolvedEntityKind.WikiArticle: |
| | | 122 | | case ResolvedEntityKind.Tutorial: |
| | 5 | 123 | | if (resolution.ArticleId.HasValue) |
| | 5 | 124 | | builder.AddAttribute(1, "ArticleId", resolution.ArticleId.Value); |
| | | 125 | | break; |
| | | 126 | | } |
| | 5 | 127 | | } |
| | | 128 | | } |