File size: 1,524 Bytes
3418204
 
 
 
 
f6d34ff
 
3418204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@inject IJSRuntime JS
@implements IDisposable

@if (!string.IsNullOrEmpty(displayMessage))
{
    <div class="fixed right-4 top-4 z-[100] pointer-events-none sm:right-6 sm:top-6">
        <div class="flex items-center gap-3 rounded-lg border border-teal-500/20 bg-teal-600 px-4 py-3 text-white shadow-lg animate-pulse-once">
            <i data-lucide="check-circle" class="h-5 w-5 shrink-0"></i>
            <span class="text-sm font-semibold">@displayMessage</span>
        </div>
    </div>
}

@code {
    [Parameter] public string? Message { get; set; }

    private string? displayMessage;
    private CancellationTokenSource? hideCts;

    protected override async Task OnParametersSetAsync()
    {
        if (!string.IsNullOrEmpty(Message) && Message != displayMessage)
        {
            hideCts?.Cancel();
            hideCts?.Dispose();
            hideCts = new CancellationTokenSource();

            displayMessage = Message;
            await InvokeAsync(StateHasChanged);
            await JS.InvokeVoidAsync("initIcons");

            var token = hideCts.Token;
            _ = HideAfterDelay(token);
        }
    }

    private async Task HideAfterDelay(CancellationToken token)
    {
        try
        {
            await Task.Delay(3000, token);
            displayMessage = null;
            await InvokeAsync(StateHasChanged);
        }
        catch (TaskCanceledException)
        {
        }
    }

    public void Dispose()
    {
        hideCts?.Cancel();
        hideCts?.Dispose();
    }
}