This commit is contained in:
Adrien Bouvais 2024-05-31 21:28:40 +02:00
parent f8bb7ffc84
commit b60ff3ee6f
5 changed files with 90 additions and 39 deletions

44
Chat.go
View File

@ -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,
"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 = <str>$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())
}

View File

@ -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)

View File

@ -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;

View File

@ -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 := `&lt;button class=&#34;copy-button button is-small is-primary is-outlined&#34; onclick=&#34;copyToClipboardCode(this)&#34;&gt;&lt;i class=&#34;fa-solid fa-copy&#34;&gt;&lt;/i&gt;&lt;/button&gt;`
buttonHTML := `<button class="copy-button button is-small is-primary is-outlined" onclick="copyToClipboardCode(this)"><i class="fa-solid fa-copy"></i></button>`
// Regular expression pattern to match &lt;pre&gt; elements
pattern := `(&lt;pre[^&gt;]*&gt;)`
// Regular expression pattern to match <pre> elements and insert the button right before <code>
pattern := `(<pre[^>]*>)`
// Compile the regular expression
re := regexp.MustCompile(pattern)
// Replace each matched &lt;pre&gt; element with the updated HTML
updatedHTML := re.ReplaceAllStringFunc(htmlContent, func(match string) string {
return `&lt;div class=&#34;code-container&#34;&gt;
&lt;div class=&#34;code-header&#34;&gt;
` + buttonHTML + `
&lt;/div&gt;
` + match + `
&lt;/div&gt;`
})
// Replace each matched <pre> element with the updated HTML
updatedHTML := re.ReplaceAllString(htmlContent, "$1"+buttonHTML)
return updatedHTML
}

View File

@ -23,7 +23,8 @@
</div>
<input class="input is-small mt-2 is-hidden" type="text" id="conversation-name-input"
name="conversation-name-input" placeholder="Conversation name" autocomplete="off">
<div class="is-flex is-justify-content-space-between mt-4">
<div
class="is-flex is-justify-content-space-between mt-4 {% if not IsDefaultConversation %}is-hidden{% endif %}">
<button disabled class="button is-small is-danger" id="delete-conversation-button"
hx-get="/deleteConversation" hx-swap="outerHTML" hx-target="#conversation-dropdown"
hx-vals="js:{conversationId: findSelectedConversationID()}">
@ -31,6 +32,12 @@
<i class="fa-solid fa-trash"></i>
</span>
</button>
<button class="button is-small is-info is-outlined {% if SelectedIsDefault %} is-hidden {% endif %}"
id="archive-default-conversation-button">
<span class="icon">
<i class="fa-solid fa-box-archive"></i>
</span>
</button>
<button class="button is-small is-danger is-outlined is-hidden" id="cancel-conversation-button">
<span class="icon">
<i class="fa-solid fa-xmark"></i>
@ -51,6 +58,14 @@
<i class="fa-solid fa-check"></i>
</span>
</button>
<button class="button is-small is-success is-outlined is-hidden"
id="confirm-archive-default-conversation-button" hx-get="/archiveDefaultConversation"
hx-include="[name='conversation-name-input']" hx-swap="outerHTML"
hx-target="#conversation-dropdown">
<span class="icon">
<i class="fa-solid fa-check"></i>
</span>
</button>
</div>
</div>
</div>
@ -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');
})
</script>
</div>