<commit_before>
import React, { Component } from "react";
import {
  SPCanvas,
  bindComponent,
  watch,
  SPScene,
} from "sp-components";
import {FlexContainer} from "./shared.style";
import ChannelUsers from "./channel-users";
import {
  TitleBar,
  ChannelName,
  CanvasWrapper,
  GoLiveButton,
} from "./channel.style";

export class Channel extends Component {
  static propTypes = {
    channel: React.PropTypes.object,
    broadcasts: React.PropTypes.array,
    SP: React.PropTypes.object.isRequired,
  };

  static subscribe(props) {
    return {
      channel: watch.one("channels", {slug: props.match.params.slug}),
      scenes: props.channel && watch("scenes", { channelId: props.channel.id }),
      broadcasts: props.channel && watch("broadcasts", {
        channelId: props.channel.id,
        stopTime: null,
      }),
    };
  }

  constructor() {
    super();
    this.state = {};
    this.isAdding = false;
  }

  componentWillUnmount() {
    this.isAdding = true;
    const { channel, SP } = this.props;
    if (!channel) {
      return;
    }
    SP.channels.update(channel.id, {
      users: channel.users.filter(u => u.userId !== SP.user.id)
    })
    .catch((err) => {
      SP.error(err);
    });
  }

  goLive() {
    const { channel, SP } = this.props;
    SP.broadcasts.create({
      channelId: channel.id,
      startTime: new Date(),
    })
    .catch((err) => {
      SP.error(err);
    });
  }

  stopLive() {
    const { broadcasts, SP } = this.props;
    // You really shouldn't be able to have more than one broadcast, but w/e
    const proms = broadcasts.map((broadcast) => {
      return SP.broadcasts.update(broadcast.id, { stopTime: new Date() });
    });
    Promise.all(proms).catch((err) => {
      SP.error(err);
    });
  }

  render() {
    const {channel, broadcasts, SP} = this.props;
    if (!channel || !channel.activeSceneId) {
      return <FlexContainer />;
    }

    // For now... if you're not in a channel, auto-join yourself.
    if (!this.isAdding && !channel.users.find(u => u.userId === SP.user.id)) {
      this.isAdding = true;
      SP.channels.update(channel.id, {
        users: [
          ...channel.users,
          {
            userId: SP.user.id,
            muted: false,
          }
        ]<commit_msg>avoid LIVE flash before we're actually LIVE<commit_after>
import React, { Component } from "react";
import {
  SPCanvas,
  bindComponent,
  watch,
  SPScene,
} from "sp-components";
import {FlexContainer} from "./shared.style";
import ChannelUsers from "./channel-users";
import {
  TitleBar,
  ChannelName,
  CanvasWrapper,
  GoLiveButton,
} from "./channel.style";

export class Channel extends Component {
  static propTypes = {
    channel: React.PropTypes.object,
    broadcasts: React.PropTypes.array,
    SP: React.PropTypes.object.isRequired,
  };

  static subscribe(props) {
    return {
      channel: watch.one("channels", {slug: props.match.params.slug}),
      scenes: props.channel && watch("scenes", { channelId: props.channel.id }),
      broadcasts: props.channel && watch("broadcasts", {
        channelId: props.channel.id,
        stopTime: null,
      }),
    };
  }

  constructor() {
    super();
    this.state = {};
    this.isAdding = false;
  }

  componentWillUnmount() {
    this.isAdding = true;
    const { channel, SP } = this.props;
    if (!channel) {
      return;
    }
    SP.channels.update(channel.id, {
      users: channel.users.filter(u => u.userId !== SP.user.id)
    })
    .catch((err) => {
      SP.error(err);
    });
  }

  goLive() {
    const { channel, SP } = this.props;
    SP.broadcasts.create({
      channelId: channel.id,
      startTime: new Date(),
    })
    .catch((err) => {
      SP.error(err);
    });
  }

  stopLive() {
    const { broadcasts, SP } = this.props;
    // You really shouldn't be able to have more than one broadcast, but w/e
    const proms = broadcasts.map((broadcast) => {
      return SP.broadcasts.update(broadcast.id, { stopTime: new Date() });
    });
    Promise.all(proms).catch((err) => {
      SP.error(err);
    });
  }

  render() {
    const {channel, broadcasts, SP} = this.props;
    if (!channel || !channel.activeSceneId || !broadcasts) {
      return <FlexContainer />;
    }

    // For now... if you're not in a channel, auto-join yourself.
    if (!this.isAdding && !channel.users.find(u => u.userId === SP.user.id)) {
      this.isAdding = true;
      SP.channels.update(channel.id, {
        users: [
          ...channel.users,
          {
            userId: SP.user.id,
            muted: false,
          }
        ]
{'fraction_non_alphanumeric': 0.15504885993485343, 'fraction_numerical': 0.0, 'mean_word_length': 2.3842762674504043, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5306714', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1474, 'n_words': 323}