Files
twenty/packages/twenty-front/src/modules/databases/hooks/useSyncRemoteTableSchemaChanges.ts
Paul Rastoin 7fd89678b7 [CHORE] Avoid isDefined duplicated reference, move it to twenty-shared (#9967)
# Introduction
Avoid having multiple `isDefined` definition across our pacakges
Also avoid importing `isDefined` from `twenty-ui` which exposes a huge
barrel for a such little util function

## In a nutshell
Removed own `isDefined.ts` definition from `twenty-ui` `twenty-front`
and `twenty-server` to move it to `twenty-shared`.
Updated imports for each packages, and added explicit dependencies to
`twenty-shared` if not already in place

Related PR https://github.com/twentyhq/twenty/pull/9941
2025-02-01 12:10:10 +01:00

73 lines
2.5 KiB
TypeScript

import { useApolloClient, useMutation } from '@apollo/client';
import { useCallback } from 'react';
import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { isDefined } from 'twenty-shared';
import {
RemoteTableInput,
SyncRemoteTableSchemaChangesMutation,
SyncRemoteTableSchemaChangesMutationVariables,
} from '~/generated-metadata/graphql';
export const useSyncRemoteTableSchemaChanges = () => {
const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();
const { refetch: refetchObjectMetadataItems } =
useFindManyObjectMetadataItems();
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
});
const [mutate, mutationInformation] = useMutation<
SyncRemoteTableSchemaChangesMutation,
SyncRemoteTableSchemaChangesMutationVariables
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, {
client: apolloMetadataClient,
});
const syncRemoteTableSchemaChanges = useCallback(
async (input: RemoteTableInput) => {
const remoteTable = await mutate({
variables: {
input,
},
update: (cache, { data }) => {
if (isDefined(data)) {
modifyRemoteTableFromCache({
cache: cache,
remoteTableName: input.name,
fieldModifiers: {
schemaPendingUpdates: () =>
data.syncRemoteTableSchemaChanges.schemaPendingUpdates || [],
status: () => data.syncRemoteTableSchemaChanges.status,
},
});
}
},
});
await refetchObjectMetadataItems();
await apolloClient.query({
query: findManyViewsQuery,
fetchPolicy: 'network-only',
});
return remoteTable;
},
[mutate, refetchObjectMetadataItems, findManyViewsQuery, apolloClient],
);
return {
syncRemoteTableSchemaChanges,
isLoading: mutationInformation.loading,
};
};