Skip to content

Commit

Permalink
Pull getInitalProvider out of component
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmurray-codecov committed May 7, 2024
1 parent f7204a7 commit 585ff68
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/pages/RepoPage/CoverageOnboarding/NewRepoTab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lazy, Suspense, useCallback, useMemo } from 'react'
import { lazy, Suspense, useMemo } from 'react'
import { Switch, useHistory, useLocation, useParams } from 'react-router-dom'

import { SentryRoute } from 'sentry'
Expand Down Expand Up @@ -31,6 +31,21 @@ const CI_PROVIDERS = {
OtherCI: 'OtherCI',
} as const
type CIProviderValue = (typeof CI_PROVIDERS)[keyof typeof CI_PROVIDERS]
type CIUrls = Record<keyof typeof CI_PROVIDERS, string>

const getInitialProvider = (provider: string, path: string, urls: CIUrls) => {
const defaultProvider =
providerToName(provider) !== 'Github'
? CI_PROVIDERS.OtherCI
: CI_PROVIDERS.GitHubActions
if (path === urls.CircleCI) {
return CI_PROVIDERS.CircleCI
}
if (path === urls.OtherCI) {
return CI_PROVIDERS.OtherCI
}
return defaultProvider
}

interface CISelectorProps {
provider: string
Expand All @@ -51,28 +66,14 @@ function CISelector({ provider, owner, repo }: CISelectorProps) {
[githubActions, circleCI, newOtherCI, provider, owner, repo]
)

const getInitialProvider = useCallback(() => {
const defaultProvider =
providerToName(provider) !== 'Github'
? CI_PROVIDERS.OtherCI
: CI_PROVIDERS.GitHubActions
if (location.pathname === urls.CircleCI) {
return CI_PROVIDERS.CircleCI
}
if (location.pathname === urls.OtherCI) {
return CI_PROVIDERS.OtherCI
}
return defaultProvider
}, [location, urls, provider])

return (
<Card>
<Card.Header>
<Card.Title size="base">Select your CI</Card.Title>
</Card.Header>
<Card.Content>
<RadioTileGroup
defaultValue={getInitialProvider()}
defaultValue={getInitialProvider(provider, location.pathname, urls)}
onValueChange={(value: CIProviderValue) => {
history.replace(urls[value])
}}
Expand Down

0 comments on commit 585ff68

Please sign in to comment.