- getBtcRates()/useBtcRates() read every BTC→fiat rate from the ONE Coinbase exchange-rates call the app already made, plus sats (1e8). No new endpoint. - The calculator's currency control is now a searchable Popover: sats + every fiat Coinbase reports, with a search box. Selecting a currency CONVERTS every money field (labour, rent, utilities, subscriptions, equipment cost, material order/shipping, packaging materials) by the live ratio; unitless fields (overhead factor, board counts, minutes, margins) are untouched. Falls back to relabel-only if rates aren't loaded. - Currency symbols/formats for the common fiats.
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { getBtcUsdRate, getBtcRates } from '@/lib/exchangeRateService';
|
|
|
|
/**
|
|
* Hook to fetch and cache BTC/USD exchange rate
|
|
*/
|
|
export function useExchangeRate() {
|
|
return useQuery({
|
|
queryKey: ['btc-usd-rate'],
|
|
queryFn: getBtcUsdRate,
|
|
staleTime: 60000, // Consider stale after 1 minute
|
|
refetchInterval: 120000, // Refetch every 2 minutes
|
|
retry: 2,
|
|
retryDelay: 1000,
|
|
});
|
|
}
|
|
|
|
/** All BTC → fiat rates (Coinbase) + `sats`. For multi-currency conversion. */
|
|
export function useBtcRates() {
|
|
return useQuery({
|
|
queryKey: ['btc-rates'],
|
|
queryFn: getBtcRates,
|
|
staleTime: 60000,
|
|
refetchInterval: 120000,
|
|
retry: 2,
|
|
retryDelay: 1000,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook to convert sats to USD with current exchange rate
|
|
*/
|
|
export function useSatsToUsd(sats: number | null | undefined) {
|
|
const { data: rate } = useExchangeRate();
|
|
|
|
return useMemo(() => {
|
|
if (!sats || !rate) return null;
|
|
return (sats / 100000000) * rate;
|
|
}, [sats, rate]);
|
|
}
|
|
|
|
/**
|
|
* Hook to convert USD to sats with current exchange rate
|
|
*/
|
|
export function useUsdToSats(usd: number | null | undefined) {
|
|
const { data: rate } = useExchangeRate();
|
|
|
|
return useMemo(() => {
|
|
if (!usd || !rate) return null;
|
|
return Math.floor((usd / rate) * 100000000);
|
|
}, [usd, rate]);
|
|
}
|