@page "/preview/{siteid:int}" @page "/preview/{siteid:int}/{topic?}" @inherits SharedDisplayLogic @using BLAIzor.Models @using BLAIzor.Services @using Google.Cloud.Speech.V1 @using Microsoft.AspNetCore.Identity.UI.Services @using BLAIzor.Components.Partials @using System.Text @using System.Text.Json @using System.Net @* @inject AIService ChatGptService *@ @rendermode InteractiveServer @* @inject IJSRuntime jsRuntime; *@ @* @inject IConfiguration configuration *@ @inject ContentService _contentService @* @inject ContentEditorService _contentEditorService *@ @* @inject ScopedContentService _scopedContentService *@ @inject IEmailSender _emailService @inject NavigationManager _navigationManager @inject IHttpContextAccessor HttpContextAccessor @inject DesignTemplateService DesignTemplateService @inject CssTemplateService CssTemplateService @inject CssInjectorService CssService @* @inject HttpClient Http *@
Home @* @if (!string.IsNullOrEmpty(dynamicallyLoadedCss)) { } *@
@*
*@
@{ if (VoiceEnabled) { if (STTEnabled) { } if(TTSEnabled) { if(!AiVoicePermitted) { } else { } } } }
@* Type anything *@ @*
*@

@{ if (!string.IsNullOrEmpty(HtmlContent.ToString())) { //
// @((MarkupString)HtmlContent.ToString()) //
if (isEmailFormVisible) {
@((MarkupString)HtmlContent.ToString())
} else {
@((MarkupString)HtmlContent.ToString())
} } else {

@StatusContent

} }
@code { // public static Preview myHome; private string? Subdomain; [Parameter] public int siteid { get; set; } [Parameter] public string? topic { get; set; } private string ChatGptResponse = string.Empty; private bool isRecording = false; private string dynamicallyLoadedCss = string.Empty; private string Menu; private void AllowAIVoice() { AiVoicePermitted = true; } private void MuteAI() { AiVoicePermitted = false; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await jsRuntime.InvokeVoidAsync("setSessionId", SessionId); await jsRuntime.InvokeVoidAsync("initHints"); } if (_initVoicePending) { Console.WriteLine("PENDING VOICE--------------------------------------------------"); _initVoicePending = false; await jsRuntime.InvokeVoidAsync("initVoiceRecorder", "ProcessAudio2"); } } protected override Task OnInitializedAsync() { VoiceEnabled = configuration?.GetSection("AiSettings")?.GetValue("VoiceActivated") ?? false; return base.OnInitializedAsync(); } public async void MenuClick(string menuName) { await CallCSharpMethod2(menuName, SessionId, true); } public void HomeClick() { //ChatGptService.OnContentReceived -= UpdateContent; AIService.OnContentReceived -= UpdateContent; _navigationManager.Refresh(true); } // private async Task HandleValidSubmit() // { // try // { // // Simulate sending an email // await ((EmailService)_emailService).SendEmailAsync(ContactFormModel, DocumentEmailAddress); // SuccessMessage = "Thank you for contacting us! Your message has been sent."; // ErrorMessage = null; // // Clear the form // ContactFormModel = new(); // } // catch (Exception ex) // { // ErrorMessage = "An error occurred while sending your message. Please try again later."; // SuccessMessage = null; // } // } private void CancelEmail() { FirstColumnClass = ""; isEmailFormVisible = false; StateHasChanged(); } public Preview() { myHome = this; // Set the static reference to the current instance } // [JSInvokable("OpenEmailForm3")] // public static async void OpenEmailForm3(string emailAddress) // { // if (myHome != null) // { // await myHome.DisplayEmailForm(emailAddress); // } // Console.Write("openEmail with: " + emailAddress); // } // public async Task DisplayEmailForm(string emailAddress) // { // FirstColumnClass = "col-12 col-md-6"; // isEmailFormVisible = true; // DocumentEmailAddress = emailAddress; // StateHasChanged(); // var result = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); // _scopedContentService.CurrentDOM = JsonSerializer.Serialize(result); // // Console.Write($"{_scopedContentService.CurrentDOM}"); // } protected override async Task OnParametersSetAsync() { SessionId = Guid.NewGuid().ToString(); SiteId = siteid; _instances[SessionId] = this; _scopedContentService.OnBrandNameChanged += HandleBrandNameChanged; SelectedBrandName = _scopedContentService.SelectedBrandName; // Subdomain = HttpContextAccessor.HttpContext?.Items["Subdomain"]?.ToString(); SiteInfo = await _scopedContentService.GetSiteInfoByIdAsync(SiteId); if (SiteInfo != null) { // SiteId = SiteInfo.Id; _scopedContentService.SelectedBrandName = SiteInfo.SiteName.ToLower(); TTSEnabled = SiteInfo.TTSActive; STTEnabled = SiteInfo.STTActive; Console.Write("Selected brand name:" + _scopedContentService.SelectedBrandName); } else { // SiteId = 1; _scopedContentService.SelectedBrandName = "default"; TTSEnabled = false; STTEnabled = false; } _scopedContentService.SelectedSiteId = SiteId; Console.Write("------------------------"); // Load the CSS template for the selected brand from the database var designTemplate = await DesignTemplateService.GetByIdAsync((int)SiteInfo.TemplateId!); var cssTemplate = await CssTemplateService.GetByDesignTemplateIdAsync((int)SiteInfo.TemplateId); CollectionName = designTemplate.QDrandCollectionName; if (cssTemplate != null) { dynamicallyLoadedCss = cssTemplate.CssContent; // Assuming Content holds the CSS string var cssPath = await CssTemplateService.SaveTempCssFileAsync(dynamicallyLoadedCss, SessionId); await jsRuntime.InvokeVoidAsync("seemgen.injectCssFile", cssPath); } Console.Write("------------------------"); // ChatGptService.OnContentReceived += UpdateContent; AIService.OnContentReceived += UpdateContent; // ChatGptService.OnStatusChangeReceived += UpdateStatus; AIService.OnStatusChangeReceived += UpdateStatus; AIService.OnTextContentAvailable += UpdateTextContentForVoice; Menu = await GetMenuList(SiteId); if (string.IsNullOrEmpty(HtmlContent.ToString())) { if (!string.IsNullOrWhiteSpace(topic)) { UserInput = topic; await ChatGptService.ProcessContentRequest(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, Menu, true); } else { await ChatGptService.GetChatGptWelcomeMessage(SessionId, SiteId, Menu); } } UserInput = string.Empty; _initVoicePending = true; } private async void UpdateContent(string receivedSessionId, string content) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { HtmlContent.Clear(); HtmlContent.Append(content); //InvokeAsync(StateHasChanged); // Ensures UI updates dynamically await InvokeAsync(() => { StateHasChanged(); }); //_scopedContentService.CurrentDOM = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); } } private async void UpdateTextContentForVoice(string receivedSessionId, string content) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { TextContent = content; await ConvertTextToSpeech(content); //_scopedContentService.CurrentDOM = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); } } private async void UpdateFinished(string receivedSessionId) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { Console.WriteLine("Content update finished"); var result = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); _scopedContentService.CurrentDOM = JsonSerializer.Serialize(result); Console.Write(_scopedContentService.CurrentDOM); } } private async Task ContentChangedInForm() { var result = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); _scopedContentService.CurrentDOM = JsonSerializer.Serialize(result); Console.Write(_scopedContentService.CurrentDOM); } private async void UpdateStatus(string receivedSessionId, string content) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { StatusContent = content; //InvokeAsync(StateHasChanged); // Ensures UI updates dynamically await InvokeAsync(() => { StateHasChanged(); }); } } public async Task Enter(KeyboardEventArgs e) { if (e.Code == "Enter" || e.Code == "NumpadEnter") { HtmlContent.Clear(); var menu = await GetMenuList(SiteId); await ChatGptService.ProcessUserIntent(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu); UserInput = string.Empty; } } // public async Task HandleVoiceCommand(string input) // { // // HtmlContent = string.Empty; // UserInput = input; // await InvokeAsync(StateHasChanged); // await SendUserQuery(); // //UserInput = string.Empty; // } // private async Task SendUserQuery() // { // welcomeStage = false; // if (!string.IsNullOrEmpty(UserInput)) // { // HtmlContent.Clear(); // var menu = await GetMenuList(SiteId); // Console.Write($"\n\n Input: {UserInput} \n\n"); // await ChatGptService.ProcessUserIntent(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu); // UserInput = string.Empty; // } // } public void Dispose() { dynamicallyLoadedCss = ""; HtmlContent.Clear(); _scopedContentService.OnBrandNameChanged -= HandleBrandNameChanged; AIService.OnContentReceived -= UpdateContent; AIService.OnStatusChangeReceived -= UpdateStatus; } public async ValueTask DisposeAsync() { await CssTemplateService.DeleteSessionCssFile(SessionId); } }