Skip to content

Commit 1dbf10e

Browse files
fix: eliminate redundant function calls
1 parent 6cfb617 commit 1dbf10e

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/optimizer-page/scan-results/ScanResults.tsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ const ScanResults: FC<Props> = ({ data }) => {
125125
}, [data?.courseUpdates, data?.customPages, intl]);
126126

127127
// Combine renderable sections with regular sections
128-
const allSectionsForBrokenLinks = useMemo(
129-
() => [...renderableSections, ...(sections || [])],
130-
[renderableSections, sections],
131-
);
132-
133-
const allSectionsForPrevRun = useMemo(
128+
const allSections = useMemo(
134129
() => [...renderableSections, ...(sections || [])],
135130
[renderableSections, sections],
136131
);
@@ -139,21 +134,21 @@ const ScanResults: FC<Props> = ({ data }) => {
139134
brokenLinksCounts,
140135
lockedLinksCounts,
141136
externalForbiddenLinksCounts,
142-
} = useMemo(() => countBrokenLinks({ sections: allSectionsForBrokenLinks }), [allSectionsForBrokenLinks]);
137+
} = useMemo(() => countBrokenLinks({ sections: allSections }), [allSections]);
143138

144139
// Calculate if there are any previous run links across all sections
145140
const hasPreviousRunLinks = useMemo(
146-
() => allSectionsForPrevRun.some(section => (
141+
() => allSections.some(section => (
147142
section.subsections.some(subsection => subsection.units.some(unit => (
148143
unit.blocks.some(block => block.previousRunLinks && block.previousRunLinks.length > 0)
149144
))))),
150-
[allSectionsForPrevRun],
145+
[allSections],
151146
);
152147

153-
// Calculate previous run links count for each section (including virtual sections)
148+
// Calculate previous run links count for each section
154149
const previousRunLinksCounts = useMemo(() => {
155-
if (!allSectionsForPrevRun) { return []; }
156-
return allSectionsForPrevRun.map(section => section.subsections.reduce(
150+
if (!allSections) { return []; }
151+
return allSections.map(section => section.subsections.reduce(
157152
(sectionTotal, subsection) => sectionTotal
158153
+ subsection.units.reduce(
159154
(unitTotal, unit) => unitTotal
@@ -165,17 +160,17 @@ const ScanResults: FC<Props> = ({ data }) => {
165160
),
166161
0,
167162
));
168-
}, [allSectionsForPrevRun]);
163+
}, [allSections]);
169164

170165
const activeFilters = Object.keys(filters).filter(key => filters[key]);
171166
const [filterBy, {
172167
add, remove, set, clear,
173168
}] = useCheckboxSetValues(activeFilters);
174169

175170
useEffect(() => {
176-
setOpenStates(allSectionsForBrokenLinks ? allSectionsForBrokenLinks.map(() => false) : []);
177-
setPrevRunOpenStates(allSectionsForPrevRun ? allSectionsForPrevRun.map(() => false) : []);
178-
}, [allSectionsForBrokenLinks, allSectionsForPrevRun]);
171+
setOpenStates(allSections ? allSections.map(() => false) : []);
172+
setPrevRunOpenStates(allSections ? allSections.map(() => false) : []);
173+
}, [allSections]);
179174

180175
if (isDataEmpty(data)) {
181176
return <InfoCard text={intl.formatMessage(messages.noDataCard)} />;
@@ -193,7 +188,7 @@ const ScanResults: FC<Props> = ({ data }) => {
193188
];
194189
// Only show sections that have at least one unit with a visible link (not just previousRunLinks)
195190
const shouldSectionRender = (sectionIndex: number): boolean => {
196-
const section = allSectionsForBrokenLinks[sectionIndex];
191+
const section = allSections[sectionIndex];
197192
const hasVisibleUnit = section.subsections.some(
198193
(subsection) => subsection.units.some((unit) => unit.blocks.some((block) => {
199194
const hasBroken = block.brokenLinks?.length > 0;
@@ -232,7 +227,7 @@ const ScanResults: FC<Props> = ({ data }) => {
232227

233228
const findNextVisibleSection = (currentIndex: number): number => {
234229
let nextIndex = currentIndex + 1;
235-
while (nextIndex < allSectionsForBrokenLinks.length) {
230+
while (nextIndex < allSections.length) {
236231
if (shouldSectionRender(nextIndex)) {
237232
return nextIndex;
238233
}
@@ -311,8 +306,8 @@ const ScanResults: FC<Props> = ({ data }) => {
311306

312307
{(() => {
313308
// Find all visible sections
314-
const visibleSections = allSectionsForBrokenLinks && allSectionsForBrokenLinks.length > 0
315-
? allSectionsForBrokenLinks
309+
const visibleSections = allSections && allSections.length > 0
310+
? allSections
316311
.map((_, index) => (shouldSectionRender(index) ? index : -1))
317312
.filter(idx => idx !== -1)
318313
: [];
@@ -323,7 +318,7 @@ const ScanResults: FC<Props> = ({ data }) => {
323318
</div>
324319
);
325320
}
326-
return allSectionsForBrokenLinks.map((section, index) => {
321+
return allSections.map((section, index) => {
327322
if (!shouldSectionRender(index)) {
328323
return null;
329324
}
@@ -336,7 +331,7 @@ const ScanResults: FC<Props> = ({ data }) => {
336331
const prevVisibleIndex = findPreviousVisibleSection(index);
337332
return prevVisibleIndex >= 0 && openStates[prevVisibleIndex];
338333
})() : true}
339-
hasNextAndIsOpen={index < allSectionsForBrokenLinks.length - 1 ? (() => {
334+
hasNextAndIsOpen={index < allSections.length - 1 ? (() => {
340335
const nextVisibleIndex = findNextVisibleSection(index);
341336
return nextVisibleIndex >= 1 && openStates[nextVisibleIndex];
342337
})() : true}
@@ -388,11 +383,11 @@ const ScanResults: FC<Props> = ({ data }) => {
388383
</div>
389384

390385
{waffleFlags.enableCourseOptimizerCheckPrevRunLinks
391-
&& allSectionsForPrevRun
392-
&& allSectionsForPrevRun.length > 0
386+
&& allSections
387+
&& allSections.length > 0
393388
&& hasPreviousRunLinks && (() => {
394389
// Filter out sections/subsections/units that have no previous run links
395-
const filteredSections = allSectionsForPrevRun.map((section) => {
390+
const filteredSections = allSections.map((section) => {
396391
// Filter subsections
397392
const filteredSubsections = section.subsections.map(subsection => {
398393
// Filter units

0 commit comments

Comments
 (0)