Files
twenty/packages/twenty-front/src/modules/settings/integrations/database-connection/components/SettingsIntegrationEditDatabaseConnectionContent.tsx
Paul Rastoin 4a4e65fe4a [REFACTOR] Twenty UI multi barrel (#11301)
# Introduction
closes https://github.com/twentyhq/core-team-issues/issues/591
Same than for `twenty-shared` made in
https://github.com/twentyhq/twenty/pull/11083.

## TODO
- [x] Manual migrate twenty-website twenty-ui imports

## What's next:
- Generate barrel and migration script factorization within own package
+ tests
- Refactoring using preconstruct ? TimeBox
- Lint circular dependencies
- Lint import from barrel and forbid them

### Preconstruct
We need custom rollup plugins addition, but preconstruct does not expose
its rollup configuration. It might be possible to handle this using the
babel overrides. But was a big tunnel.
We could give it a try afterwards ! ( allowing cjs interop and stuff
like that )
Stuck to vite lib app

Closed related PRs:
- https://github.com/twentyhq/twenty/pull/11294
- https://github.com/twentyhq/twenty/pull/11203
2025-04-03 09:47:55 +00:00

150 lines
4.9 KiB
TypeScript

import { useUpdateOneDatabaseConnection } from '@/databases/hooks/useUpdateOneDatabaseConnection';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsIntegrationDatabaseConnectionForm } from '@/settings/integrations/database-connection/components/SettingsIntegrationDatabaseConnectionForm';
import {
formatValuesForUpdate,
getEditionSchemaForForm,
getFormDefaultValuesFromConnection,
} from '@/settings/integrations/database-connection/utils/editDatabaseConnection';
import { SettingsIntegration } from '@/settings/integrations/types/SettingsIntegration';
import { SettingsPath } from '@/types/SettingsPath';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb';
import { zodResolver } from '@hookform/resolvers/zod';
import { Section } from '@react-email/components';
import pick from 'lodash.pick';
import { FormProvider, useForm } from 'react-hook-form';
import { z } from 'zod';
import {
RemoteServer,
RemoteTable,
RemoteTableStatus,
} from '~/generated-metadata/graphql';
import { useNavigateSettings } from '~/hooks/useNavigateSettings';
import { getSettingsPath } from '~/utils/navigation/getSettingsPath';
import { H2Title, Info } from 'twenty-ui/display';
export const SettingsIntegrationEditDatabaseConnectionContent = ({
connection,
integration,
databaseKey,
tables,
}: {
connection: RemoteServer;
integration: SettingsIntegration;
databaseKey: string;
tables: RemoteTable[];
}) => {
const { enqueueSnackBar } = useSnackBar();
const navigate = useNavigateSettings();
const editConnectionSchema = getEditionSchemaForForm(databaseKey);
type SettingsIntegrationEditConnectionFormValues = z.infer<
typeof editConnectionSchema
>;
const formConfig = useForm<SettingsIntegrationEditConnectionFormValues>({
mode: 'onTouched',
resolver: zodResolver(editConnectionSchema),
defaultValues: getFormDefaultValuesFromConnection({
databaseKey,
connection,
}),
});
const { updateOneDatabaseConnection } = useUpdateOneDatabaseConnection();
const settingsIntegrationsPagePath = getSettingsPath(
SettingsPath.Integrations,
);
const hasSyncedTables = tables?.some(
(table) => table?.status === RemoteTableStatus.SYNCED,
);
const { isDirty, isValid } = formConfig.formState;
const canSave = isDirty && isValid && !hasSyncedTables; // order matters here
const handleSave = async () => {
const formValues = formConfig.getValues();
const dirtyFieldKeys = Object.keys(
formConfig.formState.dirtyFields,
) as (keyof SettingsIntegrationEditConnectionFormValues)[];
try {
await updateOneDatabaseConnection({
...formatValuesForUpdate({
databaseKey,
formValues: pick(formValues, dirtyFieldKeys),
}),
id: connection?.id ?? '',
});
navigate(SettingsPath.IntegrationDatabaseConnection, {
databaseKey,
connectionId: connection?.id,
});
} catch (error) {
enqueueSnackBar((error as Error).message, {
variant: SnackBarVariant.Error,
});
}
};
// TODO: move breadcrumb to header?
return (
<>
<FormProvider
// eslint-disable-next-line react/jsx-props-no-spreading
{...formConfig}
>
<SettingsHeaderContainer>
<Breadcrumb
links={[
{
children: 'Integrations',
href: settingsIntegrationsPagePath,
},
{
children: integration.text,
href: `${settingsIntegrationsPagePath}/${databaseKey}`,
},
{ children: connection.label },
]}
/>
<SaveAndCancelButtons
isSaveDisabled={!canSave}
onCancel={() =>
navigate(SettingsPath.IntegrationDatabase, {
databaseKey,
})
}
onSave={handleSave}
/>
</SettingsHeaderContainer>
{hasSyncedTables && (
<Info
text={
'You cannot edit this connection because it has tracked tables.\nIf you need to make changes, please create a new connection or unsync the tables first.'
}
accent={'blue'}
/>
)}
<Section>
<H2Title
title="Edit Connection"
description="Edit the information to connect your database"
/>
<SettingsIntegrationDatabaseConnectionForm
databaseKey={databaseKey}
disabled={hasSyncedTables}
/>
</Section>
</FormProvider>
</>
);
};