From e573ff616dcce7da18754a8132c86d77a51b52b9 Mon Sep 17 00:00:00 2001 From: Jeff Kristian Date: Mon, 7 Oct 2024 14:12:50 -0600 Subject: [PATCH] fix(previews): add checks to create payment preview messages (#664) --- .../components/messaging/ChatPreview.svelte | 30 +++++++++++++++++-- src/lib/lang/en.json | 6 ++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/lib/components/messaging/ChatPreview.svelte b/src/lib/components/messaging/ChatPreview.svelte index dcb10ac52..7f460de44 100644 --- a/src/lib/components/messaging/ChatPreview.svelte +++ b/src/lib/components/messaging/ChatPreview.svelte @@ -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 @@ -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) diff --git a/src/lib/lang/en.json b/src/lib/lang/en.json index 49eb73e8b..28bc4e404 100644 --- a/src/lib/lang/en.json +++ b/src/lib/lang/en.json @@ -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",