* fix small style issues * fix touch gap * fixing podfile * creating standardized amountIn function * show tx ids not amounts * use standardized amount in * use standardized amount in * fixed no payment information crash * downgrade rn and react-native-quick-crypto * only show margin if two elements exist * adding backup amount * wait till hompage to call analytics data * fix restore bug * updating firebase * fix check logic * wrap to handle thrown execptions * use utils package directly * adding error handling * group notification payments insted of handling them sequentually * handle payment requset on confirm page * improving split payment ux * adding translations * adding firebase sdk warning
42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
// Pure computation — same logic tested in __tests__/budgetWarningHook.test.js.
|
|
// Uses module-level NEAR_BUDGET_LIMIT / OVER_BUDGET_LIMIT constants rather than
|
|
|
|
import { useAnalytics } from '../../context-store/analyticsContext';
|
|
import { useGlobalContextProvider } from '../../context-store/context';
|
|
import { NEAR_BUDGET_LIMIT, OVER_BUDGET_LIMIT } from '../constants';
|
|
|
|
// accepting them as parameters (test file defines its own copy inline).
|
|
export function computeBudgetWarning(budget, spentTotal) {
|
|
try {
|
|
if (!budget || !budget.amount || budget.amount <= 0) {
|
|
return {
|
|
shouldWarn: false,
|
|
isOverBudget: false,
|
|
spentPercent: 0,
|
|
leftToSpend: 0,
|
|
};
|
|
}
|
|
const budgetAmount = budget.amount;
|
|
const spentPercent = spentTotal / budgetAmount;
|
|
const leftToSpend = Math.max(budgetAmount - spentTotal, 0);
|
|
const shouldWarn = spentPercent >= NEAR_BUDGET_LIMIT;
|
|
const isOverBudget = spentPercent >= OVER_BUDGET_LIMIT;
|
|
return { shouldWarn, isOverBudget, spentPercent, leftToSpend };
|
|
} catch (err) {
|
|
console.log('compute budget warning error', err);
|
|
return {
|
|
shouldWarn: false,
|
|
isOverBudget: false,
|
|
spentPercent: 0,
|
|
leftToSpend: 0,
|
|
};
|
|
}
|
|
}
|
|
|
|
export function useBudgetWarning(sendingAmount = 0) {
|
|
const { masterInfoObject } = useGlobalContextProvider();
|
|
const { spentTotal } = useAnalytics();
|
|
const budget = masterInfoObject?.monthlyBudget ?? null;
|
|
return computeBudgetWarning(budget, spentTotal + sendingAmount);
|
|
}
|