Skip to content

Commit

Permalink
last one
Browse files Browse the repository at this point in the history
  • Loading branch information
Jekrimo committed Dec 5, 2024
1 parent 023b8e5 commit cb0648c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/lib/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
"cancel_request": "Cancel Request",
"canceledRequest": "Canceled Request",
"network": "Network",
"payment_success": "Payment Success",
"payment_declined": "Payment Declined",
"you_canceled_request": "You Canceled Request",
"paymentCanceled": "Payment Canceled",
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,9 @@ export class Transfer {
return `/reject ${id} {"details":${transfer}}`
}

toDisplayString(kind: string, amount: string, to: string): string {
toDisplayString(kind: string, amount: string, messageId: string): string {
const transfer = JSON.stringify(this, (key, value) => (key === "amount" && typeof value === "bigint" ? value.toString() : value))
console.log(transfer)
// getValidPaymentRequest(transfer)
return `/send {"kind":"${kind}", "amount":"${amount}", "details":${transfer}}`
return `/send {"kind":"${kind}", "amount":"${amount}", "messageID":"${messageId}"}`
}

async execute() {
Expand Down
65 changes: 36 additions & 29 deletions src/routes/chat/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,11 @@
function sanitizePaymentSent(message: string, sender: string, receiver: string): string {
const jsonStartIndex = message.indexOf("{")
const jsonEndIndex = message.lastIndexOf("}")
if (jsonStartIndex === -1 || jsonEndIndex === -1 || jsonStartIndex > jsonEndIndex) {
return "Invalid message format"
}
const jsonPart = message.slice(jsonStartIndex, jsonEndIndex + 1)
const jsonPart = message.slice(jsonStartIndex, jsonEndIndex + 1).trim()
let parsedMessage
try {
parsedMessage = JSON.parse(jsonPart)
Expand All @@ -178,26 +176,12 @@
return "Invalid message format"
}
const kind = parsedMessage.kind || "unknown"
const amount = parsedMessage.amount || "unknown"
const amountPreview = parsedMessage.details?.amountPreview || "unknown"
let cleanedAmountPreview = amountPreview
if (amountPreview.includes(kind)) {
cleanedAmountPreview = amountPreview.replace(kind, "").trim()
}
cleanedAmountPreview = cleanedAmountPreview.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1")
if (cleanedAmountPreview === "unknown" && amount !== "unknown") {
cleanedAmountPreview = amount.replace(kind, "").trim()
cleanedAmountPreview = cleanedAmountPreview.replace(/(\.\d*?[1-9])0+$|\.0*$/, "$1")
}
const formattedAmount = cleanedAmountPreview
if (sender !== "") {
return `${sender} sent you ${formattedAmount}`
return `${sender} sent you ${amount}`
} else {
return `You sent ${formattedAmount} to ${$users[$activeChat.users[1]]?.name}`
return `You sent ${amount} to ${$users[$activeChat.users[1]]?.name}`
}
}
Expand Down Expand Up @@ -457,14 +441,37 @@
})
function checkForActiveRequest(message: MessageType, messageLine: string) {
const idMatch = messageLine.match(/^\/reject\s([a-f0-9-]{36})$/)
if (idMatch) {
const messageId = idMatch[1]
const rejectidMatch = messageLine.match(/^\/reject\s([a-f0-9-]{36})$/)
const sendidMatch = messageLine.match(/^\/send\s.*"messageID":"([a-f0-9-]{36})"/)
const requestMatch = messageLine.match(/^\/request\s.*"messageID":"([a-f0-9-]{36})"/)
console.log(messageLine)
if (rejectidMatch) {
const messageId = rejectidMatch[1]
let wasAdded = false
console.log("Reject Match:", message)
Store.state.paymentTracker.update(payments => {
const alreadyRejected = payments.some(payment => payment.messageId === messageId)
console.log(message, payments)
if (!alreadyRejected) {
wasAdded = true
return [...payments, { messageId, senderId: message.details.origin, rejectedPayment: true }]
}
return payments
})
return wasAdded
}
if (sendidMatch) {
const messageId = sendidMatch[1]
let wasAdded = false
// console.log(":SEND", message, messageId)
Store.state.paymentTracker.update(payments => {
const alreadyRejected = payments.some(payment => payment.messageId === messageId)
if (!alreadyRejected) {
wasAdded = true
return [...payments, { messageId, senderId: message.details.origin, rejectedPayment: false }]
Expand Down Expand Up @@ -939,7 +946,7 @@
<Icon icon={Shape.NoSymbol}></Icon>
</Button>
</div>
{:else if !checkForActiveRequest(message, line)}
{:else if !checkForActiveRequest(message, line) && !$rejectedPayments.some(payment => payment.messageId === message.id)}
<Text hook="text-chat-message" class="send_coin" markdown={$_("payments.sentRequest")}></Text>
<Button
hook="text-chat-message"
Expand All @@ -949,17 +956,17 @@
<Icon icon={Shape.XMark}></Icon>
</Button>
{:else}
<!-- <Text hook="text-chat-message" class="send_coin" markdown={$_("payments.sentRequest")}></Text>
<Text hook="text-chat-message" class="send_coin" markdown={$_("payments.sentRequest")}></Text>
<Button
hook="text-chat-message"
text={$_("payments.canceledRequest")}
text="Payments succenssfull;"
appearance={Appearance.Error}
on:click={async () => sendPaymentMessage(message, line, PaymentRequestsEnum.Reject)}>
<Icon icon={Shape.XMark}></Icon>
</Button> -->
</Button>
{/if}
{:else if line.startsWith(PaymentRequestsEnum.Send) && $rejectedPayments.find(payments => payments.messageId === message.id && payments.rejectedPayment)}
<Button hook="text-chat-message" disabled text={$_("payments.Success")} appearance={Appearance.Success} />
{:else if !line.startsWith(PaymentRequestsEnum.Send) && $rejectedPayments.some(payment => payment.messageId === message.id && !payment.rejectedPayment)}
<Button hook="text-chat-message" disabled text={$_("payments.payment_success")} appearance={Appearance.Success} />
{:else}
<Button hook="text-chat-message" disabled text={$_("payments.paymentCanceled")} appearance={Appearance.Error} />
{/if}
Expand Down

0 comments on commit cb0648c

Please sign in to comment.