The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { SidePanelBackButton } from '@/side-panel/components/SidePanelBackButton';
import { SidePanelCloseButton } from '@/side-panel/components/SidePanelCloseButton';
import { SidePanelPageInfo } from '@/side-panel/components/SidePanelPageInfo';
import { SidePanelTopBarEscapeHotkeyEffect } from '@/side-panel/components/SidePanelTopBarEscapeHotkeyEffect';
import { SidePanelTopBarInputFocusEffect } from '@/side-panel/components/SidePanelTopBarInputFocusEffect';
import { SidePanelExpandButton } from '@/side-panel/components/SidePanelExpandButton';
import { SidePanelTopBarRightCornerIcon } from '@/side-panel/components/SidePanelTopBarRightCornerIcon';
import { COMMAND_MENU_SIDE_PANEL_PAGES } from '@/side-panel/constants/CommandMenuSidePanelPages';
import { SIDE_PANEL_FOCUS_ID } from '@/side-panel/constants/SidePanelFocusId';
import { SIDE_PANEL_TOP_BAR_HEIGHT } from '@/side-panel/constants/SidePanelTopBarHeight';
import { SIDE_PANEL_TOP_BAR_HEIGHT_MOBILE } from '@/side-panel/constants/SidePanelTopBarHeightMobile';
import { useHandleSidePanelBackspace } from '@/side-panel/hooks/useHandleSidePanelBackspace';
import { useHandleSidePanelEscape } from '@/side-panel/hooks/useHandleSidePanelEscape';
import { useSidePanelContextChips } from '@/side-panel/hooks/useSidePanelContextChips';
import { sidePanelNavigationStackState } from '@/side-panel/states/sidePanelNavigationStackState';
import { sidePanelSearchState } from '@/side-panel/states/sidePanelSearchState';
import { usePushFocusItemToFocusStack } from '@/ui/utilities/focus/hooks/usePushFocusItemToFocusStack';
import { useRemoveFocusItemFromFocusStackById } from '@/ui/utilities/focus/hooks/useRemoveFocusItemFromFocusStackById';
import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { AnimatePresence, motion } from 'framer-motion';
import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useRef } from 'react';
import { Key } from 'ts-key-enum';
import { SidePanelPages } from 'twenty-shared/types';
import { useIsMobile } from 'twenty-ui/utilities';
import { Input } from 'twenty-ui/primitives/input';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
const StyledInputContainer = styled.div<{ isMobile: boolean }>`
align-items: center;
background-color: ${themeCssVariables.background.secondary};
border: none;
border-bottom: 1px solid ${themeCssVariables.border.color.medium};
border-radius: 0;
box-sizing: border-box;
display: flex;
flex-shrink: 0;
font-size: ${themeCssVariables.font.size.lg};
gap: ${themeCssVariables.spacing[4]};
height: ${({ isMobile }) =>
isMobile ? SIDE_PANEL_TOP_BAR_HEIGHT_MOBILE : SIDE_PANEL_TOP_BAR_HEIGHT}px;
justify-content: space-between;
margin: 0;
outline: none;
overflow: hidden;
padding: 0 ${themeCssVariables.spacing[2]};
position: relative;
`;
const StyledContentContainer = styled.div`
align-items: center;
display: flex;
flex: 1;
gap: ${themeCssVariables.spacing[1]};
min-width: 0;
overflow: hidden;
`;
const StyledHeaderTitleContainer = styled.div`
align-items: center;
display: flex;
flex: 1;
min-width: 0;
`;
const StyledHeaderTitlePortal = styled.div`
align-items: center;
display: none;
min-width: 0;
&:not(:empty) {
display: flex;
flex: 1;
}
&:not(:empty) + [data-side-panel-page-info] {
display: none;
}
`;
const StyledHeaderActionsPortal = styled.div`
align-items: center;
display: flex;
gap: ${themeCssVariables.spacing[1]};
`;
const StyledRightControlsContainer = styled.div`
align-items: center;
display: flex;
flex-shrink: 0;
gap: ${themeCssVariables.spacing[1]};
`;
export const SidePanelTopBar = ({
setHeaderTitlePortal,
setHeaderActionsPortal,
}: {
setHeaderTitlePortal?: (element: HTMLElement | null) => void;
setHeaderActionsPortal?: (element: HTMLElement | null) => void;
}) => {
const [sidePanelSearch, setSidePanelSearch] =
useAtomState(sidePanelSearchState);
const inputRef = useRef<HTMLInputElement>(null);
const { t } = useLingui();
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSidePanelSearch(event.target.value);
};
const isMobile = useIsMobile();
const sidePanelNavigationStack = useAtomStateValue(
sidePanelNavigationStackState,
);
const sidePanelPage =
sidePanelNavigationStack.at(-1)?.page ?? SidePanelPages.CommandMenuDisplay;
const theme = useTheme();
const { contextChips } = useSidePanelContextChips();
const { pushFocusItemToFocusStack } = usePushFocusItemToFocusStack();
const { removeFocusItemFromFocusStackById } =
useRemoveFocusItemFromFocusStackById();
const handleSidePanelBackspace = useHandleSidePanelBackspace();
const handleSidePanelEscape = useHandleSidePanelEscape();
const handleInputFocus = () => {
pushFocusItemToFocusStack({
focusId: SIDE_PANEL_FOCUS_ID,
component: {
type: FocusComponentType.TEXT_INPUT,
instanceId: SIDE_PANEL_FOCUS_ID,
},
globalHotkeysConfig: {
enableGlobalHotkeysConflictingWithKeyboard: false,
},
});
};
const handleInputBlur = () => {
removeFocusItemFromFocusStackById({
focusId: SIDE_PANEL_FOCUS_ID,
});
};
const handleInputKeyDownCapture = (
event: React.KeyboardEvent<HTMLInputElement>,
) => {
if (event.nativeEvent.isComposing || event.keyCode === 229) {
return;
}
if (event.key === Key.Escape) {
event.preventDefault();
event.stopPropagation();
event.nativeEvent.stopImmediatePropagation();
handleSidePanelEscape();
return;
}
if (
event.key === Key.Backspace &&
!event.metaKey &&
!event.ctrlKey &&
handleSidePanelBackspace()
) {
event.preventDefault();
event.stopPropagation();
event.nativeEvent.stopImmediatePropagation();
}
};
const currentPage = sidePanelNavigationStack.at(-1)?.page;
const previousPage = sidePanelNavigationStack.at(-2)?.page;
const canGoBack =
currentPage !== undefined &&
COMMAND_MENU_SIDE_PANEL_PAGES.includes(currentPage)
? previousPage !== undefined &&
COMMAND_MENU_SIDE_PANEL_PAGES.includes(previousPage)
: sidePanelNavigationStack.length > 1;
const shouldShowBackButton = canGoBack;
const shouldHideCloseButton = isMobile && shouldShowBackButton;
const lastChip = contextChips.at(-1);
return (
<StyledInputContainer isMobile={isMobile}>
<SidePanelTopBarEscapeHotkeyEffect
inputRef={inputRef}
onEscape={handleSidePanelEscape}
/>
<StyledContentContainer>
<AnimatePresence>
{shouldShowBackButton && (
<motion.div
key="side-panel-back-button"
exit={{ opacity: 0, width: 0 }}
transition={{
duration: theme.animation.duration.instant,
}}
>
<SidePanelBackButton />
</motion.div>
)}
</AnimatePresence>
{!COMMAND_MENU_SIDE_PANEL_PAGES.includes(sidePanelPage) && (
<StyledHeaderTitleContainer>
<StyledHeaderTitlePortal ref={setHeaderTitlePortal} />
{lastChip && (
<div data-side-panel-page-info="">
<SidePanelPageInfo pageChip={lastChip} />
</div>
)}
</StyledHeaderTitleContainer>
)}
{COMMAND_MENU_SIDE_PANEL_PAGES.includes(sidePanelPage) && (
<>
<Input
variant="plain"
hasFocusRing={false}
size="sm"
aria-label={t`Search`}
data-testid={SIDE_PANEL_FOCUS_ID}
ref={inputRef}
value={sidePanelSearch}
placeholder={
sidePanelPage === SidePanelPages.SearchAiChats
? t`Search chat titles...`
: t`Type anything...`
}
onChange={handleSearchChange}
onKeyDownCapture={handleInputKeyDownCapture}
onFocus={handleInputFocus}
onBlur={handleInputBlur}
/>
<SidePanelTopBarInputFocusEffect inputRef={inputRef} />
</>
)}
</StyledContentContainer>
<StyledRightControlsContainer>
<StyledHeaderActionsPortal ref={setHeaderActionsPortal} />
{sidePanelPage !== SidePanelPages.RoutedPage && (
<SidePanelTopBarRightCornerIcon />
)}
<SidePanelExpandButton />
{shouldHideCloseButton ? null : <SidePanelCloseButton />}
</StyledRightControlsContainer>
</StyledInputContainer>
);
};