|
| 1 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 2 | +import { |
| 3 | + AssignmentNotificationStatus, |
| 4 | + disableAssignmentNotifications, |
| 5 | + enableAssignmentNotifications, |
| 6 | + getAssignmentNotificationStatus, |
| 7 | + isAssignmentNotificationsEnabled, |
| 8 | +} from '@/lib/notifications/assignmentNotifications'; |
| 9 | + |
| 10 | +interface UseAssignmentNotificationsParams { |
| 11 | + competitions: ApiCompetition[]; |
| 12 | + user: User | null; |
| 13 | +} |
| 14 | + |
| 15 | +export function useAssignmentNotifications({ |
| 16 | + competitions, |
| 17 | + user, |
| 18 | +}: UseAssignmentNotificationsParams) { |
| 19 | + const [status, setStatus] = useState<AssignmentNotificationStatus>( |
| 20 | + getAssignmentNotificationStatus, |
| 21 | + ); |
| 22 | + const [isSaving, setIsSaving] = useState(false); |
| 23 | + const [error, setError] = useState<string | null>(null); |
| 24 | + const [isEnabled, setIsEnabled] = useState(isAssignmentNotificationsEnabled); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + setStatus(getAssignmentNotificationStatus()); |
| 28 | + setIsEnabled(isAssignmentNotificationsEnabled()); |
| 29 | + }, [user]); |
| 30 | + |
| 31 | + const watches = useMemo( |
| 32 | + () => |
| 33 | + user |
| 34 | + ? competitions.map((competition) => ({ |
| 35 | + competitionId: competition.id, |
| 36 | + wcaUserId: user.id, |
| 37 | + })) |
| 38 | + : [], |
| 39 | + [competitions, user], |
| 40 | + ); |
| 41 | + |
| 42 | + const enable = useCallback(async () => { |
| 43 | + setIsSaving(true); |
| 44 | + setError(null); |
| 45 | + |
| 46 | + try { |
| 47 | + const nextStatus = await enableAssignmentNotifications(watches); |
| 48 | + setStatus(nextStatus); |
| 49 | + setIsEnabled(isAssignmentNotificationsEnabled()); |
| 50 | + } catch (e) { |
| 51 | + setError(e instanceof Error ? e.message : 'Unable to enable assignment notifications.'); |
| 52 | + setStatus(getAssignmentNotificationStatus()); |
| 53 | + setIsEnabled(isAssignmentNotificationsEnabled()); |
| 54 | + } finally { |
| 55 | + setIsSaving(false); |
| 56 | + } |
| 57 | + }, [watches]); |
| 58 | + |
| 59 | + const disable = useCallback(async () => { |
| 60 | + setIsSaving(true); |
| 61 | + setError(null); |
| 62 | + |
| 63 | + try { |
| 64 | + await disableAssignmentNotifications(); |
| 65 | + setStatus(getAssignmentNotificationStatus()); |
| 66 | + setIsEnabled(isAssignmentNotificationsEnabled()); |
| 67 | + } catch (e) { |
| 68 | + setError(e instanceof Error ? e.message : 'Unable to disable assignment notifications.'); |
| 69 | + setIsEnabled(isAssignmentNotificationsEnabled()); |
| 70 | + } finally { |
| 71 | + setIsSaving(false); |
| 72 | + } |
| 73 | + }, []); |
| 74 | + |
| 75 | + return { |
| 76 | + canEnable: (status === 'default' || status === 'granted') && !isEnabled && watches.length > 0, |
| 77 | + canDisable: status === 'granted' && isEnabled, |
| 78 | + enable, |
| 79 | + disable, |
| 80 | + error, |
| 81 | + isSaving, |
| 82 | + status, |
| 83 | + watchCount: watches.length, |
| 84 | + }; |
| 85 | +} |
0 commit comments