Skip to content

Commit

Permalink
feat(subdomain): add env var IS_MULTIWORKSPACE_ENABLED + add link on …
Browse files Browse the repository at this point in the history
…switch workspace button
  • Loading branch information
AMoreaux committed Nov 12, 2024
1 parent 8050c5a commit 7175b6d
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
9 changes: 5 additions & 4 deletions packages/twenty-front/src/generated/graphql.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import { Title } from '@/auth/components/Title';
import { useFindAvailableWorkspacesByEmail } from '@/auth/sign-in-up/hooks/useFindAvailableWorkspacedByEmail';
import { FormEvent, useState } from 'react';
import { FooterNote } from '@/auth/sign-in-up/components/FooterNote';
import { useSetRecoilState } from 'recoil';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { isDefined } from '~/utils/isDefined';
import { availableWorkspacesForAuthState } from '@/auth/states/availableWorkspacesForAuthState';
import {
SignInUpStep,
signInUpStepState,
} from '@/auth/states/signInUpStepState';
import { redirectToWorkspace } from '~/utils/workspace-url.helper';
import { isSignInPrefilledState } from '@/client-config/states/isSignInPrefilledState';

const StyledContentContainer = styled(motion.div)`
margin-bottom: ${({ theme }) => theme.spacing(8)};
Expand Down Expand Up @@ -57,6 +58,7 @@ const validationSchema = z

export const SignInUpGlobalScope = () => {
const theme = useTheme();
const isSignInPrefilled = useRecoilValue(isSignInPrefilledState);

const { signInWithGoogle } = useSignInWithGoogle();
const { signInWithMicrosoft } = useSignInWithMicrosoft();
Expand All @@ -72,7 +74,7 @@ export const SignInUpGlobalScope = () => {
const form = useForm<z.infer<typeof validationSchema>>({
mode: 'onChange',
defaultValues: {
email: '',
email: isSignInPrefilled === true ? '[email protected]' : '',
},
resolver: zodResolver(validationSchema),
});
Expand All @@ -81,7 +83,7 @@ export const SignInUpGlobalScope = () => {
event.preventDefault();
setShowErrors(true);

const { data, error } = await findAvailableWorkspacesByEmail(
const { data } = await findAvailableWorkspacesByEmail(
form.getValues('email'),
);
if (isDefined(data) && data.findAvailableWorkspacesByEmail.length > 1) {
Expand Down
5 changes: 4 additions & 1 deletion packages/twenty-front/src/modules/auth/states/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { createState } from 'twenty-ui';

import { Workspace } from '~/generated/graphql';

export type Workspaces = Pick<Workspace, 'id' | 'logo' | 'displayName'>;
export type Workspaces = Pick<
Workspace,
'id' | 'logo' | 'displayName' | 'subdomain'
>;

export const workspacesState = createState<Workspaces[] | null>({
key: 'workspacesState',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import { useState } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';
import { IconChevronDown, MenuItemSelectAvatar } from 'twenty-ui';
import { getImageAbsoluteURI } from '~/utils/image/getImageAbsoluteURI';
import { buildWorkspaceUrl } from '~/utils/workspace-url.helper';
import { Link } from 'react-router-dom';

const StyledLink = styled(Link)`
text-decoration: none;
width: 100%;
`;

const StyledLogo = styled.div<{ logo: string }>`
background: url(${({ logo }) => logo});
Expand Down Expand Up @@ -114,19 +121,24 @@ export const MultiWorkspaceDropdownButton = ({
dropdownComponents={
<DropdownMenuItemsContainer>
{workspaces.map((workspace) => (
<MenuItemSelectAvatar
key={workspace.id}
text={workspace.displayName ?? ''}
avatar={
<StyledLogo
logo={getImageAbsoluteURI(
workspace.logo ?? DEFAULT_WORKSPACE_LOGO,
)}
/>
}
selected={currentWorkspace?.id === workspace.id}
onClick={() => handleChange(workspace.id)}
/>
<StyledLink to={buildWorkspaceUrl(workspace.subdomain)}>
<MenuItemSelectAvatar
key={workspace.id}
text={workspace.displayName ?? ''}
avatar={
<StyledLogo
logo={getImageAbsoluteURI(
workspace.logo ?? DEFAULT_WORKSPACE_LOGO,
)}
/>
}
selected={currentWorkspace?.id === workspace.id}
onClick={(event: React.MouseEvent) => {
event.preventDefault();
handleChange(workspace.id);
}}
/>
</StyledLink>
))}
</DropdownMenuItemsContainer>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { AppPath } from '@/types/AppPath';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { useSwitchWorkspaceMutation } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
import { redirectToWorkspace } from '~/utils/workspace-url.helper';
import {
redirectToHome,
redirectToWorkspace,
} from '~/utils/workspace-url.helper';

export const useWorkspaceSwitching = () => {
const [switchWorkspaceMutation] = useSwitchWorkspaceMutation();
Expand All @@ -20,7 +23,7 @@ export const useWorkspaceSwitching = () => {
});

if (isDefined(errors) || !isDefined(data?.switchWorkspace.subdomain)) {
return (window.location.href = AppPath.Index);
return redirectToHome();
}

redirectToWorkspace(data.switchWorkspace.subdomain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const USER_QUERY_FRAGMENT = gql`
logo
displayName
domainName
subdomain
}
}
userVars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,15 @@ export class SignInUpService {
lastName: string;
picture: SignInUpServiceInput['picture'];
}) {
if (this.environmentService.get('IS_SIGN_UP_DISABLED')) {
throw new AuthException(
'Sign up is disabled',
AuthExceptionCode.FORBIDDEN_EXCEPTION,
);
if (!this.environmentService.get('IS_MULTIWORKSPACE_ENABLED')) {
const numberOfWorkspaces = await this.workspaceRepository.count();

if (numberOfWorkspaces > 0) {
throw new AuthException(
'New workspace setup is disabled',
AuthExceptionCode.FORBIDDEN_EXCEPTION,
);
}
}

const workspaceToCreate = this.workspaceRepository.create({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ export class EnvironmentVariables {
@IsOptional()
ENTERPRISE_KEY: string;

@CastToBoolean()
@IsOptional()
@IsBoolean()
IS_MULTIWORKSPACE_ENABLED = true;

// Custom Code Engine
@IsEnum(ServerlessDriverType)
@IsOptional()
Expand Down

0 comments on commit 7175b6d

Please sign in to comment.