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.

2.6 KB
Raw Blame
import type { EventEditorInitialValues } from '@app/features/calendar/components/composer/event-form-model';
import type { CreatedCalendarEvent } from '@app/features/calendar/hooks/use-event-editor';
import type { CalendarEvent } from '@app/features/calendar/types';
import { useSplitLayout } from '@components/app/split-layout/layout';
import { confirmDialog } from '@ui';
import { getOwner } from 'solid-js';

/** Values and lifecycle callbacks used to open an event composer. */
export interface OpenEventComposerOptions {
  event?: CalendarEvent;
  initialValues?: EventEditorInitialValues;
  /** Values that arrive after the composer opens, applied only to an untouched form. */
  suggestedValues?: Promise<Partial<EventEditorInitialValues>>;
  onCalendarChange?: (calendarId: string, color: string) => void;
  /** Called with a new event once it exists, before the composer closes. */
  onCreated?: (created: CreatedCalendarEvent) => void;
  onClose?: () => void;
}

/** Opens an event composer with confirmation before discarding changes. */
export function useOpenEventComposer() {
  const { popoverSplit } = useSplitLayout();
  const owner = getOwner();

  return (options: OpenEventComposerOptions = {}) => {
    let eventSaved = false;
    let formDirty = false;
    let closeConfirmationPending = false;

    return popoverSplit(
      {
        type: 'component',
        id: 'calendar-event-compose',
        params: {
          event: options.event,
          initialValues: options.initialValues,
          suggestedValues: options.suggestedValues,
          onCalendarChange: options.onCalendarChange,
          onDirtyChange: (dirty: boolean) => {
            formDirty = dirty;
          },
          onSaveSuccess: () => {
            eventSaved = true;
          },
          onCreated: options.onCreated,
        },
      },
      {
        onClose: async (close) => {
          if (closeConfirmationPending) return;

          if (!eventSaved && formDirty) {
            closeConfirmationPending = true;
            try {
              const confirmed = await confirmDialog(
                {
                  title: 'You still have remaining changes',
                  body: 'Closing this event will discard your changes.',
                  confirmLabel: 'Discard',
                  cancelLabel: 'Keep editing',
                  tone: 'danger',
                },
                { owner }
              );
              if (!confirmed) return;
            } finally {
              closeConfirmationPending = false;
            }
          }

          options.onClose?.();
          close();
        },
      }
    );
  };
}