<commit_before>import React, { Component } from 'react'
import { SidebarSwitcher } from './icons/icon-sidebar-switch';
import { Route, Link } from 'react-router-dom';
import { Controlled as CodeMirror } from 'react-codemirror2'
import { dateToDa, stringToSymbol } from '/lib/util';

import 'codemirror/mode/markdown/markdown';

export class NewPost extends Component {
  constructor(props) {
    super(props);
    this.state = {
      body: '',
      title: '',
      submit: false,
      awaiting: null,
    }

    this.postSubmit = this.postSubmit.bind(this);
    this.titleChange = this.titleChange.bind(this);
    this.bodyChange = this.bodyChange.bind(this);
  }

  postSubmit() {
    let newNote = {
      "new-note": {
        who: this.props.ship.slice(1),
        book: this.props.book,
        note: stringToSymbol(this.state.title),
        title: this.state.title,
        body: this.state.body,
      }
    }

    this.setState({
      awaiting: newNote["new-note"].note
    }, () => {
      window.api.setSpinner(true);
      window.api.action("publish", "publish-action", newNote);
    });
  }

  componentWillMount() {
    window.api.fetchNotebook(this.props.ship, this.props.book);
  }

  componentDidUpdate(prevProps, prevState) {
    let notebook = this.props.notebooks[this.props.ship][this.props.book];
    if (notebook.notes[this.state.awaiting]) {
      window.api.setSpinner(false);
      let popout = (this.props.popout) ? "popout/" : "";
      let redirect =
     `/~publish/${popout}note/${this.props.ship}/${this.props.book}/${this.state.awaiting}`;
      this.props.history.push(redirect);
    }
  }

  titleChange(evt) {
    let submit = !(evt.target.value === '' || this.state.body === '');
    this.setState({title: evt.target.value, submit: submit});
  }<commit_msg>publish: catch duplicate note titles<commit_after>import React, { Component } from 'react'
import { SidebarSwitcher } from './icons/icon-sidebar-switch';
import { Route, Link } from 'react-router-dom';
import { Controlled as CodeMirror } from 'react-codemirror2'
import { dateToDa, stringToSymbol } from '/lib/util';

import 'codemirror/mode/markdown/markdown';

export class NewPost extends Component {
  constructor(props) {
    super(props);
    this.state = {
      body: '',
      title: '',
      submit: false,
      awaiting: null,
    }

    this.postSubmit = this.postSubmit.bind(this);
    this.titleChange = this.titleChange.bind(this);
    this.bodyChange = this.bodyChange.bind(this);
  }

  postSubmit() {
    let newNote = {
      "new-note": {
        who: this.props.ship.slice(1),
        book: this.props.book,
        note: stringToSymbol(this.state.title),
        title: this.state.title,
        body: this.state.body,
      }
    }

      window.api.setSpinner(true);
      window.api.action("publish", "publish-action", newNote).then(() =>{
        this.setState({ awaiting: newNote["new-note"].note });
      }).catch((err) => {
        if (err.includes("note already exists")) {
          let timestamp = Math.floor(Date.now() / 1000);
          newNote["new-note"].note += "-" + timestamp;
          this.setState({awaiting: newNote["new-note"].note});
          window.api.action("publish", "publish-action", newNote);
        }
      });
  }

  componentWillMount() {
    window.api.fetchNotebook(this.props.ship, this.props.book);
  }

  componentDidUpdate(prevProps, prevState) {
    let notebook = this.props.notebooks[this.props.ship][this.props.book];
    if (notebook.notes[this.state.awaiting]) {
      window.api.setSpinner(false);
      let popout = (this.props.popout) ? "popout/" : "";
      let redirect =
     `/~publish/${popout}note/${this.props.ship}/${this.props.book}/${this.state.awaiting}`;
      this.props.history.push(redirect);
    }
  }

  titleChange(evt) {
    let submit = !(evt.target.value === '' || this.state.body === '');
    this.setState({title: evt.target.value, submit: submit});
  }
{'fraction_non_alphanumeric': 0.16556962025316455, 'fraction_numerical': 0.0020253164556962027, 'mean_word_length': 2.939182452642074, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 6, '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': '823756', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1284, 'n_words': 212}