AI-Studio/app/MindWork AI Studio/Components/AttachDocuments.razor.cs

122 lines
4.4 KiB
C#
Raw Normal View History

2025-10-27 14:17:20 +00:00
using AIStudio.Tools.Rust;
using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
public partial class AttachDocuments : MSGComponentBase
{
[Parameter]
public string Name { get; set; } = string.Empty;
2025-10-27 14:17:20 +00:00
[Parameter]
public HashSet<string> DocumentPaths { get; set; } = [];
2025-10-27 14:17:20 +00:00
[Parameter]
public EventCallback<HashSet<string>> DocumentPathsChanged { get; set; }
2025-10-27 14:17:20 +00:00
[Parameter]
public Func<HashSet<string>, Task> OnChange { get; set; } = _ => Task.CompletedTask;
2025-10-27 14:17:20 +00:00
[Inject]
private ILogger<AttachDocuments> Logger { get; set; } = null!;
2025-10-27 14:17:20 +00:00
[Inject]
private RustService RustService { get; init; } = null!;
#region Overrides of MSGComponentBase
protected override async Task OnInitializedAsync()
{
this.ApplyFilters([], [ Event.TAURI_EVENT_RECEIVED ]);
await base.OnInitializedAsync();
}
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
{
switch (triggeredEvent)
{
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_HOVERED }:
if(!this.isComponentHovered)
{
this.Logger.LogDebug("Attach documents component '{Name}' is not hovered, ignoring file drop hovered event.", this.Name);
return;
}
2025-10-27 14:17:20 +00:00
this.SetDragClass();
this.StateHasChanged();
2025-10-27 14:17:20 +00:00
break;
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_DROPPED, Payload: var paths }:
if(!this.isComponentHovered)
{
this.Logger.LogDebug("Attach documents component '{Name}' is not hovered, ignoring file drop dropped event.", this.Name);
return;
}
#warning Filter unsupported files
2025-10-27 14:17:20 +00:00
foreach (var path in paths)
this.DocumentPaths.Add(path);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths);
this.StateHasChanged();
2025-10-27 14:17:20 +00:00
break;
}
}
#endregion
private const string DEFAULT_DRAG_CLASS = "relative rounded-lg border-2 border-dashed pa-4 mt-4 mud-width-full mud-height-full";
private string dragClass = DEFAULT_DRAG_CLASS;
private bool isComponentHovered;
2025-10-27 14:17:20 +00:00
private async Task AddFilesManually()
{
var selectedFile = await this.RustService.SelectFile(T("Select a file to attach"));
if (selectedFile.UserCancelled)
return;
if (!File.Exists(selectedFile.SelectedFilePath))
return;
var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.');
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.AppBlocking, T("Executables are not allowed")));
return;
}
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.ImageNotSupported, T("Images are not supported yet")));
return;
}
this.DocumentPaths.Add(selectedFile.SelectedFilePath);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths);
}
private void SetDragClass() => this.dragClass = $"{DEFAULT_DRAG_CLASS} mud-border-primary border-4";
2025-10-27 14:17:20 +00:00
private void ClearDragClass() => this.dragClass = DEFAULT_DRAG_CLASS;
private void OnMouseEnter(EventArgs _)
{
this.Logger.LogDebug("Attach documents component '{Name}' is hovered.", this.Name);
this.isComponentHovered = true;
this.SetDragClass();
this.StateHasChanged();
}
private void OnMouseLeave(EventArgs _)
{
this.Logger.LogDebug("Attach documents component '{Name}' is no longer hovered.", this.Name);
this.isComponentHovered = false;
this.ClearDragClass();
this.StateHasChanged();
}
2025-10-27 14:17:20 +00:00
}