| 1 | "use client"; |
| 2 | |
| 3 | import { atLeast } from |
| 4 | import { useMutation, useQuery |
| 5 | import { useRouter } |
| 6 | import { useEffect } |
| 7 | |
| 8 | import { Alert, AlertDescription |
| 9 | import { Button } |
| 10 | import { Spinner } |
| 11 | import { apiClient, apiErrorMessage |
| 12 | import type { components |
| 13 | import type { ViewerRole |
| 14 | |
| 15 | type RepositoryImport |
| 16 | |
| 17 | const POLL_MS = 5000 |
| 18 | |
| 19 | /** Shown while the GitHub import that created this repository is unfinished. Polls until it ends, then refreshes the page so the imported code and issues appear. */ |
| 20 | export function ImportBanner({ |
| 21 | username, |
| 22 | repo, |
| 23 | viewerRole, |
| 24 | initial, |
| 25 | }: { |
| 26 | username: string |
| 27 | repo: string |
| 28 | viewerRole: ViewerRole |
| 29 | initial: RepositoryImport |
| 30 | }) { |
| 31 | const router = |
| 32 | const path = |
| 33 | |
| 34 | const { data |
| 35 | queryKey: ["repository-import", |
| 36 | initialData: initial, |
| 37 | queryFn: async () |
| 38 | const { data, |
| 39 | "/api/repositories/{username}/{repo}/import", |
| 40 | path, |
| 41 | ); |
| 42 | if (error) throw |
| 43 | return data; |
| 44 | }, |
| 45 | refetchInterval: (query) |
| 46 | query.state.data |
| 47 | }); |
| 48 | |
| 49 | useEffect(() => |
| 50 | if (data.status |
| 51 | }, [data.status |
| 52 | |
| 53 | const retry = |
| 54 | mutationFn: async () |
| 55 | const { error |
| 56 | "/api/repositories/{username}/{repo}/import/retry", |
| 57 | path, |
| 58 | ); |
| 59 | if (error) throw |
| 60 | }, |
| 61 | onSuccess: () => |
| 62 | }); |
| 63 | |
| 64 | if (data.status |
| 65 | |
| 66 | if (data.status |
| 67 | return ( |
| 68 | <Alert variant=" |
| 69 | <AlertTitle>Importing {data |
| 70 | <AlertDescription> |
| 71 | <p>{data.lastError |
| 72 | {retry.error && |
| 73 | {atLeast(viewerRole, |
| 74 | <Button |
| 75 | size="sm" |
| 76 | variant="outline" |
| 77 | className="mt-2" |
| 78 | disabled={retry.isPending |
| 79 | onClick={() => |
| 80 | > |
| 81 | {retry.isPending && |
| 82 | Retry import |
| 83 | </Button> |
| 84 | )} |
| 85 | </AlertDescription> |
| 86 | </Alert> |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return ( |
| 91 | <Alert className=" |
| 92 | <Spinner /> |
| 93 | <AlertTitle>Importing {data |
| 94 | <AlertDescription> |
| 95 | Code, releases, issues and pull requests appear as they arrive. |
| 96 | {data.lastError && |
| 97 | ` The last attempt failed (${data.lastError |
| 98 | </AlertDescription> |
| 99 | </Alert> |
| 100 | ); |
| 101 | } |