This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcodyProRoutes.tsx
More file actions
107 lines (93 loc) · 3.95 KB
/
codyProRoutes.tsx
File metadata and controls
107 lines (93 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Navigate, type RouteObject } from 'react-router-dom'
import { logger } from '@sourcegraph/common'
import { lazyComponent } from '@sourcegraph/shared/src/util/lazyComponent'
import { type LegacyLayoutRouteContext, LegacyRoute } from '../LegacyRouteContext'
import { QueryClientProvider } from './management/api/react-query/QueryClientProvider'
import { useUserCodySubscription } from './subscription/useUserCodySubscription'
import { isEmbeddedCodyProUIEnabled } from './util'
export enum CodyProRoutes {
// The checkout form for a new Cody Pro subscription.
NewProSubscription = '/cody/manage/subscription/new',
// The Manage page is labeled as the "Dashboard" page.
Manage = '/cody/manage',
// The Subscriptions page is a comparison of different Cody product tiers.
Subscription = '/cody/subscription',
SubscriptionManage = '/cody/subscription/manage',
ManageTeam = '/cody/team/manage',
}
/**
* Generally available Cody Pro routes.
*/
const stableRoutes = new Set([CodyProRoutes.Manage, CodyProRoutes.Subscription])
/**
* Determines if a given Cody Pro route should be rendered.
* If the embedded Cody Pro UI is enabled, all routes including experimental are rendered.
* Otherwise, only the generally available routes are rendered.
*/
const isRouteEnabled = (path: CodyProRoutes): boolean => (isEmbeddedCodyProUIEnabled() ? true : stableRoutes.has(path))
export const codyProRoutes: RouteObject[] = Object.values(CodyProRoutes).map(path => ({
path,
element: (
<LegacyRoute
render={props => (
<CodyProPage
path={path}
authenticatedUser={props.authenticatedUser}
telemetryRecorder={props.platformContext.telemetryRecorder}
/>
)}
condition={({ isSourcegraphDotCom, licenseFeatures }) =>
isSourcegraphDotCom && licenseFeatures.isCodyEnabled && isRouteEnabled(path)
}
/>
),
}))
const routeComponents = {
[CodyProRoutes.NewProSubscription]: lazyComponent(
() => import('./management/subscription/new/NewCodyProSubscriptionPage'),
'NewCodyProSubscriptionPage'
),
[CodyProRoutes.Manage]: lazyComponent(() => import('./management/CodyManagementPage'), 'CodyManagementPage'),
[CodyProRoutes.Subscription]: lazyComponent(
() => import('./subscription/CodySubscriptionPage'),
'CodySubscriptionPage'
),
[CodyProRoutes.SubscriptionManage]: lazyComponent(
() => import('./management/subscription/manage/CodySubscriptionManagePage'),
'CodySubscriptionManagePage'
),
[CodyProRoutes.ManageTeam]: lazyComponent(() => import('./team/CodyManageTeamPage'), 'CodyManageTeamPage'),
}
interface CodyProPageProps extends Pick<LegacyLayoutRouteContext, 'authenticatedUser' | 'telemetryRecorder'> {
path: CodyProRoutes
}
/**
* Renders the appropriate Cody Pro page component for the given route path.
*
* This is to more easily isolate the Cody Pro-specific functionality (which
* only applies to non-Enterprise users) from the rest of the Sourcegraph UI.
*/
const CodyProPage: React.FC<CodyProPageProps> = props => {
const { data } = useUserCodySubscription()
if (!data) {
return null
}
if (!data.currentUser) {
// Cody plan is not available if the user is not authenticated. Redirecting to the sign-in page.
return <Navigate to={`/sign-in?returnTo=${CodyProRoutes.Manage}`} replace={true} />
}
if (!data.currentUser.codySubscription) {
logger.error('Cody subscription data is not available.')
return null
}
const Component = routeComponents[props.path]
return (
<QueryClientProvider>
<Component
authenticatedUser={props.authenticatedUser}
telemetryRecorder={props.telemetryRecorder}
codySubscription={data.currentUser.codySubscription}
/>
</QueryClientProvider>
)
}