From 6ae0237782677eeb67fc0c9d2e76d752a66d526a Mon Sep 17 00:00:00 2001 From: Chanwit Kaewkasi Date: Mon, 1 Jun 2020 22:39:15 +0700 Subject: [PATCH] refactor ClusterPage into Page and List to avoid using hooks inside an if --- .../components/ClusterList/ClusterList.tsx | 95 +++++++++++++++++++ .../src/components/ClusterList/index.ts | 17 ++++ .../components/ClusterPage/ClusterPage.tsx | 66 +------------ plugins/gitops-profiles/src/plugin.ts | 3 +- 4 files changed, 119 insertions(+), 62 deletions(-) create mode 100644 plugins/gitops-profiles/src/components/ClusterList/ClusterList.tsx create mode 100644 plugins/gitops-profiles/src/components/ClusterList/index.ts diff --git a/plugins/gitops-profiles/src/components/ClusterList/ClusterList.tsx b/plugins/gitops-profiles/src/components/ClusterList/ClusterList.tsx new file mode 100644 index 0000000000..0bdad1c8ab --- /dev/null +++ b/plugins/gitops-profiles/src/components/ClusterList/ClusterList.tsx @@ -0,0 +1,95 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { FC } from 'react'; +import { + Content, + ContentHeader, + Header, + SupportButton, + Page, + pageTheme, + Progress, + HeaderLabel, + useApi, +} from '@backstage/core'; + +import ClusterTable from '../ClusterTable/ClusterTable'; +import { Button, Typography } from '@material-ui/core'; +import { useAsync, useLocalStorage } from 'react-use'; +import { gitOpsApiRef, ListClusterStatusesResponse } from '../../api'; + +const ClusterList: FC<{}> = () => { + const [loginInfo] = useLocalStorage<{ + token: string; + username: string; + name: string; + }>('githubLoginDetails'); + + const api = useApi(gitOpsApiRef); + + const { loading, error, value } = useAsync( + () => { + return api.listClusters({ + gitHubToken: loginInfo.token, + gitHubUser: loginInfo.username, + }); + }, + ); + let content: JSX.Element; + if (loading) { + content = ( + + + + ); + } else if (error) { + content = ( + + + Failed to load cluster, {String(error)} + + + ); + } else { + content = ( + + + + All clusters + + + + ); + } + + return ( + +
+ +
+ {content} +
+ ); +}; + +export default ClusterList; diff --git a/plugins/gitops-profiles/src/components/ClusterList/index.ts b/plugins/gitops-profiles/src/components/ClusterList/index.ts new file mode 100644 index 0000000000..e4260e5374 --- /dev/null +++ b/plugins/gitops-profiles/src/components/ClusterList/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export { default } from './ClusterList'; diff --git a/plugins/gitops-profiles/src/components/ClusterPage/ClusterPage.tsx b/plugins/gitops-profiles/src/components/ClusterPage/ClusterPage.tsx index 680459de52..c517f60dfd 100644 --- a/plugins/gitops-profiles/src/components/ClusterPage/ClusterPage.tsx +++ b/plugins/gitops-profiles/src/components/ClusterPage/ClusterPage.tsx @@ -17,9 +17,7 @@ import React, { FC, useEffect, useState } from 'react'; import { Content, - ContentHeader, Header, - SupportButton, Page, pageTheme, Table, @@ -27,11 +25,11 @@ import { HeaderLabel, useApi, } from '@backstage/core'; -import ClusterTable from '../ClusterTable/ClusterTable'; -import { Button, Link, Typography } from '@material-ui/core'; + +import { Link } from '@material-ui/core'; import { useParams } from 'react-router-dom'; -import { useAsync, useLocalStorage } from 'react-use'; -import { gitOpsApiRef, ListClusterStatusesResponse, Status } from '../../api'; +import { useLocalStorage } from 'react-use'; +import { gitOpsApiRef, Status } from '../../api'; import { transformRunStatus } from '../ProfileCatalog'; const ClusterPage: FC<{}> = () => { @@ -42,6 +40,7 @@ const ClusterPage: FC<{}> = () => { username: string; name: string; }>('githubLoginDetails'); + const [pollingLog, setPollingLog] = useState(true); const [runStatus, setRunStatus] = useState([]); const [runLink, setRunLink] = useState(''); @@ -76,61 +75,6 @@ const ClusterPage: FC<{}> = () => { return () => {}; }, [pollingLog]); - if (params.owner === undefined || params.repo === undefined) { - const { loading, error, value } = useAsync( - () => { - setPollingLog(false); - return api.listClusters({ - gitHubToken: loginInfo.token, - gitHubUser: loginInfo.username, - }); - }, - ); - let content: JSX.Element; - if (loading) { - content = ( - - - - ); - } else if (error) { - content = ( - - - Failed to load cluster, {String(error)} - - - ); - } else { - content = ( - - - - All clusters - - - - ); - } - - return ( - -
- -
- {content} -
- ); - } return (
diff --git a/plugins/gitops-profiles/src/plugin.ts b/plugins/gitops-profiles/src/plugin.ts index f5b523177b..aeedc629da 100644 --- a/plugins/gitops-profiles/src/plugin.ts +++ b/plugins/gitops-profiles/src/plugin.ts @@ -17,11 +17,12 @@ import { createPlugin } from '@backstage/core'; import ProfileCatalog from './components/ProfileCatalog'; import ClusterPage from './components/ClusterPage'; +import ClusterList from './components/ClusterList'; export const plugin = createPlugin({ id: 'gitops-profiles', register({ router }) { - router.registerRoute('/gitops-clusters', ClusterPage); + router.registerRoute('/gitops-clusters', ClusterList); router.registerRoute('/gitops-cluster/:owner/:repo', ClusterPage); router.registerRoute('/gitops-cluster-create', ProfileCatalog); },