import React from 'react'; import { RingProgress, Text, useMantineTheme, keyframes, Stack, createStyles } from '@mantine/core'; import { useNuiEvent } from '../../hooks/useNuiEvent'; import { fetchNui } from '../../utils/fetchNui'; import ScaleFade from '../../transitions/ScaleFade'; export interface CircleProgressbarProps { label?: string; duration: number; position?: 'middle' | 'bottom'; percent?: boolean; } // 33.5 is the r of the circle const progressCircle = keyframes({ '0%': { strokeDasharray: `0, ${33.5 * 2 * Math.PI}` }, '100%': { strokeDasharray: `${33.5 * 2 * Math.PI}, 0` }, }); const useStyles = createStyles((theme, params: { position: 'middle' | 'bottom'; duration: number }) => ({ container: { width: '100%', height: params.position === 'middle' ? '100%' : '20%', bottom: 0, position: 'absolute', display: 'flex', justifyContent: 'center', alignItems: 'center', }, progress: { '> svg > circle:nth-child(1)': { stroke: theme.colors.dark[5], }, // Scuffed way of grabbing the first section and animating it '> svg > circle:nth-child(2)': { transition: 'none', animation: `${progressCircle} linear forwards`, animationDuration: `${params.duration}ms`, }, }, value: { textAlign: 'center', fontFamily: 'roboto-mono', textShadow: theme.shadows.sm, color: theme.colors.gray[3], }, label: { textAlign: 'center', textShadow: theme.shadows.sm, color: theme.colors.gray[3], }, })); const CircleProgressbar: React.FC = () => { const [visible, setVisible] = React.useState(false); const [progressDuration, setProgressDuration] = React.useState(0); const [position, setPosition] = React.useState<'middle' | 'bottom'>('middle'); const [value, setValue] = React.useState(0); const [label, setLabel] = React.useState(''); const theme = useMantineTheme(); const { classes } = useStyles({ position, duration: progressDuration }); const progressComplete = () => { setVisible(false); fetchNui('progressComplete'); }; const progressCancel = () => { setValue(99); // Sets the final value to 100% kek setVisible(false); }; useNuiEvent('progressCancel', progressCancel); useNuiEvent('circleProgress', (data) => { if (visible) return; setVisible(true); setValue(0); setLabel(data.label || ''); setProgressDuration(data.duration); setPosition(data.position || 'middle'); const onePercent = data.duration * 0.01; const updateProgress = setInterval(() => { setValue((previousValue) => { const newValue = previousValue + 1; newValue >= 100 && clearInterval(updateProgress); return newValue; }); }, onePercent); }); return ( <> {value}%} /> {label && {label}} ); }; export default CircleProgressbar;