@@ -14,7 +14,7 @@ import {
1414 apiQueryClient ,
1515 instanceCan ,
1616 usePrefetchedApiQuery ,
17- type InstanceState ,
17+ type Instance ,
1818} from '@oxide/api'
1919import { PrevArrow12Icon } from '@oxide/design-system/icons/react'
2020
@@ -53,14 +53,24 @@ SerialConsolePage.loader = async ({ params }: LoaderFunctionArgs) => {
5353 return null
5454}
5555
56+ function isStarting ( i : Instance | undefined ) {
57+ return i ?. runState === 'creating' || i ?. runState === 'starting'
58+ }
59+
5660export function SerialConsolePage ( ) {
5761 const instanceSelector = useInstanceSelector ( )
5862 const { project, instance } = instanceSelector
5963
60- const { data : instanceData } = usePrefetchedApiQuery ( 'instanceView' , {
61- query : { project } ,
62- path : { instance } ,
63- } )
64+ const { data : instanceData } = usePrefetchedApiQuery (
65+ 'instanceView' ,
66+ {
67+ query : { project } ,
68+ path : { instance } ,
69+ } ,
70+ // if we land here and the instance is starting, we will not be able to
71+ // connect, so we poll and connect as soon as it's running.
72+ { refetchInterval : ( q ) => ( isStarting ( q . state . data ) ? 1000 : false ) }
73+ )
6474
6575 const ws = useRef < WebSocket | null > ( null )
6676
@@ -140,7 +150,7 @@ export function SerialConsolePage() {
140150 { connectionStatus === 'connecting' && < ConnectingSkeleton /> }
141151 { connectionStatus === 'error' && < ErrorSkeleton /> }
142152 { connectionStatus === 'closed' && ! canConnect && (
143- < CannotConnect instanceState = { instanceData . runState } />
153+ < CannotConnect instance = { instanceData } />
144154 ) }
145155 { /* closed && canConnect shouldn't be possible because there's no way to
146156 * close an open connection other than leaving the page */ }
@@ -161,21 +171,20 @@ export function SerialConsolePage() {
161171 )
162172}
163173
164- function SerialSkeleton ( {
165- children,
166- connecting,
167- } : {
174+ type SkeletonProps = {
168175 children : React . ReactNode
169- connecting ?: boolean
170- } ) {
176+ animate ?: boolean
177+ }
178+
179+ function SerialSkeleton ( { children, animate } : SkeletonProps ) {
171180 return (
172181 < div className = "relative h-full shrink grow overflow-hidden" >
173182 < div className = "h-full space-y-2 overflow-hidden" >
174183 { [ ...Array ( 200 ) ] . map ( ( _e , i ) => (
175184 < div
176185 key = { i }
177186 className = { cn ( 'h-4 rounded bg-tertiary' , {
178- 'motion-safe:animate-pulse' : connecting ,
187+ 'motion-safe:animate-pulse' : animate ,
179188 } ) }
180189 style = { {
181190 width : `${ Math . sin ( Math . sin ( i ) ) * 20 + 40 } %` ,
@@ -198,22 +207,24 @@ function SerialSkeleton({
198207}
199208
200209const ConnectingSkeleton = ( ) => (
201- < SerialSkeleton connecting >
210+ < SerialSkeleton animate >
202211 < Spinner size = "lg" />
203212 < div className = "mt-4 text-center" >
204213 < p className = "text-sans-xl" > Connecting to serial console</ p >
205214 </ div >
206215 </ SerialSkeleton >
207216)
208217
209- const CannotConnect = ( { instanceState } : { instanceState : InstanceState } ) => (
210- < SerialSkeleton >
218+ const CannotConnect = ( { instance } : { instance : Instance } ) => (
219+ < SerialSkeleton animate = { isStarting ( instance ) } >
211220 < p className = "flex items-center justify-center text-sans-xl" >
212- < span > The instance is</ span >
213- < InstanceStatusBadge className = "ml-1" status = { instanceState } />
221+ < span > The instance is </ span >
222+ < InstanceStatusBadge className = "ml-1.5 " status = { instance . runState } />
214223 </ p >
215- < p className = "mt-2 text-center text-secondary" >
216- You can only connect to the serial console on a running instance.
224+ < p className = "mt-2 text-balance text-center text-secondary" >
225+ { isStarting ( instance )
226+ ? 'Waiting for the instance to start before connecting.'
227+ : 'You can only connect to the serial console on a running instance.' }
217228 </ p >
218229 </ SerialSkeleton >
219230)
0 commit comments