|
| 1 | +import { useState } from 'react'; |
| 2 | +import { useQuery } from 'urql'; |
| 3 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; |
| 4 | +import { CheckIcon } from '@/components/ui/icon'; |
| 5 | +import { Spinner } from '@/components/ui/spinner'; |
| 6 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; |
| 7 | +import { FragmentType, graphql, useFragment } from '@/gql'; |
| 8 | +import { cn } from '@/lib/utils'; |
| 9 | +import { ExternalCompositionSettings } from './external-composition'; |
| 10 | +import { LegacyCompositionSettings } from './legacy-composition'; |
| 11 | +import { NativeCompositionSettings } from './native-composition'; |
| 12 | + |
| 13 | +const CompositionSettings_ProjectConfigurationQuery = graphql(` |
| 14 | + query CompositionSettings_ProjectConfigurationQuery($selector: ProjectSelectorInput!) { |
| 15 | + project(reference: { bySelector: $selector }) { |
| 16 | + id |
| 17 | + ...CompositionSettings_ProjectFragment |
| 18 | + } |
| 19 | + } |
| 20 | +`); |
| 21 | + |
| 22 | +const CompositionSettings_OrganizationFragment = graphql(` |
| 23 | + fragment CompositionSettings_OrganizationFragment on Organization { |
| 24 | + slug |
| 25 | + ...NativeCompositionSettings_OrganizationFragment |
| 26 | + ...ExternalCompositionSettings_OrganizationFragment |
| 27 | + ...LegacyCompositionSettings_OrganizationFragment |
| 28 | + } |
| 29 | +`); |
| 30 | + |
| 31 | +const CompositionSettings_ProjectFragment = graphql(` |
| 32 | + fragment CompositionSettings_ProjectFragment on Project { |
| 33 | + slug |
| 34 | + isNativeFederationEnabled |
| 35 | + externalSchemaComposition { |
| 36 | + endpoint |
| 37 | + } |
| 38 | + ...NativeCompositionSettings_ProjectFragment |
| 39 | + ...ExternalCompositionSettings_ProjectFragment |
| 40 | + ...LegacyCompositionSettings_ProjectFragment |
| 41 | + } |
| 42 | +`); |
| 43 | + |
| 44 | +export const CompositionSettings = (props: { |
| 45 | + project: FragmentType<typeof CompositionSettings_ProjectFragment>; |
| 46 | + organization: FragmentType<typeof CompositionSettings_OrganizationFragment>; |
| 47 | +}) => { |
| 48 | + const project = useFragment(CompositionSettings_ProjectFragment, props.project); |
| 49 | + const organization = useFragment(CompositionSettings_OrganizationFragment, props.organization); |
| 50 | + |
| 51 | + const [projectQuery] = useQuery({ |
| 52 | + query: CompositionSettings_ProjectConfigurationQuery, |
| 53 | + variables: { |
| 54 | + selector: { |
| 55 | + organizationSlug: organization.slug, |
| 56 | + projectSlug: project.slug, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + const externalCompositionConfig = project.externalSchemaComposition; |
| 62 | + const nativeCompositionEnabled = project.isNativeFederationEnabled; |
| 63 | + const activeMode = nativeCompositionEnabled |
| 64 | + ? 'native' |
| 65 | + : externalCompositionConfig |
| 66 | + ? 'external' |
| 67 | + : 'legacy'; |
| 68 | + const [selectedMode, setSelectedMode] = useState<string>(); |
| 69 | + |
| 70 | + return ( |
| 71 | + <Card> |
| 72 | + <CardHeader> |
| 73 | + <CardTitle> |
| 74 | + <a id="composition">Schema Composition</a> |
| 75 | + </CardTitle> |
| 76 | + <CardDescription>Configure how your schemas are composed.</CardDescription> |
| 77 | + </CardHeader> |
| 78 | + <CardContent> |
| 79 | + {projectQuery.fetching ? ( |
| 80 | + <Spinner /> |
| 81 | + ) : ( |
| 82 | + <Tabs value={selectedMode ?? activeMode} onValueChange={setSelectedMode}> |
| 83 | + <TabsList variant="content"> |
| 84 | + <TabsTrigger variant="content" value="native"> |
| 85 | + Native Federation v2 |
| 86 | + {activeMode === 'native' && <CheckIcon size={16} className="ml-2 inline-block" />} |
| 87 | + </TabsTrigger> |
| 88 | + <TabsTrigger variant="content" value="external"> |
| 89 | + External |
| 90 | + {activeMode === 'external' && <CheckIcon size={16} className="ml-2 inline-block" />} |
| 91 | + </TabsTrigger> |
| 92 | + <TabsTrigger |
| 93 | + variant="content" |
| 94 | + value="legacy" |
| 95 | + className={cn( |
| 96 | + 'hover:opacity-100 data-[state=active]:opacity-100', |
| 97 | + activeMode === 'legacy' ? 'opacity-100' : 'opacity-40', |
| 98 | + )} |
| 99 | + > |
| 100 | + Legacy Federation v1 |
| 101 | + {activeMode === 'legacy' && <CheckIcon size={16} className="ml-2 inline-block" />} |
| 102 | + </TabsTrigger> |
| 103 | + </TabsList> |
| 104 | + <TabsContent variant="content" value="native"> |
| 105 | + <NativeCompositionSettings |
| 106 | + project={project} |
| 107 | + organization={organization} |
| 108 | + activeCompositionMode={activeMode} |
| 109 | + /> |
| 110 | + </TabsContent> |
| 111 | + <TabsContent variant="content" value="external"> |
| 112 | + <ExternalCompositionSettings |
| 113 | + project={project} |
| 114 | + organization={organization} |
| 115 | + activeCompositionMode={activeMode} |
| 116 | + /> |
| 117 | + </TabsContent> |
| 118 | + <TabsContent variant="content" value="legacy"> |
| 119 | + <LegacyCompositionSettings |
| 120 | + project={project} |
| 121 | + organization={organization} |
| 122 | + activeCompositionMode={activeMode} |
| 123 | + /> |
| 124 | + </TabsContent> |
| 125 | + </Tabs> |
| 126 | + )} |
| 127 | + </CardContent> |
| 128 | + </Card> |
| 129 | + ); |
| 130 | +}; |
0 commit comments