8e83be43fe
Refactored error handling in `useSignUpInNewWorkspace` and `useSignInUp` hooks to provide user-friendly messages. Added validation for existing users during sign-up, updating server-side logic to throw specific exceptions (`USER_ALREADY_EXIST`). Fix https://twenty-v7.sentry.io/issues/6686753138/events/latest/?environment=prod&project=4507072499810304&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D%20QueryFailedError&referrer=latest-event&sort=date --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { useRedirectToWorkspaceDomain } from '@/domain-manager/hooks/useRedirectToWorkspaceDomain';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { ApolloError } from '@apollo/client';
|
|
import { AppPath } from 'twenty-shared/types';
|
|
import { useSignUpInNewWorkspaceMutation } from '~/generated-metadata/graphql';
|
|
import { getWorkspaceUrl } from '~/utils/getWorkspaceUrl';
|
|
import { assertIsDefinedOrThrow } from 'twenty-shared/utils';
|
|
import { useLingui } from '@lingui/react/macro';
|
|
|
|
export const useSignUpInNewWorkspace = () => {
|
|
const { redirectToWorkspaceDomain } = useRedirectToWorkspaceDomain();
|
|
const { enqueueErrorSnackBar } = useSnackBar();
|
|
const { t } = useLingui();
|
|
|
|
const [signUpInNewWorkspaceMutation] = useSignUpInNewWorkspaceMutation();
|
|
|
|
const createWorkspace = async ({ newTab } = { newTab: true }) => {
|
|
try {
|
|
const { data } = await signUpInNewWorkspaceMutation();
|
|
assertIsDefinedOrThrow(data?.signUpInNewWorkspace);
|
|
return await redirectToWorkspaceDomain(
|
|
getWorkspaceUrl(data.signUpInNewWorkspace.workspace.workspaceUrls),
|
|
AppPath.Verify,
|
|
{
|
|
loginToken: data.signUpInNewWorkspace.loginToken.token,
|
|
},
|
|
newTab ? '_blank' : '_self',
|
|
);
|
|
} catch (error) {
|
|
enqueueErrorSnackBar({
|
|
...(error instanceof ApolloError
|
|
? { apolloError: error }
|
|
: { message: t`Workspace creation failed` }),
|
|
});
|
|
}
|
|
};
|
|
|
|
return {
|
|
createWorkspace,
|
|
};
|
|
};
|