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.

8.6 KB
Raw Blame
import { t } from '@app/lib/i18n';
import { FormatButtons } from '@channel/Input/FormatButtons';
import { HeaderIsland } from '@components/app/split-layout/components/HeaderIsland';
import {
  SplitHeaderLeft,
  SplitHeaderRight,
} from '@components/app/split-layout/components/SplitHeader';
import { defaultSelectionData } from '@core/component/LexicalMarkdown/plugins';
import { TRY_INSERT_MEDIA_UPLOAD_COMMAND } from '@core/component/LexicalMarkdown/plugins/media';
import {
  NODE_TRANSFORM,
  type NodeTransformType,
} from '@core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin';
import { fileSelector } from '@core/directive/fileSelector';
import { registerHotkey } from '@core/hotkey/hotkeys';
import { TOKENS } from '@core/hotkey/tokens';
import ImageIcon from '@icon/system/regular/image.svg';
import PaperclipIcon from '@icon/system/regular/paperclip.svg?component-solid';
import TextAa from '@icon/system/regular/text-aa.svg';
import Trash from '@icon/system/regular/trash.svg';
import { Button, SendButton } from '@ui';
import { FORMAT_TEXT_COMMAND, type LexicalEditor } from 'lexical';
import { createSignal, Show } from 'solid-js';
import { CreateEventButton } from '../components/create-event-button';
import { EmailDateSelector } from '../components/email-date-selector';
import { EmailScheduleSummary } from '../components/email-schedule-summary';
import { MobileComposeActions } from '../components/mobile-compose-actions';
import { useCompose } from '../context/compose-context';
import { useComposeHotkeyScope } from '../context/compose-hotkey-scope';
import { proposesMeeting } from '../core/meeting-intent';
import { createWrittenText } from '../primitives/written-text';

export function EmailComposeToolbar(props: {
  editor?: () => LexicalEditor | undefined;
  /** Embedded drafts keep their controls in the card on phones too. */
  inline?: boolean;
}) {
  const ctx = useCompose();
  const [showFormatRibbon, setShowFormatRibbon] = createSignal(false);
  const writtenText = createWrittenText(() => props.editor?.());
  const createEvent = () => {
    const recipients = ctx.recipients();
    return (
      ctx.calendarEvents?.open({
        subject: ctx.subject(),
        guests: [...recipients.to, ...recipients.cc].map(
          (recipient) => recipient.data.email
        ),
        text: writtenText(),
      }) ?? false
    );
  };
  const hotkeyScope = useComposeHotkeyScope();
  if (hotkeyScope && ctx.calendarEvents) {
    registerHotkey({
      hotkey: 'b',
      scopeId: hotkeyScope,
      description: () => t('commands.createEventFromEmail'),
      keyDownHandler: createEvent,
      hotkeyToken: TOKENS.email.createEvent,
      condition: () => !!ctx.calendarEvents?.enabled() && !ctx.disabled(),
    });
  }

  return (
    <Show when={props.inline || !ctx.isMobile()} fallback={<MobileToolbar />}>
      <Show when={showFormatRibbon() && !ctx.disabled()}>
        <div class="flex flex-row w-full gap-2 items-center p-2 -ml-3">
          <FormatButtons
            selectionState={() => defaultSelectionData}
            onInlineFormat={(format) => {
              props.editor?.()?.dispatchCommand(FORMAT_TEXT_COMMAND, format);
            }}
            onNodeFormat={(transform: NodeTransformType) => {
              props.editor?.()?.dispatchCommand(NODE_TRANSFORM, transform);
            }}
          />
        </div>
      </Show>
      <div class="mt-2 flex min-w-0 justify-end">
        <div class="flex shrink-0 items-center gap-1">
          <Show when={ctx.hasDraft()}>
            <Button
              variant="ghost"
              onClick={ctx.onDelete}
              tooltip="Delete draft"
              size="icon-md"
              disabled={ctx.deleteDisabled?.() ?? ctx.disabled()}
            >
              <Trash class="size-4 touch:size-6 shrink-0 pointer-events-none" />
            </Button>
          </Show>
          <Show when={!ctx.hideAttachments}>
            <Button
              variant="ghost"
              ref={(el) =>
                fileSelector(el, () => ({
                  multiple: true,
                  onSelect: ctx.onAddAttachments,
                }))
              }
              tooltip="Attach"
              size="icon-md"
              disabled={ctx.disabled()}
            >
              <PaperclipIcon class="size-4 touch:size-6 shrink-0 pointer-events-none" />
            </Button>
          </Show>
          {/* Inline drafts send without waiting for inline media uploads. */}
          <Show when={props.editor && !props.inline}>
            <Button
              variant="ghost"
              onClick={() =>
                props
                  .editor?.()
                  ?.dispatchCommand(TRY_INSERT_MEDIA_UPLOAD_COMMAND, {
                    mediaType: 'image',
                    constrainedMediaDimensions: { width: 542, height: 542 },
                  })
              }
              aria-label="Insert image"
              tooltip="Insert image"
              size="icon-md"
              disabled={ctx.disabled()}
            >
              <ImageIcon class="size-4 touch:size-6 shrink-0 pointer-events-none" />
            </Button>
          </Show>
          <Button
            variant="ghost"
            tooltip="Format"
            size="icon-md"
            disabled={ctx.disabled()}
            onClick={() => setShowFormatRibbon(!showFormatRibbon())}
          >
            <TextAa class="size-4 touch:size-6 shrink-0 pointer-events-none" />
          </Button>
          <Show when={ctx.calendarEvents?.enabled()}>
            <CreateEventButton
              suggested={proposesMeeting(writtenText())}
              shortcut={hotkeyScope ? 'b' : undefined}
              disabled={ctx.disabled()}
              onClick={createEvent}
            />
          </Show>
          <Show when={ctx.scheduleEnabled}>
            <div class="shrink-0">
              <EmailDateSelector
                mobile={false}
                compact
                state={ctx.schedule.state()}
                selectedTime={ctx.schedule.selectedTime()}
                onSelectTime={ctx.schedule.onSelect}
                onCancelSchedule={ctx.schedule.onCancel}
                operation={ctx.schedule.operation()}
                disabled={ctx.schedule.pickerDisabled()}
              />
            </div>
          </Show>
          <SendButton
            appearance="composer"
            onClick={() => ctx.onSend()}
            disabled={ctx.isSavingDraft?.() || ctx.primaryActionDisabled()}
            pending={ctx.isSending()}
            tooltip={{
              content:
                ctx.sendUnavailableReason?.() ?? ctx.schedule.actionLabel(),
              shortcut: 'cmd+enter',
            }}
            aria-label={ctx.schedule.actionLabel()}
          />
        </div>
      </div>
    </Show>
  );
}

function MobileToolbar() {
  const ctx = useCompose();
  const hasScheduleSummary = () => {
    const state = ctx.schedule.state();
    return state.type === 'scheduled' || state.intent.type === 'later';
  };

  return (
    <>
      <SplitHeaderLeft>
        <Show when={hasScheduleSummary()}>
          <HeaderIsland class="max-w-[55cqw] shrink px-2">
            <EmailScheduleSummary
              state={ctx.schedule.state()}
              operation={ctx.schedule.operation()}
              onSelectTime={ctx.schedule.onSelect}
              onCancelSchedule={ctx.schedule.onCancel}
              onCheckStatus={ctx.schedule.onCheckStatus}
            />
          </HeaderIsland>
        </Show>
      </SplitHeaderLeft>
      <SplitHeaderRight>
        <MobileComposeActions
          attachRef={
            ctx.hideAttachments
              ? undefined
              : (el) =>
                  fileSelector(el, () => ({
                    multiple: true,
                    onSelect: ctx.onAddAttachments,
                  }))
          }
          editingDisabled={ctx.disabled()}
          scheduleControl={
            <Show when={ctx.scheduleEnabled}>
              <EmailDateSelector
                mobile={ctx.isMobile()}
                state={ctx.schedule.state()}
                selectedTime={ctx.schedule.selectedTime()}
                onSelectTime={ctx.schedule.onSelect}
                onCancelSchedule={ctx.schedule.onCancel}
                operation={ctx.schedule.operation()}
                disabled={ctx.schedule.pickerDisabled()}
                compact
              />
            </Show>
          }
          sendLabel={ctx.schedule.actionLabel()}
          sendTooltip={ctx.sendUnavailableReason?.()}
          sendDisabled={!!ctx.isSavingDraft?.() || ctx.primaryActionDisabled()}
          sending={ctx.isSending()}
          onSend={() => ctx.onSend()}
        />
      </SplitHeaderRight>
    </>
  );
}