|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { useRecoilValue } from 'recoil'; |
| 3 | + |
| 4 | +import { localJoinDateState } from '../DailyParticipants'; |
| 5 | +import { useRoom } from './useRoom'; |
| 6 | + |
| 7 | +interface Countdown { |
| 8 | + hours: number; |
| 9 | + minutes: number; |
| 10 | + seconds: number; |
| 11 | +} |
| 12 | + |
| 13 | +interface Props { |
| 14 | + onCountdown?(countdown: Countdown): void; |
| 15 | +} |
| 16 | + |
| 17 | +export const useRoomExp = ({ onCountdown }: Props = {}) => { |
| 18 | + const localJoinDate = useRecoilValue(localJoinDateState); |
| 19 | + const room = useRoom(); |
| 20 | + |
| 21 | + const [ejectDate, setEjectDate] = useState<Date | null>(null); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + const ejectAfterElapsed = |
| 25 | + room?.tokenConfig?.eject_after_elapsed ?? |
| 26 | + room?.config?.eject_after_elapsed ?? |
| 27 | + 0; |
| 28 | + const expUTCTimeStamp = room?.tokenConfig?.exp ?? room?.config?.exp ?? 0; |
| 29 | + const ejectAtExp = |
| 30 | + room?.tokenConfig?.eject_at_token_exp ?? |
| 31 | + room?.config?.eject_at_room_exp ?? |
| 32 | + false; |
| 33 | + |
| 34 | + let newEjectDate: Date = new Date(0); |
| 35 | + |
| 36 | + if (ejectAfterElapsed && localJoinDate) { |
| 37 | + newEjectDate = new Date( |
| 38 | + localJoinDate.getTime() + 1000 * ejectAfterElapsed |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (ejectAtExp && expUTCTimeStamp) { |
| 43 | + const expDate = new Date(expUTCTimeStamp * 1000); |
| 44 | + if ( |
| 45 | + (newEjectDate.getTime() > 0 && expDate < newEjectDate) || |
| 46 | + !newEjectDate.getTime() |
| 47 | + ) |
| 48 | + newEjectDate = expDate; |
| 49 | + } |
| 50 | + |
| 51 | + if (newEjectDate.getTime() === 0) return; |
| 52 | + |
| 53 | + setEjectDate((oldEjectDate) => |
| 54 | + oldEjectDate?.getTime() !== newEjectDate.getTime() |
| 55 | + ? newEjectDate |
| 56 | + : oldEjectDate |
| 57 | + ); |
| 58 | + }, [ |
| 59 | + localJoinDate, |
| 60 | + room?.config?.eject_after_elapsed, |
| 61 | + room?.config?.eject_at_room_exp, |
| 62 | + room?.config?.exp, |
| 63 | + room?.tokenConfig?.eject_after_elapsed, |
| 64 | + room?.tokenConfig?.eject_at_token_exp, |
| 65 | + room?.tokenConfig?.exp, |
| 66 | + ]); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + if (!ejectDate || ejectDate.getTime() === 0) return; |
| 70 | + |
| 71 | + const interval = setInterval(() => { |
| 72 | + const eject = (ejectDate?.getTime() ?? 0) / 1000; |
| 73 | + const now = Date.now() / 1000; |
| 74 | + const diff = eject - now; |
| 75 | + if (diff < 0) return; |
| 76 | + const hours = Math.max(0, Math.floor(diff / 3600)); |
| 77 | + const minutes = Math.max(0, Math.floor((diff % 3600) / 60)); |
| 78 | + const seconds = Math.max(0, Math.floor(diff % 60)); |
| 79 | + onCountdown?.({ |
| 80 | + hours, |
| 81 | + minutes, |
| 82 | + seconds, |
| 83 | + }); |
| 84 | + }, 1000); |
| 85 | + return () => { |
| 86 | + clearInterval(interval); |
| 87 | + }; |
| 88 | + }, [ejectDate, onCountdown]); |
| 89 | + |
| 90 | + return { |
| 91 | + ejectDate, |
| 92 | + }; |
| 93 | +}; |
0 commit comments