|
| 1 | +import { GitHubRepoPicker } from "@features/folder-picker/components/GitHubRepoPicker"; |
| 2 | +import { BranchSelector } from "@features/git-interaction/components/BranchSelector"; |
| 3 | +import { |
| 4 | + useGithubBranches, |
| 5 | + useGithubRepositories, |
| 6 | + useRepositoryIntegration, |
| 7 | +} from "@hooks/useIntegrations"; |
| 8 | +import { X } from "@phosphor-icons/react"; |
| 9 | +import { Button } from "@posthog/quill"; |
| 10 | +import { Box, Flex, IconButton, Text } from "@radix-ui/themes"; |
| 11 | +import { useState } from "react"; |
| 12 | + |
| 13 | +interface AutostartBaseBranchesSettingsProps { |
| 14 | + /** Current `org/repo` → base branch overrides. */ |
| 15 | + branches: Record<string, string>; |
| 16 | + /** Persist the full next mapping (the caller diffs/optimistically updates). */ |
| 17 | + onChange: (next: Record<string, string>) => void; |
| 18 | + isLoading?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Per-repository base branch overrides for auto-started inbox PRs. |
| 23 | + * |
| 24 | + * Each configured repo opens its auto-PRs against the chosen branch instead of |
| 25 | + * the repo default. Repos without an entry keep targeting the repo default |
| 26 | + */ |
| 27 | +export function AutostartBaseBranchesSettings({ |
| 28 | + branches, |
| 29 | + onChange, |
| 30 | + isLoading = false, |
| 31 | +}: AutostartBaseBranchesSettingsProps) { |
| 32 | + const { |
| 33 | + repositories: allRepositories, |
| 34 | + getIntegrationIdForRepo, |
| 35 | + isLoadingRepos, |
| 36 | + isRefreshingRepos, |
| 37 | + refreshRepositories, |
| 38 | + hasGithubIntegration, |
| 39 | + } = useRepositoryIntegration(); |
| 40 | + const disabled = !hasGithubIntegration; |
| 41 | + |
| 42 | + const [isRepoPickerOpen, setIsRepoPickerOpen] = useState(false); |
| 43 | + const [repoSearch, setRepoSearch] = useState(""); |
| 44 | + const [pendingRepo, setPendingRepo] = useState<string | null>(null); |
| 45 | + |
| 46 | + const repoPage = useGithubRepositories(repoSearch, isRepoPickerOpen); |
| 47 | + |
| 48 | + const selectableRepositories = ( |
| 49 | + isRepoPickerOpen ? repoPage.repositories : allRepositories |
| 50 | + ).filter((repo) => !(repo in branches)); |
| 51 | + |
| 52 | + const entries = Object.entries(branches); |
| 53 | + |
| 54 | + const commit = (repo: string, branch: string) => { |
| 55 | + onChange({ ...branches, [repo]: branch }); |
| 56 | + }; |
| 57 | + |
| 58 | + const remove = (repo: string) => { |
| 59 | + const next = { ...branches }; |
| 60 | + delete next[repo]; |
| 61 | + onChange(next); |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Flex |
| 66 | + direction="column" |
| 67 | + gap="2" |
| 68 | + pt="3" |
| 69 | + style={{ borderTop: "1px dashed var(--gray-5)" }} |
| 70 | + > |
| 71 | + <Flex direction="column" gap="1"> |
| 72 | + <Text className="font-medium text-(--gray-12) text-sm"> |
| 73 | + Base branch for auto-PRs |
| 74 | + </Text> |
| 75 | + <Text className="text-(--gray-11) text-[13px]"> |
| 76 | + Point auto-started inbox PRs at a specific branch per repository. |
| 77 | + Repositories without an override target their default branch. |
| 78 | + </Text> |
| 79 | + </Flex> |
| 80 | + |
| 81 | + {isLoading ? ( |
| 82 | + <Box className="h-[32px] w-[320px] animate-pulse rounded bg-gray-3" /> |
| 83 | + ) : ( |
| 84 | + <Flex direction="column" gap="2"> |
| 85 | + {entries.map(([repo, branch]) => ( |
| 86 | + <BaseBranchRow |
| 87 | + key={repo} |
| 88 | + repo={repo} |
| 89 | + value={branch} |
| 90 | + integrationId={getIntegrationIdForRepo(repo)} |
| 91 | + disabled={disabled} |
| 92 | + onCommit={commit} |
| 93 | + onRemove={remove} |
| 94 | + /> |
| 95 | + ))} |
| 96 | + |
| 97 | + <Flex align="center" gap="2"> |
| 98 | + <Box className="min-w-[220px] max-w-[280px]"> |
| 99 | + <GitHubRepoPicker |
| 100 | + value={pendingRepo} |
| 101 | + onChange={setPendingRepo} |
| 102 | + repositories={selectableRepositories} |
| 103 | + isLoading={ |
| 104 | + isLoadingRepos || (isRepoPickerOpen && repoPage.isPending) |
| 105 | + } |
| 106 | + isRefreshing={isRefreshingRepos} |
| 107 | + onRefresh={refreshRepositories} |
| 108 | + open={isRepoPickerOpen} |
| 109 | + onOpenChange={setIsRepoPickerOpen} |
| 110 | + searchQuery={repoSearch} |
| 111 | + onSearchQueryChange={setRepoSearch} |
| 112 | + hasMore={repoPage.hasMore} |
| 113 | + onLoadMore={repoPage.loadMore} |
| 114 | + disabled={disabled} |
| 115 | + placeholder="Add a repository…" |
| 116 | + size="2" |
| 117 | + /> |
| 118 | + </Box> |
| 119 | + {pendingRepo ? ( |
| 120 | + <BaseBranchRow |
| 121 | + key={`add-${pendingRepo}`} |
| 122 | + repo={pendingRepo} |
| 123 | + value={undefined} |
| 124 | + integrationId={getIntegrationIdForRepo(pendingRepo)} |
| 125 | + disabled={disabled} |
| 126 | + onCommit={(repo, branch) => { |
| 127 | + commit(repo, branch); |
| 128 | + setPendingRepo(null); |
| 129 | + setRepoSearch(""); |
| 130 | + }} |
| 131 | + /> |
| 132 | + ) : null} |
| 133 | + </Flex> |
| 134 | + </Flex> |
| 135 | + )} |
| 136 | + </Flex> |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +interface BaseBranchRowProps { |
| 141 | + repo: string; |
| 142 | + value: string | undefined; |
| 143 | + integrationId: number | undefined; |
| 144 | + disabled?: boolean; |
| 145 | + onCommit: (repo: string, branch: string) => void; |
| 146 | + onRemove?: (repo: string) => void; |
| 147 | +} |
| 148 | + |
| 149 | +function BaseBranchRow({ |
| 150 | + repo, |
| 151 | + value, |
| 152 | + integrationId, |
| 153 | + disabled = false, |
| 154 | + onCommit, |
| 155 | + onRemove, |
| 156 | +}: BaseBranchRowProps) { |
| 157 | + const isAdd = value === undefined; |
| 158 | + const [search, setSearch] = useState(""); |
| 159 | + const [draft, setDraft] = useState<string | null>(null); |
| 160 | + |
| 161 | + const branchQuery = useGithubBranches(integrationId, repo, search, true); |
| 162 | + const hasIntegrationId = !!integrationId; |
| 163 | + |
| 164 | + const selectedBranch = isAdd ? draft : (value ?? null); |
| 165 | + |
| 166 | + return ( |
| 167 | + <Flex align="center" gap="2"> |
| 168 | + {!isAdd ? ( |
| 169 | + <Text className="min-w-[220px] max-w-[280px] truncate text-(--gray-12) text-sm"> |
| 170 | + {repo} |
| 171 | + </Text> |
| 172 | + ) : null} |
| 173 | + <BranchSelector |
| 174 | + repoPath={repo} |
| 175 | + currentBranch={null} |
| 176 | + defaultBranch={branchQuery.data?.defaultBranch ?? null} |
| 177 | + workspaceMode="cloud" |
| 178 | + disabled={disabled || !hasIntegrationId} |
| 179 | + selectedBranch={selectedBranch} |
| 180 | + onBranchSelect={(branch) => { |
| 181 | + if (isAdd) { |
| 182 | + setDraft(branch); |
| 183 | + } else if (branch) { |
| 184 | + onCommit(repo, branch); |
| 185 | + } |
| 186 | + }} |
| 187 | + cloudBranches={branchQuery.data?.branches} |
| 188 | + cloudBranchesLoading={branchQuery.isPending} |
| 189 | + isRefreshing={branchQuery.isRefreshing} |
| 190 | + cloudBranchesFetchingMore={branchQuery.isFetchingMore} |
| 191 | + cloudBranchesHasMore={branchQuery.hasMore} |
| 192 | + cloudSearchQuery={search} |
| 193 | + onCloudSearchChange={setSearch} |
| 194 | + onCloudLoadMore={branchQuery.loadMore} |
| 195 | + /> |
| 196 | + {isAdd ? ( |
| 197 | + <Button |
| 198 | + size="sm" |
| 199 | + disabled={disabled || !draft} |
| 200 | + onClick={() => { |
| 201 | + if (draft) onCommit(repo, draft); |
| 202 | + }} |
| 203 | + > |
| 204 | + Add |
| 205 | + </Button> |
| 206 | + ) : ( |
| 207 | + <IconButton |
| 208 | + variant="ghost" |
| 209 | + color="gray" |
| 210 | + aria-label={`Remove base branch override for ${repo}`} |
| 211 | + disabled={disabled} |
| 212 | + onClick={() => onRemove?.(repo)} |
| 213 | + > |
| 214 | + <X size={14} /> |
| 215 | + </IconButton> |
| 216 | + )} |
| 217 | + </Flex> |
| 218 | + ); |
| 219 | +} |
0 commit comments