Files
twenty/packages/twenty-front/src/modules/settings/integrations/database-connection/hooks/useDatabaseConnection.ts
Thomas Trompette de438b0171 Add stripe connection option (#5372)
- Refactor creation and edition form so it handles stripe integration
and not only postgres
- Add a hook `useIsSettingsIntegrationEnabled` to avoid checking feature
flags everywhere
- Add zod schema for stripe

<img width="250" alt="Capture d’écran 2024-05-13 à 12 41 52"
src="https://github.com/twentyhq/twenty/assets/22936103/a77e7278-5d79-4f95-bddb-ae9ddd1426eb">
<img width="250" alt="Capture d’écran 2024-05-13 à 12 41 59"
src="https://github.com/twentyhq/twenty/assets/22936103/d617dc6a-31a4-43c8-8192-dbfb7157de1c">
<img width="250" alt="Capture d’écran 2024-05-13 à 12 42 08"
src="https://github.com/twentyhq/twenty/assets/22936103/c4e2d0e4-f826-436d-89be-4d1679a27861">

---------

Co-authored-by: Thomas Trompette <thomast@twenty.com>
2024-05-13 18:00:13 +02:00

49 lines
1.5 KiB
TypeScript

import { useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { useGetDatabaseConnection } from '@/databases/hooks/useGetDatabaseConnection';
import { useGetDatabaseConnectionTables } from '@/databases/hooks/useGetDatabaseConnectionTables';
import { useIsSettingsIntegrationEnabled } from '@/settings/integrations/hooks/useIsSettingsIntegrationEnabled';
import { useSettingsIntegrationCategories } from '@/settings/integrations/hooks/useSettingsIntegrationCategories';
import { AppPath } from '@/types/AppPath';
export const useDatabaseConnection = () => {
const { databaseKey = '', connectionId = '' } = useParams();
const navigate = useNavigate();
const [integrationCategoryAll] = useSettingsIntegrationCategories();
const integration = integrationCategoryAll.integrations.find(
({ from: { key } }) => key === databaseKey,
);
const isIntegrationEnabled = useIsSettingsIntegrationEnabled(databaseKey);
const isIntegrationAvailable = !!integration && isIntegrationEnabled;
const { connection, loading } = useGetDatabaseConnection({
databaseKey,
connectionId,
skip: !isIntegrationAvailable,
});
useEffect(() => {
if (!isIntegrationAvailable || (!loading && !connection)) {
navigate(AppPath.NotFound);
}
}, [
integration,
databaseKey,
navigate,
isIntegrationAvailable,
connection,
loading,
]);
const { tables } = useGetDatabaseConnectionTables({
connectionId,
skip: !connection,
});
return { connection, integration, databaseKey, tables };
};