@@ -31,6 +31,7 @@ type SidebarOrderedReportsContextValue = {
3131 orderedReportIDs : string [ ] ;
3232 currentReportID : string | undefined ;
3333 policyMemberAccountIDs : number [ ] ;
34+ clearLHNCache : ( ) => void ;
3435} ;
3536
3637type ReportsToDisplayInLHN = Record < string , OnyxTypes . Report & { hasErrorsOtherThanFailedReceipt ?: boolean } > ;
@@ -40,6 +41,7 @@ const SidebarOrderedReportsContext = createContext<SidebarOrderedReportsContextV
4041 orderedReportIDs : [ ] ,
4142 currentReportID : '' ,
4243 policyMemberAccountIDs : [ ] ,
44+ clearLHNCache : ( ) => { } ,
4345} ) ;
4446
4547const policySelector = ( policy : OnyxEntry < OnyxTypes . Policy > ) : PartialPolicyForSidebar =>
@@ -81,6 +83,10 @@ function SidebarOrderedReportsContextProvider({
8183 const derivedCurrentReportID = currentReportIDForTests ?? currentReportIDValue ?. currentReportID ;
8284 const prevDerivedCurrentReportID = usePrevious ( derivedCurrentReportID ) ;
8385
86+ // we need to force reportsToDisplayInLHN to re-compute when we clear currentReportsToDisplay, but the way it currently works relies on not having currentReportsToDisplay as a memo dependency, so we just need something we can change to trigger it
87+ // I don't like it either, but clearing the cache is only a hack for the debug modal and I will endeavor to make it better as I work to improve the cache correctness of the LHN more broadly
88+ const [ clearCacheDummyCounter , setClearCacheDummyCounter ] = useState ( 0 ) ;
89+
8490 const policyMemberAccountIDs = useMemo ( ( ) => getPolicyEmployeeListByIdWithoutCurrentUser ( policies , undefined , accountID ) , [ policies , accountID ] ) ;
8591 const prevBetas = usePrevious ( betas ) ;
8692 const prevPriorityMode = usePrevious ( priorityMode ) ;
@@ -185,6 +191,7 @@ function SidebarOrderedReportsContextProvider({
185191 draftComments : reportsDrafts ,
186192 } ) ;
187193 } else {
194+ Log . info ( '[useSidebarOrderedReports] building reportsToDisplay from scratch' ) ;
188195 reportsToDisplay = SidebarUtils . getReportsToDisplayInLHN (
189196 derivedCurrentReportID ,
190197 chatReports ,
@@ -201,7 +208,19 @@ function SidebarOrderedReportsContextProvider({
201208 return reportsToDisplay ;
202209 // Rule disabled intentionally — triggering a re-render on currentReportsToDisplay would cause an infinite loop
203210 // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
204- } , [ getUpdatedReports , chatReports , derivedCurrentReportID , priorityMode , betas , policies , transactionViolations , reportNameValuePairs , reportAttributes , reportsDrafts ] ) ;
211+ } , [
212+ getUpdatedReports ,
213+ chatReports ,
214+ derivedCurrentReportID ,
215+ priorityMode ,
216+ betas ,
217+ policies ,
218+ transactionViolations ,
219+ reportNameValuePairs ,
220+ reportAttributes ,
221+ reportsDrafts ,
222+ clearCacheDummyCounter ,
223+ ] ) ;
205224
206225 const deepComparedReportsToDisplayInLHN = useDeepCompareRef ( reportsToDisplayInLHN ) ;
207226 const deepComparedReportsDrafts = useDeepCompareRef ( reportsDrafts ) ;
@@ -232,6 +251,12 @@ function SidebarOrderedReportsContextProvider({
232251
233252 const orderedReports = useMemo ( ( ) => getOrderedReports ( orderedReportIDs ) , [ getOrderedReports , orderedReportIDs ] ) ;
234253
254+ const clearLHNCache = useCallback ( ( ) => {
255+ Log . info ( '[useSidebarOrderedReports] Clearing sidebar cache manually via debug modal' ) ;
256+ setCurrentReportsToDisplay ( { } ) ;
257+ setClearCacheDummyCounter ( ( current ) => current + 1 ) ;
258+ } , [ ] ) ;
259+
235260 const contextValue : SidebarOrderedReportsContextValue = useMemo ( ( ) => {
236261 // We need to make sure the current report is in the list of reports, but we do not want
237262 // to have to re-generate the list every time the currentReportID changes. To do that
@@ -256,6 +281,7 @@ function SidebarOrderedReportsContextProvider({
256281 orderedReportIDs : updatedReportIDs ,
257282 currentReportID : derivedCurrentReportID ,
258283 policyMemberAccountIDs,
284+ clearLHNCache,
259285 } ;
260286 }
261287
@@ -264,8 +290,9 @@ function SidebarOrderedReportsContextProvider({
264290 orderedReportIDs,
265291 currentReportID : derivedCurrentReportID ,
266292 policyMemberAccountIDs,
293+ clearLHNCache,
267294 } ;
268- } , [ getOrderedReportIDs , orderedReportIDs , derivedCurrentReportID , policyMemberAccountIDs , shouldUseNarrowLayout , getOrderedReports , orderedReports ] ) ;
295+ } , [ getOrderedReportIDs , orderedReportIDs , derivedCurrentReportID , policyMemberAccountIDs , shouldUseNarrowLayout , getOrderedReports , orderedReports , clearLHNCache ] ) ;
269296
270297 const currentDeps = {
271298 priorityMode,
0 commit comments