Skip to content

Commit

Permalink
fix(previews): add checks to create payment preview messages (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jekrimo authored Oct 7, 2024
1 parent e0aa87e commit e573ff6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/lib/components/messaging/ChatPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import { get } from "svelte/store"
import { tempCDN } from "$lib/utils/CommonVariables"
import { UIStore } from "$lib/state/ui"
import { t } from "svelte-i18n"
import { _ } from "svelte-i18n"
import { checkMobile } from "$lib/utils/Mobile"
import { ConversationStore } from "$lib/state/conversation"
export let chat: Chat
export let cta: boolean = false
Expand All @@ -30,8 +31,33 @@
let timeago = getTimeAgo(chat.last_message_at)
const dispatch = createEventDispatcher()
let ownId = get(Store.state.user)
$: messagePreview = (() => {
if (!chat.last_message_id) {
return $_("message_previews.none")
}
if (chat.last_message_id && !chat.last_message_preview) {
return $_("message_previews.attachment")
}
if (chat.last_message_preview.startsWith("/request")) {
try {
const sendingUserId = ConversationStore.getMessage(chat.id, chat.last_message_id)?.details.origin
const sendingUserDetails = get(Store.getUser(sendingUserId!))
const { amountPreview } = JSON.parse(chat.last_message_preview.slice(8))
return sendingUserId !== ownId.key
? $_("message_previews.coin_requested", { values: { username: sendingUserDetails.name, amount: amountPreview } })
: $_("message_previews.request_sent", { values: { amount: amountPreview } })
} catch (error) {
return "Invalid message format"
}
}
return chat.last_message_preview
})()
$: messagePreview = chat.last_message_id === "" ? "No messages sent yet." : chat.last_message_id !== "" && chat.last_message_preview === "" ? "New Attachment" : chat.last_message_preview
function getTimeAgo(dateInput: string | Date) {
const date: Date = typeof dateInput === "string" ? new Date(dateInput) : dateInput
return timeAgo.format(date)
Expand Down
6 changes: 6 additions & 0 deletions src/lib/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
"pinnedUnpin": "Unpin",
"pinnedNone": "There are no pinned messages in this chat"
},
"message_previews": {
"none": "No messages sent yet.",
"attachment": "New attachment",
"coin_requested": "{username} has requested {amount}",
"request_sent": "You sent a request for {amount}"
},
"friends": {
"copy_did": "Copy ID",
"block": "Block",
Expand Down

0 comments on commit e573ff6

Please sign in to comment.