# All files / composer/components ComposerSection.jsx

52.63% Statements 10/19
38.1% Branches 8/21
37.5% Functions 3/8
52.94% Lines 9/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143

    1x

    1x

    2x

    2x

    2x
    2x

    2x

    2x

    2x

     import React from 'react';
    import PropTypes from 'prop-types';
    import uniqBy from 'lodash.uniqby';
    import AppStore from '../stores/AppStore';
    import ComposerStore from '../stores/ComposerStore';
    import AppActionCreators from '../action-creators/AppActionCreators';
    import { NotificationScopes } from '../AppConstants';
    import Composer from '../components/Composer';
    import ProductRolloutTooltip from '../components/ProductRolloutTooltip';
    import NotificationContainer from '../components/NotificationContainer';
    import styles from './css/ComposerSection.css';

    const getComposerState = () => ({
      enabledDrafts: ComposerStore.getEnabledDrafts(),
      draftsSharedData: ComposerStore.getDraftsSharedData(),
      omniDraft: ComposerStore.getDraft('omni'),
      forceEditorFocus: ComposerStore.getMeta().forceEditorFocus,
    });

    class ComposerSection extends React.Component {
      static propTypes = {
        appState: PropTypes.object.isRequired,
        visibleNotifications: PropTypes.array.isRequired,
        areAllDraftsSaved: PropTypes.bool.isRequired,
        shouldEnableFacebookAutocomplete: PropTypes.bool.isRequired,
        shouldShowInlineSubprofileDropdown: PropTypes.bool.isRequired,
        profiles: PropTypes.array,
        selectedProfiles: PropTypes.array,
        isOmniboxEnabled: PropTypes.bool,
        composerPosition: PropTypes.object,
        hasIGDirectFlip: PropTypes.bool.isRequired,
        hasIGLocationTaggingFeature: PropTypes.bool.isRequired,
      };

      static defaultProps = {
        isOmniboxEnabled: null,
        composerPosition: null,
      };

      state = getComposerState();

      componentDidMount = () => ComposerStore.addChangeListener(this.onStoreChange);
      componentWillUnmount = () => ComposerStore.removeChangeListener(this.onStoreChange);
      onStoreChange = () => this.setState(getComposerState());

      onTwitterMaxProfileNotificationClose = () => {
        AppActionCreators.rememberTwitterMaxProfileNotificationClosedOnce();
      };

      render() {
        const { enabledDrafts, draftsSharedData, omniDraft } = this.state;

        const {
          appState, profiles, visibleNotifications, areAllDraftsSaved, selectedProfiles,
          shouldEnableFacebookAutocomplete, shouldShowInlineSubprofileDropdown,
          isOmniboxEnabled, composerPosition, hasIGDirectFlip, hasIGLocationTaggingFeature,
        } = this.props;

        const hasEnabledDrafts = enabledDrafts.length > 0 || isOmniboxEnabled;
        const composersHaveBeenExpanded = appState.composersHaveBeenExpanded;

        const getComposerComponent = (draft, index) => {
          const canUserPostToMultipleNetworks = uniqBy(profiles, (p) => p.service.name).length > 1;
          const showRolloutTooltip = (
            AppStore.getOptions().canSelectProfiles &&
            canUserPostToMultipleNetworks &&
            (isOmniboxEnabled || index === enabledDrafts.length - 1)
          );

          const children =
            showRolloutTooltip ?
              <ProductRolloutTooltip
                visibleNotifications={visibleNotifications}
                isOmniboxEnabled={isOmniboxEnabled}
              /> :
              null;

          // When focus should be forced, figure out *which* editor instance to force-focus
          const forceEditorInstanceFocus = (
            this.state.forceEditorFocus &&
            (isOmniboxEnabled || appState.expandedComposerId === draft.id)
          );

          return (
            <Composer
              appState={appState}
              draft={draft}
              key={draft.id}
              enabledDrafts={enabledDrafts}
              draftsSharedData={draftsSharedData}
              profiles={profiles}
              expandedComposerId={isOmniboxEnabled ? draft.id : appState.expandedComposerId}
              visibleNotifications={visibleNotifications}
              areAllDraftsSaved={areAllDraftsSaved}
              selectedProfiles={selectedProfiles}
              shouldEnableFacebookAutocomplete={shouldEnableFacebookAutocomplete}
              shouldShowInlineSubprofileDropdown={shouldShowInlineSubprofileDropdown}
              composerPosition={composerPosition}
              hasIGDirectFlip={hasIGDirectFlip}
              forceEditorFocus={forceEditorInstanceFocus}
              hasIGLocationTaggingFeature={hasIGLocationTaggingFeature}
            >
              {children}
            </Composer>
          );
        };

        const twitterMaxProfileNotificationClassNames = {
          container: styles.twitterMaxProfileNotificationContainer,
          notification: `${styles.twitterMaxProfileNotification} bi-notification`,
          notificationCloseButton: styles.twitterMaxProfileNotificationCloseButton,
        };

        return (
          <div className={styles.composerSection}>
            {!hasEnabledDrafts && (
              <div className={styles.emptyState}>
                {composersHaveBeenExpanded ?
                  'Your work has been saved. Please select a social account above to continue.' :
                  'Please select a social account above to continue.'}
              </div>
            )}

            <NotificationContainer
              visibleNotifications={visibleNotifications}
              scope={NotificationScopes.TWITTER_MAX_ONE_PROFILE_SELECTED}
              classNames={twitterMaxProfileNotificationClassNames}
              onClose={this.onTwitterMaxProfileNotificationClose}
              showCloseIcon
            />

            {isOmniboxEnabled &&
              getComposerComponent(omniDraft)}

            {!isOmniboxEnabled &&
              enabledDrafts.map(getComposerComponent)}
          </div>
        );
      }
    }

    export default ComposerSection;