Iceshrimp.NET/Iceshrimp.Frontend/Components/NotificationComponent.razor
2024-10-23 05:04:55 +02:00

130 lines
No EOL
5.1 KiB
Text

@* ReSharper disable once RedundantUsingDirective *@
@using Iceshrimp.Frontend.Components.Note
@using Iceshrimp.Frontend.Localization
@using Iceshrimp.Shared.Schemas.Web
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Localization> Loc;
@inject NavigationManager NavigationManager
<div class="notification">
<div class="notification-header">
@if (NotificationResponse is { User: not null })
{
<img @onclick="OpenProfile" class="user-avatar" src="@NotificationResponse.User.AvatarUrl"/>
}
<div class="notification-body">
@if (NotificationResponse is { User: not null })
{
<span @onclick="OpenProfile" class="display-name">@(NotificationResponse.User.DisplayName ?? NotificationResponse.User.Username)</span>
}
@if (NotificationResponse is { Note: not null, Type: "like", User: not null })
{
<span class="notification-text">@Loc["liked your post: "]</span>
}
else if (NotificationResponse is { Note: not null, Type: "reaction" })
{
<span class="notification-text">@Loc["reacted to your post:"]</span>
}
else if (NotificationResponse is { Type: "follow", User: not null })
{
<span class="notification-text">@Loc["Followed you."]</span>
}
else if (NotificationResponse is { Type: "followRequestReceived" })
{
<span class="notification-text">@Loc["Requested to follow you."]</span>
}
else if (NotificationResponse is { Type: "followRequestAccepted" })
{
<span class="notification-text">@Loc["Accepted your follow request."]</span>
}
else if (NotificationResponse is { Type: "mention" })
{
<span class="notification-text">@Loc["mentioned you:"]</span>
}
else if (NotificationResponse is { Type: "reply" })
{
<span class="notification-text">@Loc["replied to your post:"]</span>
}
else if (NotificationResponse is { Type: "renote" })
{
<span class="notification-text">@Loc["boosted your post:"]</span>
}
else if (NotificationResponse is { Type: "quote" })
{
<span class="notification-text">@Loc["quoted your post:"]</span>
}
else if (NotificationResponse is { Bite: { BiteBack: true }, Type: "bite" })
{
<span class="notification-text">@Loc["bit you back"]</span>
}
else if (NotificationResponse is { Note: not null, Type: "bite" })
{
<span class="notification-text">@Loc["bit your note:"]</span>
}
else if (NotificationResponse is { Type: "bite" })
{
<span class="notification-text">@Loc["bit you"]</span>
}
else
{
<span class="notification-text">
<i>@Loc["unsupported notification type"]</i>
</span>
<details>
<summary>view details</summary>
<p>ID: @NotificationResponse.Id</p>
<p>Type: @NotificationResponse.Type</p>
</details>
}
</div>
<span>@RenderDate(DateTime.Parse(NotificationResponse.CreatedAt))</span>
</div>
@if (NotificationResponse is { Note: not null })
{
<div @onclick="OpenNote" class="notification-note">
<Note NoteResponse="NotificationResponse.Note"/>
</div>
}
</div>
@code {
[Parameter] public required NotificationResponse NotificationResponse { get; set; }
private void OpenNote()
{
NavigationManager.NavigateTo($"/notes/{NotificationResponse.Note!.Id}");
}
private void OpenFollowRequests()
{
NavigationManager.NavigateTo("/follow-requests");
}
private void OpenProfile()
{
var username = $"@{NotificationResponse.User?.Username}";
if (NotificationResponse.User?.Host != null) username += $"@{NotificationResponse.User.Host}";
NavigationManager.NavigateTo($"/{username}");
}
// TODO: Deduplicate this and NoteMetadata.RenderDate
private string RenderDate(DateTime date)
{
var diff = DateTime.Now - date;
return diff switch
{
{ TotalDays: >= 365 } => Loc["{0}y", Math.Round(diff.TotalDays / 365)],
{ TotalDays: >= 30 } => Loc["{0}mo", Math.Round(diff.TotalDays / 30)],
{ TotalDays: >= 7 } => Loc["{0}d", Math.Round(diff.TotalDays / 7)],
{ TotalDays: >= 1 } => Loc["{0}d", Math.Round(diff.TotalDays)],
{ TotalHours: >= 1 } => Loc["{0}h", Math.Round(diff.TotalHours)],
{ TotalMinutes: >= 1 } => Loc["{0}m", Math.Round(diff.TotalMinutes)],
{ TotalSeconds: >= 1 } => Loc["{0}s", Math.Round(diff.TotalSeconds)],
_ => Loc["Just now"]
};
}
}