|
| 1 | +import { useQuery, UseQueryResult } from "@tanstack/react-query"; |
| 2 | +import { fetchNormalized } from "../api"; |
| 3 | +import { RawUserInfo } from "../types"; |
| 4 | +import { handleError, handlePermissionsError, handleRetry, ServerError } from "./helpers"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Hook to get a list of all user emails. |
| 8 | + */ |
| 9 | +export const useUserEmails = (): UseQueryResult<string[], Error> => { |
| 10 | + const queryResult = useQuery<string[], Error>( |
| 11 | + ["users"], |
| 12 | + async () => { |
| 13 | + const response = await fetchNormalized("/users"); |
| 14 | + if (response.ok) { |
| 15 | + return await response.json(); |
| 16 | + } else { |
| 17 | + handlePermissionsError(response.status); |
| 18 | + throw new ServerError("Failed to fetch user info"); |
| 19 | + } |
| 20 | + }, |
| 21 | + { retry: handleRetry } |
| 22 | + ); |
| 23 | + |
| 24 | + handleError(queryResult); |
| 25 | + return queryResult; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Hook to get the current user's info. |
| 30 | + */ |
| 31 | +// TODO: merge with useUserDetails |
| 32 | +export const useUserInfo = (): UseQueryResult<RawUserInfo, Error> => { |
| 33 | + const queryResult = useQuery<RawUserInfo, Error>( |
| 34 | + ["user"], |
| 35 | + async () => { |
| 36 | + const response = await fetchNormalized("/user"); |
| 37 | + if (response.ok) { |
| 38 | + return await response.json(); |
| 39 | + } else { |
| 40 | + handlePermissionsError(response.status); |
| 41 | + throw new ServerError("Failed to fetch user info"); |
| 42 | + } |
| 43 | + }, |
| 44 | + { retry: handleRetry } |
| 45 | + ); |
| 46 | + |
| 47 | + handleError(queryResult); |
| 48 | + return queryResult; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Hook to get the requested user's info |
| 53 | + */ |
| 54 | +// TODO: handle if there is no userId |
| 55 | +export const useUserDetails = (userId?: number): UseQueryResult<RawUserInfo, Error> => { |
| 56 | + const queryResult = useQuery<RawUserInfo, Error>( |
| 57 | + ["userDetails", userId], |
| 58 | + async () => { |
| 59 | + const response = await fetchNormalized(`/user/${userId}`); |
| 60 | + if (response.ok) { |
| 61 | + return await response.json(); |
| 62 | + } else { |
| 63 | + handlePermissionsError(response.status); |
| 64 | + throw new ServerError("Failed to fetch user details"); |
| 65 | + } |
| 66 | + }, |
| 67 | + { |
| 68 | + retry: handleRetry, |
| 69 | + enabled: !!userId // only run query if userId is available |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + handleError(queryResult); |
| 74 | + return queryResult; |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
| 78 | + * Hook to get user info. If userId is provided, fetches details for that user; otherwise, fetches current user's info. |
| 79 | + */ |
| 80 | +export const useUser = (userId?: number): UseQueryResult<RawUserInfo, Error> => { |
| 81 | + const queryKey = userId ? ["userDetails", userId] : ["user"]; |
| 82 | + |
| 83 | + const queryResult = useQuery<RawUserInfo, Error>( |
| 84 | + queryKey, |
| 85 | + async () => { |
| 86 | + const endpoint = userId ? `/user/${userId}` : "/user"; |
| 87 | + const response = await fetchNormalized(endpoint); |
| 88 | + if (response.ok) { |
| 89 | + return await response.json(); |
| 90 | + } else { |
| 91 | + handlePermissionsError(response.status); |
| 92 | + throw new ServerError(userId ? "Failed to fetch user details" : "Failed to fetch user info"); |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + retry: handleRetry, |
| 97 | + enabled: userId !== undefined // Only run the query if userId is provided or if fetching current user's info |
| 98 | + } |
| 99 | + ); |
| 100 | + |
| 101 | + handleError(queryResult); |
| 102 | + return queryResult; |
| 103 | +}; |
0 commit comments