-
Notifications
You must be signed in to change notification settings - Fork 898
feat: add usePaginatedQuery hook #10803
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
Changes from 61 commits
b4211f3
173e823
d715d73
8a199b7
98ae96d
37ea50b
2c1e9e3
b3a9ab4
39a2ced
4dcfd29
86f8437
f612d8f
5878326
5cc1c2d
0138f21
a38fd30
22d6c24
e717da1
7624d94
2623131
29242e9
5a9aa2d
3d67304
d977060
9224624
540c779
4b42b6f
5bc43c9
bd146a1
983b83f
a43e294
db03f3e
1b825f1
9631a27
4ea0cba
3f63ec7
614e4ff
b0c8d48
e994532
9502044
8dbbfd3
88d0a5f
132f5b1
33bf4e8
218da68
54f01f1
ede2abc
9ecab16
0c81d1c
3aa714c
2893630
ef900d4
23dc583
24361d1
755f8c0
8bff0d3
1fae773
be82728
848fa0f
c89e8e3
3624a6d
13e2f30
8f83673
5fb0643
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"coderdenttest", | ||
"coderdtest", | ||
"codersdk", | ||
"contravariance", | ||
"cronstrue", | ||
"databasefake", | ||
"dbmem", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { getAuditLogs } from "api/api"; | ||
import { type AuditLogResponse } from "api/typesGenerated"; | ||
import { useFilterParamsKey } from "components/Filter/filter"; | ||
import { type UsePaginatedQueryOptions } from "hooks/usePaginatedQuery"; | ||
|
||
export function paginatedAudits(searchParams: URLSearchParams) { | ||
return { | ||
searchParams, | ||
queryPayload: () => searchParams.get(useFilterParamsKey) ?? "", | ||
queryKey: ({ payload, pageNumber }) => { | ||
return ["auditLogs", payload, pageNumber] as const; | ||
}, | ||
queryFn: ({ payload, limit, offset }) => { | ||
return getAuditLogs({ | ||
offset, | ||
limit, | ||
q: payload, | ||
}); | ||
}, | ||
|
||
cacheTime: 5 * 1000 * 60, | ||
} as const satisfies UsePaginatedQueryOptions<AuditLogResponse, string>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { QueryClient, type UseQueryOptions } from "react-query"; | ||
import * as API from "api/api"; | ||
import { | ||
import type { | ||
AuthorizationRequest, | ||
GetUsersResponse, | ||
UpdateUserPasswordRequest, | ||
|
@@ -10,11 +10,34 @@ import { | |
} from "api/typesGenerated"; | ||
import { getAuthorizationKey } from "./authCheck"; | ||
import { getMetadataAsJSON } from "utils/metadata"; | ||
import { type UsePaginatedQueryOptions } from "hooks/usePaginatedQuery"; | ||
import { prepareQuery } from "utils/filters"; | ||
|
||
export function usersKey(req: UsersRequest) { | ||
return ["users", req] as const; | ||
} | ||
|
||
export function paginatedUsers() { | ||
return { | ||
queryPayload: ({ limit, offset, searchParams }) => { | ||
return { | ||
limit, | ||
offset, | ||
q: prepareQuery(searchParams.get("filter") ?? ""), | ||
}; | ||
}, | ||
|
||
queryKey: ({ payload }) => usersKey(payload), | ||
queryFn: ({ payload, signal }) => API.getUsers(payload, signal), | ||
cacheTime: 5 * 1000 * 60, | ||
} as const satisfies UsePaginatedQueryOptions<GetUsersResponse, UsersRequest>; | ||
} | ||
|
||
export const users = (req: UsersRequest): UseQueryOptions<GetUsersResponse> => { | ||
return { | ||
queryKey: ["users", req], | ||
queryKey: usersKey(req), | ||
queryFn: ({ signal }) => API.getUsers(req, signal), | ||
cacheTime: 5 * 1000 * 60, | ||
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. This function is still being used in one other place that doesn't require paginated data, so I can't get rid of it just yet 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. I'm trying to review kind of quickly since I'm on a time crunch this morning and have made you wait a couple days, but I just wanna make sure that this aligns with the caching stuff @BrunoQuaresma said he's doing 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. Just double-checked, and the caching PR has been committed, so these can be safely removed. There wouldn't be any conflicts – they'd just be redundant |
||
}; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* They do not have the same ESLinter exceptions baked in that the official | ||
* hooks do, especially for dependency arrays. | ||
*/ | ||
import { useCallback, useEffect, useRef } from "react"; | ||
import { useCallback, useLayoutEffect, useRef } from "react"; | ||
|
||
/** | ||
* A DIY version of useEffectEvent. | ||
|
@@ -35,7 +35,10 @@ export function useEffectEvent<TArgs extends unknown[], TReturn = unknown>( | |
callback: (...args: TArgs) => TReturn, | ||
) { | ||
const callbackRef = useRef(callback); | ||
useEffect(() => { | ||
|
||
// useLayoutEffect should be overkill here 99% of the time, but it ensures it | ||
// will run before any other layout effects that need this custom hook | ||
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. eh, you're just describing what 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. Yeah, good point |
||
useLayoutEffect(() => { | ||
callbackRef.current = callback; | ||
}, [callback]); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd maybe just put this as the return type rather that use
satisfies