Skip to content

Commit

Permalink
feat: allow contact did copy on click
Browse files Browse the repository at this point in the history
Signed-off-by: Colton Wolkins (Laptop) <colton@indicio.tech>
  • Loading branch information
TheTechmage committed Oct 4, 2023
1 parent e2d25ef commit ea446c8
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/pages/profile/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class MessageHistoryComponent
messages: Message[] = []
content: string = ""
contact: Contact
private didCopied: boolean = false
private editMode: boolean = false
private contactHover: boolean = false
private editedContactLabel: string = ""
Expand Down Expand Up @@ -266,6 +267,54 @@ class MessageHistoryComponent
ContactService.addContact(this.contact as Contact)
}

private showToast(message: string, duration: number = 2000) {
// Create a div element for the toast
const toast = document.createElement("div")
toast.className = "toast"
toast.textContent = message

// Append it to the body
document.body.appendChild(toast)

// Force a reflow to trigger the transition
void toast.offsetWidth

// Show the toast
toast.classList.add("show")

// Remove the toast after the specified duration
setTimeout(() => {
toast.classList.remove("show")

// Wait for the transition to finish before removing the element
toast.addEventListener("transitionend", () => {
document.body.removeChild(toast)
})
}, duration)
}

private copyToClipboard(text: string) {
navigator.clipboard
.writeText(text)
.then(() => {
this.didCopied = true
m.redraw() // Inform Mithril to redraw the component

// Reset after some time (e.g., 2 seconds)
setTimeout(() => {
this.didCopied = false
m.redraw()
}, 2000)

// Show the toast
// Assuming you have a toast library/method named `showToast`
this.showToast("Copied!")
})
.catch(err => {
console.error("Failed to copy text: ", err)
})
}

viewMessage(message: Message) {
switch (message.type) {
case "https://didcomm.org/basicmessage/2.0/message":
Expand Down Expand Up @@ -475,6 +524,9 @@ class MessageHistoryComponent
onmouseout: () => {
this.contactHover = false
},
onclick: () => {
this.copyToClipboard(this.contact.did)
},
},
this.contactHover
? this.contact.did
Expand Down

0 comments on commit ea446c8

Please sign in to comment.