* Remove workspace schema creation from signUp * Set user workspaceMember nullable * Remove workspace creation * Handle null workspace in tokens * Update onboarding status * Generate types * Move createWorkspace to workspace resolver * Create workspace after signup * Update createWorkspace return type * Update createWorkspace return type * Create core.workspace at signup * WIP * Fix create workspace * Fix create workspace * Clean code * Remove useless recoil set * Simplify create workspace request * Set currentWorkspace at login * Fix tests * Create a recoil value for is workspaceSchema created * Rename createWorkspace to createWorkspaceSchema * Code review returns * Use AppPath when possible * Try without state * Fix * Fixes * Rename createWorkspaceSchema to activateWorkspace * Remove defaultAvatarUrl from user * Add defaultAvatarUrl to core user This reverts commit 1701c30eb18804558293cc42043aedf96ea888df. * Add defaultAvatarUrl to core user This reverts commit 1701c30eb18804558293cc42043aedf96ea888df. * Fix ci * Fix tests * Fix storybook * Fix test * Remove useless query * Fix test * Fix test * Fix mock data * Fix test * Clean Mock Requests * Fix tentative * Revert "Clean Mock Requests" This reverts commit 8aa20a34363ffddfdee24f18fc80b27ea0ad5e1d. * Fix * Revert "Fix" This reverts commit 2df7e9b6569b8bfb53f6a45391db725e28d16a18. * Revert "Revert "Clean Mock Requests"" This reverts commit 3aefef8e9600d161434a047e845563d1b8e0692e. * Revert "Fix tentative" This reverts commit 13e7748d6f3b3858d30fb08adbc8ad347c5556ee. * Update filename --------- Co-authored-by: Charles Bochet <charles@twenty.com>
238 lines
7.3 KiB
TypeScript
238 lines
7.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
|
|
import { useRecoilValue } from 'recoil';
|
|
|
|
import { useOpenCreateActivityDrawer } from '@/activities/hooks/useOpenCreateActivityDrawer';
|
|
import { useEventTracker } from '@/analytics/hooks/useEventTracker';
|
|
import { useOnboardingStatus } from '@/auth/hooks/useOnboardingStatus';
|
|
import { OnboardingStatus } from '@/auth/utils/getOnboardingStatus';
|
|
import { isSignUpDisabledState } from '@/client-config/states/isSignUpDisabledState';
|
|
import { useCommandMenu } from '@/command-menu/hooks/useCommandMenu';
|
|
import { CommandType } from '@/command-menu/types/Command';
|
|
import { TableHotkeyScope } from '@/object-record/record-table/types/TableHotkeyScope';
|
|
import { AppBasePath } from '@/types/AppBasePath';
|
|
import { AppPath } from '@/types/AppPath';
|
|
import { PageHotkeyScope } from '@/types/PageHotkeyScope';
|
|
import { SettingsPath } from '@/types/SettingsPath';
|
|
import { IconCheckbox } from '@/ui/display/icon';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { useSetHotkeyScope } from '@/ui/utilities/hotkey/hooks/useSetHotkeyScope';
|
|
import { useGetWorkspaceFromInviteHashLazyQuery } from '~/generated/graphql';
|
|
|
|
import { useIsMatchingLocation } from '../hooks/useIsMatchingLocation';
|
|
|
|
// TODO: break down into smaller functions and / or hooks
|
|
export const PageChangeEffect = () => {
|
|
const navigate = useNavigate();
|
|
const isMatchingLocation = useIsMatchingLocation();
|
|
const { enqueueSnackBar } = useSnackBar();
|
|
|
|
const [previousLocation, setPreviousLocation] = useState('');
|
|
|
|
const onboardingStatus = useOnboardingStatus();
|
|
|
|
const setHotkeyScope = useSetHotkeyScope();
|
|
|
|
const location = useLocation();
|
|
|
|
const eventTracker = useEventTracker();
|
|
|
|
const [workspaceFromInviteHashQuery] =
|
|
useGetWorkspaceFromInviteHashLazyQuery();
|
|
const { addToCommandMenu, setToIntitialCommandMenu } = useCommandMenu();
|
|
|
|
const openCreateActivity = useOpenCreateActivityDrawer();
|
|
|
|
const isSignUpDisabled = useRecoilValue(isSignUpDisabledState);
|
|
|
|
useEffect(() => {
|
|
if (!previousLocation || previousLocation !== location.pathname) {
|
|
setPreviousLocation(location.pathname);
|
|
} else {
|
|
return;
|
|
}
|
|
}, [location, previousLocation]);
|
|
|
|
useEffect(() => {
|
|
const isMatchingOngoingUserCreationRoute =
|
|
isMatchingLocation(AppPath.SignUp) ||
|
|
isMatchingLocation(AppPath.SignIn) ||
|
|
isMatchingLocation(AppPath.Invite) ||
|
|
isMatchingLocation(AppPath.Verify);
|
|
|
|
const isMatchingOnboardingRoute =
|
|
isMatchingOngoingUserCreationRoute ||
|
|
isMatchingLocation(AppPath.CreateWorkspace) ||
|
|
isMatchingLocation(AppPath.CreateProfile) ||
|
|
isMatchingLocation(AppPath.PlanRequired);
|
|
|
|
const navigateToSignUp = () => {
|
|
enqueueSnackBar('workspace does not exist', {
|
|
variant: 'error',
|
|
});
|
|
navigate(AppPath.SignUp);
|
|
};
|
|
|
|
if (
|
|
onboardingStatus === OnboardingStatus.OngoingUserCreation &&
|
|
!isMatchingOngoingUserCreationRoute &&
|
|
!isMatchingLocation(AppPath.ResetPassword)
|
|
) {
|
|
navigate(AppPath.SignIn);
|
|
} else if (
|
|
onboardingStatus &&
|
|
[OnboardingStatus.Canceled, OnboardingStatus.Incomplete].includes(
|
|
onboardingStatus,
|
|
) &&
|
|
!isMatchingLocation(AppPath.PlanRequired)
|
|
) {
|
|
navigate(AppPath.PlanRequired);
|
|
} else if (
|
|
onboardingStatus === OnboardingStatus.OngoingWorkspaceActivation &&
|
|
!isMatchingLocation(AppPath.CreateWorkspace)
|
|
) {
|
|
navigate(AppPath.CreateWorkspace);
|
|
} else if (
|
|
onboardingStatus === OnboardingStatus.OngoingProfileCreation &&
|
|
!isMatchingLocation(AppPath.CreateProfile)
|
|
) {
|
|
navigate(AppPath.CreateProfile);
|
|
} else if (
|
|
onboardingStatus === OnboardingStatus.Completed &&
|
|
isMatchingOnboardingRoute
|
|
) {
|
|
navigate(AppPath.Index);
|
|
} else if (isMatchingLocation(AppPath.Invite)) {
|
|
const inviteHash =
|
|
matchPath({ path: '/invite/:workspaceInviteHash' }, location.pathname)
|
|
?.params.workspaceInviteHash || '';
|
|
|
|
workspaceFromInviteHashQuery({
|
|
variables: {
|
|
inviteHash,
|
|
},
|
|
onCompleted: (data) => {
|
|
if (!data.findWorkspaceFromInviteHash) {
|
|
navigateToSignUp();
|
|
}
|
|
},
|
|
onError: (_) => {
|
|
navigateToSignUp();
|
|
},
|
|
});
|
|
} else if (isMatchingLocation(AppPath.SignUp) && isSignUpDisabled) {
|
|
navigate(AppPath.SignIn);
|
|
}
|
|
}, [
|
|
enqueueSnackBar,
|
|
isMatchingLocation,
|
|
isSignUpDisabled,
|
|
location.pathname,
|
|
navigate,
|
|
onboardingStatus,
|
|
workspaceFromInviteHashQuery,
|
|
]);
|
|
|
|
useEffect(() => {
|
|
switch (true) {
|
|
case isMatchingLocation(AppPath.RecordIndexPage): {
|
|
setHotkeyScope(TableHotkeyScope.Table, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.RecordShowPage): {
|
|
setHotkeyScope(PageHotkeyScope.CompanyShowPage, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.OpportunitiesPage): {
|
|
setHotkeyScope(PageHotkeyScope.OpportunitiesPage, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.TasksPage): {
|
|
setHotkeyScope(PageHotkeyScope.TaskPage, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
|
|
case isMatchingLocation(AppPath.SignIn): {
|
|
setHotkeyScope(PageHotkeyScope.SignInUp);
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.SignUp): {
|
|
setHotkeyScope(PageHotkeyScope.SignInUp);
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.Invite): {
|
|
setHotkeyScope(PageHotkeyScope.SignInUp);
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.CreateProfile): {
|
|
setHotkeyScope(PageHotkeyScope.CreateProfile);
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.CreateWorkspace): {
|
|
setHotkeyScope(PageHotkeyScope.CreateWokspace);
|
|
break;
|
|
}
|
|
case isMatchingLocation(AppPath.PlanRequired): {
|
|
setHotkeyScope(PageHotkeyScope.PlanRequired);
|
|
break;
|
|
}
|
|
case isMatchingLocation(SettingsPath.ProfilePage, AppBasePath.Settings): {
|
|
setHotkeyScope(PageHotkeyScope.ProfilePage, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
case isMatchingLocation(
|
|
SettingsPath.WorkspaceMembersPage,
|
|
AppBasePath.Settings,
|
|
): {
|
|
setHotkeyScope(PageHotkeyScope.WorkspaceMemberPage, {
|
|
goto: true,
|
|
keyboardShortcutMenu: true,
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}, [isMatchingLocation, setHotkeyScope]);
|
|
|
|
useEffect(() => {
|
|
setToIntitialCommandMenu();
|
|
|
|
addToCommandMenu([
|
|
{
|
|
id: 'create-task',
|
|
to: '',
|
|
label: 'Create Task',
|
|
type: CommandType.Create,
|
|
Icon: IconCheckbox,
|
|
onCommandClick: () => openCreateActivity({ type: 'Task' }),
|
|
},
|
|
]);
|
|
}, [addToCommandMenu, setToIntitialCommandMenu, openCreateActivity]);
|
|
|
|
useEffect(() => {
|
|
setTimeout(() => {
|
|
eventTracker('pageview', {
|
|
location: {
|
|
pathname: location.pathname,
|
|
},
|
|
});
|
|
}, 500);
|
|
}, [eventTracker, location.pathname]);
|
|
|
|
return <></>;
|
|
};
|