Skip to content

Commit

Permalink
fix: avoid empty currency selector initially (#1088)
Browse files Browse the repository at this point in the history
* fix: default currency to usd for new hubs

* chore: default currency to uppercase

* chore: remove uppercase

* feat: add currency to error logs at function level for getBitcoinRate

* chore: use info.currency instead of state variable

* chore: filter out satoshi from fiat currencies

* feat: hide fiat currency if currency is SATS

---------

Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent 27d94ae commit 11908ce
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
22 changes: 17 additions & 5 deletions alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,19 +1150,26 @@ func (svc *albyOAuthService) GetChannelPeerSuggestions(ctx context.Context) ([]C

func (svc *albyOAuthService) GetBitcoinRate(ctx context.Context) (*BitcoinRate, error) {
client := &http.Client{Timeout: 10 * time.Second}
currency := svc.cfg.GetCurrency()

url := fmt.Sprintf("%s/rates/%s", albyInternalAPIURL, svc.cfg.GetCurrency())
url := fmt.Sprintf("%s/rates/%s", albyInternalAPIURL, currency)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
logger.Logger.WithError(err).Error("Error creating request to Bitcoin rate endpoint")
logger.Logger.WithFields(logrus.Fields{
"currency": currency,
"error": err,
}).Error("Error creating request to Bitcoin rate endpoint")
return nil, err
}
setDefaultRequestHeaders(req)

res, err := client.Do(req)
if err != nil {
logger.Logger.WithError(err).Error("Failed to fetch Bitcoin rate from API")
logger.Logger.WithFields(logrus.Fields{
"currency": currency,
"error": err,
}).Error("Failed to fetch Bitcoin rate from API")
return nil, err
}

Expand All @@ -1178,16 +1185,21 @@ func (svc *albyOAuthService) GetBitcoinRate(ctx context.Context) (*BitcoinRate,

if res.StatusCode >= 300 {
logger.Logger.WithFields(logrus.Fields{
"currency": currency,
"body": string(body),
"statusCode": res.StatusCode,
}).Error("bitcoin rate endpoint returned non-success code")
}).Error("Bitcoin rate endpoint returned non-success code")
return nil, fmt.Errorf("bitcoin rate endpoint returned non-success code: %s", string(body))
}

var rate = &BitcoinRate{}
err = json.Unmarshal(body, rate)
if err != nil {
logger.Logger.WithField("body", string(body)).WithError(err).Error("Failed to decode Bitcoin rate API response")
logger.Logger.WithFields(logrus.Fields{
"currency": currency,
"body": string(body),
"error": err,
}).Error("Failed to decode Bitcoin rate API response")
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func randomHex(n int) (string, error) {
return hex.EncodeToString(bytes), nil
}

const defaultCurrency = "usd"
const defaultCurrency = "USD"

func (cfg *config) GetCurrency() string {
currency, err := cfg.Get("Currency", "")
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/FormattedFiatAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default function FormattedFiatAmount({
const { data: info } = useInfo();
const { data: bitcoinRate } = useBitcoinRate();

if (info?.currency === "SATS") {
return null;
}

if (!bitcoinRate) {
return (
<div className="animate-pulse h-2.5 bg-muted-foreground rounded-full w-16 my-1 inline-block"></div>
Expand Down
28 changes: 9 additions & 19 deletions frontend/src/screens/settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from "react";
import Loading from "src/components/Loading";
import SettingsHeader from "src/components/SettingsHeader";
import { Label } from "src/components/ui/label";
import {
Expand All @@ -25,17 +26,7 @@ function Settings() {

const [fiatCurrencies, setFiatCurrencies] = useState<[string, string][]>([]);

const { data: info } = useInfo();

const [selectedCurrency, setSelectedCurrency] = useState<string | undefined>(
info?.currency
);

useEffect(() => {
if (info?.currency) {
setSelectedCurrency(info.currency.toUpperCase());
}
}, [info]);
const { data: info, mutate: reloadInfo } = useInfo();

useEffect(() => {
async function fetchCurrencies() {
Expand Down Expand Up @@ -68,12 +59,18 @@ function Settings() {
},
body: JSON.stringify({ currency }),
});
await reloadInfo();
toast({ title: `Currency set to ${currency}` });
} catch (error) {
console.error(error);
handleRequestError(toast, "Failed to update currencies", error);
}
}

if (!info) {
return <Loading />;
}

return (
<>
<SettingsHeader title="General" description="General Alby Hub Settings" />
Expand Down Expand Up @@ -120,14 +117,7 @@ function Settings() {
</div>
<div className="grid gap-1.5">
<Label htmlFor="currency">Fiat Currency</Label>
<Select
value={selectedCurrency}
onValueChange={async (value) => {
setSelectedCurrency(value);
await updateCurrency(value);
toast({ title: `Currency set to ${value}` });
}}
>
<Select value={info?.currency} onValueChange={updateCurrency}>
<SelectTrigger className="w-[250px] border border-gray-300 p-2 rounded-md">
<SelectValue placeholder="Select a currency" />
</SelectTrigger>
Expand Down
7 changes: 3 additions & 4 deletions wails/wails_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,9 @@ func (app *WailsApp) WailsRequestRouter(route string, method string, body string
rate, err := app.svc.GetAlbyOAuthSvc().GetBitcoinRate(ctx)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"route": route,
"method": method,
"body": body,
"currency": "usd",
"route": route,
"method": method,
"body": body,
}).WithError(err).Error("Failed to get Bitcoin rate")
return WailsRequestRouterResponse{Body: nil, Error: err.Error()}
}
Expand Down

0 comments on commit 11908ce

Please sign in to comment.