-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[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
Changes from 5 commits
61dd790
03e95db
7b77bbc
02173ea
2817863
f986c7c
c6ee7f2
83fbadc
6266dc6
ae13924
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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, | ||
|
@@ -23,7 +24,6 @@ export const useSyncRemoteTable = () => { | |
const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({ | ||
objectNameSingular: CoreObjectNameSingular.View, | ||
}); | ||
|
||
const [mutate] = useMutation< | ||
SyncRemoteTableMutation, | ||
SyncRemoteTableMutationVariables | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thomtrp what is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
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 = () => { | ||
ijreilly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const apolloMetadataClient = useApolloMetadataClient(); | ||
|
||
const [mutate] = useMutation< | ||
SyncRemoteTableMutation, | ||
SyncRemoteTableMutationVariables | ||
>(SYNC_REMOTE_TABLE_SCHEMA_CHANGES, { | ||
client: apolloMetadataClient ?? ({} as ApolloClient<any>), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fallback empty object cast for |
||
}); | ||
|
||
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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}; | ||
|
||
return ( | ||
<Toggle | ||
value={table.status === RemoteTableStatus.Synced} | ||
value={tableStatus === RemoteTableStatus.Synced} | ||
disabled={isToggleLoading} | ||
onChange={onChange} | ||
/> | ||
|
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'; | ||
|
@@ -11,7 +12,6 @@ import { | |
RemoteTable, | ||
RemoteTableStatus, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import |
||
} from '~/generated-metadata/graphql'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
export const settingsIntegrationsDatabaseTablesSchema = z.object({ | ||
syncedTablesByName: z.record(z.boolean()), | ||
|
@@ -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[], | ||
) => { | ||
|
@@ -66,24 +59,19 @@ export const SettingsIntegrationDatabaseTablesListCard = ({ | |
}: SettingsIntegrationDatabaseTablesListCardProps) => { | ||
const { syncRemoteTable } = useSyncRemoteTable(); | ||
const { unsyncRemoteTable } = useUnsyncRemoteTable(); | ||
const { syncRemoteTableSchemaChanges, isLoading } = | ||
useSyncRemoteTableSchemaChanges(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
// 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, | ||
|
@@ -96,7 +84,7 @@ export const SettingsIntegrationDatabaseTablesListCard = ({ | |
}); | ||
} | ||
}, | ||
[items, syncRemoteTable, connectionId, unsyncRemoteTable], | ||
[syncRemoteTable, connectionId, unsyncRemoteTable], | ||
); | ||
|
||
const rowRightComponent = useCallback( | ||
|
@@ -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> | ||
|
There was a problem hiding this comment.
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