From 542d57426fa8be1ccd98b6ab59ccac85e6d14a1b Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Thu, 2 Jun 2022 14:15:12 +0800 Subject: [PATCH] feat(console): connector in use status (#1012) --- .../console/src/hooks/use-connector-in-use.ts | 16 +++++++ .../src/pages/ConnectorDetails/index.tsx | 21 ++++++--- .../ConnectorName/index.module.scss | 11 +++++ .../components/ConnectorName/index.tsx | 13 +++++- .../components/ConnectorRow/index.module.scss | 23 ---------- .../components/ConnectorRow/index.tsx | 44 ++++++------------- packages/phrases/src/locales/en.ts | 6 +-- packages/phrases/src/locales/zh-cn.ts | 6 +-- 8 files changed, 74 insertions(+), 66 deletions(-) create mode 100644 packages/console/src/hooks/use-connector-in-use.ts delete mode 100644 packages/console/src/pages/Connectors/components/ConnectorRow/index.module.scss diff --git a/packages/console/src/hooks/use-connector-in-use.ts b/packages/console/src/hooks/use-connector-in-use.ts new file mode 100644 index 000000000..8bedc14d0 --- /dev/null +++ b/packages/console/src/hooks/use-connector-in-use.ts @@ -0,0 +1,16 @@ +import { SignInExperience } from '@logto/schemas'; +import useSWR from 'swr'; + +import { RequestError } from './use-api'; + +const useConnectorInUse = (connectorTarget?: string): boolean | undefined => { + const { data } = useSWR(connectorTarget && '/api/sign-in-exp'); + + if (!data || !connectorTarget) { + return; + } + + return data.socialSignInConnectorTargets.includes(connectorTarget); +}; + +export default useConnectorInUse; diff --git a/packages/console/src/pages/ConnectorDetails/index.tsx b/packages/console/src/pages/ConnectorDetails/index.tsx index 5ccce6efe..0cd0c2040 100644 --- a/packages/console/src/pages/ConnectorDetails/index.tsx +++ b/packages/console/src/pages/ConnectorDetails/index.tsx @@ -18,6 +18,7 @@ import Status from '@/components/Status'; import TabNav, { TabNavItem } from '@/components/TabNav'; import UnnamedTrans from '@/components/UnnamedTrans'; import useApi, { RequestError } from '@/hooks/use-api'; +import useConnectorInUse from '@/hooks/use-connector-in-use'; import Back from '@/icons/Back'; import Delete from '@/icons/Delete'; import More from '@/icons/More'; @@ -41,6 +42,7 @@ const ConnectorDetails = () => { const { data, error } = useSWR( connectorId && `/api/connectors/${connectorId}` ); + const inUse = useConnectorInUse(data?.type === ConnectorType.Social ? data.target : undefined); const isLoading = !data && !error; const api = useApi(); const navigate = useNavigate(); @@ -127,11 +129,20 @@ const ConnectorDetails = () => {
- - {t('connectors.connector_status', { - context: data.enabled ? 'enabled' : 'disabled', - })} - + {data.type === ConnectorType.Social && inUse !== undefined && ( + + {t('connectors.connector_status', { + context: inUse ? 'in_use' : 'not_in_use', + })} + + )} + {data.type !== ConnectorType.Social && ( + + {t('connectors.connector_status', { + context: data.enabled ? 'in_use' : 'not_in_use', + })} + + )}
{data.id}
diff --git a/packages/console/src/pages/Connectors/components/ConnectorName/index.module.scss b/packages/console/src/pages/Connectors/components/ConnectorName/index.module.scss index 163287bde..6665e2d10 100644 --- a/packages/console/src/pages/Connectors/components/ConnectorName/index.module.scss +++ b/packages/console/src/pages/Connectors/components/ConnectorName/index.module.scss @@ -1,3 +1,5 @@ +@use '@/scss/underscore' as _; + a.link { color: inherit; text-decoration: none; @@ -8,3 +10,12 @@ a.link { height: 40px; border-radius: 5px; } + +.previewTitle { + display: flex; + align-items: center; + + > *:first-child { + margin-right: _.unit(2); + } +} diff --git a/packages/console/src/pages/Connectors/components/ConnectorName/index.tsx b/packages/console/src/pages/Connectors/components/ConnectorName/index.tsx index 9c33a3cb5..1a4ff54e6 100644 --- a/packages/console/src/pages/Connectors/components/ConnectorName/index.tsx +++ b/packages/console/src/pages/Connectors/components/ConnectorName/index.tsx @@ -4,6 +4,7 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; +import Button from '@/components/Button'; import ItemPreview from '@/components/ItemPreview'; import UnnamedTrans from '@/components/UnnamedTrans'; import { connectorIconPlaceHolder, connectorTitlePlaceHolder } from '@/consts/connectors'; @@ -14,15 +15,23 @@ type Props = { type: ConnectorType; connector?: ConnectorDTO; isShowId?: boolean; + onClickSetup?: () => void; }; -const ConnectorName = ({ type, connector, isShowId = false }: Props) => { +const ConnectorName = ({ type, connector, onClickSetup, isShowId = false }: Props) => { const { t } = useTranslation(undefined); if (!connector) { return ( +
{t(connectorTitlePlaceHolder[type])}
+ {type !== ConnectorType.Social && ( +
+ } icon={} /> ); diff --git a/packages/console/src/pages/Connectors/components/ConnectorRow/index.module.scss b/packages/console/src/pages/Connectors/components/ConnectorRow/index.module.scss deleted file mode 100644 index 6bc02e195..000000000 --- a/packages/console/src/pages/Connectors/components/ConnectorRow/index.module.scss +++ /dev/null @@ -1,23 +0,0 @@ -@use '@/scss/underscore' as _; - -.statusItems { - display: flex; - - .statusItem { - display: flex; - align-items: center; - - &:not(:last-child) { - .line { - margin: 0 _.unit(2); - height: 16px; - @include _.vertical-bar; - } - } - - .platform { - margin-left: _.unit(1); - color: var(--color-caption); - } - } -} diff --git a/packages/console/src/pages/Connectors/components/ConnectorRow/index.tsx b/packages/console/src/pages/Connectors/components/ConnectorRow/index.tsx index 6ee3b90a6..a7ff39d42 100644 --- a/packages/console/src/pages/Connectors/components/ConnectorRow/index.tsx +++ b/packages/console/src/pages/Connectors/components/ConnectorRow/index.tsx @@ -1,13 +1,13 @@ import { ConnectorDTO, ConnectorType } from '@logto/schemas'; +import { conditional } from '@silverhand/essentials'; import React from 'react'; import { useTranslation } from 'react-i18next'; -import Button from '@/components/Button'; import Status from '@/components/Status'; -import { connectorPlatformLabel, connectorTitlePlaceHolder } from '@/consts/connectors'; +import { connectorTitlePlaceHolder } from '@/consts/connectors'; +import useConnectorInUse from '@/hooks/use-connector-in-use'; import ConnectorName from '../ConnectorName'; -import * as styles from './index.module.scss'; type Props = { type: ConnectorType; @@ -17,6 +17,9 @@ type Props = { const ConnectorRow = ({ type, connectors, onClickSetup }: Props) => { const { t } = useTranslation(undefined); + const inUse = useConnectorInUse( + conditional(type === ConnectorType.Social && connectors[0]?.target) + ); return ( @@ -25,41 +28,22 @@ const ConnectorRow = ({ type, connectors, onClickSetup }: Props) => { type={type} connector={connectors[0]} isShowId={type !== ConnectorType.Social} + onClickSetup={onClickSetup} /> {t(connectorTitlePlaceHolder[type])} - {type === ConnectorType.Social && ( -
- {connectors.map(({ id, enabled, platform }) => { - const status = enabled ? 'enabled' : 'disabled'; - - return ( -
- - {t(`admin_console.connectors.connector_status_${status}`)} - -
- {platform && t(connectorPlatformLabel[platform])} -
-
-
- ); + {type === ConnectorType.Social && inUse !== undefined && ( + + {t('admin_console.connectors.connector_status', { + context: inUse ? 'in_use' : 'not_in_use', })} -
+ )} {type !== ConnectorType.Social && connectors[0] && ( - {t('admin_console.connectors.connector_status_enabled')} - )} - {type !== ConnectorType.Social && !connectors[0] && ( -