import { useQuery } from '@tanstack/react-query'; import axios from 'axios'; import get from 'lodash.get'; import { createMoney, isUndefined } from '@leather-wallet/utils'; import type { ActiveFiatProvider, HiroMessage, RemoteConfig } from '../../../types/remote-config'; import { LeatherEnvironment, useLeatherEnv, useLeatherGithub } from '../../leather-query-provider'; function fetchLeatherMessages(env: string, leatherGh: LeatherEnvironment['github']) { const IS_DEV_ENV = env === 'development'; const IS_TESTING_ENV = env === 'testing'; // TODO: BRANCH_NAME is not working here for config changes on PR branches // Playwright tests fail with config changes not on main const defaultBranch = IS_DEV_ENV || IS_TESTING_ENV ? 'dev' : 'main'; const githubWalletConfigRawUrl = `https://raw.githubusercontent.com/${leatherGh.org}/${leatherGh.repo}/${ leatherGh.branchName || defaultBranch }/config/wallet-config.json`; return async function fetchLeatherMessagesImpl(): Promise { if (leatherGh.localConfig && (IS_DEV_ENV || IS_TESTING_ENV)) { return leatherGh.localConfig; } const resp = await axios.get(githubWalletConfigRawUrl); return resp.data; }; } export function useRemoteConfig() { const env = useLeatherEnv(); const leatherGh = useLeatherGithub(); const { data } = useQuery({ queryKey: ['walletConfig'], queryFn: fetchLeatherMessages(env, leatherGh), // As we're fetching from Github, a third-party, we want // to avoid any unnecessary stress on their services, so // we use quite slow stale/retry times staleTime: 1000 * 60 * 10, retryDelay: 1000 * 60, }); return data; } export function useRemoteLeatherMessages(): HiroMessage[] { const config = useRemoteConfig(); return get(config, 'messages.global', []); } export function useActiveFiatProviders() { const config = useRemoteConfig(); if (!config?.activeFiatProviders) return {} as Record; return Object.fromEntries( Object.entries(config.activeFiatProviders).filter(([_, provider]) => provider.enabled) ); } export function useHasFiatProviders() { const activeProviders = useActiveFiatProviders(); return ( activeProviders && Object.keys(activeProviders).reduce((acc, key) => activeProviders[key].enabled || acc, false) ); } export function useRecoverUninscribedTaprootUtxosFeatureEnabled() { const config = useRemoteConfig(); return get(config, 'recoverUninscribedTaprootUtxosFeatureEnabled', false); } export function useConfigFeeEstimationsMaxEnabled() { const config = useRemoteConfig(); if (isUndefined(config) || isUndefined(config?.feeEstimationsMinMax)) return; return config.feeEstimationsMinMax.maxValuesEnabled; } export function useConfigFeeEstimationsMaxValues() { const config = useRemoteConfig(); if (typeof config?.feeEstimationsMinMax === 'undefined') return; if (!config.feeEstimationsMinMax.maxValues) return; if (!Array.isArray(config.feeEstimationsMinMax.maxValues)) return; return config.feeEstimationsMinMax.maxValues.map(value => createMoney(value, 'STX')); } export function useConfigFeeEstimationsMinEnabled() { const config = useRemoteConfig(); if (isUndefined(config) || isUndefined(config?.feeEstimationsMinMax)) return; return config.feeEstimationsMinMax.minValuesEnabled; } export function useConfigFeeEstimationsMinValues() { const config = useRemoteConfig(); if (typeof config?.feeEstimationsMinMax === 'undefined') return; if (!config.feeEstimationsMinMax.minValues) return; if (!Array.isArray(config.feeEstimationsMinMax.minValues)) return; return config.feeEstimationsMinMax.minValues.map(value => createMoney(value, 'STX')); } export function useConfigNftMetadataEnabled() { const config = useRemoteConfig(); return config?.nftMetadataEnabled ?? true; } export function useConfigOrdinalsbot() { const config = useRemoteConfig(); return { integrationEnabled: config?.ordinalsbot?.integrationEnabled ?? true, mainnetApiUrl: config?.ordinalsbot?.mainnetApiUrl ?? 'https://api2.ordinalsbot.com', signetApiUrl: config?.ordinalsbot?.signetApiUrl ?? 'https://signet.ordinalsbot.com', }; } export function useConfigRunesEnabled() { const config = useRemoteConfig(); return get(config, 'runesEnabled', false); } export function useConfigSwapEnabled() { const config = useRemoteConfig(); return get(config, 'swapEnabled', false); }