Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.9 KB
Raw Blame
'use client';

import {
  Button,
  createModal,
  Flexbox,
  type ModalInstance,
  Text,
  TextArea,
  useModalContext,
} from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { t } from 'i18next';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import {
  AttachmentStrip,
  AttachmentUploadButton,
  useFeedbackAttachments,
} from '../Evidence/attachments';
import RejectFeedbackPreview from './RejectFeedbackPreview';

const styles = stylex.create({
  warning: {
    paddingBlock: '12px',
    paddingInline: '16px',
    borderRadius: cssVar.borderRadiusLG,
    backgroundColor: cssVar.colorWarningBg,
    backgroundImage: 'none',
  },
});

/**
 * A frosted scrim for the acceptance decision dialogs β€” the page behind reads
 * as a soft blur so the dialog owns focus (matches the antd modal mask, which
 * already blurs). Applied per-modal via `styles.backdrop`; a global backdrop
 * rule can't be used because base-ui popups (Select/Menu lists) share the same
 * `role=presentation` element and would frost their own content.
 */
export const frostedModalStyles = { backdrop: { backdropFilter: 'blur(4px)' } };

interface AcceptContentProps {
  /** Titles of the exceptions the user is knowingly accepting with. */
  exceptions: string[];
  /** Perform the accept; resolve true to close, false to stay open (error shown by the page). */
  onConfirm: () => Promise<boolean>;
  subjectTitle: string;
}

const AcceptContent = memo<AcceptContentProps>(({ exceptions, onConfirm, subjectTitle }) => {
  const { t: translate } = useTranslation('verify');
  const { close } = useModalContext();
  const [loading, setLoading] = useState(false);

  const handleConfirm = async () => {
    setLoading(true);
    try {
      if (await onConfirm()) close();
    } finally {
      setLoading(false);
    }
  };

  return (
    <Flexbox gap={16}>
      <Text>{translate('acceptance.accept.summary', { title: subjectTitle })}</Text>
      {exceptions.length > 0 && (
        <Flexbox className={stylex.props(styles.warning).className} gap={4}>
          <Text strong>
            {translate('acceptance.accept.exceptionsTitle', { count: exceptions.length })}
          </Text>
          {exceptions.map((title) => (
            <Text fontSize={12} key={title} type={'secondary'}>
              Β· {title}
            </Text>
          ))}
          <Text fontSize={12} type={'secondary'}>
            {translate('acceptance.accept.exceptionsHint')}
          </Text>
        </Flexbox>
      )}
      <Flexbox horizontal gap={8} justify={'flex-end'}>
        <Button disabled={loading} onClick={close}>
          {translate('acceptance.actions.cancel')}
        </Button>
        <Button loading={loading} type={'primary'} onClick={handleConfirm}>
          {translate('acceptance.actions.confirmAccept')}
        </Button>
      </Flexbox>
    </Flexbox>
  );
});

AcceptContent.displayName = 'AcceptanceAcceptContent';

/** Accept confirmation β€” spells out the terminal event and the exceptions taken with it. */
export const openAcceptModal = (options: AcceptContentProps): ModalInstance =>
  createModal({
    content: <AcceptContent {...options} />,
    footer: null,
    maskClosable: true,
    styles: frostedModalStyles,
    title: t('acceptance.actions.accept', { ns: 'verify' }),
    width: 'min(90vw, 480px)',
  });

interface RejectContentProps {
  /** Previews the open feedback the repair agent reads alongside the reason, so
      the reviewer knows whose comments and screenshots ride along. */
  acceptanceId: string;
  /** The rounds name an authoring agent β€” the server sends the reject back to
      it. Without one the dialog promises no next round: it copies the prompt. */
  dispatchAvailable: boolean;
  /** Perform the reject with an optional reason; resolve true to close. */
  onConfirm: (comment: string) => Promise<boolean>;
}

const RejectContent = memo<RejectContentProps>(({ acceptanceId, dispatchAvailable, onConfirm }) => {
  const { t: translate } = useTranslation('verify');
  const { close } = useModalContext();
  const [comment, setComment] = useState('');
  const [loading, setLoading] = useState(false);

  const handleConfirm = async () => {
    const trimmed = comment.trim();
    setLoading(true);
    try {
      if (await onConfirm(trimmed)) close();
    } finally {
      setLoading(false);
    }
  };

  return (
    <Flexbox gap={16}>
      {/* What the repair prompt carries besides the reason leads the dialog β€”
            otherwise nobody can tell whether teammates' notes go along. */}
      <RejectFeedbackPreview acceptanceId={acceptanceId} />
      <Text type={'secondary'}>
        {translate(
          dispatchAvailable ? 'acceptance.reject.description' : 'acceptance.reject.descriptionCopy',
        )}
      </Text>
      <TextArea
        autoSize={{ maxRows: 6, minRows: 3 }}
        placeholder={translate('acceptance.reject.placeholder')}
        value={comment}
        onChange={(event) => setComment(event.target.value)}
      />
      <Flexbox horizontal gap={8} justify={'flex-end'}>
        <Button disabled={loading} onClick={close}>
          {translate('acceptance.actions.cancel')}
        </Button>
        <Button loading={loading} type={'primary'} onClick={handleConfirm}>
          {translate(
            dispatchAvailable
              ? 'acceptance.actions.confirmReject'
              : 'acceptance.actions.confirmRejectCopy',
          )}
        </Button>
      </Flexbox>
    </Flexbox>
  );
});

RejectContent.displayName = 'AcceptanceRejectContent';

/** Reject dialog β€” an optional reason adds context for the next repair round. */
export const openRejectModal = (options: RejectContentProps): ModalInstance =>
  createModal({
    content: <RejectContent {...options} />,
    footer: null,
    maskClosable: true,
    styles: frostedModalStyles,
    title: t('acceptance.actions.reject', { ns: 'verify' }),
    width: 'min(90vw, 520px)',
  });

interface GroupFeedbackContentProps {
  /** Override the group-scoped description (the decision bar's global note). */
  description?: string;
  groupLabel: string;
  /** Record the feedback (note + any screenshots); resolve true to close. */
  onConfirm: (comment: string, fileIds: string[]) => Promise<boolean>;
}

const GroupFeedbackContent = memo<GroupFeedbackContentProps>(
  ({ description, groupLabel, onConfirm }) => {
    const { t: translate } = useTranslation('verify');
    const { close } = useModalContext();
    const [comment, setComment] = useState('');
    const [loading, setLoading] = useState(false);
    const { attachments, fileIds, handlePaste, remove, uploadFiles, uploading } =
      useFeedbackAttachments();

    const handleConfirm = async () => {
      const trimmed = comment.trim();
      if (!trimmed) return;
      setLoading(true);
      try {
        if (await onConfirm(trimmed, fileIds)) close();
      } finally {
        setLoading(false);
      }
    };

    return (
      <Flexbox gap={16}>
        <Text type={'secondary'}>
          {description ?? translate('acceptance.group.feedbackDescription', { label: groupLabel })}
        </Text>
        <Flexbox gap={8}>
          <TextArea
            autoSize={{ maxRows: 8, minRows: 3 }}
            placeholder={translate('acceptance.group.feedbackPlaceholder')}
            value={comment}
            onChange={(event) => setComment(event.target.value)}
            onPaste={handlePaste}
          />
          {/* One row hugging the input β€” attachments belong to the note. */}
          <Flexbox horizontal align={'center'} gap={8} wrap={'wrap'}>
            <AttachmentStrip
              attachments={attachments}
              disabled={loading}
              uploading={uploading}
              onRemove={remove}
            />
            <AttachmentUploadButton disabled={loading} onFiles={uploadFiles} />
          </Flexbox>
        </Flexbox>
        <Flexbox horizontal align={'center'} gap={8} justify={'flex-end'}>
          <Button disabled={loading} onClick={close}>
            {translate('acceptance.actions.cancel')}
          </Button>
          <Button
            disabled={!comment.trim() || uploading}
            loading={loading}
            type={'primary'}
            onClick={handleConfirm}
          >
            {translate('acceptance.group.feedbackSubmit')}
          </Button>
        </Flexbox>
      </Flexbox>
    );
  },
);

GroupFeedbackContent.displayName = 'AcceptanceGroupFeedbackContent';

/**
 * Group-scoped feedback dialog β€” the channel for concerns that belong to no
 * single check (which may well be accepted) yet must reach the next round.
 */
export const openGroupFeedbackModal = (
  options: GroupFeedbackContentProps & { title?: string },
): ModalInstance =>
  createModal({
    content: <GroupFeedbackContent {...options} />,
    footer: null,
    maskClosable: true,
    styles: frostedModalStyles,
    title: options.title ?? t('acceptance.group.feedbackTitle', { ns: 'verify' }),
    width: 'min(90vw, 520px)',
  });