SeemGen/Components/Partials/GenerateFromScratch.razor

180 lines
5.7 KiB
Plaintext

@using BLAIzor.Models
@using BLAIzor.Services
@using Newtonsoft.Json
@using BLAIzor.Components.Partials
@inject ContentEditorService ContentEditorService
@inject HtmlSnippetProcessor HtmlSnippetProcessor
@inject QDrantService QDrantService
<div>
<label for="subject">Enter the subject of your website:</label>
<input id="subject" @bind="subject" class="form-control" placeholder="e.g., Web Design Company" />
<button class="btn btn-primary mt-2" @onclick="GenerateMenuItems">Generate Menu Items</button>
</div>
@if (isLoading)
{
<p>Loading suggestions...</p>
}
else if (menuItems.Any())
{
<h4>Suggested Menu Items</h4>
<div class="row">
<div class="rz-p-sm-12">
<RadzenAccordion Multiple="true">
<Items>
@foreach (var item in menuItems)
{
<RadzenAccordionItem Text="@item.Name" Icon="account_balance_wallet">
@{
if (MenuItemsSaved)
{
<input @bind="item.Name" class="form-control border-0" placeholder="Menu item name" />
<MenuItemContentEditor Subject=@subject MenuItem="item" OnContentUpdated="UpdateMenuItem" SessionId="SessionId" />
<div class="card-footer">
<button class="btn btn-danger btn-sm mt-2" @onclick="() => RemoveMenuItem(item)">Remove</button>
<button class="btn btn-default btn-sm mt-2" @onclick="() => AddMenuItem()">Add a new menu item after this one</button>
</div>
}
}
</RadzenAccordionItem>
}
</Items>
</RadzenAccordion>
</div>
</div>
<button class="btn btn-success mt-3" @onclick="SaveAllContent">Save All</button>
}
else if (!string.IsNullOrEmpty(errorMessage))
{
<p class="text-danger">@errorMessage</p>
}
@code {
[Parameter]
public int SiteId { get; set; }
[Parameter]
public string SessionId { get; set; }
private string subject = string.Empty;
private List<MenuItemModel> menuItems = new();
private bool MenuItemsSaved = false;
private bool isLoading = false;
private string? errorMessage;
private bool hasCollection = false;
protected override async Task OnParametersSetAsync()
{
List<MenuItem> Site = await ContentEditorService.GetMenuItemsBySiteIdAsync(SiteId);
var collectionResult = await QDrantService.GetCollectionBySiteIdAsync(SiteId);
if (!string.IsNullOrEmpty(collectionResult))
{
//create colection
hasCollection = true;
Console.Write("Has collection already");
}
if (Site.Count() > 0)
{
foreach (var menuItem in Site)
{
string content;
MenuItemModel model = new MenuItemModel();
//try to get content from qDrant
if (menuItem.QdrantPointId != null)
{
content = await QDrantService.GetContentAsync(SiteId, menuItem.SortOrder);
// content = await QDrantService.GetContentAsync(SiteId, menuItem.QdrantPointId); TODO
var selectedPoint = JsonConvert.DeserializeObject<QDrantGetContentPointResult>(content)!;
if (selectedPoint != null)
{
model.Content = selectedPoint.result.payload.content;
Console.Write($"Found point: {selectedPoint.result.payload.content}");
}
}
model.Name = menuItem.Name;
menuItems.Add(model);
// UpdateMenuItem(model);
}
MenuItemsSaved = true;
}
//get menu content from QDrant for each menu item
}
private async Task GenerateMenuItems()
{
if (string.IsNullOrWhiteSpace(subject))
{
errorMessage = "Please enter a subject.";
return;
}
isLoading = true;
errorMessage = null;
try
{
var prompt = $"Suggest a list of menu items for a website about: {subject}. Please do not attach any explanation.";
var response = await ContentEditorService.GetMenuSuggestionsAsync(SessionId, prompt);
menuItems = response.Select(name => new MenuItemModel { Name = name }).ToList();
}
catch (Exception ex)
{
errorMessage = "An error occurred while generating menu items.";
}
finally
{
isLoading = false;
}
}
private async Task AddMenuItem()
{
isLoading = true;
errorMessage = null;
menuItems.Add(new());
}
private void RemoveMenuItem(MenuItemModel item)
{
menuItems.Remove(item);
}
private void UpdateMenuItem(MenuItemModel updatedItem)
{
Console.Write("Updating menu item");
var item = menuItems.FirstOrDefault(i => i.Name == updatedItem.Name);
if (item != null)
{
item.Content = updatedItem.Content;
}
else{
Console.Write("Not found");
}
}
private async Task SaveAllContent()
{
var result = await ContentEditorService.ProcessMenuItems(SiteId, hasCollection, menuItems, subject, MenuItemsSaved);
if (result == "OK")
{
MenuItemsSaved = true;
hasCollection = true;
errorMessage = "Menu saved";
}
else
{
errorMessage = result;
}
}
}