Skip to content

Commit

Permalink
Merge pull request #21 from ostyjs/fix-zap
Browse files Browse the repository at this point in the history
Fix zap
  • Loading branch information
sepehr-safari authored Jan 4, 2025
2 parents 82ccf1e + fe6e8f7 commit 878f102
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-osty",
"version": "0.5.8",
"version": "0.5.9",
"type": "module",
"license": "MIT",
"author": "Sepehr Safari",
Expand Down
41 changes: 23 additions & 18 deletions templates/react-shadcn/src/features/zap-widget/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NDKEvent, NDKTag, NDKUser } from '@nostr-dev-kit/ndk';
import { LnPayCb, NDKEvent, NDKTag, NDKUser, NDKZapper } from '@nostr-dev-kit/ndk';
import { useNdk, useRealtimeProfile } from 'nostr-hooks';
import { useState } from 'react';
import { useCallback, useState } from 'react';

import { useToast } from '@/shared/components/ui/use-toast';

Expand All @@ -19,7 +19,7 @@ export const useZapWidget = (target: NDKEvent | NDKUser | undefined) => {

const { profile } = useRealtimeProfile(target?.pubkey);

const process = () => {
const process = useCallback(() => {
if (!target || !ndk) return;

setProcessing(true);
Expand All @@ -33,25 +33,30 @@ export const useZapWidget = (target: NDKEvent | NDKUser | undefined) => {
const extraTags: NDKTag[] | undefined =
target instanceof NDKEvent ? [['e', target.id]] : undefined;

const ndkUser = ndk.getUser({ pubkey: target.pubkey });
ndkUser.zap(selectedAmount.amount * 1000, comment, extraTags).then((invoice) => {
if (typeof invoice === 'string') {
payInvoiceByWebln(invoice)
.then((res) => {
if (res) {
toast({ title: 'Successful ⚡️⚡️⚡️' });
setIsModalOpen(false);
} else {
toast({ title: 'Failed', variant: 'destructive' });
}
})
.finally(() => setProcessing(false));
const lnPay: LnPayCb = async ({ pr }) => {
const res = await payInvoiceByWebln(pr);

if (res) {
toast({ title: 'Successful ⚡️⚡️⚡️' });
setIsModalOpen(false);
} else {
toast({ title: 'Failed', variant: 'destructive' });
setProcessing(false);
}

setProcessing(false);

return res;
};

const zapper = new NDKZapper(target, selectedAmount.amount * 1000, 'msat', {
comment,
ndk,
lnPay,
tags: extraTags,
});
};

zapper.zap();
}, [target, ndk, selectedAmount, comment, toast, setIsModalOpen, setProcessing]);

return {
selectedAmount,
Expand Down
16 changes: 9 additions & 7 deletions templates/react-shadcn/src/features/zap-widget/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
export const payInvoiceByWebln = async (invoice: string): Promise<boolean> => {
import { NDKPaymentConfirmationLN } from '@nostr-dev-kit/ndk';

export const payInvoiceByWebln = async (
invoice: string,
): Promise<NDKPaymentConfirmationLN | undefined> => {
const { webln } = window as { webln?: any };

if (webln) {
try {
await webln.enable();

try {
await webln.sendPayment(invoice);

return true;
return (await webln.sendPayment(invoice)) as NDKPaymentConfirmationLN;
} catch (_) {
return false;
return undefined;
}
} catch (_) {
return false;
return undefined;
}
} else {
return false;
return undefined;
}
};

0 comments on commit 878f102

Please sign in to comment.