@page "/mod/emojis" @using Iceshrimp.Frontend.Core.Services @using Iceshrimp.Frontend.Localization @using Microsoft.AspNetCore.Authorization @using Microsoft.Extensions.Localization @using Microsoft.AspNetCore.Components.Sections @using Iceshrimp.Assets.PhosphorIcons @using Iceshrimp.Frontend.Core.Miscellaneous @using Iceshrimp.Shared.Schemas.Web @using Iceshrimp.Frontend.Components @attribute [Authorize(Roles = "moderator")] @layout ModerationLayout @inject ApiService Api; @inject GlobalComponentSvc Global; @inject IJSRuntime Js; @inject IStringLocalizer Loc; @Loc["Custom Emojis"] @if (State is State.Empty or State.Loaded && Source == "local") { }
@if (State is State.Empty or State.Error or State.Loaded) { } @if (State is State.Loaded) { @foreach (var category in Categories) { @category.Key
@foreach (var emoji in category.Value) { }
} } @if (State is State.Empty) { This instance has no emojis } @if (State is State.Loading) {
}
Upload Import
@code { private List Emojis { get; set; } = []; private Dictionary> Categories { get; set; } = new(); private string EmojiFilter { get; set; } = ""; private string HostFilter { get; set; } = ""; private string Source { get; set; } = "local"; private State State { get; set; } private InputFile UploadInput { get; set; } = null!; private IBrowserFile UploadFile { get; set; } = null!; private string UploadName { get; set; } = ""; private InputFile ImportInput { get; set; } = null!; private IBrowserFile ImportFile { get; set; } = null!; private IJSObjectReference _module = null!; private void FilterEmojis() { Categories = Emojis .Where(p => p.Name.Contains(EmojiFilter.Trim()) || p.Aliases.Count(a => a.Contains(EmojiFilter.Trim())) != 0) .OrderBy(p => p.Name) .ThenBy(p => p.Id) .GroupBy(p => p.Category) .Where(p => Source == "local" || (Source == "remote" && (p.Key?.Contains(HostFilter) ?? false))) .OrderBy(p => string.IsNullOrEmpty(p.Key)) .ThenBy(p => p.Key) .ToDictionary(p => p.Key ?? "Other", p => p.ToList()); } private async Task GetEmojis() { State = State.Loading; if (Source == "remote") { //TODO: impolement proper pagination var res = await Api.Emoji.GetRemoteEmojiAsync(new PaginationQuery()); Emojis = res.Data; } else { Emojis = await Api.Emoji.GetAllEmojiAsync(); } if (Emojis.Count == 0) { State = State.Empty; } else { FilterEmojis(); State = State.Loaded; } } // The Component is hidden, and triggered by a sepperate button. // That way we get it's functionality, without the styling limitations of the InputFile component private async Task OpenUpload() { await _module.InvokeVoidAsync("openUpload", UploadInput.Element); } private async Task OpenImport() { await _module.InvokeVoidAsync("openUpload", ImportInput.Element); } private async Task Upload(InputFileChangeEventArgs e) { if (!e.File.ContentType.StartsWith("image/")) return; UploadFile = e.File; await Global.PromptDialog?.Prompt(new EventCallback(this, UploadCallback), Loc["Set emoji name"], "", e.File.Name.Split(".")[0], buttonText: Loc["Upload"])!; } private async Task UploadCallback(string? name) { if (name == null) return; try { await Api.Emoji.UploadEmojiAsync(UploadFile, name); await Global.NoticeDialog?.Display(Loc["Successfully uploaded {0}", name])!; await GetEmojis(); } catch (ApiException e) { await Global.NoticeDialog?.Display(e.Response.Message ?? Loc["An unknown error occurred"], NoticeDialog.NoticeType.Error)!; } } private async Task Import(InputFileChangeEventArgs e) { if (e.File.ContentType != "application/zip") return; ImportFile = e.File; await Global.ConfirmDialog?.Confirm(new EventCallback(this, ImportCallback), Loc["Import {0}?", e.File.Name], Icons.FileArrowUp, Loc["Import"])!; } private async Task ImportCallback(bool import) { if (!import) return; try { await Api.Emoji.ImportEmojiAsync(ImportFile); await Global.NoticeDialog?.Display(Loc["Successfully imported emoji pack"])!; await GetEmojis(); } catch (ApiException e) { await Global.NoticeDialog?.Display(e.Response.Message ?? Loc["An unknown error occurred"], NoticeDialog.NoticeType.Error)!; } } protected override async Task OnInitializedAsync() { _module = await Js.InvokeAsync("import", "./Pages/Moderation/CustomEmojis.razor.js"); await GetEmojis(); } }