Skip to content

Commit

Permalink
type safety with enable
Browse files Browse the repository at this point in the history
d option
  • Loading branch information
icedevera committed May 6, 2024
1 parent d3545f7 commit b271d9b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/components/sections/contest/GithubFileSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const GithubFileSelect = () => {
const {data: publicReposData, isLoading: publicReposIsLoading} =
useGetPublicRepos()
const {data: repoFilesData, isLoading: repoFilesIsLoading} = useGetRepoFiles(
selectedRepo ?? {defaultBranch: '', owner: '', repo: ''},
{enabled: selectedRepo !== null},
selectedRepo ?? undefined,
)

if (session.data?.user.provider !== 'github') {
Expand Down
16 changes: 10 additions & 6 deletions src/lib/queries/github/getGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ export const useGetPublicRepos = () => {
return useQuery({...getPublicReposQueryOptions(userId), enabled: !!userId})
}

const getRepoFilesQueryOptions = (params: GetRepoFilesParams) => ({
const getRepoFilesQueryOptions = (params: GetRepoFilesParams | undefined) => ({
queryKey: queryKeys.gitHub.repoFiles(params).queryKey,
queryFn: withApiErrorHandler(() => getRepoFiles(params)),
queryFn: withApiErrorHandler(() => {
if (!params) {
throw new Error('GetRepoFilesParams is required.')
}

return getRepoFiles(params)
}),
})

export const useGetRepoFiles = (
params: GetRepoFilesParams,
options?: {enabled?: boolean},
) => useQuery({...options, ...getRepoFilesQueryOptions(params)})
export const useGetRepoFiles = (params: GetRepoFilesParams | undefined) =>
useQuery({...getRepoFilesQueryOptions(params), enabled: !!params})
2 changes: 1 addition & 1 deletion src/lib/queries/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const queryKeys = createQueryKeyStore({
},
gitHub: {
publicRepos: (userId: string | undefined) => [userId],
repoFiles: (params: GetRepoFilesParams) => [params],
repoFiles: (params: GetRepoFilesParams | undefined) => [params],
},
})

0 comments on commit b271d9b

Please sign in to comment.