Format with prettier. Use screen mode for invitation import - dialog mode is broken.

This commit is contained in:
2026-03-23 10:15:48 +00:00
parent 7fd89c5663
commit b475b23beb
47 changed files with 1718 additions and 1098 deletions

View File

@@ -1,11 +1,11 @@
import { useState, useCallback, useMemo } from 'react';
import type { SelectableUTXO, VariableInput } from '../types.js';
import type { Invitation } from '../../../../services/invitation.js';
import { formatSatoshis } from '../../../theme.js';
import { useState, useCallback, useMemo } from "react";
import type { SelectableUTXO, VariableInput } from "../types.js";
import type { Invitation } from "../../../../services/invitation.js";
import { formatSatoshis } from "../../../theme.js";
import {
autoSelectGreedyUtxos,
mapUnspentOutputsToSelectable,
} from '../../../../utils/invitation-flow.js';
} from "../../../../utils/invitation-flow.js";
/**
* Manages UTXO selection state for the wizard's inputs step.
@@ -20,7 +20,10 @@ export function useUtxoSelection() {
const [fee, setFee] = useState<bigint>(500n);
const selectedAmount = useMemo(
() => availableUtxos.filter((u) => u.selected).reduce((sum, u) => sum + u.valueSatoshis, 0n),
() =>
availableUtxos
.filter((u) => u.selected)
.reduce((sum, u) => sum + u.valueSatoshis, 0n),
[availableUtxos],
);
@@ -55,38 +58,41 @@ export function useUtxoSelection() {
* Query the invitation instance for suitable UTXOs and auto-select
* greedily to meet the required amount.
*/
const loadUtxos = useCallback(async (
invitationInstance: Invitation,
templateIdentifier: string,
variables: VariableInput[],
setStatus: (msg: string) => void,
): Promise<void> => {
setStatus('Finding suitable UTXOs...');
const loadUtxos = useCallback(
async (
invitationInstance: Invitation,
templateIdentifier: string,
variables: VariableInput[],
setStatus: (msg: string) => void,
): Promise<void> => {
setStatus("Finding suitable UTXOs...");
// Derive required amount from variables that look like satoshi/amount fields.
const requestedVar = variables.find(
(v) =>
v.id.toLowerCase().includes('satoshi') ||
v.id.toLowerCase().includes('amount'),
);
const requested = requestedVar ? BigInt(requestedVar.value || '0') : 0n;
setRequiredAmount(requested);
// Derive required amount from variables that look like satoshi/amount fields.
const requestedVar = variables.find(
(v) =>
v.id.toLowerCase().includes("satoshi") ||
v.id.toLowerCase().includes("amount"),
);
const requested = requestedVar ? BigInt(requestedVar.value || "0") : 0n;
setRequiredAmount(requested);
const unspentOutputs = await invitationInstance.findSuitableResources({
templateIdentifier,
});
const unspentOutputs = await invitationInstance.findSuitableResources({
templateIdentifier,
});
const mapped = mapUnspentOutputsToSelectable(unspentOutputs);
const autoSelected = autoSelectGreedyUtxos(mapped, requested + fee);
setAvailableUtxos(autoSelected as SelectableUTXO[]);
setStatus('Ready');
}, [fee]);
const mapped = mapUnspentOutputsToSelectable(unspentOutputs);
const autoSelected = autoSelectGreedyUtxos(mapped, requested + fee);
setAvailableUtxos(autoSelected as SelectableUTXO[]);
setStatus("Ready");
},
[fee],
);
/** Validate that the selection meets the required amounts. */
const validate = useCallback((): string | null => {
const selected = availableUtxos.filter((u) => u.selected);
if (selected.length === 0) {
return 'Please select at least one UTXO';
return "Please select at least one UTXO";
}
if (selectedAmount < requiredAmount + fee) {
return `Insufficient funds. Need ${formatSatoshis(requiredAmount + fee)}, selected ${formatSatoshis(selectedAmount)}`;