diff --git a/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/components/RowPermissions.tsx b/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/components/RowPermissions.tsx index b3223cf352f..f4101eb7c0c 100644 --- a/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/components/RowPermissions.tsx +++ b/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/components/RowPermissions.tsx @@ -1,22 +1,25 @@ import React from 'react'; import AceEditor from 'react-ace'; import { useFormContext } from 'react-hook-form'; -import { MetadataTable, Table } from '../../../hasura-metadata-types'; -import { useHttpClient } from '../../../Network'; import { useQuery } from 'react-query'; -import { DataSource, exportMetadata, Operator } from '../../../DataSource'; -import { areTablesEqual } from '../../../hasura-metadata-api'; -import { getTypeName } from '../../../GraphQLUtils'; +import { getIngForm } from '../../../../components/Services/Data/utils'; import { InputField } from '../../../../new-components/Form'; import { IconTooltip } from '../../../../new-components/Tooltip'; import { Collapse } from '../../../../new-components/deprecated'; -import { getIngForm } from '../../../../components/Services/Data/utils'; -import { RowPermissionBuilder } from './RowPermissionsBuilder'; +import { DataSource, Operator, exportMetadata } from '../../../DataSource'; +import { getTypeName } from '../../../GraphQLUtils'; +import { useHttpClient } from '../../../Network'; +import { + MetadataSelectors, + areTablesEqual, + useMetadata, +} from '../../../hasura-metadata-api'; +import { Table } from '../../../hasura-metadata-types'; import { QueryType } from '../../types'; import { ReturnValue } from '../hooks'; -import { useMetadataTable } from '../../../hasura-metadata-api/metadataHooks'; -import { getNonSelectedQueryTypePermissions } from '../utils/getMapQueryTypePermissions'; import { copyQueryTypePermissions } from '../utils/copyQueryTypePermissions'; +import { getNonSelectedQueryTypePermissions } from '../utils/getMapQueryTypePermissions'; +import { RowPermissionBuilder } from './RowPermissionsBuilder'; const NoChecksLabel = () => ( Without any checks  @@ -139,10 +142,13 @@ export const RowPermissionsSection: React.FC = ({ roleName, }) => { const { data: tableName, isLoading } = useTypeName({ table, dataSourceName }); - const metadataTable = useMetadataTable(dataSourceName, table); + + const { data: metadataTable } = useMetadata( + MetadataSelectors.findTable(dataSourceName, table) + ); const nonSelectedQueryTypePermissions = getNonSelectedQueryTypePermissions( - metadataTable?.data as MetadataTable, + metadataTable, queryType, roleName ); diff --git a/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/utils/getMapQueryTypePermissions.ts b/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/utils/getMapQueryTypePermissions.ts index 8ceb0c39e55..a78605379d0 100644 --- a/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/utils/getMapQueryTypePermissions.ts +++ b/frontend/libs/console/legacy-ce/src/lib/features/Permissions/PermissionsForm/utils/getMapQueryTypePermissions.ts @@ -53,7 +53,7 @@ const getPermissionsMappedByRole = ({ }; export const getNonSelectedQueryTypePermissions = ( - tableData: MetadataTable, + tableData: MetadataTable | undefined, currentQueryType: string, currentRole: string ) => { diff --git a/frontend/libs/console/legacy-ce/src/lib/features/hasura-metadata-api/metadataHooks.ts b/frontend/libs/console/legacy-ce/src/lib/features/hasura-metadata-api/metadataHooks.ts deleted file mode 100644 index b447b51b514..00000000000 --- a/frontend/libs/console/legacy-ce/src/lib/features/hasura-metadata-api/metadataHooks.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { useQuery } from 'react-query'; -import { exportMetadata } from '../DataSource'; -import { Table } from '../hasura-metadata-types'; -import { useHttpClient } from '../Network'; -import { DEFAULT_STALE_TIME } from '../DatabaseRelationships'; -import { areTablesEqual } from './areTablesEqual'; - -export const useMetadata = () => { - const httpClient = useHttpClient(); - return useQuery({ - queryKey: ['export_metadata'], - queryFn: async () => { - const result = await exportMetadata({ httpClient }); - return result; - }, - refetchOnWindowFocus: false, - staleTime: DEFAULT_STALE_TIME, - }); -}; - -export const useResourceVersion = () => { - const { data: result, isFetching } = useMetadata(); - return useQuery({ - queryKey: ['export_metadata', 'resource_version'], - queryFn: async () => { - if (!result) throw Error('Unable to find metadata'); - - const { resource_version } = result; - return resource_version; - }, - refetchOnWindowFocus: false, - enabled: !isFetching, - }); -}; - -export const useMetadataSource = (dataSourceName: string) => { - const { data: result, isFetching } = useMetadata(); - return useQuery({ - queryKey: ['export_metadata', dataSourceName], - queryFn: async () => { - const metadataSource = result?.metadata.sources.find( - s => s.name === dataSourceName - ); - return metadataSource; - }, - enabled: !isFetching, - refetchOnWindowFocus: false, - }); -}; - -export const useMetadataTable = (dataSourceName: string, table: Table) => { - const { data: metadataSource, isFetching } = - useMetadataSource(dataSourceName); - return useQuery({ - queryKey: ['export_metadata', dataSourceName, table], - queryFn: async () => { - const metadataTable = metadataSource?.tables.find(t => - areTablesEqual(t.table, table) - ); - return metadataTable; - }, - enabled: !isFetching, - refetchOnWindowFocus: false, - }); -}; - -export const useMetadataTables = (dataSourceName: string) => { - const { data: metadataSource, isFetching } = - useMetadataSource(dataSourceName); - return useQuery({ - queryKey: ['export_metadata', dataSourceName, 'tables'], - queryFn: async () => { - const metadataTables = metadataSource?.tables; - return metadataTables; - }, - enabled: !isFetching && !!dataSourceName, - refetchOnWindowFocus: false, - }); -}; - -export const useMetadataSources = () => { - const { data: metadata, isFetching } = useMetadata(); - return useQuery({ - queryKey: ['export_metadata', 'sources'], - queryFn: async () => { - const metadataSources = metadata?.metadata.sources; - return metadataSources; - }, - enabled: !isFetching, - refetchOnWindowFocus: false, - }); -};