From 6c8f405cb831fcea8d775e1e688f2c55100d133a Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 30 Mar 2023 16:58:34 +0100 Subject: [PATCH] remove json from chat messages --- .../components/chat-dialog/chat-dialog.html | 9 +++- static/components/chat-dialog/chat-dialog.js | 44 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/static/components/chat-dialog/chat-dialog.html b/static/components/chat-dialog/chat-dialog.html index e163c25..a88395a 100644 --- a/static/components/chat-dialog/chat-dialog.html +++ b/static/components/chat-dialog/chat-dialog.html @@ -41,10 +41,15 @@ :name="message.sender" :text="[message.msg]" :sent="message.sender == 'Me'" - :bg-color="message.sender == 'Me' ? 'white' : 'light-green-2'" + :bg-color="message.json ? 'yellow-11' : message.sender == 'Me' ? 'white' : 'light-green-2'" :stamp="message.timestamp" size="6" - /> + > diff --git a/static/components/chat-dialog/chat-dialog.js b/static/components/chat-dialog/chat-dialog.js index 6cc42d9..83ed5ba 100644 --- a/static/components/chat-dialog/chat-dialog.js +++ b/static/components/chat-dialog/chat-dialog.js @@ -59,19 +59,41 @@ async function chatDialog(path) { }, computed: { sortedMessages() { - return this.nostrMessages.sort((a, b) => b.created_at - a.created_at) + return this.nostrMessages + .map(m => { + if (!isJson(m.msg)) return m + let msg = JSON.parse(m.msg) + if (msg?.message) { + m.json = m.msg + m.msg = msg.message + return m + } + if (msg?.items) { + m.json = m.msg + m.msg = 'Order placed!' + return m + } + if (msg?.payment_options) { + m.json = m.msg + m.msg = '⚡︎ Invoice sent!' + return m + } + return m + }) + .sort((a, b) => b.created_at - a.created_at) }, ordersList() { - let orders = this.nostrMessages - .sort((a, b) => b.created_at - a.created_at) - .filter(o => isJson(o.msg)) - .reduce((acc, cur) => { - const obj = JSON.parse(cur.msg) - const key = obj.id - const curGroup = acc[key] ?? {created_at: cur.timestamp} - return {...acc, [key]: {...curGroup, ...obj}} - }, {}) - return Object.values(orders) + return Object.values( + this.nostrMessages + .sort((a, b) => b.created_at - a.created_at) + .filter(o => isJson(o.msg)) + .reduce((acc, cur) => { + const obj = JSON.parse(cur.msg) + const key = obj.id + const curGroup = acc[key] ?? {created_at: cur.timestamp} + return {...acc, [key]: {...curGroup, ...obj}} + }, {}) + ) } }, methods: {