155 lines
6.0 KiB
Plaintext
155 lines
6.0 KiB
Plaintext
@page "/folder-structure"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject IFolderStructureService FolderSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">Folder Structure</h1>
|
|
<p class="page-subtitle">Create folder hierarchies in a document library from a CSV template.</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" Single="true" />
|
|
<div class="form-row mt-8">
|
|
<LibraryPicker Profile="Session.CurrentProfile!" SiteUrl="@(_sites.FirstOrDefault()?.Url)" @bind-Library="_libraryTitle" Label="Library Title" />
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="form-label">Source</label>
|
|
<div class="flex-row">
|
|
<button class="btn btn-sm @(_mode == InputMode.Csv ? "btn-primary" : "btn-secondary")"
|
|
type="button" @onclick="() => SetMode(InputMode.Csv)">Upload CSV</button>
|
|
<button class="btn btn-sm @(_mode == InputMode.Builder ? "btn-primary" : "btn-secondary")"
|
|
type="button" @onclick="() => SetMode(InputMode.Builder)">Build visually</button>
|
|
</div>
|
|
</div>
|
|
|
|
@if (_mode == InputMode.Csv)
|
|
{
|
|
<div class="form-group">
|
|
<label class="form-label">CSV File (Level1, Level2, Level3, Level4)</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="form-group">
|
|
<label class="form-label">Folder Structure</label>
|
|
<div class="folder-builder">
|
|
@foreach (var root in _tree)
|
|
{
|
|
<FolderTreeNode Node="root" Depth="1" OnRemove="RemoveRoot" OnChanged="RebuildFromTree" />
|
|
}
|
|
@if (_tree.Count == 0)
|
|
{
|
|
<p class="text-muted">No folders yet. Add a top-level folder to start.</p>
|
|
}
|
|
</div>
|
|
<button class="btn btn-secondary btn-sm mt-8" type="button" @onclick="AddRoot">+ Add top-level folder</button>
|
|
</div>
|
|
}
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">@_rows.Count(r => r.IsValid) valid rows, @_rows.Count(r => !r.IsValid) errors.</div>
|
|
}
|
|
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunCreate" disabled="@(_running || _rows.Count == 0 || string.IsNullOrWhiteSpace(_libraryTitle))">
|
|
@(_running ? "Creating…" : "Create Folders")
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">Cancel</button> }
|
|
</div>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
|
|
|
|
@if (_summary != null)
|
|
{
|
|
<div class="card">
|
|
<div class="alert @(_summary.HasFailures ? "alert-warn" : "alert-success")">
|
|
Created: @_summary.SuccessCount folders. Failures: @_summary.FailedCount
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private enum InputMode { Csv, Builder }
|
|
private InputMode _mode = InputMode.Csv;
|
|
|
|
private List<SiteInfo> _sites = new();
|
|
private string _libraryTitle = string.Empty;
|
|
private List<CsvValidationRow<FolderStructureRow>> _rows = new();
|
|
private readonly List<FolderNode> _tree = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<string>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private void SetMode(InputMode mode)
|
|
{
|
|
_mode = mode;
|
|
_rows.Clear();
|
|
_summary = null; _error = string.Empty;
|
|
if (mode == InputMode.Builder) RebuildFromTree();
|
|
}
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
using var stream = e.File.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateFolders(stream);
|
|
}
|
|
|
|
private void AddRoot()
|
|
{
|
|
_tree.Add(new FolderNode());
|
|
RebuildFromTree();
|
|
}
|
|
|
|
private void RemoveRoot(FolderNode node)
|
|
{
|
|
_tree.Remove(node);
|
|
RebuildFromTree();
|
|
}
|
|
|
|
// Convert the visual tree into validation rows so the count + Create button share the CSV path.
|
|
private void RebuildFromTree()
|
|
{
|
|
_rows = FolderNode.Flatten(_tree)
|
|
.Select(r => new CsvValidationRow<FolderStructureRow>(r, new List<string>()))
|
|
.ToList();
|
|
}
|
|
|
|
private async Task RunCreate()
|
|
{
|
|
_error = string.Empty; _summary = null; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var validRows = _rows.Where(r => r.IsValid && r.Record != null).Select(r => r.Record!).ToList();
|
|
var siteUrl = _sites.FirstOrDefault()?.Url;
|
|
if (string.IsNullOrWhiteSpace(siteUrl)) { _error = "Please select a site."; _running = false; return; }
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
_summary = await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await FolderSvc.CreateFoldersAsync(ctx, _libraryTitle, validRows, progress, c);
|
|
}, _cts.Token);
|
|
_status = $"Complete: {_summary.SuccessCount} folders created.";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
}
|