-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathuseUserGithubRepos.ts
More file actions
35 lines (30 loc) · 952 Bytes
/
useUserGithubRepos.ts
File metadata and controls
35 lines (30 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import type { PublicProfile } from '../../../lib/user';
import { getUserGithubRepositories } from '../../../graphql/github';
import { generateQueryKey, RequestKey, StaleTime } from '../../../lib/query';
export function useUserGithubRepos(user: PublicProfile | null) {
const hasGithub = useMemo(() => {
if (!user?.socialLinks) {
return false;
}
return user.socialLinks.some((link) => link.platform === 'github');
}, [user?.socialLinks]);
const queryKey = generateQueryKey(
RequestKey.UserGithubRepos,
user,
'profile',
);
const query = useQuery({
queryKey,
queryFn: () => getUserGithubRepositories(user?.id as string),
staleTime: StaleTime.Default,
enabled: !!user?.id && hasGithub,
});
const repos = useMemo(() => query.data ?? [], [query.data]);
return {
...query,
repos,
hasGithub,
};
}