diff --git a/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx b/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx index 41a0a250537..44c0afea120 100644 --- a/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx +++ b/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx @@ -230,6 +230,7 @@ export const DatabaseRelationshipsTab = ({ }} onComplete={onComplete} driver={driver} + onClose={closeForm} /> ) : null} diff --git a/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx b/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx index 38f8c1d652d..44b804b73f6 100644 --- a/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx +++ b/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx @@ -168,7 +168,13 @@ export const DatabaseRelationshipsTable = ({ className="flex px-2 py-0.5 items-center font-semibold rounded text-secondary mr-0.5 hover:bg-indigo-50 focus:bg-indigo-100" > - Edit + {[ + 'toLocalTableFk', + 'toSameTableFk', + 'toLocalTableManual', + ].includes(relationship.type) + ? 'Rename' + : 'Edit'} + + )} + + + ); +}; diff --git a/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx b/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx new file mode 100644 index 00000000000..7468e651b80 --- /dev/null +++ b/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx @@ -0,0 +1,98 @@ +import { exportMetadata } from '@/features/DataSource'; +import { Table, useMetadataMigration } from '@/features/MetadataAPI'; +import { useHttpClient } from '@/features/Network'; +import { useFireNotification } from '@/new-components/Notifications'; +import { useCallback } from 'react'; +import { useQueryClient } from 'react-query'; + +type EditManualLocalRelationshipPayload = { + newName: string; + relationshipName: string; + fromTable: Table; + fromSource: string; +}; + +export const useRenameRelationship = () => { + const httpClient = useHttpClient(); + const { fireNotification } = useFireNotification(); + const { mutate, ...rest } = useMetadataMigration(); + const queryClient = useQueryClient(); + + const renameRelationship = useCallback( + async ({ + values, + onSuccess, + onError, + }: { + values: EditManualLocalRelationshipPayload; + onSuccess?: () => void; + onError?: (err: unknown) => void; + }) => { + try { + const { resource_version, metadata } = await exportMetadata({ + httpClient, + }); + + if (!metadata) throw Error('Unable to fetch metadata'); + + const metadataSource = metadata.sources.find( + s => s.name === values.fromSource + ); + + if (!metadataSource) throw Error('Unable to fetch metadata source'); + + const driver = metadataSource.kind; + + const type = 'rename_relationship'; + + mutate( + { + query: { + resource_version, + type: `${driver}_${type}`, + args: { + table: values.fromTable, + source: values.fromSource, + name: values.relationshipName, + new_name: values.newName, + }, + }, + }, + { + onSuccess: () => { + queryClient.refetchQueries([ + values.fromSource, + 'list_all_relationships', + ]); + + onSuccess?.(); + + fireNotification({ + type: 'success', + title: 'Success!', + message: 'A relationship renamed succesfully!', + }); + }, + onError: err => { + onError?.(err); + fireNotification({ + type: 'error', + title: 'Failed to rename relationship', + message: err?.message, + }); + }, + } + ); + } catch (err) { + fireNotification({ + title: 'Error', + type: 'error', + message: JSON.stringify(err), + }); + } + }, + [fireNotification, httpClient, mutate, queryClient] + ); + + return { renameRelationship, ...rest }; +}; diff --git a/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts b/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts index 139ac2131bb..6f9e6bceb0f 100644 --- a/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts +++ b/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts @@ -37,5 +37,6 @@ export const useListAvailableAgentsFromMetadata = () => { return result; }, + refetchOnWindowFocus: false, }); };