|
| 1 | +import { Lecture } from '@/gql/graphql'; |
1 | 2 | import { DateTime } from 'luxon'; |
2 | 3 | import { useMemo, useState } from 'react'; |
3 | 4 | import useInterval from './useInterval'; |
4 | 5 |
|
5 | | -export const useCanJoinMeeting = (joinBeforeMinutes: number, start?: string, duration?: number) => { |
| 6 | +interface UseCanJoinMeetingProps { |
| 7 | + joinBeforeMinutes: number; |
| 8 | + appointment: Pick<Lecture, 'start' | 'duration'>; |
| 9 | +} |
| 10 | + |
| 11 | +export const useCanJoinMeeting = ({ joinBeforeMinutes, appointment }: UseCanJoinMeetingProps) => { |
6 | 12 | const [now, setNow] = useState(DateTime.now()); |
7 | 13 |
|
8 | 14 | useInterval(() => { |
9 | 15 | setNow(DateTime.now()); |
10 | 16 | }, 60000); |
11 | 17 |
|
12 | | - const valCanJoinMeeting = useMemo(() => canJoinMeeting(joinBeforeMinutes, now, start, duration), [duration, joinBeforeMinutes, start, now]) as boolean; |
| 18 | + const canJoinMeeting = useMemo(() => { |
| 19 | + const end = DateTime.fromISO(appointment.start).plus({ minutes: appointment.duration }); |
| 20 | + const isAppointmentInTheFuture = DateTime.now() <= end; |
| 21 | + // Allow users to join meetings that are already over. (only if it was in the last 30 days). |
| 22 | + if (!isAppointmentInTheFuture && DateTime.fromISO(appointment.start).diffNow('days').days > -30) return true; |
13 | 23 |
|
14 | | - return valCanJoinMeeting; |
15 | | -}; |
| 24 | + if (appointment.start && appointment.duration) { |
| 25 | + const startDate = DateTime.fromISO(appointment.start).minus({ minutes: joinBeforeMinutes }); |
| 26 | + const end = DateTime.fromISO(appointment.start).plus({ minutes: appointment.duration }); |
| 27 | + return now.toUnixInteger() >= startDate.toUnixInteger() && now.toUnixInteger() <= end.toUnixInteger(); |
| 28 | + } else { |
| 29 | + return false; |
| 30 | + } |
| 31 | + }, [appointment.duration, joinBeforeMinutes, appointment.start, now]) as boolean; |
16 | 32 |
|
17 | | -const canJoinMeeting = (joinBeforeMinutes: number, now: DateTime, start?: string, duration?: number): boolean => { |
18 | | - if (start && duration) { |
19 | | - const startDate = DateTime.fromISO(start).minus({ minutes: joinBeforeMinutes }); |
20 | | - const end = DateTime.fromISO(start).plus({ minutes: duration }); |
21 | | - return now.toUnixInteger() >= startDate.toUnixInteger() && now.toUnixInteger() <= end.toUnixInteger(); |
22 | | - } else { |
23 | | - return false; |
24 | | - } |
| 33 | + return canJoinMeeting; |
25 | 34 | }; |
0 commit comments