254 lines
9.1 KiB
TypeScript
254 lines
9.1 KiB
TypeScript
import { Alert, Box, Button, Chip, Stack, ToggleButton, ToggleButtonGroup, Typography, alpha } from '@mui/material';
|
|
import { useTheme } from '@mui/material/styles';
|
|
import useMediaQuery from '@mui/material/useMediaQuery';
|
|
import TrendingDownRoundedIcon from '@mui/icons-material/TrendingDownRounded';
|
|
import TrendingUpRoundedIcon from '@mui/icons-material/TrendingUpRounded';
|
|
import LockRoundedIcon from '@mui/icons-material/LockRounded';
|
|
import { useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import type { CandleDirection, CandlePredictConfig, CandlePredictInterval, CandlePredictRound, StakeAccountKind } from '../types';
|
|
import { CustomStakeAmountField } from './CustomStakeAmountField';
|
|
import { StakeWalletSelector } from './StakeWalletSelector';
|
|
import { useServerAdjustedCountdown } from '../hooks/useServerAdjustedCountdown';
|
|
|
|
type Props = {
|
|
config: CandlePredictConfig;
|
|
interval: CandlePredictInterval;
|
|
round: CandlePredictRound | null;
|
|
selectedStake: number;
|
|
selectedDirection: CandleDirection | null;
|
|
stakeAccountKind: StakeAccountKind;
|
|
mainBalanceLabel: string;
|
|
bonusBalanceLabel: string;
|
|
testBalanceLabel: string;
|
|
onStakeChange: (amount: number) => void;
|
|
onDirectionChange: (direction: CandleDirection | null) => void;
|
|
onStakeAccountKindChange: (kind: StakeAccountKind) => void;
|
|
onSubmit: () => void;
|
|
submitting?: boolean;
|
|
hasPrediction?: boolean;
|
|
error?: string | null;
|
|
};
|
|
|
|
function isPresetStake(config: CandlePredictConfig, amount: number): boolean {
|
|
return config.stakeAmounts.some((a) => Math.abs(a - amount) < 1e-9);
|
|
}
|
|
|
|
/** Stake picker and short/long prediction controls. */
|
|
export function BettingPanel({
|
|
config,
|
|
interval,
|
|
round,
|
|
selectedStake,
|
|
selectedDirection,
|
|
stakeAccountKind,
|
|
mainBalanceLabel,
|
|
bonusBalanceLabel,
|
|
testBalanceLabel,
|
|
onStakeChange,
|
|
onDirectionChange,
|
|
onStakeAccountKindChange,
|
|
onSubmit,
|
|
submitting,
|
|
hasPrediction,
|
|
error,
|
|
}: Props) {
|
|
const { t } = useTranslation();
|
|
const theme = useTheme();
|
|
const isCompact = useMediaQuery(theme.breakpoints.down('sm'));
|
|
const { secondsToBettingEnd, secondsToBettingStart, bettingOpen: liveBettingOpen } =
|
|
useServerAdjustedCountdown(round);
|
|
|
|
const predictionOpen = liveBettingOpen && !hasPrediction && config.isActive;
|
|
const countdown = secondsToBettingEnd;
|
|
const untilOpen = secondsToBettingStart;
|
|
const minStake = config.minStakeAmount;
|
|
const maxStake = config.maxStakeAmount;
|
|
const [customStakeValid, setCustomStakeValid] = useState(true);
|
|
|
|
const formatWait = (totalSec: number) => {
|
|
const m = Math.floor(totalSec / 60);
|
|
const s = totalSec % 60;
|
|
return m > 0 ? `${m}m ${s}s` : `${s}s`;
|
|
};
|
|
|
|
const stakeOutOfRange =
|
|
selectedStake < minStake || selectedStake > maxStake || !Number.isFinite(selectedStake);
|
|
const stakeInvalid = stakeOutOfRange || !customStakeValid;
|
|
|
|
return (
|
|
<Box
|
|
sx={{
|
|
p: { xs: 2, md: 2.5 },
|
|
borderRadius: 3,
|
|
border: '1px solid rgba(255,255,255,0.08)',
|
|
background: 'linear-gradient(145deg, rgba(214,178,94,0.08), rgba(18,23,36,0.92))',
|
|
}}
|
|
>
|
|
<Stack spacing={2}>
|
|
<Box>
|
|
<Typography variant="h6" sx={{ fontWeight: 900 }}>
|
|
{t('candlePredict.betting.title')}
|
|
</Typography>
|
|
<Typography variant="body2" sx={{ opacity: 0.75, mt: 0.5 }}>
|
|
{t('candlePredict.betting.hint')}
|
|
</Typography>
|
|
</Box>
|
|
|
|
{round?.status === 'betting' && predictionOpen && (
|
|
<Chip
|
|
color="warning"
|
|
label={t('candlePredict.betting.targetRound', {
|
|
time: new Date(round.candleOpenMs).toLocaleTimeString(),
|
|
wait: formatWait(countdown),
|
|
})}
|
|
sx={{
|
|
alignSelf: 'flex-start',
|
|
fontWeight: 800,
|
|
maxWidth: '100%',
|
|
height: 'auto',
|
|
'& .MuiChip-label': { whiteSpace: 'normal', py: 0.5 },
|
|
}}
|
|
/>
|
|
)}
|
|
{round?.status === 'locked' && untilOpen > 0 && (
|
|
<Chip
|
|
color="info"
|
|
label={t('candlePredict.betting.predictionsOpenIn', { wait: formatWait(untilOpen) })}
|
|
sx={{ alignSelf: 'flex-start', fontWeight: 800 }}
|
|
/>
|
|
)}
|
|
{!config.isActive && (
|
|
<Alert severity="warning">{t('candlePredict.betting.gameDisabled')}</Alert>
|
|
)}
|
|
{hasPrediction && (
|
|
<Alert severity="success" icon={<LockRoundedIcon fontSize="inherit" />}>
|
|
{t('candlePredict.betting.predictionLocked')}
|
|
</Alert>
|
|
)}
|
|
{round?.status === 'locked' && !hasPrediction && (
|
|
<Alert severity="info">
|
|
{t('candlePredict.betting.candleInProgress', {
|
|
suffix: untilOpen > 0 ? ` (${formatWait(untilOpen)})` : '',
|
|
})}
|
|
</Alert>
|
|
)}
|
|
{round?.status === 'waiting' && (
|
|
<Alert severity="info">{t('candlePredict.betting.nextWindowOpens')}</Alert>
|
|
)}
|
|
{error && !hasPrediction && <Alert severity="error">{error}</Alert>}
|
|
|
|
<StakeWalletSelector
|
|
value={stakeAccountKind}
|
|
onChange={onStakeAccountKindChange}
|
|
mainBalanceLabel={mainBalanceLabel}
|
|
bonusBalanceLabel={bonusBalanceLabel}
|
|
testBalanceLabel={testBalanceLabel}
|
|
disabled={!predictionOpen}
|
|
/>
|
|
|
|
<Box>
|
|
<Typography variant="subtitle2" sx={{ fontWeight: 800, mb: 1 }}>
|
|
{t('candlePredict.betting.stakeAmount', {
|
|
code: config.walletTypeCode.replace('_', ' '),
|
|
})}
|
|
</Typography>
|
|
<Stack direction="row" spacing={1} useFlexGap sx={{ flexWrap: 'wrap', mb: 1.5 }}>
|
|
{config.stakeAmounts.map((amount) => (
|
|
<Chip
|
|
key={amount}
|
|
label={amount}
|
|
clickable
|
|
color={isPresetStake(config, selectedStake) && selectedStake === amount ? 'primary' : 'default'}
|
|
variant={
|
|
isPresetStake(config, selectedStake) && selectedStake === amount ? 'filled' : 'outlined'
|
|
}
|
|
onClick={() => {
|
|
setCustomStakeValid(true);
|
|
onStakeChange(amount);
|
|
}}
|
|
disabled={!predictionOpen}
|
|
sx={{ fontWeight: 800, minWidth: 48 }}
|
|
/>
|
|
))}
|
|
</Stack>
|
|
<CustomStakeAmountField
|
|
value={selectedStake}
|
|
minStake={minStake}
|
|
maxStake={maxStake}
|
|
disabled={!predictionOpen}
|
|
onValidChange={onStakeChange}
|
|
onValidityChange={setCustomStakeValid}
|
|
helperText={t('candlePredict.betting.stakeHelper', { min: minStake, max: maxStake })}
|
|
/>
|
|
</Box>
|
|
|
|
<Box>
|
|
<Typography variant="subtitle2" sx={{ fontWeight: 800, mb: 1 }}>
|
|
{t('candlePredict.betting.prediction')}
|
|
</Typography>
|
|
<ToggleButtonGroup
|
|
exclusive
|
|
value={selectedDirection}
|
|
onChange={(_, v) => onDirectionChange(v)}
|
|
fullWidth
|
|
disabled={!predictionOpen}
|
|
sx={{ gap: 1 }}
|
|
>
|
|
<ToggleButton
|
|
value="short"
|
|
sx={{
|
|
flex: 1,
|
|
py: 1.5,
|
|
fontWeight: 900,
|
|
borderRadius: '12px !important',
|
|
border: `1px solid ${alpha('#EF5350', 0.35)} !important`,
|
|
'&.Mui-selected': { backgroundColor: alpha('#EF5350', 0.2), color: '#EF5350' },
|
|
}}
|
|
>
|
|
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
|
<TrendingDownRoundedIcon />
|
|
<span>{t('candlePredict.betting.short')} ↓</span>
|
|
</Stack>
|
|
</ToggleButton>
|
|
<ToggleButton
|
|
value="long"
|
|
sx={{
|
|
flex: 1,
|
|
py: 1.5,
|
|
fontWeight: 900,
|
|
borderRadius: '12px !important',
|
|
border: `1px solid ${alpha('#4CAF50', 0.35)} !important`,
|
|
'&.Mui-selected': { backgroundColor: alpha('#4CAF50', 0.2), color: '#4CAF50' },
|
|
}}
|
|
>
|
|
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
|
<TrendingUpRoundedIcon />
|
|
<span>{t('candlePredict.betting.long')} ↑</span>
|
|
</Stack>
|
|
</ToggleButton>
|
|
</ToggleButtonGroup>
|
|
</Box>
|
|
|
|
<Button
|
|
variant="contained"
|
|
size="large"
|
|
disabled={!predictionOpen || !selectedDirection || submitting || stakeInvalid}
|
|
onClick={onSubmit}
|
|
sx={{ fontWeight: 900, py: 1.25, whiteSpace: 'normal', textAlign: 'center' }}
|
|
>
|
|
{isCompact
|
|
? t('candlePredict.betting.confirmCompact', {
|
|
stake: selectedStake,
|
|
multiplier: interval.payoutMultiplier,
|
|
})
|
|
: t('candlePredict.betting.confirmFull', {
|
|
stake: selectedStake,
|
|
multiplier: interval.payoutMultiplier,
|
|
})}
|
|
</Button>
|
|
</Stack>
|
|
</Box>
|
|
);
|
|
}
|