|
| 1 | +@using MudExtensions |
| 2 | +@using Microsoft.JSInterop |
| 3 | +@implements IAsyncDisposable |
| 4 | + |
| 5 | +@inject IJSRuntime JS |
| 6 | + |
| 7 | +<MudPopup Open="@_open" PopupPosition="PopupPosition.Bottom" Padding="16" Elevation="6" EnableAnimation="true" Class="lab-cookie-popup"> |
| 8 | + |
| 9 | + <ChildContent> |
| 10 | + <div class="lab-cookie-layout"> |
| 11 | + |
| 12 | + <div class="lab-cookie-content"> |
| 13 | + <div class="lab-cookie-icon"> |
| 14 | + <MudIcon Icon="@Icons.Material.Outlined.Shield" Size="Size.Medium" /> |
| 15 | + </div> |
| 16 | + |
| 17 | + <div class="lab-cookie-copy"> |
| 18 | + <strong>Essential cookies</strong> |
| 19 | + |
| 20 | + <p> |
| 21 | + Identity Lab uses essential cookies to keep authentication sessions secure and the application working correctly. |
| 22 | + These cookies are necessary for the service to function. |
| 23 | + </p> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + |
| 27 | + <MudButton Color="Color.Primary" Variant="Variant.Filled" Disabled="@_saving" OnClick="AcknowledgeAsync" Class="lab-cookie-button"> |
| 28 | + Got it |
| 29 | + </MudButton> |
| 30 | + |
| 31 | + </div> |
| 32 | + </ChildContent> |
| 33 | + |
| 34 | +</MudPopup> |
| 35 | + |
| 36 | + |
| 37 | +@code { |
| 38 | + private const string StorageKey = "identitylab.cookies.notice.v1"; |
| 39 | + |
| 40 | + private bool _open; |
| 41 | + private bool _saving; |
| 42 | + private bool _initialized; |
| 43 | + private bool _disposed; |
| 44 | + |
| 45 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 46 | + { |
| 47 | + if (!firstRender || _initialized) |
| 48 | + return; |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + var acknowledged = await JS.InvokeAsync<string?>("localStorage.getItem", StorageKey); |
| 53 | + |
| 54 | + if (_disposed) |
| 55 | + return; |
| 56 | + |
| 57 | + _open = acknowledged != "acknowledged"; |
| 58 | + _initialized = true; |
| 59 | + |
| 60 | + await InvokeAsync(StateHasChanged); |
| 61 | + } |
| 62 | + catch (JSException) |
| 63 | + { |
| 64 | + // Storage may be unavailable in restricted browsers. |
| 65 | + // Show the notice without persistence. |
| 66 | + _open = true; |
| 67 | + _initialized = true; |
| 68 | + await InvokeAsync(StateHasChanged); |
| 69 | + } |
| 70 | + catch (JSDisconnectedException) |
| 71 | + { |
| 72 | + // The circuit has disconnected. |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private async Task AcknowledgeAsync() |
| 77 | + { |
| 78 | + if (_saving) |
| 79 | + return; |
| 80 | + |
| 81 | + _saving = true; |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + await JS.InvokeVoidAsync("localStorage.setItem", StorageKey, "acknowledged"); |
| 86 | + } |
| 87 | + catch (JSException) |
| 88 | + { |
| 89 | + // Dismiss for this circuit even if storage is unavailable. |
| 90 | + } |
| 91 | + catch (JSDisconnectedException) |
| 92 | + { |
| 93 | + // The browser connection was lost. |
| 94 | + } |
| 95 | + finally |
| 96 | + { |
| 97 | + _open = false; |
| 98 | + _saving = false; |
| 99 | + |
| 100 | + if (!_disposed) |
| 101 | + await InvokeAsync(StateHasChanged); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public ValueTask DisposeAsync() |
| 106 | + { |
| 107 | + _disposed = true; |
| 108 | + return ValueTask.CompletedTask; |
| 109 | + } |
| 110 | +} |
0 commit comments