File size: 10,772 Bytes
47e77e5
64a3c1d
47e77e5
 
64a3c1d
47e77e5
 
 
 
 
f6d34ff
47e77e5
 
 
f6d34ff
47e77e5
 
 
 
 
 
 
f6d34ff
 
47e77e5
8c88a89
 
47e77e5
 
1201c4d
 
 
8c88a89
1201c4d
 
47e77e5
8c88a89
47e77e5
 
8c88a89
47e77e5
 
 
 
 
 
 
8c88a89
47e77e5
 
 
 
 
 
 
8c88a89
 
47e77e5
 
 
 
 
 
 
f6d34ff
64a3c1d
47e77e5
f6d34ff
47e77e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1201c4d
8c88a89
 
 
1201c4d
 
 
 
47e77e5
 
 
1201c4d
47e77e5
 
 
 
 
 
 
 
 
 
 
 
 
64a3c1d
 
 
 
47e77e5
64a3c1d
47e77e5
 
 
64a3c1d
 
 
 
 
 
47e77e5
64a3c1d
47e77e5
 
 
 
 
64a3c1d
 
47e77e5
 
 
 
64a3c1d
 
47e77e5
64a3c1d
 
 
 
 
 
 
 
 
47e77e5
 
 
64a3c1d
47e77e5
 
 
64a3c1d
47e77e5
 
 
 
1201c4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47e77e5
 
 
 
64a3c1d
47e77e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64a3c1d
47e77e5
 
 
 
 
 
 
 
64a3c1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47e77e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64a3c1d
8c88a89
47e77e5
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@rendermode @(new InteractiveServerRenderMode(prerender: false))
@implements IAsyncDisposable
@inject ApiClientService ApiClient
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager Navigation
@inject IJSRuntime JS

<div class="relative">
    <button type="button"
            @onclick="ToggleOpenAsync"
            class="control-button relative">
        <i data-lucide="bell" class="h-5 w-5"></i>
        @if (UnreadCount > 0)
        {
            <span class="absolute -right-1 -top-1 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-600 px-1 text-[10px] font-bold text-white">
                @(UnreadCount > 99 ? "99+" : UnreadCount.ToString())
            </span>
        }
    </button>

    @if (IsOpen)
    {
        <div class="absolute right-0 z-50 mt-3 w-[380px] max-w-[calc(100vw-2rem)] overflow-hidden rounded-lg border border-slate-200 bg-white shadow-lg">
            <div class="flex items-center justify-between px-4 py-3 border-b border-slate-100 bg-slate-50/80">
                <div>
                    <p class="text-sm font-semibold text-slate-900">@AppLocalization.Text("notification.notifications", "Notifications")</p>
                    <p class="text-xs text-slate-500">@UnreadCount @AppLocalization.Text("notification.unread", "unread")</p>
                </div>
                <div class="flex items-center gap-2">
                    @if (Notifications.Any(item => item.IsRead))
                    {
                        <button type="button" class="text-xs font-semibold text-rose-600 hover:text-rose-700" @onclick="RequestCleanupConfirmation">
                            @AppLocalization.Text("notification.clearRead", "Clear read")
                        </button>
                    }
                    <button type="button" class="text-xs font-semibold text-violet-600 hover:text-violet-700" @onclick="RefreshAsync">
                        @AppLocalization.Text("common.refresh", "Refresh")
                    </button>
                    <button type="button" class="text-xs font-semibold text-slate-500 hover:text-slate-700" @onclick="Close">
                        @AppLocalization.Text("common.close", "Close")
                    </button>
                </div>
            </div>

            <div class="max-h-[26rem] overflow-y-auto">
                @if (IsLoading)
                {
                    <div class="p-4 text-sm text-slate-500">@AppLocalization.Text("notification.loading", "Loading notifications...")</div>
                }
                else if (Notifications.Count == 0)
                {
                    <div class="p-6 text-center">
                        <div class="mx-auto mb-3 flex h-10 w-10 items-center justify-center rounded-full bg-slate-100 text-slate-400">
                            <i data-lucide="inbox" class="h-5 w-5"></i>
                        </div>
                        <p class="text-sm font-medium text-slate-700">@AppLocalization.Text("notification.noneYet", "No notifications yet")</p>
                        <p class="text-xs text-slate-500 mt-1">@AppLocalization.Text("notification.emptyState", "Your task alerts will show up here.")</p>
                    </div>
                }
                else
                {
                    @foreach (var item in Notifications)
                    {
                        <button type="button"
                                class="w-full border-b border-slate-100 px-4 py-3 text-left transition-colors hover:bg-slate-50 @(item.IsRead ? "bg-white" : "bg-violet-50/70")"
                                @onclick="() => OpenNotificationAsync(item)">
                            <div class="flex items-start gap-3">
                                <div class="mt-0.5 flex h-9 w-9 items-center justify-center rounded-full @(item.IsRead ? "bg-slate-100 text-slate-500" : "bg-violet-100 text-violet-700")">
                                    <i data-lucide="bell-ring" class="h-4 w-4"></i>
                                </div>
                                <div class="min-w-0 flex-1">
                                    <div class="flex items-start justify-between gap-3">
                                        <p class="text-sm font-semibold text-slate-900 truncate">@item.Title</p>
                                        @if (!item.IsRead)
                                        {
                                            <span class="mt-1 h-2.5 w-2.5 rounded-full bg-violet-600 shrink-0"></span>
                                        }
                                    </div>
                                    <p class="mt-1 text-xs text-slate-600 leading-5">@item.Body</p>
                                    <div class="mt-2 flex items-center justify-between text-[11px] text-slate-400">
                                        <span>@FormatSender(item.SenderName)</span>
                                        <span>@FormatTime(item.CreatedAt)</span>
                                    </div>
                                </div>
                            </div>
                        </button>
                    }
                }
            </div>
        </div>
    }
</div>

<ConfirmDialog IsVisible="showCleanupConfirmDialog"
               Title='@AppLocalization.Text("notification.clearReadTitle", "Clear read notifications?")'
               Message='@AppLocalization.Text("notification.clearReadMessage", "This will permanently remove all read notifications from your list.")'
               ConfirmText='@AppLocalization.Text("common.clear", "Clear")'
               Icon="trash-2"
               OnConfirm="ConfirmCleanupAsync"
               OnCancel="CloseCleanupConfirmDialog" />

@code {
    private bool IsOpen;
    private bool IsLoading;
    private bool showCleanupConfirmDialog;
    private int UnreadCount;
    private List<NotificationDto> Notifications = new();

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

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await JS.InvokeVoidAsync("initIcons");
    }

    private async Task ToggleOpenAsync()
    {
        IsOpen = !IsOpen;
        if (IsOpen)
        {
            await RefreshAsync();
        }
    }

    private void Close()
    {
        IsOpen = false;
    }

    private async Task RefreshAsync()
    {
        IsLoading = true;
        try
        {
            var authState = await AuthStateProvider.GetAuthenticationStateAsync();
            if (authState.User.Identity?.IsAuthenticated != true)
            {
                Notifications.Clear();
                UnreadCount = 0;
                return;
            }

            var client = ApiClient.CreateClient(authState.User);
            var unreadTask = client.GetFromJsonAsync<int>("Notification/unread-count");
            var itemsTask = client.GetFromJsonAsync<List<NotificationDto>>("Notification/mine?take=10");

            await Task.WhenAll(unreadTask, itemsTask);
            UnreadCount = unreadTask.Result;
            Notifications = (itemsTask.Result ?? new List<NotificationDto>())
                .Select(item =>
                {
                    item.TargetUrl = BuildTargetUrl(item);
                    return item;
                })
                .ToList();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[DEBUG NotificationBell] Failed to refresh: {ex.Message}");
        }
        finally
        {
            IsLoading = false;
            await InvokeAsync(StateHasChanged);
        }
    }

    private void RequestCleanupConfirmation()
    {
        showCleanupConfirmDialog = true;
    }

    private void CloseCleanupConfirmDialog()
    {
        showCleanupConfirmDialog = false;
    }

    private async Task ConfirmCleanupAsync()
    {
        showCleanupConfirmDialog = false;
        await ClearReadNotificationsAsync();
    }

    private async Task ClearReadNotificationsAsync()
    {
        try
        {
            var authState = await AuthStateProvider.GetAuthenticationStateAsync();
            if (authState.User.Identity?.IsAuthenticated != true)
            {
                return;
            }

            var client = ApiClient.CreateClient(authState.User);
            var response = await client.DeleteAsync("Notification/read");
            if (response.IsSuccessStatusCode)
            {
                await RefreshAsync();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[DEBUG NotificationBell] Clear read notifications failed: {ex.Message}");
        }
    }

    private async Task MarkReadAsync(NotificationDto item)
    {
        if (item.IsRead)
        {
            Navigation.NavigateTo(GetTargetUrl(item));
            return;
        }

        try
        {
            var authState = await AuthStateProvider.GetAuthenticationStateAsync();
            if (authState.User.Identity?.IsAuthenticated != true)
            {
                return;
            }

            var client = ApiClient.CreateClient(authState.User);
            var response = await client.PostAsync($"Notification/{item.Id}/read", null);
            if (response.IsSuccessStatusCode)
            {
                item.IsRead = true;
                if (UnreadCount > 0)
                {
                    UnreadCount--;
                }
                await InvokeAsync(StateHasChanged);
                Navigation.NavigateTo(GetTargetUrl(item));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"[DEBUG NotificationBell] MarkRead failed: {ex.Message}");
        }
    }

    private async Task OpenNotificationAsync(NotificationDto item)
    {
        await MarkReadAsync(item);
        IsOpen = false;
    }

    private static string GetTargetUrl(NotificationDto item)
    {
        if (!string.IsNullOrWhiteSpace(item.TargetUrl))
        {
            return item.TargetUrl;
        }

        return BuildTargetUrl(item);
    }

    private static string BuildTargetUrl(NotificationDto item)
    {
        return NotificationNavigation.BuildTargetUrl(item.SourceType, item.SourceId, item.NotificationType);
    }

    private static string FormatSender(string? senderName)
    {
        return string.IsNullOrWhiteSpace(senderName) ? "System" : senderName;
    }

    private static string FormatTime(DateTime? createdAt)
    {
        if (!createdAt.HasValue)
        {
            return string.Empty;
        }

        var value = createdAt.Value.ToLocalTime();
        return value.ToString("dd MMM, HH:mm");
    }

    public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}