Skip to content

Commit 13f9016

Browse files
authored
Merge pull request #78962 from Expensify/chuckdries/useSidebarOrderedReports-clear-cache-button
[No QA] Add debug button to clear the useSidebarOrderedReports cache
2 parents cdaa688 + c658f51 commit 13f9016

File tree

12 files changed

+60
-2
lines changed

12 files changed

+60
-2
lines changed

src/components/TestToolMenu.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import useIsAuthenticated from '@hooks/useIsAuthenticated';
33
import useLocalize from '@hooks/useLocalize';
44
import useOnyx from '@hooks/useOnyx';
5+
import {useSidebarOrderedReports} from '@hooks/useSidebarOrderedReports';
56
import useThemeStyles from '@hooks/useThemeStyles';
67
import {isUsingStagingApi} from '@libs/ApiUtils';
78
import {setShouldFailAllRequests, setShouldForceOffline, setShouldSimulatePoorConnection} from '@userActions/Network';
@@ -23,6 +24,7 @@ function TestToolMenu() {
2324
const [isDebugModeEnabled = false] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED, {canBeMissing: true});
2425
const styles = useThemeStyles();
2526
const {translate} = useLocalize();
27+
const {clearLHNCache} = useSidebarOrderedReports();
2628

2729
// Check if the user is authenticated to show options that require authentication
2830
const isAuthenticated = useIsAuthenticated();
@@ -72,6 +74,15 @@ function TestToolMenu() {
7274
onPress={() => expireSessionWithDelay()}
7375
/>
7476
</TestToolRow>
77+
78+
{/* Clears the useSidebarOrderedReports cache to re-compute from latest onyx values */}
79+
<TestToolRow title={translate('initialSettingsPage.troubleshoot.leftHandNavCache')}>
80+
<Button
81+
small
82+
text={translate('initialSettingsPage.troubleshoot.clearleftHandNavCache')}
83+
onPress={clearLHNCache}
84+
/>
85+
</TestToolRow>
7586
</>
7687
)}
7788

src/hooks/useSidebarOrderedReports.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type SidebarOrderedReportsContextValue = {
3131
orderedReportIDs: string[];
3232
currentReportID: string | undefined;
3333
policyMemberAccountIDs: number[];
34+
clearLHNCache: () => void;
3435
};
3536

3637
type 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

4547
const 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,

src/languages/de.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,8 @@ const translations: TranslationDeepObject<typeof en> = {
18051805
sentryDebugDescription: 'Sentry-Anfragen in der Konsole protokollieren',
18061806
sentryHighlightedSpanOps: 'Hervorgehobene Span-Namen',
18071807
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1808+
leftHandNavCache: 'Cache für linke Seitenleiste',
1809+
clearleftHandNavCache: 'Löschen',
18081810
},
18091811
debugConsole: {
18101812
saveLog: 'Protokoll speichern',

src/languages/en.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,8 @@ const translations = {
17811781
invalidFile: 'Invalid file',
17821782
invalidFileDescription: 'The file you are trying to import is not valid. Please try again.',
17831783
invalidateWithDelay: 'Invalidate with delay',
1784+
leftHandNavCache: 'Left Hand Nav cache',
1785+
clearleftHandNavCache: 'Clear',
17841786
recordTroubleshootData: 'Record Troubleshoot Data',
17851787
softKillTheApp: 'Soft kill the app',
17861788
kill: 'Kill',

src/languages/es.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,8 @@ const translations: TranslationDeepObject<typeof en> = {
15161516
invalidFile: 'Archivo inválido',
15171517
invalidFileDescription: 'El archivo que ests intentando importar no es válido. Por favor, inténtalo de nuevo.',
15181518
invalidateWithDelay: 'Invalidar con retraso',
1519+
leftHandNavCache: 'Caché del menú de navegación izquierdo',
1520+
clearleftHandNavCache: 'borrar',
15191521
recordTroubleshootData: 'Registrar datos de resolución de problemas',
15201522
softKillTheApp: 'Desactivar la aplicación',
15211523
kill: 'Matar',

src/languages/fr.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,8 @@ const translations: TranslationDeepObject<typeof en> = {
18081808
sentryDebugDescription: 'Enregistrer les requêtes Sentry dans la console',
18091809
sentryHighlightedSpanOps: 'Noms de spans mis en valeur',
18101810
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1811+
leftHandNavCache: 'Cache de navigation gauche',
1812+
clearleftHandNavCache: 'Effacer',
18111813
},
18121814
debugConsole: {
18131815
saveLog: 'Enregistrer le journal',

src/languages/it.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,8 @@ const translations: TranslationDeepObject<typeof en> = {
18021802
sentryDebugDescription: 'Registra le richieste Sentry nella console',
18031803
sentryHighlightedSpanOps: 'Nomi degli span evidenziati',
18041804
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1805+
leftHandNavCache: 'Cache della navigazione sinistra',
1806+
clearleftHandNavCache: 'Cancella',
18051807
},
18061808
debugConsole: {
18071809
saveLog: 'Salva registro',

src/languages/ja.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,8 @@ const translations: TranslationDeepObject<typeof en> = {
18011801
sentryDebugDescription: 'Sentryリクエストをコンソールに記録',
18021802
sentryHighlightedSpanOps: 'ハイライト表示するspan名',
18031803
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1804+
leftHandNavCache: '左側ナビキャッシュ',
1805+
clearleftHandNavCache: 'クリア',
18041806
},
18051807
debugConsole: {
18061808
saveLog: 'ログを保存',

src/languages/nl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,8 @@ const translations: TranslationDeepObject<typeof en> = {
18011801
sentryDebugDescription: 'Sentry-verzoeken naar console loggen',
18021802
sentryHighlightedSpanOps: 'Geresalteerde spannamen',
18031803
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1804+
leftHandNavCache: 'Cache van linkernavigatie',
1805+
clearleftHandNavCache: 'Wissen',
18041806
},
18051807
debugConsole: {
18061808
saveLog: 'Log opslaan',

src/languages/pl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,8 @@ const translations: TranslationDeepObject<typeof en> = {
17991799
sentryDebugDescription: 'Rejestruj żądania Sentry w konsoli',
18001800
sentryHighlightedSpanOps: 'Nazwy wyróżnionych spanów',
18011801
sentryHighlightedSpanOpsPlaceholder: 'ui.interaction.click, navigation, ui.load',
1802+
leftHandNavCache: 'Pamięć podręczna lewego panelu nawigacyjnego',
1803+
clearleftHandNavCache: 'Wyczyść',
18021804
},
18031805
debugConsole: {
18041806
saveLog: 'Zapisz log',

0 commit comments

Comments
 (0)