|
1 | | -import React from 'react'; |
| 1 | +import React, { useEffect } from 'react'; |
2 | 2 | import TitledGreyBox from '@/components/elements/TitledGreyBox'; |
3 | | -import useServer from '@/plugins/useServer'; |
4 | 3 | import tw from 'twin.macro'; |
5 | 4 | import VariableBox from '@/components/server/startup/VariableBox'; |
6 | 5 | import ServerContentBlock from '@/components/elements/ServerContentBlock'; |
| 6 | +import getServerStartup from '@/api/swr/getServerStartup'; |
| 7 | +import Spinner from '@/components/elements/Spinner'; |
| 8 | +import ServerError from '@/components/screens/ServerError'; |
| 9 | +import { httpErrorToHuman } from '@/api/http'; |
| 10 | +import { ServerContext } from '@/state/server'; |
| 11 | +import { useDeepCompareEffect } from '@/plugins/useDeepCompareEffect'; |
7 | 12 |
|
8 | 13 | const StartupContainer = () => { |
9 | | - const { invocation, variables } = useServer(); |
| 14 | + const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); |
| 15 | + const invocation = ServerContext.useStoreState(state => state.server.data!.invocation); |
| 16 | + const variables = ServerContext.useStoreState(state => state.server.data!.variables); |
| 17 | + |
| 18 | + const { data, error, isValidating, mutate } = getServerStartup(uuid, { invocation, variables }); |
| 19 | + |
| 20 | + const setServerFromState = ServerContext.useStoreActions(actions => actions.server.setServerFromState); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + // Since we're passing in initial data this will not trigger on mount automatically. We |
| 24 | + // want to always fetch fresh information from the API however when we're loading the startup |
| 25 | + // information. |
| 26 | + mutate(); |
| 27 | + }, []); |
| 28 | + |
| 29 | + useDeepCompareEffect(() => { |
| 30 | + if (!data) return; |
| 31 | + |
| 32 | + setServerFromState(s => ({ |
| 33 | + ...s, |
| 34 | + invocation: data.invocation, |
| 35 | + variables: data.variables, |
| 36 | + })); |
| 37 | + }, [ data ]); |
10 | 38 |
|
11 | 39 | return ( |
12 | | - <ServerContentBlock title={'Startup Settings'}> |
13 | | - <TitledGreyBox title={'Startup Command'}> |
14 | | - <div css={tw`px-1 py-2`}> |
15 | | - <p css={tw`font-mono bg-neutral-900 rounded py-2 px-4`}> |
16 | | - {invocation} |
17 | | - </p> |
| 40 | + !data ? |
| 41 | + (!error || (error && isValidating)) ? |
| 42 | + <Spinner centered size={Spinner.Size.LARGE}/> |
| 43 | + : |
| 44 | + <ServerError |
| 45 | + title={'Oops!'} |
| 46 | + message={httpErrorToHuman(error)} |
| 47 | + onRetry={() => mutate()} |
| 48 | + /> |
| 49 | + : |
| 50 | + <ServerContentBlock title={'Startup Settings'}> |
| 51 | + <TitledGreyBox title={'Startup Command'}> |
| 52 | + <div css={tw`px-1 py-2`}> |
| 53 | + <p css={tw`font-mono bg-neutral-900 rounded py-2 px-4`}> |
| 54 | + {data.invocation} |
| 55 | + </p> |
| 56 | + </div> |
| 57 | + </TitledGreyBox> |
| 58 | + <div css={tw`grid gap-8 grid-cols-2 mt-10`}> |
| 59 | + {data.variables.map(variable => <VariableBox key={variable.envVariable} variable={variable}/>)} |
18 | 60 | </div> |
19 | | - </TitledGreyBox> |
20 | | - <div css={tw`grid gap-8 grid-cols-2 mt-10`}> |
21 | | - {variables.map(variable => <VariableBox key={variable.envVariable} variable={variable}/>)} |
22 | | - </div> |
23 | | - </ServerContentBlock> |
| 61 | + </ServerContentBlock> |
24 | 62 | ); |
25 | 63 | }; |
26 | 64 |
|
|
0 commit comments