-
Notifications
You must be signed in to change notification settings - Fork 24
Fix search showing limited results #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carma12
wants to merge
2
commits into
freeipa:main
Choose a base branch
from
carma12:fix-search-limited-results
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ The `pathname` must be registered in `AppRoutes.tsx` and `NavRoutes.ts`. | |
| const lastIdx = page * perPage; | ||
|
|
||
| const dataResponse = useGettingMyEntitiesQuery({ | ||
| searchValue: "", | ||
| searchValue: searchValue, | ||
| sizeLimit: 0, | ||
| apiVersion: apiVersion || API_VERSION_BACKUP, | ||
| startIdx: firstIdx, | ||
|
|
@@ -41,6 +41,11 @@ The `pathname` must be registered in `AppRoutes.tsx` and `NavRoutes.ts`. | |
| const { data: batchResponse, isLoading, isFetching, error } = dataResponse; | ||
| ``` | ||
|
|
||
| > **Important:** Always pass `searchValue` (not `""`) to the query hook. RTK Query | ||
| > auto-refetches whenever its parameters change (`searchValue`, `startIdx`, `stopIdx`), | ||
| > so both pagination and filtering are handled automatically. Using a hardcoded `""` | ||
| > causes a race condition where the unfiltered query response overwrites search results. | ||
|
|
||
| ## Step 4: Derive State with useMemo (Recommended) | ||
|
|
||
| Use `useMemo` to derive `elementsList` and `totalCount` from the query response — **do not** use `useEffect` + `useState` to sync state. | ||
|
|
@@ -64,10 +69,14 @@ Use it to type the search data state: | |
|
|
||
| // Derive elementsList and totalCount | ||
| const { elementsList, totalCount } = useMemo(() => { | ||
| // Search results are fetched with stopIdx: 100 (all matches at once), | ||
| // so paginate them client-side using page/perPage. | ||
| if (isSearchActive && searchData) { | ||
| const start = (page - 1) * perPage; | ||
| const end = start + perPage; | ||
| return { | ||
| elementsList: searchData.elementsList, | ||
| totalCount: searchData.totalCount, | ||
| elementsList: searchData.elementsList.slice(start, end), | ||
| totalCount: searchData.elementsList.length, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -81,7 +90,7 @@ Use it to type the search data state: | |
| } | ||
|
|
||
| return { elementsList: [], totalCount: 0 }; | ||
| }, [batchResponse, isSearchActive, searchData]); | ||
| }, [batchResponse, isSearchActive, searchData, page, perPage]); | ||
|
|
||
| // Derive showTableRows from loading states | ||
| const showTableRows = useMemo(() => { | ||
|
|
@@ -124,22 +133,50 @@ This pattern avoids eslint warnings about calling `setState` in `useEffect`. | |
| > **Note:** No manual `useEffect` for pagination is needed. RTK Query automatically | ||
| > re-fetches when `startIdx`/`stopIdx` change (derived from `page`/`perPage`). | ||
|
|
||
| ## Step 7: Search Handler | ||
| ## Step 7: Search Value Update Handler | ||
|
|
||
| `SearchInputLayout` buffers keystrokes locally — it does **not** call `updateSearchValue` | ||
| on every keystroke. Instead, it calls `updateSearchValue(value)` and | ||
| `submitSearchValue(value)` only when the user presses Enter or clicks the search button. | ||
| This avoids firing an API request on every keystroke. | ||
|
|
||
| `updateSearchValue` resets pagination to page 1 and updates the committed search value | ||
| (which drives the RTK Query parameter and URL sync): | ||
|
|
||
| ```tsx | ||
| const updateSearchValue = (value: string) => { | ||
| setPage(1); | ||
| setSearchValue(value); | ||
| }; | ||
| ``` | ||
|
|
||
| > **Do not** add search-as-you-type behavior by calling `setSearchValue` inside | ||
| > `SearchInputLayout`'s `onChange`. The component deliberately buffers input locally | ||
| > and only propagates on submit or clear. | ||
|
|
||
| ## Step 8: Search Submit Handler | ||
|
|
||
| `submitSearchValue` is called by `SearchInputLayout` when the user presses Enter or | ||
| clicks the search button. It receives the **current input value** as an argument to | ||
| avoid stale closures (since `updateSearchValue` and `submitSearchValue` are called in | ||
| the same event handler, the React state from `updateSearchValue` has not yet committed). | ||
|
|
||
| ```tsx | ||
| const [searchEntities, searchResult] = useSearchMyEntitiesEntriesMutation({}); | ||
| const [searchDisabled, setSearchIsDisabled] = useState(false); | ||
|
|
||
| const submitSearchValue = () => { | ||
| const submitSearchValue = (value?: string) => { | ||
| const search = value ?? searchValue; | ||
| setPage(1); | ||
| setSearchIsDisabled(true); | ||
| setIsSearchActive(true); | ||
|
|
||
| searchEntities({ | ||
| searchValue, | ||
| searchValue: search, | ||
| sizeLimit: 0, | ||
| apiVersion: apiVersion || API_VERSION_BACKUP, | ||
| startIdx: firstIdx, | ||
| stopIdx: lastIdx, | ||
| startIdx: 0, | ||
| stopIdx: 100, | ||
| }).then((result) => { | ||
| if ("data" in result) { | ||
| const searchError = result.data?.error; | ||
|
|
@@ -170,6 +207,17 @@ This pattern avoids eslint warnings about calling `setState` in `useEffect`. | |
| }; | ||
| ``` | ||
|
|
||
| > **Important — `value` parameter:** Always use the `value` argument (falling back to | ||
| > `searchValue` with `??`) instead of reading `searchValue` directly from the closure. | ||
| > Both `updateSearchValue` and `submitSearchValue` are called in the same React event | ||
| > handler, so the state set by `updateSearchValue` is not yet available when | ||
| > `submitSearchValue` runs. | ||
|
|
||
| > **Important — `stopIdx: 100`:** Use a fixed upper bound (100) instead of `perPage`. | ||
| > The LDAP backend has a size limit close to this value, and using `perPage` (e.g. 10) | ||
| > would miss entries beyond the first page of results when searching from a page other | ||
| > than page 1. | ||
|
Comment on lines
+216
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. But even if you put a wild number like, e.g. |
||
|
|
||
| ## Legacy Pattern (Avoid) | ||
|
|
||
| The older pattern using `useEffect` + `setState` triggers eslint warnings: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.