From b60ff3ee6fab4b75fb5d3d4c8ea800c4ccf742a7 Mon Sep 17 00:00:00 2001 From: Adrien Date: Fri, 31 May 2024 21:28:40 +0200 Subject: [PATCH] fix --- Chat.go | 48 +++++++++++++++++++++++- main.go | 1 + static/style.css | 23 +++--------- utils.go | 22 +++-------- views/partials/popover-conversation.html | 35 +++++++++++++++-- 5 files changed, 90 insertions(+), 39 deletions(-) diff --git a/Chat.go b/Chat.go index 073006b..d54c19e 100644 --- a/Chat.go +++ b/Chat.go @@ -748,9 +748,17 @@ func GenerateConversationPopoverHTML(isActive bool) string { panic(err) } + selectedIsDefault := false + for _, conversation := range conversations { + if conversation.Name == "Default" && conversation.Selected { + selectedIsDefault = true + } + } + out, err := conversationPopoverTmpl.Execute(pongo2.Context{ - "Conversations": conversations, - "IsActive": isActive, + "Conversations": conversations, + "IsActive": isActive, + "SelectedIsDefault": selectedIsDefault, }) if err != nil { fmt.Println("Error generating conversation popover") @@ -915,3 +923,39 @@ func SelectConversationHandler(c *fiber.Ctx) error { return c.SendString(generateChatHTML()) } + +func ArchiveDefaultConversationHandler(c *fiber.Ctx) error { + edgeClient = edgeClient.WithoutGlobals().WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}) + name := c.FormValue("conversation-name-input") + + err := edgeClient.Execute(edgeCtx, ` + UPDATE Conversation + FILTER .user = global currentUser AND .name = 'Default' + SET { + name = $0 + }; + `, name) + if err != nil { + fmt.Println("Error archiving default conversation") + panic(err) + } + + err = edgeClient.Execute(edgeCtx, ` + WITH + C := ( + SELECT Conversation + FILTER .user = global currentUser + ) + INSERT Conversation { + name := "Default", + user := global currentUser, + position := count(C) + 1 + } + `, name) + if err != nil { + fmt.Println("Error creating conversation") + panic(err) + } + + return c.SendString(generateChatHTML()) +} diff --git a/main.go b/main.go index 22c346c..b4af8ab 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,7 @@ func main() { app.Post("/clearChat", ClearChatHandler) app.Get("/userMessage", GetUserMessageHandler) app.Post("/editMessage", EditMessageHandler) + app.Post("/archiveDefaultConversation", ArchiveDefaultConversationHandler) // Settings routes app.Post("/addKeys", addKeys) diff --git a/static/style.css b/static/style.css index e0f60ab..cc6862d 100644 --- a/static/style.css +++ b/static/style.css @@ -4,26 +4,12 @@ html { } /* Stuff for message boxes */ - -#chat-messages .message-content .code-container { - position: relative; - border-radius: 8px; - overflow: hidden; -} - -#chat-messages .message-content .code-header { - background-color: #f0f0f0; - padding: 5px; - display: flex; - justify-content: flex-end; - align-items: center; -} - #chat-messages .message-content pre { overflow-x: auto; white-space: pre; max-width: 662px; - margin: 0; + position: relative; + border-radius: 8px; } #chat-messages .message-content pre code { @@ -33,10 +19,11 @@ html { } .copy-button { - margin-left: 5px; + position: absolute; + top: 5px; + right: 5px; } - .content { font-size: 14px; line-height: 1.5; diff --git a/utils.go b/utils.go index 46b0d04..8703545 100644 --- a/utils.go +++ b/utils.go @@ -6,7 +6,6 @@ package main import ( "bytes" "fmt" - "html" "regexp" "github.com/yuin/goldmark" @@ -14,15 +13,13 @@ import ( ) func markdownToHTML(markdownText string) string { - escapedText := html.EscapeString(markdownText) - var buf bytes.Buffer md := goldmark.New( goldmark.WithExtensions( highlighting.NewHighlighting(highlighting.WithStyle("monokai")), ), ) - if err := md.Convert([]byte(escapedText), &buf); err != nil { + if err := md.Convert([]byte(markdownText), &buf); err != nil { fmt.Println("failed to convert markdown to HTML") panic(err) } @@ -31,23 +28,16 @@ func markdownToHTML(markdownText string) string { } func addCopyButtonsToCode(htmlContent string) string { - buttonHTML := `<button class="copy-button button is-small is-primary is-outlined" onclick="copyToClipboardCode(this)"><i class="fa-solid fa-copy"></i></button>` + buttonHTML := `` - // Regular expression pattern to match <pre> elements - pattern := `(<pre[^>]*>)` + // Regular expression pattern to match
 elements and insert the button right before 
+	pattern := `(]*>)`
 
 	// Compile the regular expression
 	re := regexp.MustCompile(pattern)
 
-	// Replace each matched <pre> element with the updated HTML
-	updatedHTML := re.ReplaceAllStringFunc(htmlContent, func(match string) string {
-		return `<div class="code-container">
-                    <div class="code-header">
-                        ` + buttonHTML + `
-                    </div>
-                    ` + match + `
-                </div>`
-	})
+	// Replace each matched 
 element with the updated HTML
+	updatedHTML := re.ReplaceAllString(htmlContent, "$1"+buttonHTML)
 
 	return updatedHTML
 }
diff --git a/views/partials/popover-conversation.html b/views/partials/popover-conversation.html
index 6460955..ce58f8c 100644
--- a/views/partials/popover-conversation.html
+++ b/views/partials/popover-conversation.html
@@ -23,7 +23,8 @@
                 
                 
-                
+
+ +
@@ -83,9 +98,11 @@ } if (name === 'Default') { - document.getElementById('delete-conversation-button').disabled = true; + document.getElementById('delete-conversation-button').classList.add('is-hidden'); + document.getElementById('archive-default-conversation-button').classList.remove('is-hidden'); } else { - document.getElementById('delete-conversation-button').disabled = false; + document.getElementById('delete-conversation-button').classList.remove('is-hidden'); + document.getElementById('archive-default-conversation-button').classList.add('is-hidden'); } } @@ -113,6 +130,17 @@ } }); + document.getElementById('archive-default-conversation-button').addEventListener('click', function () { + document.getElementById('conversation-name-input').classList.remove('is-hidden'); + document.getElementById('confirm-archive-default-conversation-button').classList.remove('is-hidden'); + document.getElementById('create-conversation-button').classList.add('is-hidden'); + document.getElementById('cancel-conversation-button').classList.remove('is-hidden'); + document.getElementById('delete-conversation-button').classList.add('is-hidden'); + document.getElementById('conversation-list').classList.add('is-hidden'); + document.getElementById('archive-default-conversation-button').classList.add('is-hidden'); + }) + + document.getElementById('create-conversation-button').addEventListener('click', function () { document.getElementById('conversation-name-input').classList.remove('is-hidden'); document.getElementById('confirm-conversation-button').classList.remove('is-hidden'); @@ -129,6 +157,7 @@ document.getElementById('cancel-conversation-button').classList.add('is-hidden'); document.getElementById('delete-conversation-button').classList.remove('is-hidden'); document.getElementById('conversation-list').classList.remove('is-hidden'); + document.getElementById('archive-default-conversation-button').classList.add('is-hidden'); }) \ No newline at end of file