Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Show DID on Mouseover #41

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,16 @@ export class Agent {
eventbus.emit(message.type, { sender: from, receiver: to, message })
}

public onMessage(type: string, callback: (message: AgentMessage) => void): EventListenerHandle {
public onMessage(
type: string,
callback: (message: AgentMessage) => void
): EventListenerHandle {
return eventbus.on(type, callback)
}

public onAnyMessage(callback: (message: AgentMessage) => void): EventListenerHandle {
public onAnyMessage(
callback: (message: AgentMessage) => void
): EventListenerHandle {
return eventbus.on("messageReceived", callback)
}

Expand Down
74 changes: 72 additions & 2 deletions src/pages/profile/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ class MessageHistoryComponent
messages: Message[] = []
content: string = ""
contact: Contact
private didCopied: boolean = false
private editMode: boolean = false
private contactHover: boolean = false
private editedContactLabel: string = ""
private autoScroll: boolean = true
private eventbus: ScopedEventBus
Expand Down Expand Up @@ -265,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 @@ -376,6 +426,8 @@ class MessageHistoryComponent
alignItems: "flex-end",
flexGrow: "2",
flexDirection: "column",
alignSelf: "flex-start",
position: "relative",
},
},
[
Expand Down Expand Up @@ -445,7 +497,14 @@ class MessageHistoryComponent
)
: m(
"span",
{ style: { display: "flex", alignItems: "center" } },
{
style: {
display: "flex",
alignItems: "center",
position: "absolute",
width: "100%",
},
},
[
m(
"span",
Expand All @@ -459,8 +518,19 @@ class MessageHistoryComponent
whiteSpace: "nowrap",
overflow: "hidden",
},
onmouseover: () => {
this.contactHover = true
},
onmouseout: () => {
this.contactHover = false
},
onclick: () => {
this.copyToClipboard(this.contact.did)
},
},
this.contact.label || this.contact.did
this.contactHover
? this.contact.did
: this.contact.label || this.contact.did
),
m(
"button.button.is-white.is-small",
Expand Down