253 lines
9.2 KiB
TypeScript
253 lines
9.2 KiB
TypeScript
import clsx from 'clsx'
|
|
import { getBenefit } from 'common/supporter-config'
|
|
import { User } from 'common/user'
|
|
import { formatMoney } from 'common/util/format'
|
|
import { GiOpenChest, GiTwoCoins } from 'react-icons/gi'
|
|
import {
|
|
TbBulb,
|
|
TbChartBar,
|
|
TbClock,
|
|
TbCoin,
|
|
TbGift,
|
|
TbLock,
|
|
TbMoon,
|
|
TbSparkles,
|
|
TbTrendingUp,
|
|
} from 'react-icons/tb'
|
|
import { Col } from 'web/components/layout/col'
|
|
import { Modal } from 'web/components/layout/modal'
|
|
import { Row } from 'web/components/layout/row'
|
|
import { LoadingIndicator } from 'web/components/widgets/loading-indicator'
|
|
import { Tooltip } from 'web/components/widgets/tooltip'
|
|
import { useAPIGetter } from 'web/hooks/use-api-getter'
|
|
|
|
export function DailyFreeLoanModal(props: {
|
|
isOpen: boolean
|
|
setOpen: (open: boolean) => void
|
|
user: User
|
|
}) {
|
|
const { isOpen, setOpen, user } = props
|
|
|
|
const { data: freeLoanData } = useAPIGetter('get-free-loan-available', {
|
|
userId: user.id,
|
|
})
|
|
|
|
if (!freeLoanData) {
|
|
return (
|
|
<Modal open={isOpen} setOpen={setOpen}>
|
|
<Col className="bg-canvas-0 items-center justify-center rounded-md p-8">
|
|
<LoadingIndicator />
|
|
</Col>
|
|
</Modal>
|
|
)
|
|
}
|
|
|
|
const canClaim = freeLoanData.canClaim
|
|
const freeLoanAvailable = freeLoanData.available
|
|
const currentFreeLoan = freeLoanData.currentFreeLoan ?? 0
|
|
const currentMarginLoan = freeLoanData.currentMarginLoan ?? 0
|
|
const totalLoan = freeLoanData.totalLoan ?? 0
|
|
const todaysClaim = freeLoanData.todaysFreeLoan ?? 0
|
|
|
|
// Determine the reason for ineligibility
|
|
const alreadyClaimedToday = todaysClaim > 0
|
|
const atMaxLoanLimit =
|
|
freeLoanData.totalLoan >= freeLoanData.maxLoan && freeLoanData.maxLoan > 0
|
|
const noEligiblePositions =
|
|
!alreadyClaimedToday && !atMaxLoanLimit && freeLoanAvailable < 1
|
|
|
|
return (
|
|
<Modal open={isOpen} setOpen={setOpen} size="md">
|
|
<Col className="bg-canvas-0 text-ink-1000 overflow-hidden rounded-xl">
|
|
{/* Hero Section */}
|
|
<div
|
|
className={clsx(
|
|
'relative px-6 py-8',
|
|
canClaim && freeLoanAvailable >= 1
|
|
? 'bg-gradient-to-br from-amber-400 via-yellow-400 to-orange-400'
|
|
: alreadyClaimedToday
|
|
? 'bg-canvas-0'
|
|
: 'bg-gradient-to-br from-slate-400 via-slate-500 to-slate-600'
|
|
)}
|
|
>
|
|
{/* Decorative coins */}
|
|
{!alreadyClaimedToday && (
|
|
<div
|
|
aria-hidden="true"
|
|
className="pointer-events-none absolute inset-0 overflow-hidden opacity-20"
|
|
>
|
|
<GiTwoCoins className="absolute -right-4 -top-4 h-24 w-24" />
|
|
<TbCoin className="absolute -bottom-2 -left-2 h-16 w-16" />
|
|
</div>
|
|
)}
|
|
|
|
<Col className="relative z-10 items-center gap-4">
|
|
{/* Icon */}
|
|
<div
|
|
className={clsx(
|
|
'flex h-20 w-20 items-center justify-center rounded-full',
|
|
canClaim && freeLoanAvailable >= 1
|
|
? 'animate-pulse bg-white/30 shadow-lg shadow-amber-600/30'
|
|
: alreadyClaimedToday
|
|
? 'bg-amber-100 dark:bg-amber-900/40'
|
|
: 'bg-white/20'
|
|
)}
|
|
>
|
|
{alreadyClaimedToday ? (
|
|
<GiOpenChest className="h-12 w-12 text-amber-700 dark:text-amber-500" />
|
|
) : (
|
|
<GiTwoCoins className="h-12 w-12 text-white drop-shadow-md" />
|
|
)}
|
|
</div>
|
|
|
|
{/* Title */}
|
|
<Col className="items-center gap-1 text-center">
|
|
<h2
|
|
className={clsx(
|
|
'text-2xl font-bold',
|
|
alreadyClaimedToday
|
|
? 'text-ink-900'
|
|
: 'text-white drop-shadow-sm'
|
|
)}
|
|
>
|
|
Daily Loan
|
|
</h2>
|
|
<p
|
|
className={clsx(
|
|
'text-sm',
|
|
alreadyClaimedToday ? 'text-ink-600' : 'text-white/80'
|
|
)}
|
|
>
|
|
Free loans every day
|
|
</p>
|
|
</Col>
|
|
</Col>
|
|
</div>
|
|
|
|
{/* Stats Section */}
|
|
<div className="border-ink-100 border-b px-6 py-4">
|
|
<Row className="gap-4">
|
|
<Col className="flex-1 items-center gap-1 rounded-lg bg-gradient-to-br from-green-50 to-emerald-50 p-3 dark:from-green-900/20 dark:to-emerald-900/20">
|
|
<span className="text-xs font-medium text-green-600 dark:text-green-400">
|
|
CLAIMED TODAY
|
|
</span>
|
|
<span className="text-xl font-bold text-green-700 dark:text-green-300">
|
|
{formatMoney(todaysClaim)}
|
|
</span>
|
|
</Col>
|
|
<Col className="flex-1 items-center gap-1 rounded-lg bg-gradient-to-br from-blue-50 to-primary-50 p-3 dark:from-blue-900/20 dark:to-primary-900/20">
|
|
<span className="text-xs font-medium text-blue-600 dark:text-blue-400">
|
|
TOTAL LOAN
|
|
</span>
|
|
<span className="text-xl font-bold text-blue-700 dark:text-blue-300">
|
|
{formatMoney(totalLoan)}
|
|
</span>
|
|
</Col>
|
|
</Row>
|
|
<p className="text-ink-500 mt-2 text-center text-xs">
|
|
{totalLoan > 0 && (
|
|
<>
|
|
Free: {formatMoney(currentFreeLoan)} • Margin:{' '}
|
|
{formatMoney(currentMarginLoan)} •{' '}
|
|
</>
|
|
)}
|
|
Max: {formatMoney(freeLoanData.maxLoan)}
|
|
</p>
|
|
</div>
|
|
|
|
{/* Action Hint */}
|
|
{canClaim && freeLoanAvailable >= 1 && (
|
|
<div className="bg-amber-50 px-6 py-3 dark:bg-amber-900/20">
|
|
<p className="flex items-center justify-center gap-1 text-center text-sm font-medium text-amber-800 dark:text-amber-200">
|
|
<TbBulb aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
Click the{' '}
|
|
<span className="inline-flex items-center gap-1">
|
|
<GiTwoCoins className="inline h-4 w-4" /> coins
|
|
</span>{' '}
|
|
on the stats bar to claim!
|
|
</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Status message */}
|
|
{!(canClaim && freeLoanAvailable >= 1) && (
|
|
<div className="bg-canvas-50 px-6 py-3">
|
|
<p className="text-ink-600 flex items-center justify-center gap-1.5 text-center text-sm">
|
|
{atMaxLoanLimit ? (
|
|
<>
|
|
<TbLock aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span>
|
|
Max loan ({formatMoney(freeLoanData.maxLoan)}) reached. Repay
|
|
some to unlock more.
|
|
</span>
|
|
</>
|
|
) : noEligiblePositions ? (
|
|
<>
|
|
<TbChartBar aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span>
|
|
Invest in{' '}
|
|
<Tooltip text="Listed, ranked markets with 10+ traders that are 24+ hours old">
|
|
<span className="cursor-help underline decoration-dotted">
|
|
eligible markets
|
|
</span>
|
|
</Tooltip>{' '}
|
|
to start earning daily loans!
|
|
</span>
|
|
</>
|
|
) : alreadyClaimedToday ? (
|
|
<>
|
|
<TbMoon aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span>Next loan available after midnight PT</span>
|
|
</>
|
|
) : (
|
|
<>
|
|
<TbClock aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span>Come back tomorrow!</span>
|
|
</>
|
|
)}
|
|
</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* How it works */}
|
|
<Col className="gap-3 px-6 py-4">
|
|
<h3 className="text-ink-900 text-xs font-semibold uppercase tracking-wide">
|
|
How it works
|
|
</h3>
|
|
<Row className="gap-3">
|
|
<div className="bg-canvas-50 flex-1 rounded-lg p-3 text-center">
|
|
<TbTrendingUp
|
|
aria-hidden="true"
|
|
className="text-ink-700 mx-auto mb-1 h-6 w-6"
|
|
/>
|
|
<p className="text-ink-700 text-xs font-medium">
|
|
{Math.round(
|
|
getBenefit(user.entitlements, 'freeLoanRate') * 100
|
|
)}
|
|
% of positions
|
|
</p>
|
|
<p className="text-ink-500 text-xs">daily</p>
|
|
</div>
|
|
<div className="bg-canvas-50 flex-1 rounded-lg p-3 text-center">
|
|
<TbGift
|
|
aria-hidden="true"
|
|
className="text-ink-700 mx-auto mb-1 h-6 w-6"
|
|
/>
|
|
<p className="text-ink-700 text-xs font-medium">0% interest</p>
|
|
<p className="text-ink-500 text-xs">forever</p>
|
|
</div>
|
|
<div className="bg-canvas-50 flex-1 rounded-lg p-3 text-center">
|
|
<TbSparkles
|
|
aria-hidden="true"
|
|
className="text-ink-700 mx-auto mb-1 h-6 w-6"
|
|
/>
|
|
<p className="text-ink-700 text-xs font-medium">Auto-repaid</p>
|
|
<p className="text-ink-500 text-xs">on resolution</p>
|
|
</div>
|
|
</Row>
|
|
</Col>
|
|
</Col>
|
|
</Modal>
|
|
)
|
|
}
|