feat: attachments panel, file previews, print preview and history (#34)

Documents need to show, upload and generate their files from the interface,
and users need to see who changed a record. This adds the attachment list
(upload, preview, download, lock, trash and restore, older versions, ZIP,
generate/regenerate), in-app previews for PDF, images, XML, text, CSV and
XLSX, an A4 print preview with "save as attachment", and an audit history
list read through a permission-checked AuditTrailService.

Files are served by /files endpoints that require a signed-in user and the
company permissions; only PDF and images open inline, with their own CSP
that allows framing by this site only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 19:59:37 +01:00
1 parent f415dc37a8
commit 8198e0a754
20 files changed
+1786 -24

No files matched your search

@@ -3,9 +3,12 @@
@inherits DocumentPage<SampleDocument, SampleLine>
@inject IWebHostEnvironment Environment
@inject TimeProvider Clock
@inject ShellSession Session
@inject AttachmentService Attachments
@using Khaemhat.Application.Audit
@* A document page built on DocumentPage (issue #29): toolbar, header, tabs with Righe first, shortcuts. Fake data,
development only. *@
development only; the attachments (issue #34) are real, on the test document of the generators. *@
<KhPageTitle Text="@T["Pages.Catalog.Document"]" Icon="file" />
@if (!Environment.IsDevelopment() || Record is null)
@@ -65,6 +68,14 @@ else
</KhForm>
break;
case TabIds.Attachments:
<div class="kh-tab-body"><KhAttachmentList Entity="@DemoDocument.Entity" EntityId="SampleId" OnCountChanged="SetAttachments" /></div>
break;
case TabIds.History:
<div class="kh-tab-body"><KhAuditList Items="History" FieldName="FieldLabel" /></div>
break;
default:
<div class="pb"><p class="muted">@T["Pages.Catalog.TabPlaceholder"]</p></div>
break;
@@ -72,13 +83,22 @@ else
</ChildContent>
</KhRecordLayout>
<KhDrawer Open="_drawer" Title="@T["Ui.Attachments.Title"]" Icon="paperclip" Wide="true" OnClose="() => _drawer = false">
<KhAttachmentList Entity="@DemoDocument.Entity" EntityId="SampleId" OnCountChanged="SetAttachments" />
</KhDrawer>
<KhRadarDialog TItem="SampleItem" Id="catalog.items" Open="_lookupLine is not null" Title="@T["Pages.Catalog.Grids.ItemRadar"]" Columns="ItemColumns"
Source="CatalogSamples.ItemsAsync" KeyOf="i => i.Code" InitialSearch="@_lookupLine?.Code" OnPick="PickItemAsync" OnClose="() => _lookupLine = null"
OnCreate="CreateItem" />
}
@code {
/// <summary>The test document the attachments belong to.</summary>
public const long SampleId = 1;
private int _saved;
private int _attachments;
private bool _drawer;
private KhLinesGrid<SampleLine>? _lines;
private SampleLine? _lookupLine;
private TaskCompletionSource? _lookupDone;
@@ -155,7 +175,7 @@ else
}
/// <inheritdoc />
public override IReadOnlyList<KhTabItem> Tabs => Record is null ? [] : StandardTabs(attachments: 2, links: 1);
public override IReadOnlyList<KhTabItem> Tabs => Record is null ? [] : StandardTabs(attachments: _attachments, links: 1);
/// <inheritdoc />
public override bool IsReadOnly => Record?.Issued == true;
@@ -167,7 +187,7 @@ else
protected override string? NewUrl => "sviluppo/documento";
/// <inheritdoc />
protected override int? AttachmentCount => 2;
protected override int? AttachmentCount => _attachments;
/// <inheritdoc />
protected override bool CanMove => true;
@@ -187,6 +207,51 @@ else
Record = CatalogSamples.Document(Today);
}
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await Session.EnsureLoadedAsync();
if (Session.Actor is { CompanyCode: { } company } actor && Session.Permissions.Allows(AttachmentService.ReadPermission))
{
_attachments = await Attachments.CountAsync(actor, company, DemoDocument.Entity, SampleId);
}
}
/// <inheritdoc />
protected override Task AttachmentsAsync()
{
// Toolbar commands are plain callbacks: render the page for the panel.
_drawer = !_drawer;
StateHasChanged();
return Task.CompletedTask;
}
/// <inheritdoc />
protected override Task PrintAsync()
{
Navigation.NavigateTo("sviluppo/documento/stampa");
return Task.CompletedTask;
}
private void SetAttachments(int count) => _attachments = count;
// A pretend history: the sample document is not saved anywhere.
private IReadOnlyList<AuditItem> History =>
[
new(3, Clock.GetUtcNow().UtcDateTime.AddMinutes(-12), AuditAction.Update, Session.Actor?.UserName,
[new("Discount", "0", "5"), new("Notes", null, T["Pages.Catalog.Fields.Notes"])]),
new(2, Clock.GetUtcNow().UtcDateTime.AddHours(-3), AuditAction.Update, "anna", [new("Number", "2026/0417", Record?.Number)]),
new(1, Clock.GetUtcNow().UtcDateTime.AddDays(-1), AuditAction.Insert, "anna", []),
];
private string? FieldLabel(string field) => field switch
{
"Discount" => T["Pages.Catalog.Fields.Discount"],
"Notes" => T["Pages.Catalog.Fields.Notes"],
"Number" => T["Pages.Catalog.Fields.Number"],
_ => null,
};
/// <inheritdoc />
protected override IEnumerable<ToolbarItem> ContextActions()
{
@@ -0,0 +1,110 @@
@page "/sviluppo/documento/stampa"
@attribute [Authorize]
@using System.Globalization
@using Khaemhat.Application.Security
@using Khaemhat.Web.Hosting
@inject IWebHostEnvironment Environment
@inject ShellSession Session
@inject FileGenerationService Generation
@inject ToastService Toasts
@inject TimeProvider Clock
@* The print preview of the test document (issue #34, mockup 025): the same lines the PDF generator writes, as an A4
sheet in HTML; Stampa prints the sheet only, the PDF comes from DemoPdfGenerator. Development only. *@
<KhPageTitle Text="@T["Pages.Print.Title"]" Icon="printer" />
@if (!Environment.IsDevelopment())
{
<div class="kh-message"><h1>@T["Pages.Catalog.DevOnly"]</h1></div>
}
else
{
<div class="phead">
<div>
<div class="crumbs">@T["Pages.Print.Crumb"] › <b>@T["Pages.Print.Title"]</b></div>
<h1>@T.Format("Pages.Print.Heading", DemoDocument.Title, Number)</h1>
</div>
</div>
<KhPrintPreview PdfUrl="@PdfUrl(false)" PdfDownloadUrl="@PdfUrl(true)" OnSaveAttachment="SaveAsync" Saving="_saving">
<div class="kh-sheet-head">
<div>
<div class="kh-sheet-company">@(Session.Company?.Name ?? Company)</div>
<div class="kh-sheet-muted">@T.Format("Pages.Print.CompanyLine", Company)</div>
</div>
<div class="kh-sheet-title">
<div class="k">@DemoDocument.Title</div>
<div class="n">@T.Format("Pages.Print.Number", Number, T.Date(Today))</div>
</div>
</div>
<p class="kh-sheet-intro">@T.Format("Pages.Print.Intro", Company, Session.Actor?.UserName ?? string.Empty)</p>
<table class="kh-sheet-table">
<thead>
<tr>
@for (var i = 0; i < DemoDocument.Headers.Count; i++)
{
<th class="@(i >= 2 ? "num" : null)">@DemoDocument.Headers[i]</th>
}
</tr>
</thead>
<tbody>
@foreach (var line in DemoDocument.Lines)
{
<tr>
<td class="mono">@line.Code</td>
<td>@line.Description</td>
<td class="num">@line.Quantity.ToString("#,##0.##", T.Culture)</td>
<td class="num">@DemoDocument.Euro(line.UnitPrice, T.Culture)</td>
<td class="num">@DemoDocument.Euro(DemoDocument.Amount(line), T.Culture)</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="4" class="num">@T["Pages.Print.Total"]</td>
<td class="num"><b>@DemoDocument.Euro(DemoDocument.Total, T.Culture)</b></td>
</tr>
</tfoot>
</table>
<p class="kh-sheet-foot">@T["Pages.Print.Footer"]</p>
</KhPrintPreview>
}
@code {
private bool _saving;
private static string Number => SampleDocumentPage.SampleId.ToString(CultureInfo.InvariantCulture);
private string Company => Session.Actor?.CompanyCode ?? string.Empty;
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
/// <inheritdoc />
protected override Task OnInitializedAsync() => Session.EnsureLoadedAsync();
private string? PdfUrl(bool download) =>
Session.Actor?.CompanyCode is null ? null : FileEndpoints.Generated(DemoDocument.Entity, SampleDocumentPage.SampleId, DemoPdfGenerator.GeneratorCode, download);
private async Task SaveAsync()
{
if (Session.Actor is not { CompanyCode: { } company } actor)
{
return;
}
_saving = true;
try
{
var saved = await Generation.GenerateAsync(actor, company, DemoDocument.Entity, SampleDocumentPage.SampleId, DemoPdfGenerator.GeneratorCode);
Toasts.Show(ToastKind.Success, T["Ui.Attachments.GeneratedToast"], saved.FileName);
}
catch (Exception ex) when (ex is AccessDeniedException or ValidationFailedException)
{
Toasts.Show(ToastKind.Error, T["Ui.Print.SaveAttachment"], T["Ui.Attachments.Denied"]);
}
finally
{
_saving = false;
}
}
}