51 lines
1.7 KiB
Plaintext
51 lines
1.7 KiB
Plaintext
@using BTCPayApp.UI.Util
|
|
@using NBitcoin
|
|
|
|
@inject DisplayFormatter DisplayFormatter
|
|
|
|
<span @attributes="InputAttributes" @onclick="ChangeDisplayCurrency" @onclick:stopPropagation class="@CssClass">
|
|
@DisplayValue
|
|
</span>
|
|
|
|
@code {
|
|
[Parameter, EditorRequired]
|
|
public required long Sats { get; set; }
|
|
|
|
[Parameter, EditorRequired]
|
|
public required string Currency { get; set; }
|
|
|
|
[Parameter]
|
|
public decimal? Rate { get; set; }
|
|
|
|
[Parameter]
|
|
public EventCallback OnChangeDisplayCurrency { get; set; }
|
|
|
|
[Parameter(CaptureUnmatchedValues = true)]
|
|
public Dictionary<string, object>? InputAttributes { get; set; }
|
|
|
|
private async Task ChangeDisplayCurrency()
|
|
{
|
|
if (OnChangeDisplayCurrency.HasDelegate)
|
|
await OnChangeDisplayCurrency.InvokeAsync();
|
|
}
|
|
|
|
private bool CanDisplayFiat => !string.IsNullOrEmpty(Currency) && DisplayFormatter.HasCurrency(Currency) && Rate.HasValue;
|
|
private decimal Btc => new Money(Sats, MoneyUnit.Satoshi).ToDecimal(MoneyUnit.BTC);
|
|
private string CurrencyUnit => Currency switch
|
|
{
|
|
CurrencyDisplay.BTC => CurrencyDisplay.BTC,
|
|
CurrencyDisplay.SATS => CurrencyDisplay.SATS,
|
|
_ => CanDisplayFiat
|
|
? Currency
|
|
: CurrencyDisplay.SATS
|
|
};
|
|
private decimal CurrencyValue => CurrencyUnit switch
|
|
{
|
|
CurrencyDisplay.SATS => Sats,
|
|
CurrencyDisplay.BTC => Btc,
|
|
_ => CanDisplayFiat ? Btc * Rate!.Value : Sats
|
|
};
|
|
private string DisplayValue => DisplayFormatter.Currency(CurrencyValue, CurrencyUnit, DisplayFormatter.CurrencyFormat.Symbol);
|
|
private string? CssClass => InputAttributes?.ContainsKey("class") is true ? InputAttributes["class"].ToString() : null;
|
|
}
|