<commit_before>import { forwardRef, useLayoutEffect, useState, useMemo, useCallback } from 'react';
import { Link } from 'react-router';
import cn from 'classnames';
import {
  faExclamationTriangle,
  faLink,
  faEdit,
  faBookmark as faBookmarkSolid,
  faSignOutAlt,
} from '@fortawesome/free-solid-svg-icons';
import {
  faClock,
  faCommentDots,
  faTrashAlt,
  faBookmark,
} from '@fortawesome/free-regular-svg-icons';
import { noop } from 'lodash';
import { useDispatch } from 'react-redux';

import { andJoin } from '../../utils/and-join';
import { copyURL } from '../../utils/copy-url';
import { leaveDirect } from '../../redux/action-creators';
import { ButtonLink } from '../button-link';
import { Throbber } from '../throbber';
import { Icon } from '../fontawesome-icons';
import TimeDisplay from '../time-display';

import styles from '../dropdown-menu.module.scss';

export const PostMoreMenu = forwardRef(function PostMoreMenu(
  {
    user,
    post: {
      id: postId,
      isEditable = false,
      canBeRemovedFrom = [],
      isModeratable = false,
      isDeletable = false,
      isModeratingComments = false,
      commentsDisabled = false,
      createdAt,
      updatedAt,
      isSaved = false,
      savePostStatus = {},
      createdBy: postCreatedBy,
      isDirect = false,
    },
    toggleEditingPost,
    toggleModeratingComments,
    enableComments,
    disableComments,
    deletePost,
    perGroupDeleteEnabled = false,
    doAndClose,
    permalink,
    toggleSave,
    fixed = false,
  },
  ref,
) {
  const dispatch = useDispatch();
  const doLeaveDirect = useCallback(
    () =>
      confirm(
        "Are you sure you want to leave this conversation? You won't be able to read it unless the author invites you back again.",
      ) && dispatch(leaveDirect(postId)),
    [dispatch, postId],
  );

  const amIAuthenticated = !!user.id;
  const isOwnPost = amIAuthenticated && postCreatedBy?.id === user.id;

  const deleteLines = useMemo(() => {
    const result = [];
    // Not owned post
    if (!isEditable) {
      // Can we remove post from the single group?
      if (perGroupDeleteEnabled) {
        if (!isDeletable || canBeRemovedFrom.length > 1) {
          for (const group of canBeRemovedFrom) {
            result.push({ text: `Remove from @${group}`, onClick: deletePost(group) });
          }
        }
      } else if (!isDeletable) {
        result.push({
          text: `Remove from ${andJoin(canBeRemovedFrom.map((g) => `@${g}`))}`,
          onClick: deletePost(),
        });
      }
    }
    if (isDeletable) {
      result.push({ text: 'Delete', onClick: deletePost() });
    }
    return result;
  }, [isEditable, isDeletable, canBeRemovedFrom, perGroupDeleteEnabled, deletePost]);

  const menuGroups = [
    [
      isEditable && (
        <div className={styles.item} key="edit-post">
          <ButtonLink className={styles.link} onClick={doAndClose(toggleEditingPost)}>
            <Iconic icon={faEdit}>Edit</Iconic>
          </ButtonLink>
        </div>
      ),
      isModeratable && (
        <div className={styles.item} key="moderate-comments">
          <ButtonLink className={styles.link} onClick={doAndClose(toggleModeratingComments)}>
            <Iconic icon={faCommentDots}>
              {isModeratingComments ? 'Stop moderating comments' : 'Moderate comments'}
            </Iconic>
          </ButtonLink>
        </div>
      ),
      (isEditable || isModeratable) && (
        <div className={styles.item} key="toggle-comments">
          <ButtonLink
            className={styles.link}
            onClick={commentsDisabled ? doAndClose(enableComments) : doAndClose(disableComments)}
          >
            <Iconic icon={faCommentDots}>
              {commentsDisabled ? 'Enable comments' : 'Disable comments'}
            </Iconic>
          </ButtonLink>
        </div>
      ),
    ],
    deleteLines.map(({ text, onClick }) => (<commit_msg>Simplify rendering checks for enable/disable comments action<commit_after>import { forwardRef, useLayoutEffect, useState, useMemo, useCallback } from 'react';
import { Link } from 'react-router';
import cn from 'classnames';
import {
  faExclamationTriangle,
  faLink,
  faEdit,
  faBookmark as faBookmarkSolid,
  faSignOutAlt,
} from '@fortawesome/free-solid-svg-icons';
import {
  faClock,
  faCommentDots,
  faTrashAlt,
  faBookmark,
} from '@fortawesome/free-regular-svg-icons';
import { noop } from 'lodash';
import { useDispatch } from 'react-redux';

import { andJoin } from '../../utils/and-join';
import { copyURL } from '../../utils/copy-url';
import { leaveDirect } from '../../redux/action-creators';
import { ButtonLink } from '../button-link';
import { Throbber } from '../throbber';
import { Icon } from '../fontawesome-icons';
import TimeDisplay from '../time-display';

import styles from '../dropdown-menu.module.scss';

export const PostMoreMenu = forwardRef(function PostMoreMenu(
  {
    user,
    post: {
      id: postId,
      isEditable = false,
      canBeRemovedFrom = [],
      isModeratable = false,
      isDeletable = false,
      isModeratingComments = false,
      commentsDisabled = false,
      createdAt,
      updatedAt,
      isSaved = false,
      savePostStatus = {},
      createdBy: postCreatedBy,
      isDirect = false,
    },
    toggleEditingPost,
    toggleModeratingComments,
    enableComments,
    disableComments,
    deletePost,
    perGroupDeleteEnabled = false,
    doAndClose,
    permalink,
    toggleSave,
    fixed = false,
  },
  ref,
) {
  const dispatch = useDispatch();
  const doLeaveDirect = useCallback(
    () =>
      confirm(
        "Are you sure you want to leave this conversation? You won't be able to read it unless the author invites you back again.",
      ) && dispatch(leaveDirect(postId)),
    [dispatch, postId],
  );

  const amIAuthenticated = !!user.id;
  const isOwnPost = amIAuthenticated && postCreatedBy?.id === user.id;

  const deleteLines = useMemo(() => {
    const result = [];
    // Not owned post
    if (!isEditable) {
      // Can we remove post from the single group?
      if (perGroupDeleteEnabled) {
        if (!isDeletable || canBeRemovedFrom.length > 1) {
          for (const group of canBeRemovedFrom) {
            result.push({ text: `Remove from @${group}`, onClick: deletePost(group) });
          }
        }
      } else if (!isDeletable) {
        result.push({
          text: `Remove from ${andJoin(canBeRemovedFrom.map((g) => `@${g}`))}`,
          onClick: deletePost(),
        });
      }
    }
    if (isDeletable) {
      result.push({ text: 'Delete', onClick: deletePost() });
    }
    return result;
  }, [isEditable, isDeletable, canBeRemovedFrom, perGroupDeleteEnabled, deletePost]);

  const menuGroups = [
    [
      isEditable && (
        <div className={styles.item} key="edit-post">
          <ButtonLink className={styles.link} onClick={doAndClose(toggleEditingPost)}>
            <Iconic icon={faEdit}>Edit</Iconic>
          </ButtonLink>
        </div>
      ),
      isModeratable && (
        <div className={styles.item} key="moderate-comments">
          <ButtonLink className={styles.link} onClick={doAndClose(toggleModeratingComments)}>
            <Iconic icon={faCommentDots}>
              {isModeratingComments ? 'Stop moderating comments' : 'Moderate comments'}
            </Iconic>
          </ButtonLink>
        </div>
      ),
      (isEditable || isModeratable) && (
        <div className={styles.item} key="toggle-comments">
          {commentsDisabled ? (
            <ButtonLink className={styles.link} onClick={doAndClose(enableComments)}>
              <Iconic icon={faCommentDots}>Enable comments</Iconic>
            </ButtonLink>
          ) : (
            <ButtonLink className={styles.link} onClick={doAndClose(disableComments)}>
              <Iconic icon={faCommentDots}>Disable comments</Iconic>
            </ButtonLink>
          )}
        </div>
      ),
    ],
    deleteLines.map(({ text, onClick }) => (
{'fraction_non_alphanumeric': 0.13236027944111775, 'fraction_numerical': 0.000249500998003992, 'mean_word_length': 2.6457480673033196, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7574439', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2372, 'n_words': 549}