Skip to content

Commit

Permalink
Refactor zap
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowCait committed Oct 8, 2023
1 parent 2b982df commit 553483b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
36 changes: 17 additions & 19 deletions web/src/routes/ZapDialog.svelte
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
<script lang="ts">
import { nip57, type Event } from 'nostr-tools';
import { nip57 } from 'nostr-tools';
import QRCode from 'qrcode';
import { readRelays, writeRelays } from '../stores/Author';
import { createEventDispatcher, onMount } from 'svelte';
import { writeRelays } from '../stores/Author';
import { createEventDispatcher } from 'svelte';
import { Signer } from '$lib/Signer';
import type { User } from './types';
import { Api } from '$lib/Api';
import { pool } from '../stores/Pool';
import type { EventItem } from '$lib/Items';
export let event: Event;
export let eventItem: EventItem;
export function openZapDialog() {
console.log('[zap open]');
dialog.showModal();
}
let user: User | undefined;
let sats = 50;
let zapComment = '';
let invoice = '';
let dialog: HTMLDialogElement;
const dispatch = createEventDispatcher();
onMount(() => {
const api = new Api($pool, $readRelays);
api.fetchUserEvent(event.pubkey).then((userEvent) => {
user = userEvent?.user;
});
});
async function zap() {
const amount = sats * 1000;
const zapRequest = nip57.makeZapRequest({
profile: event.pubkey,
event: event.id,
profile: eventItem.event.pubkey,
event: eventItem.event.id,
amount,
comment: zapComment,
relays: $writeRelays
});
const zapRequestEvent = await Signer.signEvent(zapRequest);
console.log('[zap request]', zapRequestEvent, user);
console.log('[zap request]', zapRequestEvent, eventItem.metadata?.content);
const encoded = encodeURI(JSON.stringify(zapRequestEvent));
const url = `${user?.zapEndpoint}?amount=${amount}&nostr=${encoded}`;
const zapUrl = (await eventItem.metadata?.zapUrl()) ?? null;
if (zapUrl === null) {
console.error('[zap url not found]', eventItem.metadata?.content?.lud16);
return;
}
const url = `${zapUrl.href}?amount=${amount}&nostr=${encoded}`;
console.log('[zap url]', url);
const response = await fetch(url);
Expand Down Expand Up @@ -71,7 +67,9 @@
<dialog bind:this={dialog} on:click={closeZapDialog}>
<div class="zap-dialog">
{#if invoice === ''}
<div>@{user?.name ?? user?.display_name}</div>
<div>
@{eventItem.metadata?.content?.name ?? eventItem.metadata?.content?.display_name}
</div>
<form on:submit|preventDefault={zap}>
<div>
<input
Expand Down
7 changes: 4 additions & 3 deletions web/src/routes/timeline/Note.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { rom } from '../../stores/Author';
import CreatedAt from '../CreatedAt.svelte';
import { Api } from '$lib/Api';
import { getContext, onMount } from 'svelte';
import { onMount } from 'svelte';
import ZapDialog from '../ZapDialog.svelte';
import Content from '../content/Content.svelte';
import { Signer } from '$lib/Signer';
Expand All @@ -36,7 +36,8 @@
export let createdAtFormat: 'auto' | 'time' = 'auto';
export let full = false;
$: metadata = (item as EventItem).metadata;
$: eventItem = item as EventItem;
$: metadata = eventItem.metadata;
if ($rom) {
readonly = true;
Expand Down Expand Up @@ -273,7 +274,7 @@
</script>

<article class="timeline-item">
<ZapDialog event={item.event} bind:this={zapDialogComponent} on:zapped={onZapped} />
<ZapDialog {eventItem} bind:this={zapDialogComponent} on:zapped={onZapped} />
<div>
<a href="/{nip19.npubEncode(item.event.pubkey)}">
<img class="picture" src={metadata?.content?.picture} alt="" />
Expand Down

0 comments on commit 553483b

Please sign in to comment.