From f2fa67a1a909d1ffca17d5f51ce3c50f37215883 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 18 Aug 2024 14:18:41 +0800 Subject: [PATCH] problem: not clear when someone makes a purchase resolve https://github.com/nostrocket/hypergolic/issues/94 --- src/components/Pie.svelte | 8 +++- src/components/PruchaseToast.svelte | 48 +++++++++++++++++++++ src/components/RocketDashboard.svelte | 60 +++++++++++++++++++++++++++ src/routes/+layout.svelte | 15 ++++--- 4 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 src/components/PruchaseToast.svelte diff --git a/src/components/Pie.svelte b/src/components/Pie.svelte index c8554c3..9678006 100644 --- a/src/components/Pie.svelte +++ b/src/components/Pie.svelte @@ -2,6 +2,7 @@ import { ndk } from '@/ndk.js'; import { Chart } from 'flowbite-svelte'; import { writable } from 'svelte/store'; + import { Toaster } from '$lib/components/ui/sonner'; export let data: { pubkey: string; merits: number; sats: number }[]; let pubkeys = Array.from(data, (x) => x.pubkey); @@ -130,4 +131,9 @@ // - +
+ +
+ +
+
diff --git a/src/components/PruchaseToast.svelte b/src/components/PruchaseToast.svelte new file mode 100644 index 0000000..01de3b0 --- /dev/null +++ b/src/components/PruchaseToast.svelte @@ -0,0 +1,48 @@ + + +
+ {#if zapPurchase.ZapReceipt.content} +
{zapPurchase.ZapReceipt.content}
+ {:else} + + {#await fetchEvent(zapPurchase.ProductID, $ndk)} +
New purchase
+ {:then product} +
{`Purchase of ${new Product(product).Name()} from ${rocket.Name()}.`}
+ {/await} + {/if} + {#if zapPurchase.BuyerPubkey} +
+ +
+ {/if} + {#if zapPurchase.Amount} +
+ Amount: {(zapPurchase.Amount / 1000).toFixed(0)} + {(zapPurchase.Amount / 1000).toFixed(0) === '1' ? 'sat' : 'sats'} +
+ {/if} + {#if zapPurchase.ZapReceipt.created_at} +
+ {unixToRelativeTime(zapPurchase.ZapReceipt.created_at * 1000)} +
+ {/if} +
diff --git a/src/components/RocketDashboard.svelte b/src/components/RocketDashboard.svelte index a56ab36..d85b1e2 100644 --- a/src/components/RocketDashboard.svelte +++ b/src/components/RocketDashboard.svelte @@ -2,8 +2,10 @@ import * as Breadcrumb from '$lib/components/ui/breadcrumb/index.js'; import Button from '@/components/ui/button/button.svelte'; import * as Card from '@/components/ui/card'; + import PurchaseToast from './PruchaseToast.svelte'; import { Rocket, ZapPurchase } from '@/event_helpers/rockets'; import { devmode } from '@/stores/session'; + import { toast } from 'svelte-sonner'; import type { NDKEvent } from '@nostr-dev-kit/ndk'; import BitcoinAssociations from './AssociatedBitcoinAddresses.svelte'; import MeritRequests from './MeritRequests.svelte'; @@ -11,10 +13,47 @@ import ProductFomo from './ProductFomo.svelte'; import ProposedProducts from './ProposedProducts.svelte'; import UpdateMission from './UpdateMission.svelte'; + import { onMount } from 'svelte'; export let rocket: NDKEvent; $: unratifiedZaps = new Map(); + let lastCheckTime = Date.now() / 1000; // Current time in seconds + + function checkNewZaps() { + const currentTime = Date.now() / 1000; + const recentZaps = Array.from(unratifiedZaps.values()).filter( + (zap) => + zap.ZapReceipt.created_at && + zap.ZapReceipt.created_at > lastCheckTime && + zap.ZapReceipt.created_at <= currentTime + ); + + recentZaps.forEach((zapPurchase) => { + toast(PurchaseToast, { + componentProps: { + zapPurchase, + rocket: new Rocket(rocket) + } + }); + }); + + lastCheckTime = currentTime; + } + $: { + if (unratifiedZaps.size > 0) { + checkNewZaps(); + } + } + onMount(() => { + lastCheckTime = Date.now() / 1000 - 30; // 30 seconds ago + }); + + $: lasted = Array.from(unratifiedZaps.values()).sort((a, b) => { + if (a.ZapReceipt.created_at && b.ZapReceipt.created_at) { + return b.ZapReceipt.created_at - a.ZapReceipt.created_at; + } else return 0; + })[0];
@@ -32,6 +71,27 @@
+ {#if $devmode} + + + {/if} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index bd96b84..9a6445a 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -27,16 +27,19 @@ sessionStarted = true; } - onMount(()=>{getBitcoinTip();}) - - setInterval(function () { + onMount(() => { getBitcoinTip(); - }, 2* 60 * 1000); + }); + + setInterval( + function () { + getBitcoinTip(); + }, + 2 * 60 * 1000 + );
- -