|
1 | 1 | import { dataSource, Operations } from '@/dataSources'; |
2 | 2 | import { ComputedField, TableColumn } from '@/dataSources/types'; |
3 | | -import { useRoles } from '@/features/MetadataAPI'; |
4 | | -import { useAllFunctions, useSchemaList, useTables } from '@/hooks'; |
| 3 | +import { |
| 4 | + useMetadataTableComputedFields, |
| 5 | + useMetadataTablePermissions, |
| 6 | + useRoles, |
| 7 | +} from '@/features/MetadataAPI'; |
| 8 | +import { useAllFunctions, useSchemaList, useSingleTable } from '@/hooks'; |
5 | 9 | import { QualifiedTable } from '@/metadata/types'; |
6 | 10 |
|
7 | 11 | export type RolePermissions = { |
@@ -91,42 +95,32 @@ interface RolePermission { |
91 | 95 | }; |
92 | 96 | } |
93 | 97 |
|
94 | | -export const useRolePermissions = ({ |
95 | | - name: tableName, |
96 | | - schema: schemaName, |
97 | | -}: QualifiedTable) => { |
| 98 | +export const useRolePermissions = (table: QualifiedTable) => { |
98 | 99 | const { data: schemaList } = useSchemaList(); |
99 | | - const { data: tables } = useTables( |
100 | | - { schemas: schemaList! }, |
101 | | - { enabled: !!schemaList } |
102 | | - ); |
103 | | - const currentTableSchema = tables?.find( |
104 | | - ({ table_schema, table_name }) => |
105 | | - table_schema === schemaName && table_name === tableName |
106 | | - ); |
| 100 | + const { data: currentTableSchema } = useSingleTable(table); |
| 101 | + const { data: permissions } = useMetadataTablePermissions(table); |
| 102 | + const { data: computedFields } = useMetadataTableComputedFields(table); |
107 | 103 | const { data: allFunctions } = useAllFunctions(schemaList!, { |
108 | 104 | enabled: !!schemaList, |
109 | 105 | }); |
110 | 106 | const { data: roles } = useRoles(); |
111 | 107 |
|
112 | | - if (!currentTableSchema || !allFunctions) { |
| 108 | + if (!permissions || !allFunctions) { |
113 | 109 | return { supportedQueries: [], rolePermissions: [] }; |
114 | 110 | } |
115 | 111 |
|
116 | | - const currentRolePermissions = currentTableSchema.permissions.reduce( |
117 | | - (acc, p) => { |
118 | | - acc[p.role_name] = p.permissions; |
119 | | - return acc; |
120 | | - }, |
121 | | - {} as Record<string, any> |
122 | | - ); |
| 112 | + const currentRolePermissions = permissions.reduce((acc, p) => { |
| 113 | + acc[p.role_name] = p.permissions; |
| 114 | + return acc; |
| 115 | + }, {} as Record<string, any>); |
123 | 116 |
|
124 | | - const supportedQueries = dataSource.getTableSupportedQueries( |
125 | | - currentTableSchema |
126 | | - ); |
| 117 | + let supportedQueries: Operations[] = []; |
| 118 | + if (currentTableSchema) { |
| 119 | + supportedQueries = dataSource.getTableSupportedQueries(currentTableSchema); |
| 120 | + } |
127 | 121 |
|
128 | 122 | const groupedComputedFields = dataSource.getGroupedTableComputedFields( |
129 | | - currentTableSchema.computed_fields, |
| 123 | + computedFields ?? [], |
130 | 124 | allFunctions |
131 | 125 | ); |
132 | 126 |
|
|
0 commit comments