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

fix(previews): add checks to create payment preview messages #664

Merged
merged 8 commits into from
Oct 7, 2024
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
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