Iceshrimp.NET/Iceshrimp.Frontend/Components/Note/NoteComponent.razor
2024-06-19 01:56:55 +02:00

109 lines
No EOL
3.2 KiB
Text

@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)
{
<NoteFooter Reactions="Note.Reactions" Likes="Note.Likes" IsLiked="Note.Liked"/>
}
</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);
}
public void Renote()
{
var renote = new NoteCreateRequest
{
Text = "",
Cw = null,
ReplyId = null,
RenoteId = Note.Id,
MediaIds = null,
Visibility = Note.Visibility,
IdempotencyKey = null
};
ApiService.Notes.CreateNote(renote);
}
}