Repository navigation
refactor: remove unsafe assertions from SelectionList section flattening - #102964
Conversation
Seatbelt-Operation: restore98854-selection-sign-e13e42ee-d56d-42f9-bd7e-e667fb74c316
|
@ikevin127 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
|
||
| const useFlattenedSections: UseFlattenedSections = (sections, initiallyFocusedItemKey) => { | ||
| 'use memo'; | ||
| type Item = TupleToUnion<typeof sections>['data'][number]; |
There was a problem hiding this comment.
❌ CONSISTENCY-18 (docs)
The type Item = TupleToUnion<typeof sections>['data'][number] derivation combined with the 'use memo' directive is non-obvious: a maintainer can't tell from the code why the generic TItem can't be referenced directly inside the hook body or why this type indirection is required. The previous implementation documented this exact constraint ("OXC bails on type params inside hooks"), but that comment was removed in this refactor, so the reasoning is now lost.
Add a short comment explaining why the item type is recovered via TupleToUnion<typeof sections> and why 'use memo' is used, e.g.:
const useFlattenedSections: UseFlattenedSections = (sections, initiallyFocusedItemKey) => {
'use memo';
// Recover the item type from the runtime `sections` param instead of a type param, because OXC's React
// Compiler can't memoize a hook that declares its own generic ("Unsupported declaration type for hoisting").
type Item = TupleToUnion<typeof sections>['data'][number];Reviewed at: 985e9ad | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
There was a problem hiding this comment.
Removed the TupleToUnion workaround and restored a plain generic hook. Added a short explanation for 'use memo'. Babel and OXC both compile and memoize the hook.
Reviewer Checklist
Screenshots/VideosScreen.Recording.2026-10-17.at.18.39.14.mov |
| type UseFlattenedSections = <TItem extends ListItem>(sections: Array<Section<TItem>>, initiallyFocusedItemKey?: string | null) => UseFlattenedSectionsResultGeneric<TItem>; | ||
|
|
||
| const useFlattenedSections: UseFlattenedSections = (sections, initiallyFocusedItemKey) => { | ||
| 'use memo'; | ||
| type Item = TupleToUnion<typeof sections>['data'][number]; |
There was a problem hiding this comment.
src/components/SelectionList/hooks/useFlattenedSections.ts L21-25
🟡 We don't need the UseFlattenedSections function type, the TupleToUnion<typeof sections>['data'][number] trick or the type-fest import. I ran both compilers through config/reactCompiler/checkBoth.mjs against a plain generic function declaration.
Babel and OXC both report compiled + memoized, and isDivergent is false. So the old "OXC bails on type params" limitation is gone once useMemo is gone:
/**
* Hook that flattens sections with headers and items into a single array for FlashList.
* Also computes disabled indexes, selected items, and initial focus index.
*/
function useFlattenedSections<TItem extends ListItem>(sections: Array<Section<TItem>>, initiallyFocusedItemKey?: string | null): UseFlattenedSectionsResult<TItem> {
// The body calls no hooks, so React Compiler's infer mode would skip it without this directive
'use memo';
const data: Array<FlattenedItem<TItem>> = [];
const selectedOptions: TItem[] = [];
// ...
const itemData: SectionListItem<TItem> = {This also brings back the JSDoc the PR deleted, and it answers the bot's L25 comment by explaining the one thing that's actually non-obvious here (why 'use memo' is needed).
Since the non-generic result type is gone, can we rename UseFlattenedSectionsResultGeneric to UseFlattenedSectionsResult ?
There was a problem hiding this comment.
Changed the hook to a normal generic function, removed the function-type alias and type-fest import, restored its JSDoc, and renamed the result type to UseFlattenedSectionsResult. Kept 'use memo' with a short explanation. Both compilers report compiled and memoized.
|
|
||
| import {createElement} from 'react'; | ||
|
|
||
| type SpecialItem = ListItem & {specialID: number}; |
There was a problem hiding this comment.
tests/unit/hooks/useFlattenedSections.test.ts L9
🟡 Instead of adding a new test-only type, can we use the existing production CalendarPickerListItem from @components/DatePicker/CalendarPicker/types ? It's ListItem & {value: number}, which is exactly the shape we need to prove that a specialized field survives flattening:
import type CalendarPickerListItem from '@components/DatePicker/CalendarPicker/types';
// ...
const sections: Array<Section<CalendarPickerListItem>> = [There was a problem hiding this comment.
Replaced the test-only item type with the production CalendarPickerListItem. The test now checks that its value field survives flattening.
| type SpecialItem = ListItem & {specialID: number}; | ||
|
|
||
| describe('useFlattenedSections', () => { | ||
| it('preserves headers, rows, selection, disability, focus, and specialized fields', () => { |
There was a problem hiding this comment.
tests/unit/hooks/useFlattenedSections.test.ts L12
🟡 This single it contains two Given/When/Then flows: the flattening output and the memoization reuse/recompute. If it fails, the test name doesn't tell us which behavior broke.
Can we split it into it('flattens headers and rows while keeping selection, disabled state and focus') and it('reuses the result for unchanged inputs and recomputes when they change'), with shared sections in a beforeEach or a helper ?
There was a problem hiding this comment.
Split flattening behavior and memoization behavior into separate tests, with shared fixtures. Both tests pass.
| expect(result.current.flattenedData.at(4)).toMatchObject({specialID: 3, isDisabled: true}); | ||
| expect(result.current.selectedItems.map((item) => item.specialID)).toEqual([1, 3]); | ||
| expect(result.current.disabledIndexes).toEqual([0, 2, 3, 4]); | ||
| expect([result.current.itemsCount, result.current.initialFocusedIndex, result.current.firstFocusableIndex]).toEqual([3, 1, 1]); |
There was a problem hiding this comment.
tests/unit/hooks/useFlattenedSections.test.ts L37
🟢 Packing three values into one array makes a failure read like [3, -1, 1] vs [3, 1, 1], and you have to work out which field changed.
Can we use one expect per field, or toMatchObject({itemsCount: 3, initialFocusedIndex: 1, firstFocusableIndex: 1}) ?
There was a problem hiding this comment.
Replaced the packed array assertion with separate named-field assertions so a failure identifies the affected field.
| {keyForList: 'off', specialID: 2, isDisabled: true}, | ||
| ], | ||
| }, | ||
| {sectionIndex: 1, customHeader: createElement('div'), isDisabled: true, data: [{keyForList: 'same', specialID: 3, isSelected: true}]}, |
There was a problem hiding this comment.
tests/unit/hooks/useFlattenedSections.test.ts L23
🟢 NAB: this is a React Native test suite, so createElement(View) (or Text) is closer to what real customHeader consumers pass than a DOM 'div'.
There was a problem hiding this comment.
Replaced the HTML custom-header fixture with a React Native View.
|
@KJ21-ENG The PR needs one simplification and some test cleanup - there are no blockers or behavior changes.
Please tag me once the comments are addressed. |
Seatbelt-Operation: reviewer-fix-102964-aedae95f-766a-4315-bf05-3525ca3b63a0
|
@ikevin127 Could you please check this once you get a chance, Thanks! |
|
No product review needed. |
|
🚧 blimpich has triggered a test Expensify/App build. You can view the workflow run here. |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
🚀 Deployed to staging by https://github.com/blimpich in version: 9.5.15-1 🚀
|
|
No help site update is needed. This PR is an internal type-safety and memoization refactor of |
|
🚀 Deployed to production by https://github.com/grgia in version: 9.5.15-3 🚀
Bundle Size Analysis (Sentry): |
Explanation of Change
Keep specialized item types through SelectionList section flattening, narrow rows before checking selection, and remove the unwritten
flatIndextype field. The same section, disabled-item, selection, and focus calculations now use React Compiler memoization instead ofuseMemo.Fixed Issues
$ #94739
PROPOSAL: #94739 (comment)
Tests
Relevant upstream TypeScript, Jest, and React Compiler checks passed.
tests/unit/hooks/useFlattenedSections.test.tscovers headers, custom headers, empty sections, specialized fields, disabled rows, repeated keys, focus lookup and result reuse/recomputation.Sections and selection
Setup: Use populated Recents and Contacts, an existing chat and your self-DM.
Keyboard navigation
Offline tests
Not applicable: section flattening and focus/selection calculations use loaded data; this refactor changes no network, storage, or reconnect behavior.
QA Steps
Same as tests.
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Recordings (1)
Windows desktop Chrome: section rows, contact selection/deselection, disabled self-DM and keyboard navigation were observed. Console errors were observed without confirmed attribution to this PR. Native Android/iOS, mobile-web and macOS Chrome/Safari results are unverified.
pr102964-sections-selection-keyboard-desktop.mp4