Skip to content

The open alternative to Salesforce, designed for AI.

4.6 KB
Raw Blame
import { type DialogProps } from '@/ui/feedback/dialog-manager/types/DialogProps';
import { styled } from '@linaria/react';
import { motion } from 'framer-motion';
import { Key } from 'ts-key-enum';

import { DIALOG_CLICK_OUTSIDE_ID } from '@/ui/feedback/dialog-manager/constants/DialogClickOutsideId';
import { DIALOG_FOCUS_ID } from '@/ui/feedback/dialog-manager/constants/DialogFocusId';
import { DIALOG_LISTENER_ID } from '@/ui/feedback/dialog-manager/constants/DialogListenerId';
import { RootStackingContextZIndices } from '@/ui/layout/constants/RootStackingContextZIndices';
import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
import { useRef } from 'react';
import { isDefined } from 'twenty-shared/utils';
import { Button } from 'twenty-ui/primitives/input';
import { themeCssVariables } from 'twenty-ui/theme';

const StyledDialogOverlayBase = styled.div`
  align-items: center;
  background: ${themeCssVariables.background.overlayPrimary};
  display: flex;
  height: calc(100dvh / var(--t-zoom, 1));
  justify-content: center;
  left: 0;
  position: fixed;
  top: 0;
  width: calc(100vw / var(--t-zoom, 1));
  z-index: ${RootStackingContextZIndices.Dialog};
`;
const StyledDialogOverlay = motion.create(StyledDialogOverlayBase);

const StyledDialogContainerBase = styled.div`
  background: ${themeCssVariables.background.primary};
  border-radius: ${themeCssVariables.border.radius.md};
  display: flex;
  flex-direction: column;
  max-width: 320px;
  padding: 2em;
  position: relative;
  width: 100%;
`;
const StyledDialogContainer = motion.create(StyledDialogContainerBase);

const StyledDialogTitle = styled.span`
  color: ${themeCssVariables.font.color.primary};
  font-size: ${themeCssVariables.font.size.md};
  font-weight: ${themeCssVariables.font.weight.semiBold};
  margin-bottom: ${themeCssVariables.spacing[6]};
  text-align: center;
`;

const StyledDialogMessage = styled.span`
  color: ${themeCssVariables.font.color.primary};
  font-size: ${themeCssVariables.font.size.sm};
  font-weight: ${themeCssVariables.font.weight.regular};
  margin-bottom: ${themeCssVariables.spacing[6]};
  text-align: center;
`;

const StyledDialogButtonContainer = styled.div`
  display: flex;
  justify-content: center;
  margin-bottom: ${themeCssVariables.spacing[2]};
`;

export const Dialog = ({
  title,
  message,
  buttons = [],
  children,
  className,
  onClose,
  id,
}: DialogProps) => {
  const dialogVariants = {
    open: { opacity: 1 },
    closed: { opacity: 0 },
  };

  const containerVariants = {
    open: { y: 0 },
    closed: { y: '50vh' },
  };

  const handleEnter = (event: KeyboardEvent) => {
    const confirmButton = buttons.find((button) => button.role === 'confirm');

    event.preventDefault();

    if (isDefined(confirmButton)) {
      confirmButton?.onClick?.(event);
      onClose?.();
    }
  };

  const handleEscape = (event: KeyboardEvent) => {
    event.preventDefault();
    onClose?.();
  };

  useHotkeysOnFocusedElement({
    keys: [Key.Enter],
    callback: handleEnter,
    focusId: DIALOG_FOCUS_ID,
    dependencies: [buttons],
  });

  useHotkeysOnFocusedElement({
    keys: [Key.Escape],
    callback: handleEscape,
    focusId: DIALOG_FOCUS_ID,
    dependencies: [handleEscape],
  });

  const dialogRef = useRef<HTMLDivElement>(null);

  useListenClickOutside({
    refs: [dialogRef],
    callback: () => {
      onClose?.();
    },
    listenerId: DIALOG_LISTENER_ID,
  });

  return (
    <StyledDialogOverlay
      variants={dialogVariants}
      initial="closed"
      animate="open"
      exit="closed"
      className={className}
      data-click-outside-id={DIALOG_CLICK_OUTSIDE_ID}
    >
      <StyledDialogContainer
        variants={containerVariants}
        transition={{ damping: 15, stiffness: 100 }}
        id={id}
        ref={dialogRef}
      >
        {title && <StyledDialogTitle>{title}</StyledDialogTitle>}
        {message && <StyledDialogMessage>{message}</StyledDialogMessage>}
        {children}
        {buttons.map(({ color, onClick, title: key, variant }) => (
          <StyledDialogButtonContainer key={key}>
            <Button
              onClick={(event: React.MouseEvent<HTMLButtonElement>) => {
                onClose?.();
                onClick?.(event);
              }}
              fullWidth={true}
              color={color}
              variant={variant ?? 'outline'}
            >
              {key}
            </Button>
          </StyledDialogButtonContainer>
        ))}
      </StyledDialogContainer>
    </StyledDialogOverlay>
  );
};