131 lines
3.3 KiB
TypeScript
131 lines
3.3 KiB
TypeScript
import type {
|
|
HistoryItem,
|
|
WalletHistoryInput,
|
|
WalletHistoryItem,
|
|
WalletHistoryOutput,
|
|
} from "../services/history.js";
|
|
|
|
export type HistoryColorName =
|
|
| "info"
|
|
| "warning"
|
|
| "success"
|
|
| "error"
|
|
| "muted"
|
|
| "text";
|
|
|
|
export type HistoryRowType =
|
|
| "history_item"
|
|
| "history_input"
|
|
| "history_output";
|
|
|
|
export interface HistoryDisplayRow {
|
|
id: string;
|
|
type: HistoryRowType;
|
|
label: string;
|
|
description?: string;
|
|
timestamp?: number;
|
|
isNested: boolean;
|
|
valueSatoshis?: bigint;
|
|
reserved?: boolean;
|
|
input?: WalletHistoryInput;
|
|
output?: WalletHistoryOutput;
|
|
item?: WalletHistoryItem;
|
|
}
|
|
|
|
export function formatHistoryDate(timestamp?: number): string | undefined {
|
|
if (!timestamp) return undefined;
|
|
return new Date(timestamp).toLocaleDateString();
|
|
}
|
|
|
|
export function buildHistoryDisplayRows(
|
|
items: HistoryItem[],
|
|
): HistoryDisplayRow[] {
|
|
const rows: HistoryDisplayRow[] = [];
|
|
|
|
for (const item of items) {
|
|
const roles = item.roles.length > 0 ? item.roles.join(", ") : "unknown";
|
|
if (item.source === "utxo") {
|
|
for (const output of item.outputs) {
|
|
rows.push({
|
|
id: `${item.id}-output-${output.id}`,
|
|
type: "history_output",
|
|
label: output.outpoint
|
|
? `${output.outpoint.txid}:${output.outpoint.index}`
|
|
: output.outputIdentifier ?? "Output",
|
|
description: `${item.template} | ${roles} | ${output.description}`,
|
|
timestamp: item.createdAtTimestamp,
|
|
isNested: false,
|
|
valueSatoshis: output.valueSatoshis,
|
|
reserved: output.reserved,
|
|
output,
|
|
item,
|
|
});
|
|
}
|
|
continue;
|
|
}
|
|
|
|
rows.push({
|
|
id: item.id,
|
|
type: "history_item",
|
|
label: `${item.template} | ${roles} | ${item.description}`,
|
|
description: item.action,
|
|
timestamp: item.createdAtTimestamp,
|
|
isNested: false,
|
|
valueSatoshis: item.valueSatoshis,
|
|
item,
|
|
});
|
|
|
|
if (item.source !== "invitation") continue;
|
|
|
|
for (const input of item.inputs) {
|
|
rows.push({
|
|
id: `${item.id}-input-${input.id}`,
|
|
type: "history_input",
|
|
label: `${input.outpoint.txid}:${input.outpoint.index}`,
|
|
description: input.description,
|
|
isNested: true,
|
|
valueSatoshis: input.valueSatoshis,
|
|
input,
|
|
item,
|
|
});
|
|
}
|
|
|
|
for (const output of item.outputs) {
|
|
rows.push({
|
|
id: `${item.id}-output-${output.id}`,
|
|
type: "history_output",
|
|
label: output.outpoint
|
|
? `${output.outpoint.txid}:${output.outpoint.index}`
|
|
: output.outputIdentifier ?? "Output",
|
|
description: output.description,
|
|
isNested: true,
|
|
valueSatoshis: output.valueSatoshis,
|
|
reserved: output.reserved,
|
|
output,
|
|
item,
|
|
});
|
|
}
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
export function getHistoryItemColorName(
|
|
row: HistoryDisplayRow,
|
|
isSelected: boolean = false,
|
|
): HistoryColorName {
|
|
if (isSelected) return "info";
|
|
switch (row.type) {
|
|
case "history_input":
|
|
return "error";
|
|
case "history_output":
|
|
return row.reserved ? "warning" : "success";
|
|
case "history_item":
|
|
if ((row.valueSatoshis ?? 0n) < 0n) return "error";
|
|
if ((row.valueSatoshis ?? 0n) > 0n) return "success";
|
|
return "text";
|
|
default:
|
|
return "text";
|
|
}
|
|
}
|