2026-07-06 18:27:25 +00:00
|
|
|
using AIStudio.Tools.Rust;
|
2025-06-30 16:56:48 +00:00
|
|
|
using AIStudio.Tools.Services;
|
2025-12-15 12:26:55 +00:00
|
|
|
using AIStudio.Tools.Validation;
|
2025-06-30 16:56:48 +00:00
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
|
|
|
|
|
|
public partial class ReadFileContent : MSGComponentBase
|
|
|
|
|
{
|
2025-11-24 11:37:18 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public string Text { get; set; } = string.Empty;
|
|
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public string FileContent { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<string> FileContentChanged { get; set; }
|
2026-02-01 13:50:19 +00:00
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
2026-07-06 18:27:25 +00:00
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool EnableDragDrop { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// On which layer to register the drop area. Higher layers have priority over lower layers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int Layer { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Catch all documents that are hovered over the AI Studio window and not only over the drop zone.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool CatchAllDocuments { get; set; }
|
2025-06-30 16:56:48 +00:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private RustService RustService { get; init; } = null!;
|
|
|
|
|
|
2025-11-14 12:34:30 +00:00
|
|
|
[Inject]
|
|
|
|
|
private IDialogService DialogService { get; init; } = null!;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private ILogger<ReadFileContent> Logger { get; init; } = null!;
|
2025-12-10 12:48:13 +00:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private PandocAvailabilityService PandocAvailabilityService { get; init; } = null!;
|
2026-07-06 18:27:25 +00:00
|
|
|
|
|
|
|
|
private const string DEFAULT_DRAG_CLASS = "relative rounded-lg border-2 border-dashed pa-3 mb-3 mud-width-full";
|
|
|
|
|
|
|
|
|
|
private string ButtonText => string.IsNullOrWhiteSpace(this.Text) ? T("Use file content as input") : this.Text;
|
|
|
|
|
private string dragClass = DEFAULT_DRAG_CLASS;
|
|
|
|
|
private uint numDropAreasAboveThis;
|
|
|
|
|
private bool isComponentHovered;
|
|
|
|
|
|
|
|
|
|
#region Overrides of MSGComponentBase
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
if (this.EnableDragDrop)
|
|
|
|
|
{
|
|
|
|
|
this.ApplyFilters([], [ Event.TAURI_EVENT_RECEIVED, Event.REGISTER_FILE_DROP_AREA, Event.UNREGISTER_FILE_DROP_AREA ]);
|
|
|
|
|
await this.MessageBus.SendMessage(this, Event.REGISTER_FILE_DROP_AREA, this.Layer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
|
|
|
|
|
{
|
|
|
|
|
if (!this.EnableDragDrop)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (this.Disabled && triggeredEvent == Event.TAURI_EVENT_RECEIVED)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (triggeredEvent)
|
|
|
|
|
{
|
|
|
|
|
case Event.REGISTER_FILE_DROP_AREA when sendingComponent != this:
|
|
|
|
|
{
|
|
|
|
|
if(data is int layer && layer > this.Layer)
|
|
|
|
|
{
|
|
|
|
|
this.numDropAreasAboveThis++;
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Event.UNREGISTER_FILE_DROP_AREA when sendingComponent != this:
|
|
|
|
|
{
|
|
|
|
|
if(data is int layer && layer > this.Layer && this.numDropAreasAboveThis > 0)
|
|
|
|
|
this.numDropAreasAboveThis--;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_HOVERED }:
|
|
|
|
|
if(!this.CanCatchDroppedFile())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.SetDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_CANCELED }:
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.WINDOW_NOT_FOCUSED }:
|
|
|
|
|
this.isComponentHovered = false;
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_DROPPED, Payload: var paths }:
|
|
|
|
|
if(!this.CanCatchDroppedFile())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await this.LoadFirstValidFile(paths);
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-11-14 12:34:30 +00:00
|
|
|
|
2025-06-30 16:56:48 +00:00
|
|
|
private async Task SelectFile()
|
|
|
|
|
{
|
2026-02-01 13:50:19 +00:00
|
|
|
if (this.Disabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2026-07-06 18:27:25 +00:00
|
|
|
if (!await this.EnsurePandocAvailability())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var selectedFile = await this.RustService.SelectFile(T("Select file to read its content"));
|
|
|
|
|
if (selectedFile.UserCancelled)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogInformation("User cancelled the file selection");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.LoadFileIfValid(selectedFile.SelectedFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> EnsurePandocAvailability()
|
|
|
|
|
{
|
2025-12-15 11:45:54 +00:00
|
|
|
// Ensure that Pandoc is installed and ready:
|
|
|
|
|
var pandocState = await this.PandocAvailabilityService.EnsureAvailabilityAsync(
|
|
|
|
|
showSuccessMessage: false,
|
|
|
|
|
showDialog: true);
|
|
|
|
|
|
|
|
|
|
// Check if Pandoc is available after the check / installation:
|
|
|
|
|
if (!pandocState.IsAvailable)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogWarning("The user cancelled the Pandoc installation or Pandoc is not available. Aborting file selection.");
|
2026-07-06 18:27:25 +00:00
|
|
|
return false;
|
2025-12-15 11:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:27:25 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LoadFirstValidFile(List<string> paths)
|
|
|
|
|
{
|
|
|
|
|
if (!await this.EnsurePandocAvailability())
|
2025-06-30 16:56:48 +00:00
|
|
|
return;
|
2026-07-06 18:27:25 +00:00
|
|
|
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
|
|
|
|
if (await this.LoadFileIfValid(path))
|
|
|
|
|
return;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
2026-07-06 18:27:25 +00:00
|
|
|
}
|
2025-12-08 20:15:45 +00:00
|
|
|
|
2026-07-06 18:27:25 +00:00
|
|
|
private async Task<bool> LoadFileIfValid(string filePath)
|
|
|
|
|
{
|
|
|
|
|
if(!File.Exists(filePath))
|
2025-12-08 20:15:45 +00:00
|
|
|
{
|
2026-07-06 18:27:25 +00:00
|
|
|
this.Logger.LogWarning("Selected file does not exist: '{FilePath}'", filePath);
|
|
|
|
|
return false;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:27:25 +00:00
|
|
|
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(FileExtensionValidation.UseCase.DIRECTLY_LOADING_CONTENT, filePath))
|
2025-06-30 16:56:48 +00:00
|
|
|
{
|
2026-07-06 18:27:25 +00:00
|
|
|
this.Logger.LogWarning("User attempted to load unsupported file: {FilePath}", filePath);
|
|
|
|
|
return false;
|
2025-06-30 16:56:48 +00:00
|
|
|
}
|
2025-12-08 20:15:45 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-07-06 18:27:25 +00:00
|
|
|
var fileContent = await UserFile.LoadFileData(filePath, this.RustService, this.DialogService);
|
2025-12-08 20:15:45 +00:00
|
|
|
await this.FileContentChanged.InvokeAsync(fileContent);
|
2026-07-06 18:27:25 +00:00
|
|
|
this.Logger.LogInformation("Successfully loaded file content: {FilePath}", filePath);
|
|
|
|
|
return true;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-07-06 18:27:25 +00:00
|
|
|
this.Logger.LogError(ex, "Failed to load file content: {FilePath}", filePath);
|
2025-12-08 20:15:45 +00:00
|
|
|
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Error, T("Failed to load file content")));
|
2026-07-06 18:27:25 +00:00
|
|
|
return false;
|
2025-12-08 20:15:45 +00:00
|
|
|
}
|
2025-06-30 16:56:48 +00:00
|
|
|
}
|
2026-07-06 18:27:25 +00:00
|
|
|
|
|
|
|
|
private bool CanCatchDroppedFile() => this.numDropAreasAboveThis is 0 && (this.isComponentHovered || this.CatchAllDocuments);
|
|
|
|
|
|
|
|
|
|
private void SetDragClass() => this.dragClass = $"{DEFAULT_DRAG_CLASS} mud-border-primary border-2";
|
|
|
|
|
|
|
|
|
|
private void ClearDragClass() => this.dragClass = DEFAULT_DRAG_CLASS;
|
|
|
|
|
|
|
|
|
|
private void OnMouseEnter(EventArgs _)
|
|
|
|
|
{
|
|
|
|
|
if(this.Disabled || this.numDropAreasAboveThis > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.Logger.LogDebug("Read file content component is hovered.");
|
|
|
|
|
this.isComponentHovered = true;
|
|
|
|
|
this.SetDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMouseLeave(EventArgs _)
|
|
|
|
|
{
|
|
|
|
|
if(this.Disabled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.Logger.LogDebug("Read file content component is no longer hovered.");
|
|
|
|
|
this.isComponentHovered = false;
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|