SeemGen/Components/Partials/ManageContentGroupsPartial....

209 lines
6.3 KiB
Plaintext

@using BLAIzor.Models
@using BLAIzor.Services
@using Microsoft.AspNetCore.Components.Authorization
@inherits EditorUIComponentBase<ContentGroup>
@inject ContentEditorService contentEditorService
@inject NavigationManager navigation
@inject NotificationService notificationService
@inject DialogService dialogService
@attribute [Authorize]
@if (isLoading)
{
<p>Loading content groups...</p>
}
else
{
@foreach (var group in groups)
{
if (selectedGroupId == group.Id)
{
<EditContentGroup Group="group" OnContentGroupSaveClicked="SaveGroup" OnContentGroupDeleteClicked="DeleteGroup" OnManageContentItemClicked="ContentItemManagedCallback" OnForceReChunkClicked="Rechunk"></EditContentGroup>
}
else
{
<div class="rounded bg-panel p-3 mb-3 d-flex justify-content-between align-items-center">
<a style="width:100%" @onclick="() => SelectGroup(group.Id)">
<div style="width:100%; cursor:pointer;">
<strong>@group.Name</strong><br />
<small class="text-muted">@group.Type</small>
</div>
</a>
</div>
}
}
<button class="pointer bg-transparent border-0 p-0" style="width:100%; text-align: left;" @onclick="AddNewGroup">
<div class="mb-2 p-3 reference-button bg-panel-gradient-highlight pointer">
<div class="text-content">
<strong>Add new group</strong>
<br />
<small class="text-muted">Add a new content group like "Blog", News, "Manuals" etc...</small>
</div>
<div class="icon-buttons">
<div class="icon-circle"><i class="fa-solid fa-plus text-white"></i></div>
</div>
</div>
</button>
}
@code {
[Parameter]
public int SiteInfoId { get; set; }
[Parameter] public Func<string, int, Task> OnManageContentItemClicked { get; set; }
private List<ContentGroup> groups = new();
private bool isLoading = true;
private int? selectedGroupId = null;
private async Task SelectGroup(int groupId)
{
selectedGroupId = groupId;
var group = groups.FirstOrDefault(g => g.Id == groupId);
if (group != null)
{
await NotifyContentEditStartedAsync(group);
}
}
private async Task DeselectGroup()
{
selectedGroupId = null;
StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
isLoading = true;
groups = (await contentEditorService.GetContentGroupsBySiteInfoIdAsync(SiteInfoId))
.Where(g => g.SiteInfoId == SiteInfoId)
.OrderByDescending(g => g.LastUpdated)
.ToList();
isLoading = false;
}
private void EditGroup(ContentGroup group)
{
navigation.NavigateTo($"/content-group/{group.Id}/items");
}
private async Task Rechunk(ContentGroup group)
{
var confirmationResult = await dialogService.Confirm("Are you sure?", "Rechunk group", new ConfirmOptions() { OkButtonText = "Rechunk", CancelButtonText = "Oops, no" });
if (confirmationResult == true)
{
await ReallyRechunk(group);
}
else
{
//do nothing?
}
}
private async Task ReallyRechunk(ContentGroup group)
{
group.LastUpdated = DateTime.UtcNow;
var siteInfo = await contentEditorService.GetSiteInfoByIdAsync(group.SiteInfoId);
var result = await contentEditorService.ForceRechunkContentGroupAsync(group.Id, siteInfo.VectorCollectionName);
if (result != null)
{
await NotifyContentUpdatedAsync(group); // This triggers the base class logic
await DeselectGroup();
}
}
private async Task SaveGroup(ContentGroup group)
{
group.LastUpdated = DateTime.UtcNow;
group.Version = group.Version + 1;
var result = await contentEditorService.UpdateContentGroupByIdAsync(group);
if (result != null)
{
await NotifyContentUpdatedAsync(group); // This triggers the base class logic
await DeselectGroup();
}
}
private async Task DeleteGroup(ContentGroup group)
{
var confirmationResult = await dialogService.Confirm("Are you sure?", "Delete group", new ConfirmOptions() { OkButtonText = "Delete", CancelButtonText = "Oops, no" });
if (confirmationResult == true)
{
await ReallyDeleteGroup(group);
}
else
{
//do nothing?
}
}
private async Task ReallyDeleteGroup(ContentGroup group)
{
var result = await contentEditorService.DeleteContentGroupByIdAsync(group.Id); ;
groups.Remove(group);
var message = NotificationHelper.CreateNotificationMessage("Group deleted", 2, "Success", "ContentGroup deleted");
ShowNotification(message);
StateHasChanged();
}
private async Task AddNewGroup()
{
var newGroup = new ContentGroup
{
SiteInfoId = SiteInfoId,
Name = "New Group",
Slug = $"group-{DateTime.UtcNow.Ticks}",
Type = "manual",
VectorSize = 384,
EmbeddingModel = "default",
CreatedAt = DateTime.UtcNow,
LastUpdated = DateTime.UtcNow,
Version = 1
};
var result = await contentEditorService.CreateContentGroupAsync(newGroup);
if (result != null)
{
var message = NotificationHelper.CreateNotificationMessage("Group created", 2, "Success", "ContentGroup created");
ShowNotification(message);
groups.Insert(0, newGroup);
selectedGroupId = newGroup.Id;
}
}
private async Task ContentItemManagedCallback(string method, int itemId)
{
if (OnManageContentItemClicked != null)
await OnManageContentItemClicked.Invoke(method, itemId);
}
public void ShowNotification(NotificationMessage message)
{
notificationService.Notify(message);
}
RenderFragment GetMessage()
{
return __builder =>
{
<text>
Are <b>you</b> sure?
</text>
};
}
}