feat: add zap

This commit is contained in:
2024-03-11 15:36:17 +07:00
parent bb6badfed6
commit e6b97ab9ae
13 changed files with 349 additions and 451 deletions

View File

@@ -404,17 +404,26 @@ export class Ark {
try {
const cmd: boolean = await invoke("set_nwc", { uri });
return cmd;
} catch {
return false;
} catch (e) {
throw new Error(String(e));
}
}
public async nwc_status() {
public async load_nwc() {
try {
const cmd: boolean = await invoke("nwc_status");
const cmd: boolean = await invoke("load_nwc");
return cmd;
} catch {
return false;
} catch (e) {
throw new Error(String(e));
}
}
public async get_balance() {
try {
const cmd: number = await invoke("get_balance");
return cmd;
} catch (e) {
throw new Error(String(e));
}
}
@@ -422,8 +431,8 @@ export class Ark {
try {
const cmd: boolean = await invoke("zap_profile", { id, amount, message });
return cmd;
} catch {
return false;
} catch (e) {
throw new Error(String(e));
}
}
@@ -431,8 +440,8 @@ export class Ark {
try {
const cmd: boolean = await invoke("zap_event", { id, amount, message });
return cmd;
} catch {
return false;
} catch (e) {
throw new Error(String(e));
}
}
@@ -486,8 +495,7 @@ export class Ark {
return content.url as string;
} catch (e) {
console.error(String(e));
return null;
throw new Error(String(e));
}
}
@@ -549,7 +557,16 @@ export class Ark {
});
}
public open_zap() {
// todo
public open_zap(id: string, pubkey: string, account: string) {
return new WebviewWindow(`zap-${id}`, {
title: "Nostr Wallet Connect",
url: `/zap/${id}?pubkey=${pubkey}&account=${account}`,
minWidth: 400,
width: 400,
height: 500,
hiddenTitle: true,
titleBarStyle: "overlay",
fileDropEnabled: true,
});
}
}

View File

@@ -1,15 +1,22 @@
import { useArk } from "@lume/ark";
import { ZapIcon } from "@lume/icons";
import { toast } from "sonner";
import { useNoteContext } from "../provider";
export function NoteZap() {
const ark = useArk();
const event = useNoteContext();
const zap = async () => {
const nwc = await ark.nwc_status();
if (!nwc) {
ark.open_nwc();
} else {
ark.open_zap();
try {
const nwc = await ark.load_nwc();
if (!nwc) {
ark.open_nwc();
} else {
ark.open_zap(event.id, event.pubkey);
}
} catch (e) {
toast.error(String(e));
}
};

View File

@@ -9,6 +9,7 @@
},
"dependencies": {
"@tanstack/react-query": "^5.24.1",
"bitcoin-units": "^1.0.0",
"clsx": "^2.1.0",
"dayjs": "^1.11.10",
"nostr-tools": "^2.3.1",

View File

@@ -3,6 +3,7 @@ import relativeTime from "dayjs/plugin/relativeTime";
import updateLocale from "dayjs/plugin/updateLocale";
import { nip19 } from "nostr-tools";
import { AUDIOS, IMAGES, VIDEOS } from "./constants";
import { BitcoinUnit } from "bitcoin-units";
dayjs.extend(relativeTime);
dayjs.extend(updateLocale);
@@ -89,3 +90,34 @@ export function canPreview(text: string) {
return true;
}
// source: https://github.com/synonymdev/bitkit/blob/master/src/utils/displayValues/index.ts
export function getBitcoinDisplayValues(satoshis: number) {
let bitcoinFormatted = new BitcoinUnit(satoshis, "satoshis")
.getValue()
.toFixed(10)
.replace(/\.?0+$/, "");
const [bitcoinWhole, bitcoinDecimal] = bitcoinFormatted.split(".");
// format sats to group thousands
// 4000000 -> 4 000 000
let res = "";
bitcoinFormatted
.split("")
.reverse()
.forEach((c, index) => {
if (index > 0 && index % 3 === 0) {
res = " " + res;
}
res = c + res;
});
bitcoinFormatted = res;
return {
bitcoinFormatted,
bitcoinWhole,
bitcoinDecimal,
};
}