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:
parent
f128d90f13
commit
887befb761
2 changed files with 28 additions and 9 deletions
25
packages/console/src/hooks/use-enabled-connector-types.ts
Normal file
25
packages/console/src/hooks/use-enabled-connector-types.ts
Normal 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;
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue