121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
import type {
|
|
HistoryItem,
|
|
HistoryInvitationItem,
|
|
HistoryUtxoItem,
|
|
} from "../services/history.js";
|
|
|
|
export type HistoryColorName =
|
|
| "info"
|
|
| "warning"
|
|
| "success"
|
|
| "error"
|
|
| "muted"
|
|
| "text";
|
|
|
|
export type HistoryRowType =
|
|
| "invitation"
|
|
| "invitation_input"
|
|
| "invitation_output"
|
|
| "utxo";
|
|
|
|
export interface HistoryDisplayRow {
|
|
id: string;
|
|
type: HistoryRowType;
|
|
label: string;
|
|
description?: string;
|
|
timestamp?: number;
|
|
isNested: boolean;
|
|
utxo?: HistoryUtxoItem;
|
|
invitation?: HistoryInvitationItem;
|
|
}
|
|
|
|
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) {
|
|
if (item.kind === "invitation") {
|
|
rows.push({
|
|
id: item.id,
|
|
type: "invitation",
|
|
label: item.description,
|
|
timestamp: item.createdAtTimestamp,
|
|
isNested: false,
|
|
invitation: item,
|
|
});
|
|
|
|
for (const input of item.inputs) {
|
|
const satsPrefix =
|
|
input.valueSatoshis !== undefined
|
|
? `${input.valueSatoshis.toLocaleString()} sats `
|
|
: "";
|
|
rows.push({
|
|
id: `${item.id}-input-${input.id}`,
|
|
type: "invitation_input",
|
|
label: `${satsPrefix}${input.outpoint.txid}:${input.outpoint.index}`,
|
|
description: input.description,
|
|
isNested: true,
|
|
utxo: input,
|
|
invitation: item,
|
|
});
|
|
}
|
|
|
|
for (const output of item.outputs) {
|
|
rows.push({
|
|
id: `${item.id}-output-${output.id}`,
|
|
type: "invitation_output",
|
|
label:
|
|
output.valueSatoshis !== undefined
|
|
? `${output.valueSatoshis.toLocaleString()} sats`
|
|
: "Output",
|
|
description: output.description,
|
|
isNested: true,
|
|
utxo: output,
|
|
invitation: item,
|
|
});
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
rows.push({
|
|
id: item.id,
|
|
type: "utxo",
|
|
label:
|
|
item.valueSatoshis !== undefined
|
|
? `${item.valueSatoshis.toLocaleString()} sats`
|
|
: "UTXO",
|
|
description: item.description,
|
|
isNested: false,
|
|
utxo: item,
|
|
});
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
export function getHistoryItemColorName(
|
|
row: HistoryDisplayRow,
|
|
isSelected: boolean = false,
|
|
): HistoryColorName {
|
|
if (isSelected) return "info";
|
|
switch (row.type) {
|
|
case "invitation":
|
|
return "text";
|
|
case "invitation_input":
|
|
return "error";
|
|
case "invitation_output":
|
|
return "success";
|
|
case "utxo":
|
|
return row.utxo?.reserved ? "warning" : "success";
|
|
default:
|
|
return "text";
|
|
}
|
|
}
|