AI-Studio/app/MindWork AI Studio/Dialogs/PandocDialog.razor.cs

109 lines
3.1 KiB
C#
Raw Normal View History

using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Dialogs;
public partial class PandocDialog : ComponentBase
{
[Inject]
private HttpClient HttpClient { get; set; } = null!;
[Inject]
private RustService RustService { get; init; } = null!;
[Inject]
protected IJSRuntime JsRuntime { get; init; } = null!;
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = null!;
private static readonly string LICENCE_URI = "https://raw.githubusercontent.com/jgm/pandoc/master/COPYRIGHT";
private static string PANDOC_VERSION = "1.0.0";
private bool isPandocAvailable;
private bool showSkeleton;
private bool showInstallPage;
private string? licenseText;
private bool isLoading;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
this.showSkeleton = true;
await this.CheckPandocAvailabilityAsync();
PANDOC_VERSION = await Pandoc.FetchLatestVersionAsync();
}
#endregion
private void Cancel() => this.MudDialog.Cancel();
private async Task CheckPandocAvailabilityAsync()
{
this.isPandocAvailable = await Pandoc.CheckAvailabilityAsync(false);
this.showSkeleton = false;
await this.InvokeAsync(this.StateHasChanged);
}
private async Task InstallPandocAsync() => await Pandoc.InstallAsync(this.RustService);
private void ProceedToInstallation() => this.showInstallPage = true;
private async Task GetInstaller()
{
var uri = await Pandoc.GenerateInstallerUriAsync();
var filename = this.FilenameFromUri(uri);
await this.JsRuntime.InvokeVoidAsync("triggerDownload", uri, filename);
}
private async Task GetArchive()
{
var uri = await Pandoc.GenerateUriAsync();
var filename = this.FilenameFromUri(uri);
await this.JsRuntime.InvokeVoidAsync("triggerDownload", uri, filename);
}
private string FilenameFromUri(string uri)
{
var index = uri.LastIndexOf('/');
return uri[(index + 1)..];
}
private async Task OnExpandedChanged(bool newVal)
{
if (newVal)
{
this.isLoading = true;
try
{
await Task.Delay(600);
this.licenseText = await this.LoadLicenseTextAsync();
}
catch (Exception ex)
{
this.licenseText = "Error loading license text, please consider following the links to the GPL.";
}
finally
{
this.isLoading = false;
}
}
else
{
await Task.Delay(350);
this.licenseText = string.Empty;
}
}
private async Task<string> LoadLicenseTextAsync()
{
var response = await this.HttpClient.GetAsync(LICENCE_URI);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return content;
}
}