da3c3d2b9d
Closes https://github.com/twentyhq/core-team-issues/issues/1560 If viewId is defined in a groupBy query, we want to apply all filters of the view to the query. This required to move a lot of code from twenty-front to twenty-shared to convert the filters as stored in the db into graphql filters, applying the right combinations between filters etc., which was previously only done in the FE. This PR does not handle any field filters, it will be done in a later pr
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { type AppPath, ConnectedAccountProvider } from 'twenty-shared/types';
|
|
|
|
import { useRedirect } from '@/domain-manager/hooks/useRedirect';
|
|
import { CustomError } from 'twenty-shared/utils';
|
|
import { REACT_APP_SERVER_BASE_URL } from '~/config';
|
|
import {
|
|
type CalendarChannelVisibility,
|
|
type MessageChannelVisibility,
|
|
useGenerateTransientTokenMutation,
|
|
} from '~/generated-metadata/graphql';
|
|
|
|
const getProviderUrl = (provider: ConnectedAccountProvider) => {
|
|
switch (provider) {
|
|
case ConnectedAccountProvider.GOOGLE:
|
|
return 'google-apis';
|
|
case ConnectedAccountProvider.MICROSOFT:
|
|
return 'microsoft-apis';
|
|
default:
|
|
throw new CustomError(
|
|
`Provider ${provider} is not supported`,
|
|
'UNSUPPORTED_PROVIDER',
|
|
);
|
|
}
|
|
};
|
|
|
|
export const useTriggerApisOAuth = () => {
|
|
const [generateTransientToken] = useGenerateTransientTokenMutation();
|
|
const { redirect } = useRedirect();
|
|
|
|
const triggerApisOAuth = useCallback(
|
|
async (
|
|
provider: ConnectedAccountProvider,
|
|
{
|
|
redirectLocation,
|
|
messageVisibility,
|
|
calendarVisibility,
|
|
loginHint,
|
|
}: {
|
|
redirectLocation?: AppPath | string;
|
|
messageVisibility?: MessageChannelVisibility;
|
|
calendarVisibility?: CalendarChannelVisibility;
|
|
loginHint?: string;
|
|
} = {},
|
|
) => {
|
|
const authServerUrl = REACT_APP_SERVER_BASE_URL;
|
|
|
|
const transientToken = await generateTransientToken();
|
|
|
|
const token =
|
|
transientToken.data?.generateTransientToken.transientToken.token;
|
|
|
|
let params = `transientToken=${token}`;
|
|
|
|
params += redirectLocation
|
|
? `&redirectLocation=${encodeURIComponent(redirectLocation)}`
|
|
: '';
|
|
|
|
params += calendarVisibility
|
|
? `&calendarVisibility=${calendarVisibility}`
|
|
: '';
|
|
|
|
params += messageVisibility
|
|
? `&messageVisibility=${messageVisibility}`
|
|
: '';
|
|
|
|
params += loginHint ? `&loginHint=${loginHint}` : '';
|
|
|
|
redirect(`${authServerUrl}/auth/${getProviderUrl(provider)}?${params}`);
|
|
},
|
|
[generateTransientToken, redirect],
|
|
);
|
|
|
|
return { triggerApisOAuth };
|
|
};
|