[frontend] Refactor Note and NoteComponent

This commit is contained in:
Lilian 2024-07-02 02:47:04 +02:00
parent 84e2b2f13a
commit 78d3036644
No known key found for this signature in database
4 changed files with 117 additions and 101 deletions

View file

@ -1,4 +1,10 @@
@using Iceshrimp.Frontend.Core.Services
@using Iceshrimp.Shared.Schemas @using Iceshrimp.Shared.Schemas
@inject ApiService ApiService
@inject NavigationManager NavigationManager
@inject ComposeService ComposeService
<CascadingValue Value="this">
@if (NoteResponse.Renote != null) @if (NoteResponse.Renote != null)
{ {
<div class="renote"> <div class="renote">
@ -25,9 +31,103 @@ else
{ {
<NoteComponent Note="NoteResponse" Quote="NoteResponse.Quote" Indented="Indented"/> <NoteComponent Note="NoteResponse" Quote="NoteResponse.Quote" Indented="Indented"/>
} }
</CascadingValue>
@code { @code {
[Parameter] [EditorRequired] public required NoteResponse NoteResponse { get; set; } [Parameter] [EditorRequired] public required NoteResponse NoteResponse { get; set; }
[Parameter] public bool Indented { get; set; } [Parameter] public bool Indented { get; set; }
//FIXME: This needs to be rewritten.
public void React(string name, bool target, string? url = null)
{
var index = NoteResponse.Reactions.FindIndex(x => x.Name == name);
switch (target)
{
case true:
ApiService.Notes.ReactToNote(NoteResponse.Id, name);
if (index == -1 && url != null)
{
NoteResponse.Reactions.Add(new NoteReactionSchema
{
NoteId = NoteResponse.Id,
Name = name,
Count = 1,
Reacted = true,
Url = url
});
}
else
{
if (NoteResponse.Reactions[index].Reacted != true)
{
NoteResponse.Reactions[index].Count++;
}
break;
}
StateHasChanged();
break;
case false:
ApiService.Notes.RemoveReactionFromNote(NoteResponse.Id, name);
if (NoteResponse.Reactions[index].Count <= 1)
{
NoteResponse.Reactions.RemoveAt(index);
}
else
{
NoteResponse.Reactions[index].Count--;
}
StateHasChanged();
break;
}
}
public void Like()
{
if (NoteResponse.Liked)
{
ApiService.Notes.UnlikeNote(NoteResponse.Id);
NoteResponse.Liked = false;
NoteResponse.Likes--;
StateHasChanged();
}
else
{
ApiService.Notes.LikeNote(NoteResponse.Id);
NoteResponse.Liked = true;
NoteResponse.Likes++;
StateHasChanged();
}
}
public void Reply()
{
ComposeService.ComposeDialog?.OpenDialog(NoteResponse);
}
public void Renote()
{
ApiService.Notes.RenoteNote(NoteResponse.Id);
NoteResponse.Renotes++;
StateHasChanged();
}
public void DoQuote()
{
ComposeService.ComposeDialog?.OpenDialog(null, NoteResponse);
}
public async Task Redraft()
{
await ApiService.Notes.DeleteNote(NoteResponse.Id);
ComposeService.ComposeDialog?.OpenDialogRedraft(NoteResponse);
}
public void Delete()
{
ApiService.Notes.DeleteNote(NoteResponse.Id);
}
} }

View file

@ -51,91 +51,4 @@
{ {
NavigationManager.NavigateTo($"/notes/{Quote!.Id}"); NavigationManager.NavigateTo($"/notes/{Quote!.Id}");
} }
}
//FIXME: This needs to be rewritten.
public void React(string name, bool target, string? url = null)
{
var index = Note.Reactions.FindIndex(x => x.Name == name);
switch (target)
{
case true:
ApiService.Notes.ReactToNote(Note.Id, name);
if (index == -1 && url != null)
{
Note.Reactions.Add(new NoteReactionSchema
{
NoteId = Note.Id,
Name = name,
Count = 1,
Reacted = true,
Url = url
});
}
else
{
if (Note.Reactions[index].Reacted != true)
{
Note.Reactions[index].Count++;
}
break;
}
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()
{
ApiService.Notes.RenoteNote(Note.Id);
Note.Renotes++;
StateHasChanged();
}
public void DoQuote()
{
ComposeService.ComposeDialog?.OpenDialog(null, Note);
}
public void Delete()
{
ApiService.Notes.DeleteNote(Note.Id);
}
}

View file

@ -58,11 +58,14 @@
<button class="btn positioned" @onclick="ToggleMenu" @onclick:stopPropagation="true"> <button class="btn positioned" @onclick="ToggleMenu" @onclick:stopPropagation="true">
<Icon Name="Icons.DotsThreeOutline" Size="1.3em"/> <Icon Name="Icons.DotsThreeOutline" Size="1.3em"/>
<Menu @ref="Menu"> <Menu @ref="Menu">
@if (NoteComponent.Note.User.Id == Session.Current?.Id) @if (Note.NoteResponse.User.Id == Session.Current?.Id)
{ {
<MenuElement Icon="Icons.Trash" OnSelect="NoteComponent.Delete"> <MenuElement Icon="Icons.Trash" OnSelect="Note.Delete">
<Text>@Loc["Delete"]</Text> <Text>@Loc["Delete"]</Text>
</MenuElement> </MenuElement>
<MenuElement Icon="Icons.Eraser" OnSelect="Note.Redraft">
<Text>@Loc["Redraft"]</Text>
</MenuElement>
} }
<MenuElement Icon="Icons.ArrowSquareOut" OnSelect="OpenOriginal"> <MenuElement Icon="Icons.ArrowSquareOut" OnSelect="OpenOriginal">
<Text>@Loc["Open original page"]</Text> <Text>@Loc["Open original page"]</Text>
@ -80,32 +83,32 @@
private EmojiPicker EmojiPicker { get; set; } = null!; private EmojiPicker EmojiPicker { get; set; } = null!;
private Menu Menu { get; set; } = null!; private Menu Menu { get; set; } = null!;
[CascadingParameter] NoteComponent NoteComponent { get; set; } = null!; [CascadingParameter] Note Note { get; set; } = null!;
private void ToggleMenu() => Menu.Toggle(); private void ToggleMenu() => Menu.Toggle();
private void Delete() => NoteComponent.Delete(); private void Delete() => Note.Delete();
private void OpenOriginal() => Js.InvokeVoidAsync("open", NoteComponent.Note.Url, "_blank"); private void OpenOriginal() => Js.InvokeVoidAsync("open", Note.NoteResponse.Url, "_blank");
private void Like() private void Like()
{ {
NoteComponent.Like(); Note.Like();
} }
private void Reply() private void Reply()
{ {
NoteComponent.Reply(); Note.Reply();
} }
private void Renote() private void Renote()
{ {
if (RenotePossible) NoteComponent.Renote(); if (RenotePossible) Note.Renote();
} }
private void Quote() private void Quote()
{ {
NoteComponent.DoQuote(); Note.DoQuote();
} }
private async Task ToggleEmojiPicker() private async Task ToggleEmojiPicker()
@ -115,6 +118,6 @@
private void React(EmojiResponse emoji) private void React(EmojiResponse emoji)
{ {
NoteComponent.React(emoji.Name, true, emoji.PublicUrl); Note.React(emoji.Name, true, emoji.PublicUrl);
} }
} }

View file

@ -17,10 +17,10 @@
@code { @code {
[Parameter] [EditorRequired] public required NoteReactionSchema Reaction { get; set; } [Parameter] [EditorRequired] public required NoteReactionSchema Reaction { get; set; }
[CascadingParameter] NoteComponent NoteComponent { get; set; } = null!; [CascadingParameter] Note Note { get; set; } = null!;
private void React() private void React()
{ {
NoteComponent.React(Reaction.Name, !Reaction.Reacted); Note.React(Reaction.Name, !Reaction.Reacted);
} }
} }