Skip to content

[FE] Update remote table schema + refactor Tables list #5548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
const apolloClient = useMemo(() => {
apolloRef.current = new ApolloFactory({
uri: `${REACT_APP_SERVER_BASE_URL}/graphql`,
cache: new InMemoryCache(),
cache: new InMemoryCache({
typePolicies: {
RemoteTable: {
keyFields: ['name'],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Check if we have an issue when we migrate between the show page of two databases that have a table with the same name. If so we could add connectionId to the cache id to make it unique

},
},
}),
headers: {
...(currentWorkspace?.currentCacheVersion && {
'X-Schema-Version': currentWorkspace.currentCacheVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { gql } from '@apollo/client';

import { REMOTE_TABLE_FRAGMENT } from '@/databases/graphql/fragments/remoteTableFragment';

export const SYNC_REMOTE_TABLE_SCHEMA_CHANGES = gql`
${REMOTE_TABLE_FRAGMENT}
mutation syncRemoteTable($input: RemoteTableInput!) {
syncRemoteTableSchemaChanges(input: $input) {
...RemoteTableFields
}
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { useCallback } from 'react';
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';

import { SYNC_REMOTE_TABLE } from '@/databases/graphql/mutations/syncRemoteTable';
import { GET_MANY_REMOTE_TABLES } from '@/databases/graphql/queries/findManyRemoteTables';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRecordTableFromCache';
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 { RemoteTableStatus } from '~/generated/graphql';
import {
RemoteTableInput,
SyncRemoteTableMutation,
Expand All @@ -23,7 +24,6 @@ export const useSyncRemoteTable = () => {
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
});

const [mutate] = useMutation<
SyncRemoteTableMutation,
SyncRemoteTableMutationVariables
Expand All @@ -37,20 +37,17 @@ export const useSyncRemoteTable = () => {
variables: {
input,
},
awaitRefetchQueries: true,
refetchQueries: [
{
query: GET_MANY_REMOTE_TABLES,
variables: {
input: {
id: input.remoteServerId,
},
update: (cache) => {
modifyRemoteTableFromCache({
cache: cache,
remoteTableName: input.name,
fieldModifiers: {
status: () => RemoteTableStatus.Synced,
},
},
],
});
},
});

// TODO: we should return the tables with the columns and store in cache instead of refetching
await refetchObjectMetadataItems();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomtrp what is this for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it, when you sync a table and go back to main app, you will not see the remote table displayed. You would need to refresh so it re-triggers this call.

await apolloClient.query({
query: findManyViewsQuery,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useCallback } from 'react';
import { ApolloClient, useMutation } from '@apollo/client';

import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRecordTableFromCache';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import {
RemoteTableInput,
SyncRemoteTableMutation,
SyncRemoteTableMutationVariables,
} from '~/generated-metadata/graphql';

export const useSyncRemoteTableSchemaChanges = () => {
const apolloMetadataClient = useApolloMetadataClient();

const [mutate] = useMutation<
SyncRemoteTableMutation,
SyncRemoteTableMutationVariables
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, {
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback empty object cast for ApolloClient is unnecessary here. Consider removing ?? ({} as ApolloClient<any>).

});

const syncRemoteTableSchemaChanges = useCallback(
async (input: RemoteTableInput) => {
const remoteTable = await mutate({
variables: {
input,
},
update: (cache) => {
modifyRemoteTableFromCache({
cache: cache,
remoteTableName: input.name,
fieldModifiers: {
schemaPendingUpdates: () => [],
},
});
},
});

return remoteTable;
},
[mutate],
);

return {
syncRemoteTableSchemaChanges,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { useCallback } from 'react';
import { ApolloClient, useMutation } from '@apollo/client';

import { UNSYNC_REMOTE_TABLE } from '@/databases/graphql/mutations/unsyncRemoteTable';
import { GET_MANY_REMOTE_TABLES } from '@/databases/graphql/queries/findManyRemoteTables';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRecordTableFromCache';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
import {
RemoteTableInput,
RemoteTableStatus,
UnsyncRemoteTableMutation,
UnsyncRemoteTableMutationVariables,
} from '~/generated-metadata/graphql';
Expand All @@ -29,17 +30,15 @@ export const useUnsyncRemoteTable = () => {
variables: {
input,
},
awaitRefetchQueries: true,
refetchQueries: [
{
query: GET_MANY_REMOTE_TABLES,
variables: {
input: {
id: input.remoteServerId,
},
update: (cache) => {
modifyRemoteTableFromCache({
cache: cache,
remoteTableName: input.name,
fieldModifiers: {
status: () => RemoteTableStatus.NotSynced,
},
},
],
});
},
});

await refetchObjectMetadataItems();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApolloCache } from '@apollo/client';
import { Modifiers } from '@apollo/client/cache';

import { RemoteTable } from '~/generated-metadata/graphql';

export const modifyRemoteTableFromCache = ({
cache,
fieldModifiers,
remoteTableName,
}: {
cache: ApolloCache<object>;
fieldModifiers: Modifiers<RemoteTable>;
remoteTableName: string;
}) => {
const remoteTableCacheId = `RemoteTable:{"name":"${remoteTableName}"}`;

cache.modify({
id: remoteTableCacheId,
fields: fieldModifiers,
optimistic: true,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import { Toggle } from '@/ui/input/components/Toggle';
import { RemoteTableStatus } from '~/generated-metadata/graphql';

export const SettingsIntegrationRemoteTableSyncStatusToggle = ({
table,
tableName,
tableStatus,
onSyncUpdate,
}: {
table: { id: string; name: string; status: RemoteTableStatus };
tableName: string;
tableStatus: RemoteTableStatus;
onSyncUpdate: (value: boolean, tableName: string) => Promise<void>;
}) => {
const [isToggleLoading, setIsToggleLoading] = useState(false);

const onChange = async (newValue: boolean) => {
if (isToggleLoading) return;
setIsToggleLoading(true);
await onSyncUpdate(newValue, table.name);
await onSyncUpdate(newValue, tableName);
setIsToggleLoading(false);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomtrp for the record setIsToggleLoading(false) will never be executed if there is a sync error (for instance, the table has no id). We could wrap onSyncUpdate in a try and execute setIsToggleLoading(false) in a finally block. But I noticed that then its making the situation worse because the toggle stays on because that's how the implementation of Toggle works.
I will look into this in a further PR has this problem already exists.

};

return (
<Toggle
value={table.status === RemoteTableStatus.Synced}
value={tableStatus === RemoteTableStatus.Synced}
disabled={isToggleLoading}
onChange={onChange}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback, useState } from 'react';
import { useCallback } from 'react';
import styled from '@emotion/styled';
import { z } from 'zod';

import { useSyncRemoteTable } from '@/databases/hooks/useSyncRemoteTable';
import { useSyncRemoteTableSchemaChanges } from '@/databases/hooks/useSyncRemoteTableSchemaChanges';
import { useUnsyncRemoteTable } from '@/databases/hooks/useUnsyncRemoteTable';
import { SettingsListCard } from '@/settings/components/SettingsListCard';
import { SettingsIntegrationRemoteTableSyncStatusToggle } from '@/settings/integrations/components/SettingsIntegrationRemoteTableSyncStatusToggle';
Expand All @@ -11,7 +12,6 @@ import {
RemoteTable,
RemoteTableStatus,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import isDefined has been removed but is still used in the onSyncUpdate function. Ensure that this removal is intentional and that the function works correctly without it.

} from '~/generated-metadata/graphql';
import { isDefined } from '~/utils/isDefined';

export const settingsIntegrationsDatabaseTablesSchema = z.object({
syncedTablesByName: z.record(z.boolean()),
Expand All @@ -32,13 +32,6 @@ const StyledRowRightContainer = styled.div`
gap: ${({ theme }) => theme.spacing(1)};
`;

const StyledText = styled.h3`
color: ${({ theme }) => theme.font.color.tertiary};
font-size: ${({ theme }) => theme.font.size.md};
font-weight: ${({ theme }) => theme.font.weight.regular};
margin: 0;
`;

const getDistantTableUpdatesText = (
schemaPendingUpdates: DistantTableUpdate[],
) => {
Expand Down Expand Up @@ -66,24 +59,19 @@ export const SettingsIntegrationDatabaseTablesListCard = ({
}: SettingsIntegrationDatabaseTablesListCardProps) => {
const { syncRemoteTable } = useSyncRemoteTable();
const { unsyncRemoteTable } = useUnsyncRemoteTable();
const { syncRemoteTableSchemaChanges, isLoading } =
useSyncRemoteTableSchemaChanges();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isLoading variable from useSyncRemoteTableSchemaChanges is imported but not used. Consider removing it if it's not needed.


// We need to use a state because the table status update re-render the whole list of toggles
const [items] = useState(
tables.map((table) => ({
...table,
id: table.name,
updatesText: table.schemaPendingUpdates
? getDistantTableUpdatesText(table.schemaPendingUpdates)
: null,
})),
);
const items = tables.map((table) => ({
...table,
id: table.name,
updatesText: table.schemaPendingUpdates
? getDistantTableUpdatesText(table.schemaPendingUpdates)
: null,
}));

const onSyncUpdate = useCallback(
async (isSynced: boolean, tableName: string) => {
const table = items.find((table) => table.name === tableName);

if (!isDefined(table)) return;

if (isSynced) {
await syncRemoteTable({
remoteServerId: connectionId,
Expand All @@ -96,7 +84,7 @@ export const SettingsIntegrationDatabaseTablesListCard = ({
});
}
},
[items, syncRemoteTable, connectionId, unsyncRemoteTable],
[syncRemoteTable, connectionId, unsyncRemoteTable],
);

const rowRightComponent = useCallback(
Expand All @@ -111,9 +99,9 @@ export const SettingsIntegrationDatabaseTablesListCard = ({
};
}) => (
<StyledRowRightContainer>
{item.updatesText && <StyledText>{item.updatesText}</StyledText>}
<SettingsIntegrationRemoteTableSyncStatusToggle
table={item}
tableName={item.name}
tableStatus={item.status}
onSyncUpdate={onSyncUpdate}
/>
</StyledRowRightContainer>
Expand Down
Loading