import { content } from "@deadhead/content"; import { useState } from "react"; import { formatCredits } from "../format"; import { errorMessage, useGameAction, useLoadedPlayer } from "../state/actions"; import { Button, Meter, Panel } from "../ui"; /** Why a pay-down amount cannot be sent, or null if it can. */ export function payDownProblem(amount: number, credits: number, balance: number): string | null { if (!Number.isInteger(amount) || amount <= 0) return "Enter a whole number of credits."; if (amount > balance) return `You only owe ${formatCredits(balance)}.`; if (amount > credits) return `You only have ${formatCredits(credits)}.`; return null; } /** The deal's loan on HQ: balance, paid-off progress and paying it down (Step 10A.14). */ export function LoanCard() { const state = useLoadedPlayer(); const repay = useGameAction("repayLoan"); const [input, setInput] = useState(""); const { credits, loanBalance } = state.company; const total = content.config.dealLoanAmount; const paidOff = Math.max(0, Math.min(100, Math.floor(((total - loanBalance) / total) * 100))); const amount = Number(input); const problem = input === "" ? null : payDownProblem(amount, credits, loanBalance); return (

Loan from Halvorsen

{formatCredits(loanBalance)} left

{Math.round(content.config.loanRepaymentShare * 100)}% of every payout goes to the lender until it's paid off.

{paidOff}% paid off

{ e.preventDefault(); if (input === "" || problem) return; repay.mutate({ amount }, { onSuccess: () => setInput("") }); }} > setInput(e.target.value)} className="min-h-11 min-w-0 flex-1 rounded-lg border border-space-600 bg-space-950/70 px-3 font-display tabular-nums placeholder:text-ink-500 focus:border-amber-400 focus:outline-none" />
{problem &&

{problem}

} {repay.isError && (

{errorMessage(repay.error)}

)}
); }