Add bitcoin connect (#215)

* feat: add bitcoin connect

* feat: improve zap screen
This commit is contained in:
雨宮蓮
2024-06-21 14:56:10 +07:00
committed by GitHub
parent 1283432632
commit 3fbd66dece
17 changed files with 545 additions and 445 deletions

View File

@@ -1,6 +1,6 @@
import type { Metadata } from "@lume/types";
import { type Result, commands } from "./commands";
import { Window } from "@tauri-apps/api/window";
import { type Result, commands } from "./commands";
export class NostrAccount {
static async getAccounts() {
@@ -99,8 +99,28 @@ export class NostrAccount {
}
}
static async loadWallet() {
const query = await commands.loadWallet();
if (query.status === "ok") {
return Number.parseInt(query.data);
} else {
throw new Error(query.error);
}
}
static async setWallet(uri: string) {
const query = await commands.setNwc(uri);
const query = await commands.setWallet(uri);
if (query.status === "ok") {
return query.data;
} else {
throw new Error(query.error);
}
}
static async removeWallet() {
const query = await commands.removeWallet();
if (query.status === "ok") {
return query.data;
@@ -110,7 +130,7 @@ export class NostrAccount {
}
static async getProfile() {
const query = await commands.getCurrentUserProfile();
const query = await commands.getCurrentProfile();
if (query.status === "ok") {
return JSON.parse(query.data) as Metadata;
@@ -119,16 +139,6 @@ export class NostrAccount {
}
}
static async getBalance() {
const query = await commands.getBalance();
if (query.status === "ok") {
return Number.parseInt(query.data);
} else {
return 0;
}
}
static async getContactList() {
const query = await commands.getContactList();

View File

@@ -180,25 +180,25 @@ try {
else return { status: "error", error: e as any };
}
},
async setNwc(uri: string) : Promise<Result<boolean, string>> {
async setWallet(uri: string) : Promise<Result<boolean, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("set_nwc", { uri }) };
return { status: "ok", data: await TAURI_INVOKE("set_wallet", { uri }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async loadNwc() : Promise<Result<boolean, string>> {
async loadWallet() : Promise<Result<string, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("load_nwc") };
return { status: "ok", data: await TAURI_INVOKE("load_wallet") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
}
},
async getBalance() : Promise<Result<string, string>> {
async removeWallet() : Promise<Result<null, null>> {
try {
return { status: "ok", data: await TAURI_INVOKE("get_balance") };
return { status: "ok", data: await TAURI_INVOKE("remove_wallet") };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@@ -407,9 +407,9 @@ try {
else return { status: "error", error: e as any };
}
},
async openWindow(label: string, title: string, url: string, width: number, height: number) : Promise<Result<null, string>> {
async openWindow(window: Window) : Promise<Result<null, string>> {
try {
return { status: "ok", data: await TAURI_INVOKE("open_window", { label, title, url, width, height }) };
return { status: "ok", data: await TAURI_INVOKE("open_window", { window }) };
} catch (e) {
if(e instanceof Error) throw e;
else return { status: "error", error: e as any };
@@ -438,6 +438,7 @@ export type Meta = { content: string; images: string[]; videos: string[]; events
export type Relays = { connected: string[]; read: string[] | null; write: string[] | null; both: string[] | null }
export type RichEvent = { raw: string; parsed: Meta | null }
export type Settings = { proxy: string | null; image_resize_service: string | null; use_relay_hint: boolean; content_warning: boolean; display_avatar: boolean; display_zap_button: boolean; display_repost_button: boolean; display_media: boolean }
export type Window = { label: string; title: string; url: string; width: number; height: number; maximizable: boolean; minimizable: boolean }
/** tauri-specta globals **/

View File

@@ -18,7 +18,15 @@ export class LumeWindow {
const label = `event-${event.id}`;
const url = `/events/${root ?? reply ?? event.id}`;
const query = await commands.openWindow(label, "Thread", url, 500, 800);
const query = await commands.openWindow({
label,
url,
title: "Thread",
width: 500,
height: 800,
maximizable: true,
minimizable: true,
});
if (query.status === "ok") {
return query.data;
@@ -29,13 +37,15 @@ export class LumeWindow {
static async openProfile(pubkey: string) {
const label = `user-${pubkey}`;
const query = await commands.openWindow(
const query = await commands.openWindow({
label,
"Profile",
`/users/${pubkey}`,
500,
800,
);
url: `/users/${pubkey}`,
title: "Profile",
width: 500,
height: 800,
maximizable: true,
minimizable: true,
});
if (query.status === "ok") {
return query.data;
@@ -60,7 +70,15 @@ export class LumeWindow {
}
const label = `editor-${reply_to ? reply_to : 0}`;
const query = await commands.openWindow(label, "Editor", url, 560, 340);
const query = await commands.openWindow({
label,
url,
title: "Editor",
width: 560,
height: 340,
maximizable: true,
minimizable: false,
});
if (query.status === "ok") {
return query.data;
@@ -69,45 +87,35 @@ export class LumeWindow {
}
}
static async openZap(id: string, pubkey: string) {
const nwc = await commands.loadNwc();
static async openZap(id: string) {
const wallet = await commands.loadWallet();
if (nwc.status === "ok") {
const status = nwc.data;
if (!status) {
const label = "nwc";
await commands.openWindow(
label,
"Nostr Wallet Connect",
"/nwc",
400,
600,
);
} else {
const label = `zap-${id}`;
await commands.openWindow(
label,
"Zap",
`/zap/${id}?pubkey=${pubkey}`,
400,
500,
);
}
if (wallet.status === "ok") {
await commands.openWindow({
label: `zap-${id}`,
url: `/zap/${id}`,
title: "Zap",
width: 360,
height: 460,
maximizable: false,
minimizable: false,
});
} else {
throw new Error(nwc.error);
await LumeWindow.openSettings("bitcoin-connect");
}
}
static async openSettings() {
static async openSettings(path?: string) {
const label = "settings";
const query = await commands.openWindow(
const query = await commands.openWindow({
label,
"Settings",
"/settings/general",
800,
500,
);
url: path ? `/settings/${path}` : "/settings/general",
title: "Settings",
width: 800,
height: 500,
maximizable: false,
minimizable: false,
});
if (query.status === "ok") {
return query.data;
@@ -118,30 +126,15 @@ export class LumeWindow {
static async openSearch() {
const label = "search";
const query = await commands.openWindow(
const query = await commands.openWindow({
label,
"Search",
"/search",
400,
600,
);
if (query.status === "ok") {
return query.data;
} else {
throw new Error(query.error);
}
}
static async openActivity(account: string) {
const label = "activity";
const query = await commands.openWindow(
label,
"Activity",
`/activity/${account}/texts`,
400,
600,
);
url: "/search",
title: "Search",
width: 400,
height: 600,
maximizable: false,
minimizable: false,
});
if (query.status === "ok") {
return query.data;

View File

@@ -81,7 +81,7 @@ export function displayLongHandle(str: string) {
const handle = split[0];
const service = split[1];
return handle.substring(0, 16) + "..." + "@" + service;
return `${handle.substring(0, 16)}...@${service}`;
}
// convert number to K, M, B, T, etc.
@@ -127,7 +127,7 @@ export function getBitcoinDisplayValues(satoshis: number) {
.reverse()
.forEach((c, index) => {
if (index > 0 && index % 3 === 0) {
res = " " + res;
res = ` ${res}`;
}
res = c + res;
});