-
Notifications
You must be signed in to change notification settings - Fork 15
Rebuild ITwinGrid data loading on a shared infinite query core #233
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2418655
feat: add useEventCallback for stable access to the latest callback
LukaszKokot ea6c644
feat: add useReportChanges to report each distinct value once
LukaszKokot 344f11d
feat: add the pure infinite query state machine
LukaszKokot 44877fd
chore: satisfy the repo's default-case rule on the exhaustive switch
LukaszKokot 1de776a
feat: add the useInfiniteQuery core hook
LukaszKokot 3c013c7
test: pin the pending request's identity across a keep
LukaszKokot d7dc141
feat: extract the iTwins page request into iTwinsApi
LukaszKokot 005ec99
refactor: rebuild useITwinData on the shared infinite query core
LukaszKokot ea958d0
test: pin the cleared favorites filter and inline token provider beha…
LukaszKokot f186d0a
chore: add the rush change file
LukaszKokot 5e12253
test: pin that a shouldRefetchFavorites flip does not restart an unfi…
LukaszKokot f6388ef
docs: tighten the comments on the new hooks and the adapter
LukaszKokot e58a573
docs: record why the query keys on credential presence
LukaszKokot 2701a6e
refactor: report state changes without a dedicated hook
LukaszKokot ce53559
Change comment
LukaszKokot 65a48da
Address code review comments
LukaszKokot 1ba02cb
refactor: key the query on the access token itself
LukaszKokot 249846d
refactor: rename the query-change policy to shouldRestartQuery
LukaszKokot 04a6353
docs: note the preserved behavior for hasMore and isFetching
LukaszKokot 7d43e71
Merge remote-tracking branch 'origin/main' into lk/useitwindata-infin…
LukaszKokot 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
20 changes: 20 additions & 0 deletions
20
.../changes/@itwin/imodel-browser-react/lk-useitwindata-infinite-query_2026-09-04-00-00.json
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@itwin/imodel-browser-react", | ||
| "comment": "Stop a `shouldRefetchFavorites` flip from restarting a query with more pages to load", | ||
| "type": "patch" | ||
| }, | ||
| { | ||
| "packageName": "@itwin/imodel-browser-react", | ||
| "comment": "Do not offer `fetchMore` while an access token is required", | ||
| "type": "patch" | ||
| }, | ||
| { | ||
| "packageName": "@itwin/imodel-browser-react", | ||
| "comment": "Omit the empty `Cache-Control` header unless the favorites cache is bypassed", | ||
| "type": "patch" | ||
| } | ||
| ], | ||
| "packageName": "@itwin/imodel-browser-react" | ||
| } |
212 changes: 212 additions & 0 deletions
212
packages/modules/imodel-browser/src/containers/ITwinGrid/iTwinsApi.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
| * See LICENSE.md in the project root for license terms and full copyright notice. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { rest } from "msw"; | ||
|
|
||
| import { server } from "../../tests/mocks/server"; | ||
| import { ITwinFull } from "../../types"; | ||
| import { | ||
| buildITwinsPageUrl, | ||
| fetchITwinsPage, | ||
| ITwinQueryParams, | ||
| ITWINS_PAGE_SIZE, | ||
| } from "./iTwinsApi"; | ||
|
|
||
| describe("iTwinsApi", () => { | ||
| const baseQuery: ITwinQueryParams = { | ||
| requestType: "", | ||
| filterText: "", | ||
| iTwinSubClass: "Project", | ||
| orderby: undefined, | ||
| accessToken: "accessToken", | ||
| }; | ||
|
|
||
| describe("buildITwinsPageUrl", () => { | ||
| it("pages the default request", () => { | ||
| expect(buildITwinsPageUrl({ query: baseQuery, page: 2 })).toEqual( | ||
| "https://api.bentley.com/itwins/?subClass=Project&$skip=200&$top=100" | ||
| ); | ||
| }); | ||
|
|
||
| it("uses the endpoint of a client side filtered request type", () => { | ||
| expect( | ||
| buildITwinsPageUrl({ | ||
| query: { ...baseQuery, requestType: "favorites" }, | ||
| page: 0, | ||
| }) | ||
| ).toContain("/itwins/favorites?subClass=Project"); | ||
| }); | ||
|
|
||
| it("sends an empty subClass for All", () => { | ||
| expect( | ||
| buildITwinsPageUrl({ | ||
| query: { ...baseQuery, iTwinSubClass: "All" }, | ||
| page: 0, | ||
| }) | ||
| ).toContain("?subClass=&"); | ||
| }); | ||
|
|
||
| it("uses the server environment prefix", () => { | ||
| expect( | ||
| buildITwinsPageUrl({ | ||
| query: { ...baseQuery, serverEnvironmentPrefix: "dev" }, | ||
| page: 0, | ||
| }) | ||
| ).toContain("https://dev-api.bentley.com/itwins/"); | ||
| }); | ||
|
|
||
| it("searches with the filter text, encoded and trimmed", () => { | ||
| expect( | ||
| buildITwinsPageUrl({ | ||
| query: { ...baseQuery, filterText: " a b+c " }, | ||
| page: 0, | ||
| }) | ||
| ).toContain("&$search=a%20b%2Bc"); | ||
| }); | ||
|
|
||
| it("orders with the orderby, encoded", () => { | ||
| expect( | ||
| buildITwinsPageUrl({ | ||
| query: { ...baseQuery, orderby: "displayName ASC" }, | ||
| page: 0, | ||
| }) | ||
| ).toContain("&$orderby=displayName%20ASC"); | ||
| }); | ||
|
|
||
| it("drops search and orderby for a client side filtered request type", () => { | ||
| const url = buildITwinsPageUrl({ | ||
| query: { | ||
| ...baseQuery, | ||
| requestType: "recents", | ||
| filterText: "ignored", | ||
| orderby: "displayName ASC", | ||
| }, | ||
| page: 0, | ||
| }); | ||
|
|
||
| expect(url).not.toContain("$search"); | ||
| expect(url).not.toContain("$orderby"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fetchITwinsPage", () => { | ||
| const iTwins: ITwinFull[] = [{ id: "alpha", displayName: "alpha" }]; | ||
| const requestWatcher = jest.fn(); | ||
|
|
||
| beforeAll(() => server.listen()); | ||
| afterEach(() => { | ||
| server.resetHandlers(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
| afterAll(() => server.close()); | ||
|
|
||
| const pinResponse = ({ | ||
| status = 200, | ||
| body = { iTwins }, | ||
| totalCount, | ||
| text, | ||
| }: { | ||
| status?: number; | ||
| body?: unknown; | ||
| totalCount?: string; | ||
| text?: string; | ||
| }) => | ||
| server.use( | ||
| rest.get("https://api.bentley.com/itwins/", (req, res, ctx) => { | ||
| requestWatcher({ | ||
| authorization: req.headers.get("Authorization"), | ||
| cacheControl: req.headers.get("Cache-Control"), | ||
| accept: req.headers.get("Accept"), | ||
| prefer: req.headers.get("Prefer"), | ||
| totalCount: req.headers.get("x-total-count"), | ||
| }); | ||
| return res( | ||
| ctx.status(status), | ||
| ...(text === undefined ? [ctx.json(body)] : [ctx.text(text)]), | ||
| ...(totalCount === undefined | ||
| ? [] | ||
| : [ctx.set("x-total-count", totalCount)]) | ||
| ); | ||
| }) | ||
| ); | ||
|
|
||
| const fetchWith = ( | ||
| overrides: Partial<Parameters<typeof fetchITwinsPage>[0]> = {} | ||
| ) => | ||
| fetchITwinsPage({ | ||
| query: baseQuery, | ||
| page: 0, | ||
| accessToken: "accessToken", | ||
| bypassCache: false, | ||
| signal: new AbortController().signal, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| it("returns the page and no total count when the response carries none", async () => { | ||
| pinResponse({}); | ||
|
|
||
| await expect(fetchWith()).resolves.toEqual({ | ||
| items: iTwins, | ||
| hasMore: false, | ||
| totalCount: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it("reads the total count header", async () => { | ||
| pinResponse({ totalCount: "42" }); | ||
|
|
||
| await expect(fetchWith()).resolves.toMatchObject({ totalCount: 42 }); | ||
| }); | ||
|
|
||
| it("has more pages when the page is full", async () => { | ||
| const fullPage = Array.from( | ||
| { length: ITWINS_PAGE_SIZE }, | ||
| (_unused, index) => ({ id: `id${index}` }) | ||
| ); | ||
| pinResponse({ body: { iTwins: fullPage } }); | ||
|
|
||
| await expect(fetchWith()).resolves.toMatchObject({ hasMore: true }); | ||
| }); | ||
|
|
||
| it("sends the platform headers and no Cache-Control by default", async () => { | ||
| pinResponse({}); | ||
|
|
||
| await fetchWith(); | ||
|
|
||
| expect(requestWatcher).toHaveBeenCalledWith({ | ||
| authorization: "accessToken", | ||
| cacheControl: null, | ||
| accept: "application/vnd.bentley.itwin-platform.v1+json", | ||
| prefer: "return=representation", | ||
| totalCount: "true", | ||
| }); | ||
| }); | ||
|
|
||
| it("sends no-cache when told to bypass the cache", async () => { | ||
| pinResponse({}); | ||
|
|
||
| await fetchWith({ bypassCache: true }); | ||
|
|
||
| expect(requestWatcher).toHaveBeenCalledWith( | ||
| expect.objectContaining({ cacheControl: "no-cache" }) | ||
| ); | ||
| }); | ||
|
|
||
| it("resolves a token provider at request time", async () => { | ||
| pinResponse({}); | ||
|
|
||
| await fetchWith({ accessToken: async () => "fresh" }); | ||
|
|
||
| expect(requestWatcher).toHaveBeenCalledWith( | ||
| expect.objectContaining({ authorization: "fresh" }) | ||
| ); | ||
| }); | ||
|
|
||
| it("throws what the api answered", async () => { | ||
| pinResponse({ status: 401, text: "no soup for you" }); | ||
|
|
||
| await expect(fetchWith()).rejects.toEqual(new Error("no soup for you")); | ||
| }); | ||
| }); | ||
| }); |
94 changes: 94 additions & 0 deletions
94
packages/modules/imodel-browser/src/containers/ITwinGrid/iTwinsApi.ts
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
| * See LICENSE.md in the project root for license terms and full copyright notice. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| import { LoadedPage } from "../../hooks/infiniteQueryReducer"; | ||
| import { AccessTokenProvider, ITwinDataQuery, ITwinFull } from "../../types"; | ||
| import { _getAPIServer } from "../../utils/_apiOverrides"; | ||
|
|
||
| export const ITWINS_PAGE_SIZE = 100; | ||
|
|
||
| /** Favorites and recents come whole and are filtered in the browser. */ | ||
| export const isClientSideFiltered = ( | ||
| requestType: ITwinDataQuery["requestType"] | ||
| ) => requestType === "favorites" || requestType === "recents"; | ||
|
|
||
| /** Everything that identifies a request, so two equal values generate the same query. */ | ||
| export interface ITwinQueryParams extends ITwinDataQuery { | ||
| accessToken?: AccessTokenProvider; | ||
| serverEnvironmentPrefix?: "" | "dev" | "qa"; | ||
| providedData?: ITwinFull[]; | ||
| } | ||
|
|
||
| export const buildITwinsPageUrl = ({ | ||
| query, | ||
| page, | ||
| }: { | ||
| query: ITwinQueryParams; | ||
| page: number; | ||
| }) => { | ||
| const { requestType, filterText, iTwinSubClass, orderby } = query; | ||
| const clientSideFiltered = isClientSideFiltered(requestType); | ||
| const endpoint = clientSideFiltered ? requestType : ""; | ||
| const subClass = `?subClass=${iTwinSubClass === "All" ? "" : iTwinSubClass}`; | ||
| const paging = `&$skip=${page * ITWINS_PAGE_SIZE}&$top=${ITWINS_PAGE_SIZE}`; | ||
| // Hand-built rather than URLSearchParams, which would encode the $ and turn spaces into +. | ||
| const search = | ||
| clientSideFiltered || !filterText | ||
| ? "" | ||
| : `&$search=${encodeURIComponent(filterText.trim())}`; | ||
| const ordering = | ||
| clientSideFiltered || !orderby | ||
| ? "" | ||
| : `&$orderby=${encodeURIComponent(orderby.trim())}`; | ||
| const server = _getAPIServer(query.serverEnvironmentPrefix); | ||
| return `${server}/itwins/${endpoint}${subClass}${paging}${search}${ordering}`; | ||
| }; | ||
|
|
||
| export interface FetchITwinsPageOptions { | ||
| query: ITwinQueryParams; | ||
| page: number; | ||
| accessToken: AccessTokenProvider; | ||
| bypassCache: boolean; | ||
| signal: AbortSignal; | ||
| } | ||
|
|
||
| const platformHeaders = async ({ | ||
| accessToken, | ||
| bypassCache, | ||
| }: { | ||
| accessToken: AccessTokenProvider; | ||
| bypassCache: boolean; | ||
| }): Promise<Record<string, string>> => ({ | ||
| Authorization: | ||
| typeof accessToken === "function" ? await accessToken() : accessToken, | ||
| Accept: "application/vnd.bentley.itwin-platform.v1+json", | ||
| Prefer: "return=representation", | ||
| "x-total-count": "true", | ||
| ...(bypassCache ? { "Cache-Control": "no-cache" } : {}), | ||
| }); | ||
|
|
||
| /** Rejects with the text a non-OK response carried, so callers see the API's own message. */ | ||
| export const fetchITwinsPage = async ({ | ||
| query, | ||
| page, | ||
| accessToken, | ||
| bypassCache, | ||
| signal, | ||
| }: FetchITwinsPageOptions): Promise<LoadedPage<ITwinFull>> => { | ||
| const response = await fetch(buildITwinsPageUrl({ query, page }), { | ||
| signal, | ||
| headers: await platformHeaders({ accessToken, bypassCache }), | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error(await response.text()); | ||
| } | ||
| const { iTwins }: { iTwins: ITwinFull[] } = await response.json(); | ||
| const totalCountHeader = response.headers.get("x-total-count"); | ||
| return { | ||
| items: iTwins, | ||
| hasMore: iTwins.length === ITWINS_PAGE_SIZE, | ||
| totalCount: | ||
| totalCountHeader !== null ? Number(totalCountHeader) : undefined, | ||
| }; | ||
| }; | ||
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.