-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
OptionListContextProvider.tsx
194 lines (163 loc) · 7.13 KB
/
OptionListContextProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import usePrevious from '@hooks/usePrevious';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import type {OptionList} from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, Report} from '@src/types/onyx';
import {usePersonalDetails} from './OnyxProvider';
type OptionsListContextProps = {
/** List of options for reports and personal details */
options: OptionList;
/** Function to initialize the options */
initializeOptions: () => void;
/** Flag to check if the options are initialized */
areOptionsInitialized: boolean;
/** Function to reset the options */
resetOptions: () => void;
};
type OptionsListProviderProps = {
/** Actual content wrapped by this component */
children: React.ReactNode;
};
const OptionsListContext = createContext<OptionsListContextProps>({
options: {
reports: [],
personalDetails: [],
},
initializeOptions: () => {},
areOptionsInitialized: false,
resetOptions: () => {},
});
const isEqualPersonalDetail = (prevPersonalDetail: PersonalDetails, personalDetail: PersonalDetails) =>
prevPersonalDetail?.firstName === personalDetail?.firstName &&
prevPersonalDetail?.lastName === personalDetail?.lastName &&
prevPersonalDetail?.login === personalDetail?.login &&
prevPersonalDetail?.displayName === personalDetail?.displayName;
function OptionsListContextProvider({children}: OptionsListProviderProps) {
const areOptionsInitialized = useRef(false);
const [options, setOptions] = useState<OptionList>({
reports: [],
personalDetails: [],
});
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const personalDetails = usePersonalDetails();
const prevPersonalDetails = usePrevious(personalDetails);
/**
* This effect is used to update the options list when reports change.
*/
useEffect(() => {
// there is no need to update the options if the options are not initialized
if (!areOptionsInitialized.current || !reports) {
return;
}
// Since reports updates can happen in bulk, and some reports depend on other reports, we need to recreate the whole list from scratch.
const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports;
setOptions((prevOptions) => {
const newOptions = {
...prevOptions,
reports: newReports,
};
return newOptions;
});
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [reports]);
/**
* This effect is used to update the options list when personal details change.
*/
useEffect(() => {
// there is no need to update the options if the options are not initialized
if (!areOptionsInitialized.current) {
return;
}
if (!personalDetails) {
return;
}
const newReportOptions: Array<{
replaceIndex: number;
newReportOption: OptionsListUtils.SearchOption<Report>;
}> = [];
Object.keys(personalDetails).forEach((accountID) => {
const prevPersonalDetail = prevPersonalDetails?.[accountID];
const personalDetail = personalDetails[accountID];
if (prevPersonalDetail && personalDetail && isEqualPersonalDetail(prevPersonalDetail, personalDetail)) {
return;
}
Object.values(reports ?? {})
.filter((report) => !!Object.keys(report?.participants ?? {}).includes(accountID) || (ReportUtils.isSelfDM(report) && report?.ownerAccountID === Number(accountID)))
.forEach((report) => {
if (!report) {
return;
}
const newReportOption = OptionsListUtils.createOptionFromReport(report, personalDetails);
const replaceIndex = options.reports.findIndex((option) => option.reportID === report.reportID);
newReportOptions.push({
newReportOption,
replaceIndex,
});
});
});
// since personal details are not a collection, we need to recreate the whole list from scratch
const newPersonalDetailsOptions = OptionsListUtils.createOptionList(personalDetails).personalDetails;
setOptions((prevOptions) => {
const newOptions = {...prevOptions};
newOptions.personalDetails = newPersonalDetailsOptions;
newReportOptions.forEach((newReportOption) => (newOptions.reports[newReportOption.replaceIndex] = newReportOption.newReportOption));
return newOptions;
});
// This effect is used to update the options list when personal details change so we ignore all dependencies except personalDetails
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [personalDetails]);
const loadOptions = useCallback(() => {
const optionLists = OptionsListUtils.createOptionList(personalDetails, reports);
setOptions({
reports: optionLists.reports,
personalDetails: optionLists.personalDetails,
});
}, [personalDetails, reports]);
const initializeOptions = useCallback(() => {
if (areOptionsInitialized.current) {
return;
}
loadOptions();
areOptionsInitialized.current = true;
}, [loadOptions]);
const resetOptions = useCallback(() => {
if (!areOptionsInitialized.current) {
return;
}
areOptionsInitialized.current = false;
setOptions({
reports: [],
personalDetails: [],
});
}, []);
return (
<OptionsListContext.Provider // eslint-disable-next-line react-compiler/react-compiler
value={useMemo(() => ({options, initializeOptions, areOptionsInitialized: areOptionsInitialized.current, resetOptions}), [options, initializeOptions, resetOptions])}
>
{children}
</OptionsListContext.Provider>
);
}
const useOptionsListContext = () => useContext(OptionsListContext);
// Hook to use the OptionsListContext with an initializer to load the options
const useOptionsList = (options?: {shouldInitialize: boolean}) => {
const {shouldInitialize = true} = options ?? {};
const {initializeOptions, options: optionsList, areOptionsInitialized, resetOptions} = useOptionsListContext();
useEffect(() => {
if (!shouldInitialize || areOptionsInitialized) {
return;
}
initializeOptions();
}, [shouldInitialize, initializeOptions, areOptionsInitialized]);
return {
initializeOptions,
options: optionsList,
areOptionsInitialized,
resetOptions,
};
};
export default OptionsListContextProvider;
export {useOptionsListContext, useOptionsList, OptionsListContext};