about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/UpdateAvailableDetector.razor
blob: 5197a6fad68b29fd1c660ee222c023fbb995ea0b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@* Source: https://whuysentruit.medium.com/blazor-wasm-pwa-adding-a-new-update-available-notification-d9f65c4ad13 *@
@inject IJSRuntime _jsRuntime

@if (_newVersionAvailable)
{
    <button type="button" class="btn btn-warning shadow floating-update-button" onclick="window.location.reload()">
        A new version of the application is available. Click here to reload.
    </button>
}

@code {

    private bool _newVersionAvailable = false;

    protected override async Task OnInitializedAsync()
    {
        await RegisterForUpdateAvailableNotification();
    }

    private async Task RegisterForUpdateAvailableNotification()
    {
        await _jsRuntime.InvokeAsync<object>(
            identifier: "registerForUpdateAvailableNotification",
            DotNetObjectReference.Create(this),
            nameof(OnUpdateAvailable));
    }

    [JSInvokable(nameof(OnUpdateAvailable))]
    public Task OnUpdateAvailable()
    {
        _newVersionAvailable = true;

        StateHasChanged();

        return Task.CompletedTask;
    }

}