0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(console): connector setup warning (#2445)

This commit is contained in:
Xiao Yijun 2022-11-16 18:20:47 +08:00 committed by GitHub
parent f128d90f13
commit 887befb761
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 9 deletions

View file

@ -0,0 +1,25 @@
import type { ConnectorResponse, ConnectorType } from '@logto/schemas';
import { useCallback, useMemo } from 'react';
import useSWR from 'swr';
import type { RequestError } from './use-api';
const useEnabledConnectorTypes = () => {
const { data: connectors } = useSWR<ConnectorResponse[], RequestError>('/api/connectors');
const enabledConnectorTypes = useMemo(
() => connectors?.filter(({ enabled }) => enabled).map(({ type }) => type) ?? [],
[connectors]
);
const isConnectorTypeEnabled = useCallback(
(connectorType: ConnectorType) => enabledConnectorTypes.includes(connectorType),
[enabledConnectorTypes]
);
return {
isConnectorTypeEnabled,
};
};
export default useEnabledConnectorTypes;

View file

@ -1,25 +1,19 @@
import type { ConnectorResponse } from '@logto/schemas';
import { ConnectorType } from '@logto/schemas';
import { useTranslation } from 'react-i18next';
import useSWR from 'swr';
import Alert from '@/components/Alert';
import type { RequestError } from '@/hooks/use-api';
import useEnabledConnectorTypes from '@/hooks/use-enabled-connector-types';
type Props = {
requiredConnectors: ConnectorType[];
};
const ConnectorSetupWarning = ({ requiredConnectors }: Props) => {
const { data: connectors } = useSWR<ConnectorResponse[], RequestError>('/api/connectors');
const { isConnectorTypeEnabled } = useEnabledConnectorTypes();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
if (!connectors) {
return null;
}
const missingConnectors = requiredConnectors.filter(
(connectorType) => !connectors.some(({ type, enabled }) => type === connectorType && enabled)
(connectorType) => !isConnectorTypeEnabled(connectorType)
);
if (missingConnectors.length === 0) {