|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import ContentLoader from 'react-content-loader'; |
| 3 | +import { View } from 'react-native'; |
| 4 | +import useSWR from 'swr'; |
| 5 | + |
| 6 | +import { A, H6Section, Label, useLayout } from '~/common/styleguide'; |
| 7 | +import EntityCounter from '~/components/Package/EntityCounter'; |
| 8 | +import UserAvatar from '~/components/Package/UserAvatar'; |
| 9 | +import Tooltip from '~/components/Tooltip'; |
| 10 | +import { type GitHubUser } from '~/types'; |
| 11 | +import { TimeRange } from '~/util/datetime'; |
| 12 | +import { pluralize } from '~/util/strings'; |
| 13 | +import tw from '~/util/tailwind'; |
| 14 | + |
| 15 | +type Props = { |
| 16 | + fullName: string; |
| 17 | +}; |
| 18 | + |
| 19 | +const LIMIT = 38; |
| 20 | + |
| 21 | +export default function RepositoryContributors({ fullName }: Props) { |
| 22 | + const { isSmallScreen } = useLayout(); |
| 23 | + const { data, isLoading } = useSWR( |
| 24 | + `https://api.github.com/repos/${fullName}/contributors?per_page=${LIMIT}`, |
| 25 | + (url: string) => fetch(url).then(res => res.json()), |
| 26 | + { |
| 27 | + dedupingInterval: TimeRange.HOUR * 1000, |
| 28 | + revalidateOnFocus: false, |
| 29 | + } |
| 30 | + ); |
| 31 | + |
| 32 | + const contributors: GitHubUser[] = useMemo( |
| 33 | + () => |
| 34 | + data?.sort((a: GitHubUser, b: GitHubUser) => (a.contributions < b.contributions ? 1 : -1)), |
| 35 | + [data] |
| 36 | + ); |
| 37 | + |
| 38 | + if (isLoading) { |
| 39 | + return ( |
| 40 | + <> |
| 41 | + <H6Section style={tw`mt-3 flex gap-1.5`}>Contributors</H6Section> |
| 42 | + <ContentLoader |
| 43 | + speed={2} |
| 44 | + width="100%" |
| 45 | + height={36} |
| 46 | + backgroundColor={tw.prefixMatch('dark') ? '#2a2e36' : '#f3f3f3'} |
| 47 | + foregroundColor={tw.prefixMatch('dark') ? '#383c42' : '#ecebeb'}> |
| 48 | + <circle cx="18" cy="18" r="18" /> |
| 49 | + <circle cx="62" cy="18" r="18" /> |
| 50 | + <circle cx="106" cy="18" r="18" /> |
| 51 | + <circle cx="150" cy="18" r="18" /> |
| 52 | + <circle cx="194" cy="18" r="18" /> |
| 53 | + </ContentLoader> |
| 54 | + </> |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + const hasMore = contributors?.length >= LIMIT; |
| 59 | + |
| 60 | + return ( |
| 61 | + <> |
| 62 | + <H6Section style={tw`mt-3 flex gap-1.5`}> |
| 63 | + Contributors |
| 64 | + <EntityCounter count={hasMore ? `${LIMIT}+` : contributors.length} /> |
| 65 | + {!isSmallScreen && hasMore && ( |
| 66 | + <A href={`https://github.com/${fullName}/contributors`} style={tw`ml-auto`}> |
| 67 | + <Label style={tw`font-light`}>See all contributors</Label> |
| 68 | + </A> |
| 69 | + )} |
| 70 | + </H6Section> |
| 71 | + <View style={tw`flex-row flex-wrap items-start gap-2`}> |
| 72 | + {contributors.map(contributor => ( |
| 73 | + <View style={tw`flex flex-row items-center gap-3 bg-transparent`} key={contributor.login}> |
| 74 | + <Tooltip |
| 75 | + sideOffset={2} |
| 76 | + delayDuration={100} |
| 77 | + trigger={ |
| 78 | + <View> |
| 79 | + <A href={contributor.html_url} style={tw`contents`}> |
| 80 | + <UserAvatar src={contributor.avatar_url} alt={`${contributor.login} avatar`} /> |
| 81 | + </A> |
| 82 | + </View> |
| 83 | + }> |
| 84 | + <View> |
| 85 | + <Label style={tw`text-[13px] leading-[18px]`}>{contributor.login}</Label> |
| 86 | + <span style={tw`text-[11px] font-light text-palette-gray4 dark:text-secondary`}> |
| 87 | + {contributor.contributions} {pluralize('contribution', contributor.contributions)} |
| 88 | + </span> |
| 89 | + </View> |
| 90 | + </Tooltip> |
| 91 | + </View> |
| 92 | + ))} |
| 93 | + </View> |
| 94 | + </> |
| 95 | + ); |
| 96 | +} |
0 commit comments