|
| 1 | +import {useFocusEffect} from '@react-navigation/native'; |
| 2 | +import {useCallback, useEffect, useEffectEvent, useRef} from 'react'; |
| 3 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 4 | +import useOnyx from '@hooks/useOnyx'; |
| 5 | +import {getUserToInviteOption} from '@libs/OptionsListUtils'; |
| 6 | +import type {SearchOption} from '@libs/OptionsListUtils'; |
| 7 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 8 | +import type {Login, PersonalDetails, PersonalDetailsList} from '@src/types/onyx'; |
| 9 | +import type NewGroupChatDraft from '@src/types/onyx/NewGroupChatDraft'; |
| 10 | +import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; |
| 11 | +import type SelectedOption from './types'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Keeps the NewChatPage's `selectedOptions` state aligned with the `NEW_GROUP_CHAT_DRAFT` Onyx draft. |
| 15 | + * |
| 16 | + * - On mount / reload, restores the draft participants into `selectedOptions` once so an |
| 17 | + * in-progress group chat survives refreshes. |
| 18 | + * - While the screen is in the background (e.g. the user navigated to NewChatConfirmPage), mirrors |
| 19 | + * participant removals made against the draft back into `selectedOptions` so the two stay |
| 20 | + * consistent when the user returns. |
| 21 | + */ |
| 22 | +function useGroupChatDraftParticipantSync( |
| 23 | + allPersonalDetailOptions: Array<SearchOption<PersonalDetails>>, |
| 24 | + areAllPersonalDetailOptionsLoaded: boolean, |
| 25 | + allPersonalDetails: OnyxEntry<PersonalDetailsList>, |
| 26 | + loginList: OnyxEntry<Login>, |
| 27 | + currentUserEmail: string, |
| 28 | + currentUserAccountID: number, |
| 29 | + selectedOptions: SelectedOption[], |
| 30 | + setSelectedOptions: (options: SelectedOption[]) => void, |
| 31 | +) { |
| 32 | + const shouldRestoreSelectedOptionsRef = useRef(true); |
| 33 | + const isScreenInBackgroundRef = useRef(false); |
| 34 | + |
| 35 | + const draftParticipantsSelector = (draft: NewGroupChatDraft | undefined) => { |
| 36 | + const isSubscriptionActive = shouldRestoreSelectedOptionsRef.current || isScreenInBackgroundRef.current; |
| 37 | + return isSubscriptionActive ? draft?.participants : undefined; |
| 38 | + }; |
| 39 | + |
| 40 | + const [draftParticipants, draftParticipantsMetadata] = useOnyx(ONYXKEYS.NEW_GROUP_CHAT_DRAFT, { |
| 41 | + selector: draftParticipantsSelector, |
| 42 | + }); |
| 43 | + |
| 44 | + const restoreParticipantsFromDraft = useEffectEvent(() => { |
| 45 | + // Flip the ref first so the useOnyx selector disables the subscription |
| 46 | + shouldRestoreSelectedOptionsRef.current = false; |
| 47 | + |
| 48 | + const restoredOptionsFromDraft = (draftParticipants ?? []).reduce<SelectedOption[]>((result, participant) => { |
| 49 | + if (participant.accountID === currentUserAccountID) { |
| 50 | + return result; |
| 51 | + } |
| 52 | + const option = |
| 53 | + allPersonalDetailOptions.find((personalDetail) => personalDetail.accountID === participant.accountID) ?? |
| 54 | + getUserToInviteOption({ |
| 55 | + searchValue: participant?.login, |
| 56 | + personalDetails: allPersonalDetails, |
| 57 | + loginList, |
| 58 | + currentUserEmail, |
| 59 | + }); |
| 60 | + if (option) { |
| 61 | + result.push({...option, isSelected: true}); |
| 62 | + } |
| 63 | + return result; |
| 64 | + }, []); |
| 65 | + |
| 66 | + // No draft or only original creator in draft |
| 67 | + if (!restoredOptionsFromDraft.length) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + setSelectedOptions(restoredOptionsFromDraft); |
| 72 | + }); |
| 73 | + |
| 74 | + // NewChatConfirmPage can only deselect participants, |
| 75 | + // so we don't need the complex logic from the restoreParticipantsFromDraft. |
| 76 | + // Simple filtering out of deselected participants is enough here |
| 77 | + const syncSelectedOptionsWithDraft = useEffectEvent(() => { |
| 78 | + const draftLogins = new Set((draftParticipants ?? []).map((participant) => participant.login)); |
| 79 | + const filteredSelectionOptions = selectedOptions.filter((option) => draftLogins.has(option.login)); |
| 80 | + |
| 81 | + setSelectedOptions(filteredSelectionOptions); |
| 82 | + }); |
| 83 | + |
| 84 | + useFocusEffect( |
| 85 | + useCallback(() => { |
| 86 | + isScreenInBackgroundRef.current = false; |
| 87 | + |
| 88 | + return () => { |
| 89 | + isScreenInBackgroundRef.current = true; |
| 90 | + }; |
| 91 | + }, []), |
| 92 | + ); |
| 93 | + |
| 94 | + // Handle removing participants on other pages (e.g. NewChatConfirmPage) |
| 95 | + useEffect(() => { |
| 96 | + if (!isScreenInBackgroundRef.current) { |
| 97 | + return; |
| 98 | + } |
| 99 | + syncSelectedOptionsWithDraft(); |
| 100 | + }, [draftParticipants]); |
| 101 | + |
| 102 | + const areRestoreInputsReady = areAllPersonalDetailOptionsLoaded && !isLoadingOnyxValue(draftParticipantsMetadata); |
| 103 | + |
| 104 | + // Handle reload with existing draft participants |
| 105 | + useEffect(() => { |
| 106 | + if (!shouldRestoreSelectedOptionsRef.current || !areRestoreInputsReady) { |
| 107 | + return; |
| 108 | + } |
| 109 | + restoreParticipantsFromDraft(); |
| 110 | + }, [draftParticipants, areRestoreInputsReady]); |
| 111 | +} |
| 112 | + |
| 113 | +export default useGroupChatDraftParticipantSync; |
0 commit comments