@using BLAIzor.Models @using BLAIzor.Services @using Microsoft.AspNetCore.Components.Authorization @inject ContentEditorService ContentEditorService @inject ContentEditorAIService ContentEditorAIService @inject AuthenticationStateProvider AuthenticationStateProvider @inject CustomAuthenticationStateProvider CustomAuthProvider @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 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); } }