The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { isResizablePanelDraggingState } from '@/ui/layout/resizable-panel/states/isResizablePanelDraggingState';
import { SidePanelAskAiHandoffEffect } from '@/side-panel/components/SidePanelAskAiHandoffEffect';
import { SidePanelRouter } from '@/side-panel/components/SidePanelRouter';
import { SidePanelWidthEffect } from '@/side-panel/components/SidePanelWidthEffect';
import { SIDE_PANEL_CLICK_OUTSIDE_ID } from '@/side-panel/constants/SidePanelClickOutsideId';
import { SIDE_PANEL_CONSTRAINTS } from '@/side-panel/constants/SidePanelConstraints';
import { useSidePanelCloseAnimationCompleteCleanup } from '@/side-panel/hooks/useSidePanelCloseAnimationCompleteCleanup';
import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';
import { isSidePanelClosingState } from '@/side-panel/states/isSidePanelClosingState';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import {
SIDE_PANEL_WIDTH_VAR,
sidePanelWidthState,
} from '@/side-panel/states/sidePanelWidthState';
import { DialogContainerContext } from '@/ui/layout/dialog/contexts/DialogContainerContext';
import { ResizeHandle } from 'twenty-ui/primitives/layout';
import { useLingui } from '@lingui/react/macro';
import { getUiZoom } from '@/ui/theme/utils/getUiZoom';
import { ParentClickOutsideIdContext } from '@/ui/utilities/pointer-event/contexts/ParentClickOutsideIdContext';
import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { styled } from '@linaria/react';
import { useReducedMotion } from 'framer-motion';
import { useStore } from 'jotai';
import { type AnimationEvent, useCallback, useState } from 'react';
import { themeCssVariables } from 'twenty-ui/theme';
const StyledSidePanelWrapper = styled.div<{
isOpen: boolean;
isResizing: boolean;
}>`
flex-shrink: 0;
min-width: 0;
overflow: hidden;
transition: ${({ isResizing }) =>
isResizing
? 'none'
: `width calc(${themeCssVariables.animation.duration.normal} * 1s)`};
width: ${({ isOpen }) => (isOpen ? `var(${SIDE_PANEL_WIDTH_VAR})` : '0px')};
@keyframes sidePanelShrinkFromFullWidth {
from {
width: 100%;
}
}
&[data-shrink-from-full-width='true'] {
animation: sidePanelShrinkFromFullWidth
calc(${themeCssVariables.animation.duration.normal} * 1s);
}
`;
const StyledSidePanel = styled.aside<{ isShrinkingFromFullWidth: boolean }>`
background: ${themeCssVariables.background.primary};
border-left: 1px solid ${themeCssVariables.border.color.medium};
box-sizing: border-box;
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
position: relative;
width: ${({ isShrinkingFromFullWidth }) =>
isShrinkingFromFullWidth ? '100%' : `var(${SIDE_PANEL_WIDTH_VAR})`};
`;
const StyledModalContainer = styled.div`
height: 100%;
left: 0;
pointer-events: none;
position: absolute;
top: 0;
width: 100%;
z-index: 1;
`;
export const SidePanelForDesktop = () => {
const { t } = useLingui();
const store = useStore();
const isSidePanelOpened = useAtomStateValue(isSidePanelOpenedState);
const [sidePanelWidth, setSidePanelWidth] = useAtomState(sidePanelWidthState);
const { closeSidePanelMenu } = useSidePanelMenu();
const { sidePanelCloseAnimationCompleteCleanup } =
useSidePanelCloseAnimationCompleteCleanup();
const shouldReduceMotion = useReducedMotion();
const [modalContainer, setModalContainer] = useState<HTMLDivElement | null>(
null,
);
const [isResizing, setIsResizing] = useState(false);
const [liveWidth, setLiveWidth] = useState<number | null>(null);
const [shouldRenderContent, setShouldRenderContent] =
useState(isSidePanelOpened);
const [isShrinkingFromFullWidth, setIsShrinkingFromFullWidth] =
useState(false);
const handleContinueChatFromFullWidth = useCallback(() => {
if (shouldReduceMotion === true) {
return;
}
setIsShrinkingFromFullWidth(true);
}, [shouldReduceMotion]);
const setIsResizablePanelDragging = useSetAtomState(
isResizablePanelDraggingState,
);
const shouldShowContent = isSidePanelOpened || shouldRenderContent;
if (isSidePanelOpened && !shouldRenderContent) {
setShouldRenderContent(true);
}
const handleTransitionEnd = () => {
if (isSidePanelOpened) {
return;
}
setShouldRenderContent(false);
if (store.get(isSidePanelClosingState.atom)) {
sidePanelCloseAnimationCompleteCleanup();
}
};
const handleAnimationEnd = (event: AnimationEvent<HTMLDivElement>) => {
if (event.target !== event.currentTarget) {
return;
}
setIsShrinkingFromFullWidth(false);
handleTransitionEnd();
};
const handleModalContainerRef = useCallback(
(element: HTMLDivElement | null) => {
setModalContainer(element);
},
[],
);
const handleWidthChange = useCallback(
(width: number) => {
setSidePanelWidth(width);
setLiveWidth(null);
setIsResizing(false);
setIsResizablePanelDragging(false);
},
[setSidePanelWidth, setIsResizablePanelDragging],
);
const handleWidthPreview = (width: number) => {
setLiveWidth(width);
document.documentElement.style.setProperty(
SIDE_PANEL_WIDTH_VAR,
`${width}px`,
);
};
const handleResizeEnd = () => {
setLiveWidth(null);
setIsResizing(false);
setIsResizablePanelDragging(false);
};
const handleResizeStart = useCallback(() => {
setIsResizing(true);
setIsResizablePanelDragging(true);
}, [setIsResizablePanelDragging]);
const handleCollapse = useCallback(() => {
closeSidePanelMenu();
setLiveWidth(null);
setIsResizing(false);
setIsResizablePanelDragging(false);
}, [closeSidePanelMenu, setIsResizablePanelDragging]);
return (
<>
<SidePanelWidthEffect />
<SidePanelAskAiHandoffEffect
onContinueChatFromFullWidth={handleContinueChatFromFullWidth}
/>
{isSidePanelOpened && (
<ResizeHandle
edge="left"
min={SIDE_PANEL_CONSTRAINTS.min}
max={SIDE_PANEL_CONSTRAINTS.max}
value={liveWidth ?? sidePanelWidth}
onValueChange={handleWidthPreview}
onValueCommitted={handleWidthChange}
onActivate={handleCollapse}
placement="gap"
aria-label={t`Resize side panel`}
scale={getUiZoom}
onResizeEnd={handleResizeEnd}
onResizeStart={handleResizeStart}
/>
)}
<StyledSidePanelWrapper
isOpen={isSidePanelOpened}
isResizing={isResizing}
onTransitionEnd={handleTransitionEnd}
onAnimationEnd={handleAnimationEnd}
data-shrink-from-full-width={isShrinkingFromFullWidth}
data-side-panel=""
data-click-outside-id={SIDE_PANEL_CLICK_OUTSIDE_ID}
>
<StyledSidePanel isShrinkingFromFullWidth={isShrinkingFromFullWidth}>
<StyledModalContainer ref={handleModalContainerRef} />
<DialogContainerContext.Provider
value={{ container: modalContainer }}
>
<ParentClickOutsideIdContext.Provider
value={SIDE_PANEL_CLICK_OUTSIDE_ID}
>
{shouldShowContent && <SidePanelRouter />}
</ParentClickOutsideIdContext.Provider>
</DialogContainerContext.Provider>
</StyledSidePanel>
</StyledSidePanelWrapper>
</>
);
};