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

refactor(console): apply useSWRImmutable for connector factory requests

This commit is contained in:
Xiao Yijun 2023-08-03 14:28:54 +08:00
parent 56b0a2cd18
commit 91421c8172
No known key found for this signature in database
GPG key ID: 6F648FC1262DB420
5 changed files with 17 additions and 14 deletions

View file

@ -11,6 +11,7 @@ import { TenantsContext } from '@/contexts/TenantsProvider';
import DynamicT from '@/ds-components/DynamicT'; import DynamicT from '@/ds-components/DynamicT';
import ModalLayout from '@/ds-components/ModalLayout'; import ModalLayout from '@/ds-components/ModalLayout';
import type { RequestError } from '@/hooks/use-api'; import type { RequestError } from '@/hooks/use-api';
import useConnectorFactories from '@/hooks/use-connector-factories';
import useSubscriptionPlan from '@/hooks/use-subscription-plan'; import useSubscriptionPlan from '@/hooks/use-subscription-plan';
import * as modalStyles from '@/scss/modal.module.scss'; import * as modalStyles from '@/scss/modal.module.scss';
@ -35,10 +36,7 @@ function CreateConnectorForm({ onClose, isOpen: isFormOpen, type }: Props) {
ConnectorResponse[], ConnectorResponse[],
RequestError RequestError
>('api/connectors'); >('api/connectors');
const { data: factories, error: factoriesError } = useSWR< const { data: factories, error: factoriesError } = useConnectorFactories();
ConnectorFactoryResponse[],
RequestError
>('api/connector-factories');
const isLoading = !factories && !existingConnectors && !connectorsError && !factoriesError; const isLoading = !factories && !existingConnectors && !connectorsError && !factoriesError;
const [activeGroupId, setActiveGroupId] = useState<string>(); const [activeGroupId, setActiveGroupId] = useState<string>();
const [activeFactoryId, setActiveFactoryId] = useState<string>(); const [activeFactoryId, setActiveFactoryId] = useState<string>();

View file

@ -0,0 +1,9 @@
import { type ConnectorFactoryResponse } from '@logto/schemas';
import useSWRImmutable from 'swr/immutable';
import { type RequestError } from './use-api';
const useConnectorFactories = () =>
useSWRImmutable<ConnectorFactoryResponse[], RequestError>('api/connector-factories');
export default useConnectorFactories;

View file

@ -7,6 +7,7 @@ import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useLocation, useParams } from 'react-router-dom'; import { useLocation, useParams } from 'react-router-dom';
import useSWR, { useSWRConfig } from 'swr'; import useSWR, { useSWRConfig } from 'swr';
import useSWRImmutable from 'swr/immutable';
import Delete from '@/assets/icons/delete.svg'; import Delete from '@/assets/icons/delete.svg';
import More from '@/assets/icons/more.svg'; import More from '@/assets/icons/more.svg';
@ -59,7 +60,7 @@ function ConnectorDetails() {
data: connectorFactory, data: connectorFactory,
error: fetchConnectorFactoryError, error: fetchConnectorFactoryError,
mutate: mutateConnectorFactory, mutate: mutateConnectorFactory,
} = useSWR<ConnectorFactoryResponse, RequestError>( } = useSWRImmutable<ConnectorFactoryResponse, RequestError>(
data?.isStandard && `api/connector-factories/${data.connectorId}` data?.isStandard && `api/connector-factories/${data.connectorId}`
); );

View file

@ -1,6 +1,6 @@
import type { ConnectorFactoryResponse } from '@logto/schemas'; import type { ConnectorFactoryResponse } from '@logto/schemas';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import useSWR from 'swr'; import useSWRImmutable from 'swr/immutable';
import UnnamedTrans from '@/components/UnnamedTrans'; import UnnamedTrans from '@/components/UnnamedTrans';
import { connectorTitlePlaceHolder } from '@/consts/connectors'; import { connectorTitlePlaceHolder } from '@/consts/connectors';
@ -21,7 +21,7 @@ function ConnectorTypeColumn({ connectorGroup: { type, connectors } }: Props) {
const firstStandardConnector = standardConnectors[0]; const firstStandardConnector = standardConnectors[0];
const { data: connectorFactory } = useSWR<ConnectorFactoryResponse>( const { data: connectorFactory } = useSWRImmutable<ConnectorFactoryResponse>(
firstStandardConnector && `api/connector-factories/${firstStandardConnector.connectorId}` firstStandardConnector && `api/connector-factories/${firstStandardConnector.connectorId}`
); );

View file

@ -1,12 +1,10 @@
import { withAppInsights } from '@logto/app-insights/react'; import { withAppInsights } from '@logto/app-insights/react';
import { ServiceConnector } from '@logto/connector-kit'; import { ServiceConnector } from '@logto/connector-kit';
import { ConnectorType } from '@logto/schemas'; import { ConnectorType } from '@logto/schemas';
import type { ConnectorFactoryResponse } from '@logto/schemas';
import { conditional } from '@silverhand/essentials'; import { conditional } from '@silverhand/essentials';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import useSWR from 'swr';
import Plus from '@/assets/icons/plus.svg'; import Plus from '@/assets/icons/plus.svg';
import SocialConnectorEmptyDark from '@/assets/images/social-connector-empty-dark.svg'; import SocialConnectorEmptyDark from '@/assets/images/social-connector-empty-dark.svg';
@ -18,8 +16,8 @@ import { ConnectorsTabs } from '@/consts/page-tabs';
import Button from '@/ds-components/Button'; import Button from '@/ds-components/Button';
import TabNav, { TabNavItem } from '@/ds-components/TabNav'; import TabNav, { TabNavItem } from '@/ds-components/TabNav';
import TablePlaceholder from '@/ds-components/Table/TablePlaceholder'; import TablePlaceholder from '@/ds-components/Table/TablePlaceholder';
import type { RequestError } from '@/hooks/use-api';
import useConnectorApi from '@/hooks/use-connector-api'; import useConnectorApi from '@/hooks/use-connector-api';
import useConnectorFactories from '@/hooks/use-connector-factories';
import useConnectorGroups from '@/hooks/use-connector-groups'; import useConnectorGroups from '@/hooks/use-connector-groups';
import useDocumentationUrl from '@/hooks/use-documentation-url'; import useDocumentationUrl from '@/hooks/use-documentation-url';
import useTenantPathname from '@/hooks/use-tenant-pathname'; import useTenantPathname from '@/hooks/use-tenant-pathname';
@ -68,10 +66,7 @@ function Connectors() {
const { getDocumentationUrl } = useDocumentationUrl(); const { getDocumentationUrl } = useDocumentationUrl();
const { createConnector } = useConnectorApi(); const { createConnector } = useConnectorApi();
const { data, error, mutate } = useConnectorGroups(); const { data, error, mutate } = useConnectorGroups();
const { data: factories, error: factoriesError } = useSWR< const { data: factories, error: factoriesError } = useConnectorFactories();
ConnectorFactoryResponse[],
RequestError
>('api/connector-factories');
const isLoading = !data && !factories && !error && !factoriesError; const isLoading = !data && !factories && !error && !factoriesError;