Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.9 KB
Raw Blame
import {
  type EmailScheduleState,
  scheduleTimeLocked,
} from '@app/features/email-compose/primitives/email-send-schedule';
import { fireAndForget } from '@core/util/fireAndForget';
import ClockIcon from '@icon/system/regular/clock.svg';
import { buttonClasses, cn, Tooltip } from '@ui';
import { addYears } from 'date-fns/addYears';
import { format } from 'date-fns/format';
import { type JSX, Show, type VoidComponent } from 'solid-js';
import { DateSelector } from './date-selector';

interface EmailDateSelectorProps {
  state: EmailScheduleState;
  selectedTime?: Date;
  mobile: boolean;
  onSelectTime?: (date: Date | null) => void | boolean;
  onCancelSchedule?: () => Promise<boolean>;
  operation?: 'idle' | 'committing' | 'updating' | 'cancelling';
  /** Only show the clock icon, no date text. */
  compact?: boolean;
  /** Render content inline instead of in a portal. */
  disablePortal?: boolean;
  disabled?: boolean;
  trigger?: (state: {
    selectedDate: Date | null;
    formattedDate: string | undefined;
  }) => JSX.Element;
}

export const EmailDateSelector: VoidComponent<EmailDateSelectorProps> = (
  props
) => {
  const isCompact = () => props.compact || props.mobile;
  const formattedDate = () =>
    props.selectedTime
      ? format(props.selectedTime, 'MMM d, yyyy  h:mm a')
      : undefined;
  const isConfirmed = () => props.state.type === 'scheduled';
  const hasProposal = () =>
    props.state.type === 'scheduled' && !!props.state.proposedTime;
  const accessibleLabel = () => {
    if (scheduleTimeLocked(props.state))
      return 'Send time cannot be changed during delivery or recovery.';
    const label = formattedDate();
    if (!label) return 'Choose send time';
    if (!isConfirmed()) return `Send time set to ${label}. Open to change it.`;
    if (hasProposal()) {
      const confirmed = (
        props.state as Extract<EmailScheduleState, { type: 'scheduled' }>
      ).confirmedTime;
      return `Proposed send time ${label}. The email remains scheduled for ${format(confirmed, 'MMM d, yyyy  h:mm a')} until you choose Update schedule.`;
    }
    return props.mobile
      ? `Scheduled for ${label}. Open to propose a new time or cancel the schedule.`
      : `Scheduled for ${label}. Open to propose a new time.`;
  };
  const clearable = () =>
    (props.state.type === 'editing' && props.state.intent.type === 'later') ||
    hasProposal();

  return (
    <Tooltip label="Schedule send time" class="min-w-0 max-w-full">
      <div class="min-w-0 max-w-full">
        <DateSelector
          selectedDate={props.selectedTime}
          onSelectDate={props.onSelectTime}
          disabled={props.disabled || scheduleTimeLocked(props.state)}
          disablePriorToDate={new Date()}
          disableAfterDate={addYears(new Date(), 1)}
          disablePortal={props.disablePortal}
          triggerLabel={accessibleLabel()}
          clearable={props.mobile && clearable()}
          clearLabel={hasProposal() ? 'Discard proposed time' : 'Clear time'}
          showCurrentValue={props.mobile && isConfirmed()}
          currentLabel={
            hasProposal()
              ? 'Proposed:'
              : isConfirmed()
                ? 'Scheduled:'
                : undefined
          }
          footer={
            <Show
              when={
                props.mobile &&
                isConfirmed() &&
                !scheduleTimeLocked(props.state)
              }
            >
              <div class="flex flex-col gap-2">
                <p class="px-1 text-xs text-ink-muted">
                  {hasProposal()
                    ? 'The original schedule stays active until Update schedule succeeds.'
                    : 'Cancel the schedule before editing the message.'}
                </p>
                <button
                  type="button"
                  class="rounded-lg px-2 py-1.5 text-left text-sm text-failure hover:bg-hover disabled:text-ink-extra-muted"
                  disabled={props.operation !== 'idle'}
                  onPointerDown={(event) => event.preventDefault()}
                  onClick={() =>
                    fireAndForget(props.onCancelSchedule?.(), 'email-compose')
                  }
                >
                  {props.operation === 'cancelling'
                    ? 'Cancelling schedule…'
                    : 'Cancel schedule'}
                </button>
              </div>
            </Show>
          }
          triggerClass={cn(
            buttonClasses({
              variant: 'ghost',
              size: props.mobile ? 'icon-sm' : 'icon-md',
            }),
            props.selectedTime &&
              !isCompact() &&
              'min-w-0 max-w-full shrink gap-1 aspect-auto! bg-accent/20 text-accent hover:bg-accent/15! hover:text-accent! not-touch:min-w-[33.75px] not-touch:w-auto! not-touch:px-2'
          )}
          trigger={(state) => {
            // The form-owned value is authoritative. Reading the selector's
            // internal value here can leave the clock accented for one render
            // after the summary's Cancel action clears the time.
            const selectedLabel = formattedDate;
            const showExpanded = () => !isCompact() && !!selectedLabel();

            if (props.trigger) {
              return props.trigger({
                selectedDate: state.selectedDate,
                formattedDate: selectedLabel(),
              });
            }

            return (
              <div class="flex min-w-0 items-center gap-1">
                <ClockIcon
                  class={cn(
                    'size-4 shrink-0 touch:size-6',
                    props.selectedTime && 'text-accent'
                  )}
                />
                <Show when={showExpanded()}>
                  <span class="min-w-0 truncate text-sm">
                    {selectedLabel()}
                  </span>
                </Show>
              </div>
            );
          }}
        />
      </div>
    </Tooltip>
  );
};