Fixed the file preview showing an empty document while the file was still being read (#907)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-08-10 17:10:55 +02:00
1 parent 0eebb164c2
commit ed52abfd37
6 files changed
+60 -15

No files matched your search

@@ -33,6 +33,16 @@
@T("The specified file could not be found. The file have been moved, deleted, renamed, or is otherwise inaccessible.")
</MudAlert>
}
else if (this.isLoadingContent)
{
<MudJustifiedText Typo="Typo.body1" Class="my-3">
@T("Please wait while we load the content of your file. Depending on the file type and size, this may take a moment.")
</MudJustifiedText>
<MudSkeleton Width="30%" Height="42px"/>
<MudSkeleton Width="80%"/>
<MudSkeleton Width="100%"/>
<MudSkeleton Width="90%"/>
}
else if (this.loadFailureMessage is not null)
{
<MudAlert Severity="Severity.Error" Variant="Variant.Filled" Class="my-2">
@@ -25,7 +25,13 @@ public partial class DocumentCheckDialog : MSGComponentBase
/// Set when reading the file failed, so the dialog shows the reason instead of empty content.
/// </summary>
private string? loadFailureMessage;
/// <summary>
/// True while we extract the file content. Reading happens after the first render, so the
/// dialog can tell the user that it is working instead of showing an empty document.
/// </summary>
private bool isLoadingContent;
[Inject]
private RustService RustService { get; init; } = null!;
@@ -35,24 +41,40 @@ public partial class DocumentCheckDialog : MSGComponentBase
[Inject]
private ILogger<DocumentCheckDialog> Logger { get; init; } = null!;
protected override async Task OnInitializedAsync()
{
//
// Decide before the first render whether we have to read the file at all. Images are shown
// as they are, a missing file shows its own message, and content a caller already handed
// us is reused instead of being extracted a second time:
//
this.isLoadingContent =
this.Document is not null &&
!this.Document.IsImage &&
this.Document.Exists &&
string.IsNullOrWhiteSpace(this.FileContent);
await base.OnInitializedAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && this.Document is not null)
{
if (!this.isLoadingContent)
return;
try
{
if (!this.Document.IsImage)
{
var extraction = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
this.FileContent = extraction.Content;
var extraction = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
this.FileContent = extraction.Content;
//
// This dialog exists so the user can check what we hand to the AI. Showing an
// empty document when reading the file failed would answer that question wrong.
//
if (!extraction.HasUsableContent)
this.loadFailureMessage = extraction.ToUserMessage(this.Document.FileName);
}
//
// This dialog exists so the user can check what we hand to the AI. Showing an
// empty document when reading the file failed would answer that question wrong.
//
if (!extraction.HasUsableContent)
this.loadFailureMessage = extraction.ToUserMessage(this.Document.FileName);
}
catch (Exception ex)
{
@@ -60,8 +82,11 @@ public partial class DocumentCheckDialog : MSGComponentBase
this.FileContent = string.Empty;
this.loadFailureMessage = FileExtractionErrorCode.INTERNAL.ToUserMessage(this.Document.FileName);
}
this.StateHasChanged();
finally
{
this.isLoadingContent = false;
this.StateHasChanged();
}
}
else if (firstRender)
this.Logger.LogWarning("Document check dialog opened without a valid file path.");