@page "/preview-snippet/{TemplateId:int}/{Snippet}"
@using BLAIzor.Components.Layout
@using BLAIzor.Models
@using BLAIzor.Services
@using Newtonsoft.Json
@using BLAIzor.Components.Partials
@attribute [Authorize]
@layout PreviewLayout
@inject DesignTemplateService DesignTemplateService
@inject CssTemplateService CssTemplateService
@inject NavigationManager NavigationManager
@inject QDrantService QDrantService
@inject HtmlSnippetProcessor HtmlSnippetProcessor
@inject IJSRuntime JSRuntime
@((MarkupString)CurrentSnippet.SampleHtml)
@code {
[Parameter]
public int TemplateId { get; set; }
[Parameter]
public string Snippet { get; set; } = "";
private bool isLoading = true;
private DesignTemplate currentTemplate = new();
private CssTemplate currentCssTemplate = new();
private bool hasCss = false;
private bool hasCollection = false;
private List SnippetList = [];
private HtmlSnippet CurrentSnippet = new();
private string SessionId = "";
private static readonly Dictionary _instances = new();
protected override async Task OnInitializedAsync()
{
SessionId = Guid.NewGuid().ToString();
_instances[SessionId] = this;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("setSessionId", SessionId);
await JSRuntime.InvokeVoidAsync("setHtmlEditorSourceMode");
await LoadTemplate();
var cssPath = await CssTemplateService.SaveTempCssFileAsync(currentCssTemplate.CssContent, SessionId);
await JSRuntime.InvokeVoidAsync("seemgen.injectCssFile", cssPath);
}
}
private async Task LoadTemplate()
{
isLoading = true;
// Load the design template and its associated CSS template from the database
currentTemplate = await DesignTemplateService.GetByIdAsync(TemplateId);
currentCssTemplate = await CssTemplateService.GetByDesignTemplateIdAsync(TemplateId);
var currentCollection = await QDrantService.GetCollectionByNameAsync(currentTemplate.QDrandCollectionName);
Console.Write(currentCollection);
if(currentCssTemplate == null)
{
hasCss = false;
currentCssTemplate = new();
currentCssTemplate.DesignTemplateId = TemplateId;
}
else
{
hasCss = true;
}
if(!string.IsNullOrEmpty(currentCollection))
{
hasCollection = true;
var collectionCount = await QDrantService.GetCollectionCount(currentTemplate.QDrandCollectionName);
for(int i=1; i <= collectionCount; i++)
{
var snippetItem = await QDrantService.GetSnippetAsync(i, currentTemplate.QDrandCollectionName);
var selectedPoint = JsonConvert.DeserializeObject(snippetItem)!;
Console.Write($"Id: {selectedPoint.result.id}, html: {selectedPoint.result.payload.Html}");
SnippetList.Add(selectedPoint.result.payload);
}
CurrentSnippet = SnippetList.Where(x => x.Name == Snippet).FirstOrDefault();
}
else
{
//let's create a collection
var result = QDrantService.CreateQdrantCollectionAsync(currentTemplate.QDrandCollectionName);
}
isLoading = false;
await InvokeAsync(StateHasChanged);
}
}