The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useExecuteTasksOnAnyLocationChange } from '@/app/hooks/useExecuteTasksOnAnyLocationChange';
import { useWorkspaceRouteObjects } from '@/app/routing/components/WorkspaceRouteObjectsProvider';
import { isAppEffectRedirectEnabledState } from '@/auth/states/isAppEffectRedirectEnabledState';
import { useReturnToPath } from '@/auth/hooks/useReturnToPath';
import { useIsOnAuthOrOnboardingPage } from '@/auth/hooks/useIsOnAuthOrOnboardingPage';
import { isLogConsoleFullScreenState } from '@/log-console/states/isLogConsoleFullScreenState';
import { useSidePanelHistory } from '@/side-panel/hooks/useSidePanelHistory';
import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';
import { SIDE_PANEL_PATH_SEARCH_PARAM } from '@/side-panel/routing/constants/SidePanelPathSearchParam';
import { isWorkspaceLocationAvailableOnSurface } from '@/app/routing/utils/isWorkspaceLocationAvailableOnSurface';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import { sidePanelNavigationStackState } from '@/side-panel/states/sidePanelNavigationStackState';
import { sidePanelPageInfoSelector } from '@/side-panel/states/sidePanelPageInfoSelector';
import { MAIN_CONTEXT_STORE_INSTANCE_ID } from '@/context-store/constants/MainContextStoreInstanceId';
import { contextStoreCurrentViewIdComponentState } from '@/context-store/states/contextStoreCurrentViewIdComponentState';
import { contextStoreCurrentViewTypeComponentState } from '@/context-store/states/contextStoreCurrentViewTypeComponentState';
import { ContextStoreViewType } from '@/context-store/types/ContextStoreViewType';
import { CoreObjectNamePlural } from '@/object-metadata/types/CoreObjectNamePlural';
import { useActiveRecordBoardCard } from '@/object-record/record-board/hooks/useActiveRecordBoardCard';
import { useFocusedRecordBoardCard } from '@/object-record/record-board/hooks/useFocusedRecordBoardCard';
import { useResetFocusStackToRecordIndex } from '@/object-record/record-index/hooks/useResetFocusStackToRecordIndex';
import { useResetRecordSelection } from '@/object-record/record-selection/hooks/useResetRecordSelection';
import { useActiveRecordTableRow } from '@/object-record/record-table/hooks/useActiveRecordTableRow';
import { useFocusedRecordTableRow } from '@/object-record/record-table/hooks/useFocusedRecordTableRow';
import { useOpenNewRecordTitleCell } from '@/object-record/record-title-cell/hooks/useOpenNewRecordTitleCell';
import { newRecordTitleCellToOpenState } from '@/object-record/record-title-cell/states/newRecordTitleCellToOpenState';
import { getRecordIndexIdFromObjectNamePluralAndViewId } from '@/object-record/utils/getRecordIndexIdFromObjectNamePluralAndViewId';
import { PageFocusId } from '@/types/PageFocusId';
import { useResetFocusStackToFocusItem } from '@/ui/utilities/focus/hooks/useResetFocusStackToFocusItem';
import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { currentPageLayoutIdState } from '@/page-layout/states/currentPageLayoutIdState';
import { useStore } from 'jotai';
import { useEffect, useState } from 'react';
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
import { AppBasePath, AppPath, SidePanelPages } from 'twenty-shared/types';
import { isDefined, isSafeInternalPath } from 'twenty-shared/utils';
import { usePageChangeEffectNavigateLocation } from '@/app/hooks/usePageChangeEffectNavigateLocation';
import { getPageLayoutIdForLocation } from '@/app/utils/getPageLayoutIdForLocation';
import { isMatchingLocation } from '~/utils/isMatchingLocation';
// TODO: break down into smaller functions and / or hooks
export const PageChangeEffect = () => {
const store = useStore();
const navigate = useNavigate();
const [previousLocation, setPreviousLocation] = useState('');
const location = useLocation();
const workspaceRouteObjects = useWorkspaceRouteObjects();
const sidePanelPathFromUrl = new URLSearchParams(location.search).get(
SIDE_PANEL_PATH_SEARCH_PARAM,
);
const hasRoutedSidePanelTarget =
isDefined(sidePanelPathFromUrl) &&
isSafeInternalPath(sidePanelPathFromUrl) &&
isWorkspaceLocationAvailableOnSurface(
workspaceRouteObjects,
'side-panel',
sidePanelPathFromUrl,
);
const pageChangeEffectNavigateLocation =
usePageChangeEffectNavigateLocation();
// TODO: make useResetRecordSelection accept an empty recordIndexId, then drop CoreObjectNamePlural.Person
const objectNamePlural =
matchPath(AppPath.RecordIndexPage, location.pathname)?.params
.objectNamePlural ?? CoreObjectNamePlural.Person;
const contextStoreCurrentViewId = useAtomComponentStateValue(
contextStoreCurrentViewIdComponentState,
MAIN_CONTEXT_STORE_INSTANCE_ID,
);
const contextStoreCurrentViewType = useAtomComponentStateValue(
contextStoreCurrentViewTypeComponentState,
MAIN_CONTEXT_STORE_INSTANCE_ID,
);
const recordIndexId = getRecordIndexIdFromObjectNamePluralAndViewId(
objectNamePlural,
contextStoreCurrentViewId || '',
);
const { resetRecordSelection } = useResetRecordSelection(recordIndexId);
const { unfocusRecordTableRow } = useFocusedRecordTableRow(recordIndexId);
const { deactivateRecordTableRow } = useActiveRecordTableRow(recordIndexId);
const { deactivateBoardCard } = useActiveRecordBoardCard(recordIndexId);
const { unfocusBoardCard } = useFocusedRecordBoardCard(recordIndexId);
const { executeTasksOnAnyLocationChange } =
useExecuteTasksOnAnyLocationChange();
const isAppEffectRedirectEnabled = useAtomStateValue(
isAppEffectRedirectEnabledState,
);
const { closeSidePanelMenu } = useSidePanelMenu();
const { removePageFromSidePanelHistory } = useSidePanelHistory();
const { saveReturnToPath, getReturnToPath, clearReturnToPath } =
useReturnToPath();
const isOnAuthOrOnboardingPage = useIsOnAuthOrOnboardingPage();
const { resetFocusStackToFocusItem } = useResetFocusStackToFocusItem();
const { resetFocusStackToRecordIndex } = useResetFocusStackToRecordIndex();
const { openNewRecordTitleCell } = useOpenNewRecordTitleCell();
useEffect(() => {
if (!previousLocation || previousLocation !== location.pathname) {
if (!hasRoutedSidePanelTarget) {
const currentPage = store.get(sidePanelPageInfoSelector.atom).page;
const shouldKeepSidePanelOpen =
currentPage === SidePanelPages.AskAI ||
currentPage === SidePanelPages.RoutedPage;
if (!shouldKeepSidePanelOpen) {
closeSidePanelMenu();
} else {
// Leaving a page ends the layout edit it hosted, so its page layout
// pages must not be reachable again through the panel history
store
.get(sidePanelNavigationStackState.atom)
.filter((navigationItem) =>
isDefined(navigationItem.pageLayoutSidePanelTarget),
)
.forEach(({ pageId }) => removePageFromSidePanelHistory(pageId));
}
}
setPreviousLocation(location.pathname);
executeTasksOnAnyLocationChange();
store.set(isLogConsoleFullScreenState.atom, false);
const newPageLayoutId = getPageLayoutIdForLocation({
location,
store,
});
store.set(currentPageLayoutIdState.atom, newPageLayoutId);
}
}, [
location,
previousLocation,
executeTasksOnAnyLocationChange,
store,
hasRoutedSidePanelTarget,
closeSidePanelMenu,
removePageFromSidePanelHistory,
]);
useEffect(() => {
if (
isDefined(pageChangeEffectNavigateLocation) &&
isAppEffectRedirectEnabled
) {
if (
pageChangeEffectNavigateLocation === AppPath.SignInUp &&
!isOnAuthOrOnboardingPage
) {
saveReturnToPath(
`${window.location.pathname}${window.location.search}${window.location.hash}`,
);
}
const consumedReturnToPath =
getReturnToPath() === pageChangeEffectNavigateLocation;
navigate(pageChangeEffectNavigateLocation);
if (consumedReturnToPath) {
clearReturnToPath();
}
}
}, [
navigate,
pageChangeEffectNavigateLocation,
isAppEffectRedirectEnabled,
isOnAuthOrOnboardingPage,
saveReturnToPath,
getReturnToPath,
clearReturnToPath,
store,
]);
useEffect(() => {
const isLeavingRecordIndexPage = !!matchPath(
AppPath.RecordIndexPage,
previousLocation,
);
if (isLeavingRecordIndexPage) {
resetRecordSelection();
if (contextStoreCurrentViewType === ContextStoreViewType.Table) {
unfocusRecordTableRow();
deactivateRecordTableRow();
}
if (contextStoreCurrentViewType === ContextStoreViewType.Kanban) {
deactivateBoardCard();
unfocusBoardCard();
}
}
if (location.pathname === previousLocation) {
return;
}
switch (true) {
case isMatchingLocation(location, AppPath.RecordIndexPage): {
resetFocusStackToRecordIndex();
break;
}
case isMatchingLocation(location, AppPath.RecordShowPage): {
const isSidePanelOpen = store.get(isSidePanelOpenedState.atom);
if (!isSidePanelOpen) {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.RecordShowPage,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.RecordShowPage,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: true,
enableGlobalHotkeysConflictingWithKeyboard: true,
},
},
});
}
const newRecordTitleCellToOpen = store.get(
newRecordTitleCellToOpenState.atom,
);
if (isDefined(newRecordTitleCellToOpen)) {
const objectRecordIdFromPath = matchPath(
AppPath.RecordShowPage,
location.pathname,
)?.params.objectRecordId;
if (newRecordTitleCellToOpen.recordId === objectRecordIdFromPath) {
openNewRecordTitleCell(newRecordTitleCellToOpen);
}
store.set(newRecordTitleCellToOpenState.atom, null);
}
break;
}
case isMatchingLocation(location, AppPath.AiChatInbox): {
const isEnteringAiChatInbox = !matchPath(
AppPath.AiChatInbox,
previousLocation,
);
if (!isEnteringAiChatInbox) {
break;
}
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.AiChatInbox,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.AiChatInbox,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: true,
enableGlobalHotkeysConflictingWithKeyboard: true,
},
},
shouldKeepItemsAbovePage: store.get(isSidePanelOpenedState.atom),
});
break;
}
case isMatchingLocation(location, AppPath.PageLayoutPage): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.PageLayoutPage,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.PageLayoutPage,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: true,
enableGlobalHotkeysConflictingWithKeyboard: true,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.SignInUp): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.SignInUp,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.SignInUp,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.Invite): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.InviteTeam,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.InviteTeam,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.CreateProfile): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.CreateProfile,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.CreateProfile,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.WorkspaceActivation): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.WorkspaceActivation,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.WorkspaceActivation,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.SyncEmails): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.SyncEmail,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.SyncEmail,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.InviteTeam): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.InviteTeam,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.InviteTeam,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case isMatchingLocation(location, AppPath.PlanRequired): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.PlanRequired,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.PlanRequired,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
case location.pathname.startsWith(AppBasePath.Settings): {
resetFocusStackToFocusItem({
focusStackItem: {
focusId: PageFocusId.Settings,
componentInstance: {
componentType: FocusComponentType.PAGE,
componentInstanceId: PageFocusId.Settings,
},
globalHotkeysConfig: {
enableGlobalHotkeysWithModifiers: false,
enableGlobalHotkeysConflictingWithKeyboard: false,
},
},
});
break;
}
}
}, [
location,
previousLocation,
contextStoreCurrentViewType,
resetRecordSelection,
unfocusRecordTableRow,
deactivateRecordTableRow,
deactivateBoardCard,
unfocusBoardCard,
resetFocusStackToRecordIndex,
resetFocusStackToFocusItem,
openNewRecordTitleCell,
store,
]);
return <></>;
};