|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import { RefreshCw } from 'lucide-react'; |
3 | 4 | import { useParams } from 'next/navigation'; |
4 | | -import { FC } from 'react'; |
| 5 | +import { signIn, useSession } from 'next-auth/react'; |
| 6 | +import { usePostHog } from 'posthog-js/react'; |
| 7 | +import { |
| 8 | + Children, |
| 9 | + cloneElement, |
| 10 | + ComponentProps, |
| 11 | + FC, |
| 12 | + ReactElement, |
| 13 | + useCallback, |
| 14 | + useEffect, |
| 15 | + useRef, |
| 16 | + useState, |
| 17 | +} from 'react'; |
| 18 | +import { ClipLoader } from 'react-spinners'; |
| 19 | +import { toast } from 'sonner'; |
5 | 20 |
|
6 | 21 | import { Button } from '@/components/ui/button'; |
| 22 | +import { |
| 23 | + Dialog, |
| 24 | + DialogClose, |
| 25 | + DialogContent, |
| 26 | + DialogDescription, |
| 27 | + DialogFooter, |
| 28 | + DialogHeader, |
| 29 | + DialogTitle, |
| 30 | + DialogTrigger, |
| 31 | +} from '@/components/ui/dialog'; |
| 32 | +import { graphqlClient } from '@/lib/graphql/graphql-client'; |
7 | 33 | import { cn } from '@/lib/utils'; |
| 34 | +import { ProfileFetchingStatusDocument, UserFetchingStatus } from '@/types/generated/graphql'; |
8 | 35 |
|
9 | 36 | type FetchUserButtonProps = { |
10 | | - label?: string; |
11 | 37 | className?: string; |
| 38 | + fetchingStatus?: UserFetchingStatus | null; |
| 39 | + fetchingUpdatedAt?: number | null; |
| 40 | + children: ReactElement<ComponentProps<'button'>>; |
12 | 41 | }; |
13 | 42 |
|
14 | | -export const FetchUserButton: FC<FetchUserButtonProps> = ({ label = 'Fetch user from GitHub', className }) => { |
15 | | - // const { data: session } = useSession(); |
16 | | - const params = useParams<{ login: string }>(); |
| 43 | +const REFRESH_INTERVAL = 1_000; |
| 44 | +const CHECK_STATUS_INTERVAL = 20_000; |
| 45 | +const REFRESH_TIMEOUT = 10 * 60 * 1_000; |
| 46 | +const FETCH_MESSAGES = ['Fetching…', 'Still fetching 😐', '👀 👀 👀', 'Ping! Still not ready…']; |
| 47 | + |
| 48 | +export const FetchUserButton: FC<FetchUserButtonProps> = ({ |
| 49 | + fetchingStatus, |
| 50 | + fetchingUpdatedAt, |
| 51 | + className, |
| 52 | + children, |
| 53 | +}) => { |
| 54 | + const { data: session } = useSession(); |
| 55 | + const { login } = useParams<{ login: string }>(); |
| 56 | + const now = Date.now(); |
| 57 | + const posthog = usePostHog(); |
| 58 | + const fetchAttempt = useRef(0); |
| 59 | + const timerRef = useRef<NodeJS.Timeout | null>(null); |
| 60 | + const [loadingLabel, setLoadingLabel] = useState(FETCH_MESSAGES[fetchAttempt.current]); |
| 61 | + const initialFetchingDuration = now - (fetchingUpdatedAt || now); |
| 62 | + const [fetchingDuration, setFetchingDuration] = useState( |
| 63 | + fetchingStatus === 'FETCHING' && initialFetchingDuration < REFRESH_TIMEOUT ? initialFetchingDuration : null, |
| 64 | + ); |
| 65 | + |
| 66 | + const checkFetchingStatus = useCallback(async () => { |
| 67 | + setLoadingLabel('Checking…'); |
| 68 | + const data = await graphqlClient(ProfileFetchingStatusDocument, { login }); |
| 69 | + |
| 70 | + if (data.user === null) { |
| 71 | + if (timerRef.current) { |
| 72 | + clearTimeout(timerRef.current); |
| 73 | + } |
| 74 | + setFetchingDuration(null); |
| 75 | + toast.error('User not found on GitHub'); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (data.user?.fetchingStatus === 'FETCHING') { |
| 80 | + fetchAttempt.current += 1; |
| 81 | + setLoadingLabel(FETCH_MESSAGES[fetchAttempt.current % FETCH_MESSAGES.length]); |
| 82 | + } else { |
| 83 | + window.location.reload(); |
| 84 | + } |
| 85 | + }, [login]); |
17 | 86 |
|
18 | 87 | const fetchUser = async () => { |
19 | | - const res = await fetch(`/api/profile/${params.login}`, { method: 'POST' }); |
| 88 | + setFetchingDuration(0); |
| 89 | + |
| 90 | + posthog.capture('profile.fetch', { login }); |
| 91 | + |
| 92 | + const res = await fetch(`/api/profile/${login}`, { method: 'POST' }); |
20 | 93 |
|
21 | 94 | if (!res.ok) { |
22 | | - throw new Error('Failed to fetch user'); |
| 95 | + toast.error('Failed to fetch user profile'); |
| 96 | + setFetchingDuration(null); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + useEffect(() => { |
| 101 | + if (fetchingDuration === null) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const secondsPassed = Math.floor(fetchingDuration / 1000); |
| 106 | + if (secondsPassed > 0 && secondsPassed % (CHECK_STATUS_INTERVAL / 1000) === 0) { |
| 107 | + checkFetchingStatus(); |
23 | 108 | } |
24 | 109 |
|
25 | | - return res.json(); |
| 110 | + if (fetchingDuration >= REFRESH_TIMEOUT) { |
| 111 | + toast.error('Fetching user profile took too long. Please try again later.'); |
| 112 | + setFetchingDuration(null); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + timerRef.current = setTimeout(() => { |
| 117 | + setFetchingDuration((prev) => (prev || 0) + REFRESH_INTERVAL); |
| 118 | + }, REFRESH_INTERVAL); |
| 119 | + }, [fetchingDuration, checkFetchingStatus]); |
| 120 | + |
| 121 | + const child = Children.only(children); |
| 122 | + const childWithOnClick = cloneElement(child, { |
| 123 | + onClick: session?.user ? fetchUser : undefined, |
| 124 | + disabled: fetchingDuration !== null, |
| 125 | + children: |
| 126 | + fetchingDuration !== null ? ( |
| 127 | + <> |
| 128 | + {loadingLabel} |
| 129 | + <ClipLoader loading size={16} /> |
| 130 | + </> |
| 131 | + ) : ( |
| 132 | + child.props.children |
| 133 | + ), |
| 134 | + }); |
| 135 | + |
| 136 | + const attachDialogIfNeeded = () => { |
| 137 | + if (session?.user) { |
| 138 | + return childWithOnClick; |
| 139 | + } |
| 140 | + |
| 141 | + return ( |
| 142 | + <Dialog> |
| 143 | + <DialogTrigger asChild>{childWithOnClick}</DialogTrigger> |
| 144 | + <DialogContent> |
| 145 | + <DialogHeader> |
| 146 | + <DialogTitle>Hold up — need your OK!</DialogTitle> |
| 147 | + <DialogDescription> |
| 148 | + Fetching a GitHub profile actually fires off around 20–30 GraphQL calls under the hood. That’s no biggie |
| 149 | + for your personal token, but it’d blow through our shared quota. |
| 150 | + </DialogDescription> |
| 151 | + <DialogDescription>Mind signing in with GitHub so we can pull in that user on demand?</DialogDescription> |
| 152 | + </DialogHeader> |
| 153 | + <DialogFooter> |
| 154 | + <DialogClose asChild> |
| 155 | + <Button type="button" variant="secondary"> |
| 156 | + Maybe later |
| 157 | + </Button> |
| 158 | + </DialogClose> |
| 159 | + <Button onClick={() => signIn('github')}>Sign in with GitHub</Button> |
| 160 | + </DialogFooter> |
| 161 | + </DialogContent> |
| 162 | + </Dialog> |
| 163 | + ); |
26 | 164 | }; |
27 | 165 |
|
28 | 166 | return ( |
29 | | - <Button onClick={fetchUser} className={cn(className)}> |
30 | | - {label} |
31 | | - </Button> |
| 167 | + <div className={cn('flex flex-col gap-2', className)}> |
| 168 | + {attachDialogIfNeeded()} |
| 169 | + {fetchingDuration !== null && ( |
| 170 | + <div className="text-xs text-muted-foreground">Time passed: {Math.floor(fetchingDuration / 1000)}s</div> |
| 171 | + )} |
| 172 | + </div> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +export const FetchUserButtonForNotFoundPage: FC<Omit<FetchUserButtonProps, 'children'>> = (props) => { |
| 177 | + return ( |
| 178 | + <FetchUserButton {...props}> |
| 179 | + <Button size="lg">Fetch profile from GitHub</Button> |
| 180 | + </FetchUserButton> |
| 181 | + ); |
| 182 | +}; |
| 183 | + |
| 184 | +export const FetchUserButtonForProfilePage: FC<Omit<FetchUserButtonProps, 'children'>> = (props) => { |
| 185 | + return ( |
| 186 | + <FetchUserButton {...props} className={cn('flex-grow', props.className)}> |
| 187 | + <Button size="sm" className="flex-grow"> |
| 188 | + Refresh |
| 189 | + <RefreshCw className="size-4" /> |
| 190 | + </Button> |
| 191 | + </FetchUserButton> |
32 | 192 | ); |
33 | 193 | }; |
0 commit comments