Iceshrimp.NET/Iceshrimp.Frontend/Components/Note/NoteComponent.razor

114 lines
No EOL
3.3 KiB
Text

@using AngleSharp.Dom
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Shared.Schemas
@inject ApiService ApiService;
@inject NavigationManager NavigationManager
@inject ComposeService ComposeService
<CascadingValue Value="this">
<div class="note-header">
<NoteUserInfo
AvatarUrl="@Note.User.AvatarUrl"
DisplayName="@Note.User.DisplayName"
Username="@Note.User.Username"
Host="@Note.User.Host"
Indented="Indented"
/>
<NoteMetadata
Visibility="@Note.Visibility"
InstanceName="@Note.User.InstanceName"
CreatedAt="DateTime.Parse(Note.CreatedAt)"></NoteMetadata>
</div>
<NoteBody NoteBase="Note" OverLength="@CheckLen()" Indented="Indented"/>
@if (Quote != null)
{
<div @onclick="OpenQuote" @onclick:stopPropagation="true" class="quote">
<NoteComponent Note="Quote" AsQuote="true"></NoteComponent>
</div>
}
@if (!AsQuote)
{
@* FIXME: Allow renotes when the post is from the current user *@
<NoteFooter
Reactions="Note.Reactions"
Likes="Note.Likes"
IsLiked="Note.Liked"
Renotes="Note.Renotes"
RenotePossible="@(Note.Visibility == NoteVisibility.Public || Note.Visibility == NoteVisibility.Home)"
/>
}
</CascadingValue>
@code {
[Parameter][EditorRequired] public required NoteBase Note { get; set; }
[Parameter] public bool Indented { get; set; }
[Parameter] public NoteBase? Quote { get; set; }
[Parameter] public bool AsQuote { get; set; }
private bool CheckLen()
{
return Note.Text?.Length > 500;
}
private void OpenQuote()
{
NavigationManager.NavigateTo($"/notes/{Quote!.Id}");
}
public void React(string name, bool target)
{
var index = Note.Reactions.FindIndex(x => x.Name == name);
switch (target)
{
case true:
ApiService.Notes.ReactToNote(Note.Id, name);
Note.Reactions[index].Count++;
StateHasChanged();
break;
case false:
ApiService.Notes.RemoveReactionFromNote(Note.Id, name);
if (Note.Reactions[index].Count <= 1)
{
Note.Reactions.RemoveAt(index);
}
else
{
Note.Reactions[index].Count--;
}
StateHasChanged();
break;
}
}
public void Like()
{
if (Note.Liked)
{
ApiService.Notes.UnlikeNote(Note.Id);
Note.Liked = false;
Note.Likes--;
StateHasChanged();
}
else
{
ApiService.Notes.LikeNote(Note.Id);
Note.Liked = true;
Note.Likes++;
StateHasChanged();
}
}
public void Reply()
{
ComposeService.ComposeDialog?.OpenDialog(Note, null);
}
public void Renote()
{
ApiService.Notes.RenoteNote(Note.Id);
Note.Renotes++;
StateHasChanged();
}
public void DoQuote()
{
ComposeService.ComposeDialog?.OpenDialog(null, Note);
}
}