SeemGen/Components/Pages/Preview.razor

474 lines
16 KiB
Plaintext

@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 *@
<div class="page" style="z-index: 1">
<NavMenu MenuString="@Menu" OnMenuClicked=@MenuClick></NavMenu>
<main>
<article class="content text-center" style="position: relative; z-index: 4;">
<PageTitle>Home</PageTitle>
<VideoComponent SelectedBrandName="@SelectedBrandName" />
@* <HeadContent>
@if (!string.IsNullOrEmpty(dynamicallyLoadedCss))
{
<style>@dynamicallyLoadedCss</style>
}
</HeadContent> *@
<div id="maincontrol">
@* <div class="hoverslide"> *@
<div class="displaysearch">
<div class="searchBox">
<input @oninput="(e) => UserInput = e.Value.ToString()"
@onkeydown="@Enter" class="searchInput" type="text" name="" value="@UserInput" placeholder="Ask any question">
<button data-hint="ask anything" class="searchButton border-0" @onclick="SendUserQuery" href="#">
<i class="fa-solid fa-hexagon-nodes-bolt" style="font-size:20px"></i>
</button>
</div>
@{
if (VoiceEnabled)
{
if (STTEnabled)
{
<button data-hint="talk" id="recButton" class="btn btn-primary voicebutton" onclick="startRecording()"><i class="fa-solid fa-microphone"></i></button>
<button id="stopButton" class="btn btn-primary voicebutton" onclick="stopRecording()" hidden><i class="fa-solid fa-microphone-slash"></i></button>
}
if(TTSEnabled)
{
if(!AiVoicePermitted)
{
<button data-hint="listen" class="btn btn-primary voicebutton" @onclick="AllowAIVoice"><i class="fa-solid fa-volume-xmark"></i>
</button>
}
else
{
<button data-hint="listen" class="btn btn-primary voicebutton" @onclick="MuteAI"><i class="fa-solid fa-volume-high"></i></button>
}
<audio id="audioPlayer" hidden style="display: none;"></audio>
}
}
}
</div>
@* Type anything *@
@* </div> *@
</div>
<p id="recordingText"></p>
<div id="currentContent">
@{
if (!string.IsNullOrEmpty(HtmlContent.ToString()))
{
// <div class="pt-5 @FirstColumnClass">
// @((MarkupString)HtmlContent.ToString())
// </div>
if (isEmailFormVisible)
{
<div class="conteiner-fluid">
<div class="row">
<div class="pt-5 @FirstColumnClass">
@((MarkupString)HtmlContent.ToString())
</div>
<div class="pt-5 col-12 col-md-6">
<ContactFormComponent ContactFormModel="@ContactFormModel" DocumentEmailAddress="@DocumentEmailAddress" OnDataChanged="@ContentChangedInForm"></ContactFormComponent>
<button @onclick="CancelEmail" class="btn btn-primary">Cancel</button>
</div>
</div>
</div>
}
else
{
<div class="pt-5 @FirstColumnClass">
@((MarkupString)HtmlContent.ToString())
</div>
}
}
else
{
<div class="text-center row" style="height: 70vh;">
<p>@StatusContent</p>
<div class="mydiv"></div>
<div class="mydiv"></div>
<div class="mydiv"></div>
<div class="mydiv"></div>
<div class="mydiv"></div>
</div>
}
}
</div>
<button class="btn btn-primary" @onclick="HomeClick"><i class="fa-solid fa-rotate"></i></button>
</article>
</main>
</div>
<script>
var sessionId = null;
function setSessionId(id) {
sessionId = id;
console.log("Session ID set:", sessionId);
}
function callAI(inputString) {
console.log(sessionId);
DotNet.invokeMethodAsync('BLAIzor', 'CallCSharpMethod2', inputString, sessionId, false)
.then(response => console.log(response))
.catch(error => console.error(error));
}
</script>
<script>
function openCalendar(calendlyUserName) {
console.log(calendlyUserName);
Calendly.initPopupWidget({
url: 'https://calendly.com/' + calendlyUserName + '?name=JohnDoe&email=john.doe@example.com'
});
}
</script>
<script>
function openContactForm(emailAddress) {
console.log(emailAddress);
if (emailAddress) {
DotNet.invokeMethodAsync('BLAIzor', 'OpenEmailForm2', emailAddress)
}
}
</script>
@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<bool>("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<object>("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<string>("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<string>("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<object>("getDivContent", "currentContent");
_scopedContentService.CurrentDOM = JsonSerializer.Serialize(result);
Console.Write(_scopedContentService.CurrentDOM);
}
}
private async Task ContentChangedInForm()
{
var result = await jsRuntime.InvokeAsync<object>("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);
}
}