feat: balance history

This commit is contained in:
kieran 2024-07-19 14:48:59 +01:00
parent 171ace38bd
commit bd312f553f
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
6 changed files with 63 additions and 1 deletions

View File

@ -56,6 +56,7 @@ export function NewStream({ ev, onFinish }: Omit<StreamEditorProps, "onFinish">
showEndpoints={false}
showEditor={true}
showForwards={false}
showBalanceHistory={false}
/>
);
}

View File

@ -0,0 +1,25 @@
import { BalanceHistoryResult, NostrStreamProvider } from "@/providers/zsz";
import { useEffect, useState } from "react";
import { FormattedMessage } from "react-intl";
export default function BalanceHistory({ provider }: { provider?: NostrStreamProvider }) {
const [page,] = useState(0);
const [rows, setRows] = useState<BalanceHistoryResult>();
useEffect(() => {
if (!provider) return;
provider.history(page)
.then(setRows);
}, []);
return <div className="grid auto-rows-auto grid-cols-3 gap-1">
<div><FormattedMessage defaultMessage="Time" /></div>
<div><FormattedMessage defaultMessage="Description" /></div>
<div><FormattedMessage defaultMessage="Amount" /></div>
{rows?.items.map(a => <>
<div>{new Date(a.created * 1000).toLocaleString()}</div>
<div>{a.desc}</div>
<div>{a.type === 0 ? "+" : "-"}{a.amount}</div>
</>)}
</div>
}

View File

@ -14,18 +14,21 @@ import { AddForwardInputs } from "./fowards";
import StreamKey from "./stream-key";
import AccountTopup from "./topup";
import AccountWithdrawl from "./withdraw";
import BalanceHistory from "./history";
export default function NostrProviderDialog({
provider,
showEndpoints,
showEditor,
showForwards,
showBalanceHistory,
...others
}: {
provider: NostrStreamProvider;
showEndpoints: boolean;
showEditor: boolean;
showForwards: boolean;
showBalanceHistory: boolean;
} & StreamEditorProps) {
const system = useContext(SnortContext);
const [topup, setTopup] = useState(false);
@ -257,11 +260,27 @@ export default function NostrProviderDialog({
);
}
function balanceHist() {
if (!info || !showBalanceHistory) return;
return (
<div className="flex flex-col gap-4">
<h3>
<FormattedMessage defaultMessage="Balance History" />
</h3>
<div className="flex flex-col gap-1">
<BalanceHistory provider={provider}/>
</div>
</div>
);
}
return (
<>
{showEndpoints && streamEndpoints()}
{streamEditor()}
{forwardInputs()}
{balanceHist()}
</>
);
}

View File

@ -24,6 +24,7 @@ export function DashboardSettingsButton({ ev }: { ev?: TaggedNostrEvent }) {
showEndpoints={true}
showForwards={true}
showEditor={false}
showBalanceHistory={false}
/>
</div>
</Modal>

View File

@ -17,6 +17,7 @@ export function StreamSettingsTab() {
showEndpoints={true}
showEditor={false}
showForwards={true}
showBalanceHistory={true}
/>
</div>
</>

View File

@ -92,7 +92,7 @@ export class NostrStreamProvider implements StreamProvider {
}
async withdraw(invoice: string) {
return await this.#getJson<{ fee: number; preimage: string }>("POST", `withdraw?invoice=${invoice}`);
return await this.#getJson<{ fee: number; preimage: string, error?: string }>("POST", `withdraw?invoice=${invoice}`);
}
async acceptTos(): Promise<void> {
@ -144,6 +144,10 @@ export class NostrStreamProvider implements StreamProvider {
return `${this.url}clip/${id}/${clipId}`;
}
async history(page = 0, pageSize = 100) {
return await this.#getJson<BalanceHistoryResult>("GET", `history?page=${page}&pageSize=${pageSize}`);
}
async #getJson<T>(method: "GET" | "POST" | "PATCH" | "DELETE", path: string, body?: unknown): Promise<T> {
const pub = (() => {
if (this.#publisher) {
@ -205,3 +209,14 @@ interface IngestEndpoint {
interface TopUpResponse {
pr: string;
}
export interface BalanceHistoryResult {
items: Array<{
created: number,
type: number,
amount: number,
desc?: string
}>
page: number,
pageSize: number
}