SeemGen/Components/Partials/MenuItemContentEditor.razor

218 lines
7.1 KiB
Plaintext

@using BLAIzor.Models
@using BLAIzor.Services
@using Microsoft.AspNetCore.Components.Authorization
@inject ContentEditorService ContentEditorService
@inject ContentEditorAIService ContentEditorAIService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject CustomAuthenticationStateProvider CustomAuthProvider
<div class="menu-item-editor">
@{
if (!string.IsNullOrEmpty(WordFile))
{
<button class="btn btn-primary mb-2" @onclick="GenerateFromText" disabled="@IsLoading">Generate from text</button>
}
}
<button class="btn btn-primary mb-2" @onclick="GenerateContent" disabled="@IsLoading">Generate by AI</button>
@* <textarea class="form-control border-0 text-white" @bind="GeneratedContent" rows="5"></textarea> *@
<div class="row">
<RadzenTextArea @bind-Value=@MenuItem.ContentDescription></RadzenTextArea>
</div>
<div class="row">
<RadzenHtmlEditor @bind-Value=@GeneratedContent style="height: 450px; color:#000; background-color: rgba(255,255,255,0.4)" Input=@OnInput Change=@OnChange Paste=@OnPaste UploadComplete=@OnUploadComplete Execute=@OnExecute UploadUrl="upload/image">
<RadzenHtmlEditorUndo />
<RadzenHtmlEditorRedo />
<RadzenHtmlEditorSource />
</RadzenHtmlEditor>
</div>
<InputFile class="btn btn-default" type="file" multiple OnChange=HandleFileUpload accept=".mp3,.mp4,.jpg,.png" />
@* <EventConsole @ref=@console /> *@
@if (IsLoading)
{
<p>Loading content...</p>
}
</div>
@code {
[Parameter] public string Subject { get; set; } = string.Empty;
[Parameter] public string WordFile { get; set; } = string.Empty;
[Parameter] public string SessionId { get; set; } = string.Empty;
[Parameter] public MenuItemModel MenuItem { get; set; } = new("");
[Parameter] public EventCallback<MenuItemModel> OnContentUpdated { get; set; }
private bool IsLoading = false;
private string GeneratedContent = string.Empty;
// private string Description = string.Empty;
private string? userId;
private string? userName;
private AuthenticationState? authState;
//EventConsole console;
protected override async Task OnParametersSetAsync()
{
GeneratedContent = MenuItem.Content;
// Description = MenuItem.ContentDescription;
authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity?.IsAuthenticated == true)
{
userId = CustomAuthProvider.GetUserId();
userName = CustomAuthProvider.GetUserName();
}
}
private async Task GenerateContent()
{
IsLoading = true;
try
{
Console.Write($"Subject: {Subject}");
var prompt = $"Generate website content for a page titled '{MenuItem.MenuItem.Name}' of a '{Subject}' website. Please do not attach any explanation.";
GeneratedContent = await ContentEditorAIService.GetGeneratedContentAsync(SessionId, prompt);
MenuItem.Content = GeneratedContent;
await OnContentUpdated.InvokeAsync(MenuItem);
}
catch
{
GeneratedContent = "An error occurred while generating content.";
}
finally
{
IsLoading = false;
}
}
private async Task GenerateFromText()
{
IsLoading = true;
try
{
Console.Write($"Subject: {Subject}");
var prompt = $"This is the provided text: {WordFile}. Look for the relevant content for a page titled '{MenuItem.MenuItem.Name}' of a '{Subject}' website. Please do not attach any explanation.";
GeneratedContent = await ContentEditorAIService.GetGeneratedContentAsync(SessionId, prompt);
MenuItem.Content = GeneratedContent;
await OnContentUpdated.InvokeAsync(MenuItem);
}
catch
{
GeneratedContent = "An error occurred while generating content.";
}
finally
{
IsLoading = false;
}
}
async Task OnPaste(HtmlEditorPasteEventArgs args)
{
// console.Log($"Paste: {args.Html}");
MenuItem.Content = args.Html;
await OnContentUpdated.InvokeAsync(MenuItem);
}
async Task OnChange(string html)
{
// console.Log($"Change: {html}");
MenuItem.Content = html;
await OnContentUpdated.InvokeAsync(MenuItem);
}
async Task OnInput(string html)
{
// console.Log($"Input: {html}");
MenuItem.Content = html;
await OnContentUpdated.InvokeAsync(MenuItem);
}
void OnExecute(HtmlEditorExecuteEventArgs args)
{
//console.Log($"Execute: {args.CommandName}");
}
void OnUploadComplete(UploadCompleteEventArgs args)
{
//console.Log($"Upload complete: {args.RawResponse}");
}
private async Task HandleFileUpload(InputFileChangeEventArgs e)
{
if (e.FileCount == 0) return;
IsLoading = true;
try
{
var uploadPath = Path.Combine("wwwroot", "uploads", userId);
foreach (var file in e.GetMultipleFiles())
{
var folder = GetFolderForFile(file.ContentType);
var folderPath = Path.Combine(uploadPath, folder);
// Create directories if they don't exist
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// Save file
var filePath = Path.Combine(folderPath, file.Name);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.OpenReadStream(50*1024*1024).CopyToAsync(stream);
}
// Add relative path to content
var relativePath = $"/uploads/{userId}/{folder}/{file.Name}";
AppendFilePathToContent(file.ContentType, relativePath);
}
}
catch (Exception ex)
{
// console.Log($"Error uploading files: {ex.Message}");
}
finally
{
IsLoading = false;
}
}
private string GetFolderForFile(string contentType)
{
return contentType switch
{
var type when type.StartsWith("image/") => "images",
var type when type.StartsWith("video/") => "videos",
var type when type.StartsWith("audio/") => "audio",
_ => "others"
};
}
private void AppendFilePathToContent(string contentType, string relativePath)
{
if (contentType.StartsWith("image/"))
{
GeneratedContent += $"\nPhoto URL: {relativePath}";
}
else if (contentType.StartsWith("video/"))
{
GeneratedContent += $"\nVideo URL: {relativePath}";
}
else if (contentType.StartsWith("audio/"))
{
GeneratedContent += $"\nAudio URL: {relativePath}";
}
MenuItem.Content = GeneratedContent;
OnContentUpdated.InvokeAsync(MenuItem);
}
}