@@ -10,31 +10,32 @@ import {
1010} from '@/features/clusters/upsert/lib/calculateDefaultDeploymentPerformanceAndRegionPlans' ;
1111import { UpsertClusterSchema } from '@/features/clusters/upsert/upsertClusterSchema' ;
1212import { useLocalStorage } from '@/hooks/useLocalStorage' ;
13- import { SchemaPlan , SchemaRegion } from '@/lib/api.gen' ;
13+ import { SchemaPlan } from '@/lib/api.gen' ;
1414import { Cluster , Organization } from '@/lib/api.patch' ;
1515import { sortByField } from '@/lib/arrays/sort/byField' ;
1616import { groupThenKeyBy } from '@/lib/groupThenKeyBy' ;
1717import { LocalStorageKeys } from '@/lib/storage/localStorageKeys' ;
1818import { useQuery } from '@tanstack/react-query' ;
1919import { useParams , useRouteContext } from '@tanstack/react-router' ;
20- import { useMemo , useState } from 'react' ;
20+ import { useMemo } from 'react' ;
2121import { z } from 'zod' ;
2222
2323export function UpsertCluster ( ) {
2424 const { organizationId, clusterId } : { organizationId : string ; clusterId ?: string } = useParams ( { strict : false } ) ;
25- const [ onlyShowAvailableHosts , setOnlyShowAvailableHosts ] = useState ( true ) ;
26- const { organization, cluster } : { organization : Organization ; cluster ?: Cluster } = useRouteContext ( { strict : false } ) ;
25+ const { organization, cluster } : {
26+ organization : Organization ;
27+ cluster ?: Cluster
28+ } = useRouteContext ( { strict : false } ) ;
2729 const [ savedClusterState , setSavedClusterState ] = useLocalStorage < null | ( {
2830 clusterId ?: string
2931 } & z . infer < typeof UpsertClusterSchema > ) > ( LocalStorageKeys . SavedClusterState , null ) ;
3032
3133 const { data : planTypes } = useQuery ( getPlanTypesOptions ( organizationId ) ) ;
32- const { data : regionLocationsAvailableHosts } = useQuery ( getRegionLocationsOptions ( {
34+ const { data : regionLocationsColocated } = useQuery ( getRegionLocationsOptions ( {
3335 availableHosts : true ,
3436 organizationId,
3537 } ) ) ;
36- const { data : regionLocationsAll } = useQuery ( getRegionLocationsOptions ( { organizationId } ) ) ;
37- const regionLocations = onlyShowAvailableHosts ? regionLocationsAvailableHosts : regionLocationsAll ;
38+ const { data : regionLocationsDedicated } = useQuery ( getRegionLocationsOptions ( { organizationId } ) ) ;
3839
3940 const alreadyUsingFree = useMemo ( ( ) => {
4041 for ( const orgCluster of organization ?. clusters ?? [ ] ) {
@@ -55,55 +56,53 @@ export function UpsertCluster() {
5556
5657 const deploymentToPerformanceToPlan = useMemo < Record < string , Record < string , SchemaPlan > > > ( ( ) =>
5758 groupThenKeyBy ( planTypes ?. sort ( sortByField ( 'priceUsd' ) ) || [ ] , 'deploymentDescription' , 'performanceDescription' ) , [ planTypes ] ) ;
58- const regionNameToLatencyToRegion = useMemo < Record < string , Record < string , SchemaRegion > > > ( ( ) =>
59- groupThenKeyBy ( regionLocations ?. sort ( sortByField ( 'latencyDescription' ) ) || [ ] , 'region' , 'latencyDescription' ) , [ regionLocations ] ) ;
6059
6160 const defaultValues = useMemo < null | z . infer < typeof UpsertClusterSchema > > ( ( ) => {
6261 if ( savedClusterState ) {
6362 return savedClusterState ;
6463 }
65- if ( ! planTypes || ! regionLocationsAvailableHosts || ! regionLocationsAll || ( clusterId && ! cluster ) ) {
64+ if ( ! planTypes || ! regionLocationsColocated || ! regionLocationsDedicated || ( clusterId && ! cluster ) ) {
6665 return null ;
6766 }
6867
6968 const selectedPlan = planTypes ?. find ( planType => planType . id === cluster ?. plans ?. [ 0 ] . planId ) ;
7069
7170 const regionPlans : z . infer < typeof UpsertClusterSchema . shape . regionPlans > = [ ] ;
7271 const instances : z . infer < typeof UpsertClusterSchema . shape . instances > = [ ] ;
73- const regionLocations = onlyShowAvailableHosts ? regionLocationsAvailableHosts : regionLocationsAll ;
72+ const regionLocations = selectedPlan ?. deploymentDescription !== 'Dedicated'
73+ ? regionLocationsColocated
74+ : regionLocationsDedicated ;
7475 const defaults = calculateDefaultDeploymentPerformanceAndRegionPlans ( planTypes , regionLocations , alreadyUsingFree ) ;
7576
7677 let isSelfManaged = false ;
77- if ( planTypes && regionLocations ) {
78- if ( cluster ) {
79- if ( cluster . plans ) {
80- for ( const plan of cluster . plans ) {
81- if ( plan . regionId ) {
82- const selectedRegion = regionLocations . find ( regionLocation => regionLocation . id === plan . regionId ) ;
83- if ( selectedRegion ) {
84- regionPlans . push ( {
85- regionName : selectedRegion . region ,
86- latencyDescription : selectedRegion . latencyDescription ,
87- } ) ;
88- }
78+ if ( cluster ) {
79+ if ( cluster . plans ) {
80+ for ( const plan of cluster . plans ) {
81+ if ( plan . regionId ) {
82+ const selectedRegion = regionLocations . find ( regionLocation => regionLocation . id === plan . regionId ) ;
83+ if ( selectedRegion ) {
84+ regionPlans . push ( {
85+ regionName : selectedRegion . region ,
86+ latencyDescription : selectedRegion . latencyDescription ,
87+ } ) ;
8988 }
9089 }
9190 }
92- if ( ! regionPlans . length && cluster . instances ) {
93- for ( const instance of cluster . instances ) {
94- if ( instance . status !== 'REMOVED' ) {
95- isSelfManaged = true ;
96- instances . push ( {
97- fqdn : instance . instanceFqdn ,
98- port : instance . operationsApiPort ,
99- secure : instance . operationsApiSecure ? 'true' : 'false' ,
100- } ) ;
101- }
91+ }
92+ if ( ! regionPlans . length && cluster . instances ) {
93+ for ( const instance of cluster . instances ) {
94+ if ( instance . status !== 'REMOVED' ) {
95+ isSelfManaged = true ;
96+ instances . push ( {
97+ fqdn : instance . instanceFqdn ,
98+ port : instance . operationsApiPort ,
99+ secure : instance . operationsApiSecure ? 'true' : 'false' ,
100+ } ) ;
102101 }
103102 }
104- } else if ( defaults ) {
105- regionPlans . push ( ...defaults . regionPlans ) ;
106103 }
104+ } else if ( defaults ) {
105+ regionPlans . push ( ...defaults . regionPlans ) ;
107106 }
108107 if ( ! isSelfManaged && ! regionPlans . length ) {
109108 regionPlans . push ( { regionName : '' , latencyDescription : '' } ) ;
@@ -119,9 +118,9 @@ export function UpsertCluster() {
119118 instances,
120119 regionPlans,
121120 } ;
122- } , [ alreadyUsingFree , cluster , clusterId , onlyShowAvailableHosts , planTypes , regionLocationsAll , regionLocationsAvailableHosts , savedClusterState ] ) ;
121+ } , [ alreadyUsingFree , cluster , clusterId , planTypes , regionLocationsColocated , regionLocationsDedicated , savedClusterState ] ) ;
123122
124- const isLoading = ! defaultValues || ! organization || ! planTypes || ! regionLocations ;
123+ const isLoading = ! defaultValues || ! organization || ! planTypes || ! regionLocationsColocated || ! regionLocationsDedicated ;
125124 if ( isLoading ) {
126125 return (
127126 < SubNavSimpleLayout >
@@ -148,18 +147,17 @@ export function UpsertCluster() {
148147 return (
149148 < SubNavSimpleLayout >
150149 < ClusterForm
150+ alreadyUsingFree = { alreadyUsingFree }
151151 clusterId = { clusterId }
152152 defaultValues = { defaultValues }
153153 deploymentToPerformanceToPlan = { deploymentToPerformanceToPlan }
154154 organization = { organization }
155155 organizationId = { organizationId }
156156 planTypes = { planTypes }
157- regionLocations = { regionLocations }
158- regionNameToLatencyToRegion = { regionNameToLatencyToRegion }
159- setOnlyShowAvailableHosts = { setOnlyShowAvailableHosts }
157+ regionLocationsColocated = { regionLocationsColocated }
158+ regionLocationsDedicated = { regionLocationsDedicated }
160159 setSavedClusterState = { setSavedClusterState }
161160 startOffOnBilling = { ! ! savedClusterState }
162- alreadyUsingFree = { alreadyUsingFree }
163161 />
164162 </ SubNavSimpleLayout >
165163 ) ;
0 commit comments