| 1 | "use client"; |
| 2 | |
| 3 | import { useQuery } from |
| 4 | import { LockIcon } |
| 5 | import { useState } |
| 6 | import { toast } |
| 7 | |
| 8 | import { Button } |
| 9 | import { |
| 10 | Field, |
| 11 | FieldDescription, |
| 12 | FieldError, |
| 13 | FieldLabel, |
| 14 | } from "@/components/ui/field"; |
| 15 | import { |
| 16 | Combobox, |
| 17 | ComboboxContent, |
| 18 | ComboboxEmpty, |
| 19 | ComboboxInput, |
| 20 | ComboboxItem, |
| 21 | ComboboxList, |
| 22 | } from "@/components/ui/combobox"; |
| 23 | import { Spinner } |
| 24 | import { apiClient, apiErrorMessage |
| 25 | import type { components |
| 26 | import { authClient } |
| 27 | |
| 28 | type GitHubRepository |
| 29 | |
| 30 | /** The optional GitHub repository to import. Until a GitHub account is linked it offers to link one instead, since the import reads with that account's token. */ |
| 31 | export function GitHubSourceField({ |
| 32 | id, |
| 33 | connected, |
| 34 | error, |
| 35 | value, |
| 36 | onChange, |
| 37 | }: { |
| 38 | id: string |
| 39 | connected: boolean |
| 40 | error?: { |
| 41 | value: string |
| 42 | onChange: (source |
| 43 | }) { |
| 44 | const [linking, |
| 45 | |
| 46 | return ( |
| 47 | <Field> |
| 48 | <FieldLabel htmlFor={ |
| 49 | Import from GitHub |
| 50 | <span className="font-normal text-muted-foreground |
| 51 | </FieldLabel> |
| 52 | {connected ? ( |
| 53 | <RepositoryPicker |
| 54 | id={id} |
| 55 | value={value} |
| 56 | onChange={onChange} |
| 57 | invalid={Boolean(error |
| 58 | /> |
| 59 | ) : ( |
| 60 | <Button |
| 61 | type="button" |
| 62 | variant="outline" |
| 63 | className="w-fit" |
| 64 | disabled={linking} |
| 65 | onClick={() => |
| 66 | setLinking(true); |
| 67 | // `repo` lets the import read private repositories as well as public ones. |
| 68 | void authClient |
| 69 | .linkSocial({ |
| 70 | provider: "github", |
| 71 | scopes: ["repo"], |
| 72 | callbackURL: window.location |
| 73 | }) |
| 74 | .then(({ error |
| 75 | if (!error) return |
| 76 | setLinking(false); |
| 77 | toast.error(error |
| 78 | }); |
| 79 | }} |
| 80 | > |
| 81 | {linking && <Spinner |
| 82 | Connect GitHub to import |
| 83 | </Button> |
| 84 | )} |
| 85 | <FieldDescription> |
| 86 | Brings over the code, releases, issues and pull requests. Leave empty |
| 87 | for an empty repository. |
| 88 | </FieldDescription> |
| 89 | <FieldError errors={ |
| 90 | </Field> |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** Picks from the linked account's repositories rather than taking a typed name, so a typo cannot start an import of the wrong thing. The list is fetched once and filtered as the user types. */ |
| 95 | function RepositoryPicker({ |
| 96 | id, |
| 97 | value, |
| 98 | onChange, |
| 99 | invalid, |
| 100 | }: { |
| 101 | id: string |
| 102 | value: string |
| 103 | onChange: (source |
| 104 | invalid: boolean |
| 105 | }) { |
| 106 | // A modal dialog makes everything outside it inert, so the list renders inside the field rather than at the end of <body>, where it could be neither scrolled nor clicked. |
| 107 | const [container, |
| 108 | |
| 109 | const { |
| 110 | data: repositories = |
| 111 | isPending, |
| 112 | error, |
| 113 | } = useQuery({ |
| 114 | queryKey: ["github-repositories"], |
| 115 | queryFn: async () |
| 116 | const { data, |
| 117 | "/api/imports/github/repositories", |
| 118 | ); |
| 119 | if (error) throw |
| 120 | return data.repositories |
| 121 | }, |
| 122 | }); |
| 123 | |
| 124 | const selected = |
| 125 | repositories.find((repository |
| 126 | (value ? { |
| 127 | |
| 128 | return ( |
| 129 | <Combobox<GitHubRepository |
| 130 | items={repositories} |
| 131 | value={selected} |
| 132 | onValueChange={(repository) |
| 133 | itemToStringLabel={(repository) |
| 134 | isItemEqualToValue={(item, |
| 135 | > |
| 136 | <ComboboxInput |
| 137 | id={id} |
| 138 | className="w-full" |
| 139 | placeholder="Search your GitHub repositories" |
| 140 | aria-invalid={invalid} |
| 141 | showClear={Boolean(value |
| 142 | /> |
| 143 | <div ref={setContainer |
| 144 | <ComboboxContent container={ |
| 145 | <ComboboxEmpty> |
| 146 | {isPending |
| 147 | ? "Loading your GitHub repositories..." |
| 148 | : error |
| 149 | ? error.message |
| 150 | : "No repositories found."} |
| 151 | </ComboboxEmpty> |
| 152 | <ComboboxList> |
| 153 | {(repository: GitHubRepository |
| 154 | <ComboboxItem key={ |
| 155 | <div className="min-w-0 |
| 156 | <div className="flex items-center gap-1.5 |
| 157 | <span className="truncate |
| 158 | {repository.private && |
| 159 | <LockIcon |
| 160 | className="size-3 text-muted-foreground" |
| 161 | aria-label="Private" |
| 162 | /> |
| 163 | )} |
| 164 | </div> |
| 165 | {repository.description && |
| 166 | <p className="truncate text-xs text-muted-foreground |
| 167 | {repository.description} |
| 168 | </p> |
| 169 | )} |
| 170 | </div> |
| 171 | </ComboboxItem> |
| 172 | )} |
| 173 | </ComboboxList> |
| 174 | </ComboboxContent> |
| 175 | </Combobox> |
| 176 | ); |
| 177 | } |