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.

3.8 KB
Raw Blame
import { useMaybeSoup } from '@app/features/next-soup/soup-context';
import { restoreSoupFocus } from '@app/features/next-soup/utils';
import { useGlobalNotificationSource } from '@components/app/GlobalAppState';
import { toast } from '@core/component/Toast/Toast';
import { fireAndForget } from '@core/util/fireAndForget';
import ArrowCounterClockwise from '@icon/system/regular/arrow-counter-clockwise.svg?component-solid';
import { Telemetry } from '@macro-inc/observability';
import type { NotificationStack, UnifiedNotification } from '@notifications';
import {
  executeMarkNotificationsDone,
  executeMarkNotificationsUndone,
  getAllNotificationsFromGroup,
} from '@notifications';
import { useUndoableMutation } from '@queries/undo';

interface NotificationActionsProps {
  stack: NotificationStack;
  entityId?: string;
  onMarkAsDone?: () => void;
  onMarkAsRead?: () => void;
}

interface SingleNotificationActionsProps {
  notification: UnifiedNotification;
  onMarkAsDone?: () => void;
  onMarkAsRead?: () => void;
}

type MarkStackDoneVariables = { notificationIds: string[] };

export const reportNotificationActionError = (error: unknown) =>
  Telemetry.error(error, { surface: 'notifications' });

export function useNotificationStackActions(props: NotificationActionsProps) {
  const notificationSource = useGlobalNotificationSource();
  const soup = useMaybeSoup();

  const mutation = useUndoableMutation<void, Error, MarkStackDoneVariables>(
    () => ({
      mutationFn: (vars) => executeMarkNotificationsDone(vars.notificationIds),
      onError: () => {
        toast.failure('Failed to mark as done');
      },
      undoFn: (vars) => executeMarkNotificationsUndone(vars.notificationIds),
      redoFn: (vars) => executeMarkNotificationsDone(vars.notificationIds),
      undoLabel: 'Mark Done',
      onPushed: (handle) => {
        let toastId: number | undefined;

        const showToast = () => {
          toastId = toast.success('Marked as done', {
            actions: [
              {
                label: 'Undo',
                icon: ArrowCounterClockwise,
                onClick: () => {
                  fireAndForget(
                    handle.undo({
                      onError: () => toast.failure('Failed to undo'),
                    }),
                    'notifications'
                  );
                  if (props.entityId) soup?.focus.set(props.entityId);
                  fireAndForget(
                    restoreSoupFocus(props.entityId),
                    'notifications'
                  );
                },
              },
            ],
            duration: 10_000,
            stack: true,
            hideOnMobile: true,
          });
        };

        showToast();
        props.onMarkAsDone?.();

        return {
          onUndone: () => {
            if (toastId !== undefined) toast.dismiss(toastId);
          },
          onRedone: showToast,
        };
      },
    })
  );

  const markStackAsDone = () => {
    const notifications = getAllNotificationsFromGroup(props.stack);
    mutation.mutate({ notificationIds: notifications.map((n) => n.id) });
  };

  const markStackAsRead = async () => {
    const notifications = getAllNotificationsFromGroup(props.stack);
    await notificationSource.bulkMarkAsRead(notifications);
    props.onMarkAsRead?.();
  };

  return { markStackAsDone, markStackAsRead };
}

export function useNotificationActions(props: SingleNotificationActionsProps) {
  const notificationSource = useGlobalNotificationSource();

  const markAsDone = async () => {
    await notificationSource.markAsDone(props.notification);
    props.onMarkAsDone?.();
  };

  const markAsRead = async () => {
    await notificationSource.markAsRead(props.notification);
    props.onMarkAsRead?.();
  };

  return { markAsDone, markAsRead };
}