<commit_before>"use strict"
import React from 'react'
import Transmit from 'react-transmit'
import request from '../utils/request'
import './Category.css'
import './lexicon.css'
import {Link} from 'react-router'
import classNames from 'classnames'
import categoryData from './data/Category'
import Posts from '../Posts/Posts.jsx'

class Issue extends React.Component {
    render() {
        var {title, content, post_count, clickHandler} = this.props

        return (
            <div className="issue_item"
                 onClick={clickHandler}>
                <div className="issue_item_title">
                    <span className="prompt"></span>
                    <span className="q_text" dangerouslySetInnerHTML={{ __html: title }} />
                    <span className="Issue-titleIcon"><i className="fa fa-comments-o"></i></span>
                    <span className="issue_item_discuss_count">{post_count || ''}</span>
                </div>
                <div className="q_text" dangerouslySetInnerHTML={{ __html: content }} />
            </div>
        )
    }
}

class Category extends React.Component {
    static contextTypes = { router: React.PropTypes.func }

    constructor (props) { super(props)
        this.state = {
            showDiscussion: !!this.props.params.postID,
            showFullDiscussion: false,
            posts: [] // 主題底下的 post id，用來做隱藏 link 
        }
    }

    _toggleShowDiscussion (postID, event){
        window.scrollTo(500, 0)

        if(postID) {
            this.props.setQueryParams({
                postID: postID
            })
        }

        this.setState({
            showDiscussion: !this.state.showDiscussion,
            showFullDiscussion: false
        })

        var {proposalName, category, page} = this.props.params
        var currentState = history.state

        if(!this.state.showDiscussion){
            var newURL = `/${proposalName}/${category}/${page}/${postID}/`
            history.pushState(currentState, document.title, newURL)
            
        }else{
            var newURL = `/${proposalName}/${category}/${page}/`
            history.pushState(currentState, document.title, newURL)
        }

    }

    _toggleShowDiscussionWeb (postID, event){
        window.scrollTo(500, 0)

        var discussion = document.getElementsByClassName("Category-discussion")
        discussion.scrollTop = 0

        if(postID){
            this.props.setQueryParams({
                postID: postID
            })
        }

        var {proposalName, category, page} = this.props.params
        var currentState = history.state

        if(!this.state.showDiscussion){
            this.setState({
                showDiscussion: true
            })

            var newURL = `/${proposalName}/${category}/${page}/${postID}/`
            history.pushState(currentState, document.title, newURL)
            
        }else{
            var newURL = `/${proposalName}/${category}/${page}/`
            istory.pushState(currentState, document.title, newURL)
        }

        this.setState({
            showFullDiscussion: false
        })
    }

    _hideDiscussion(){
        this.setState({
            showDiscussion: false
        })
        var {proposalName, category, page} = this.props.params
        var currentState = history.state
        var newURL = `/${proposalName}/${category}/${page}/`
        history.pushState(currentState, document.title, newURL)
    }

    _toggleShowFullDiscussion(){
        this.setState({
            showFullDiscussion: !this.state.showFullDiscussion
        })
    }

    componentWillMount() {
        const {proposalName, category, postID} = this.props.params
        const metaData = categoryData[proposalName][category]
        //console.log(metaData.discussions_id)
        this.setState({
            posts: metaData.discussions_id
        })

        this.props.setQueryParams({
            gitbookURL: metaData.gitbook_url,
            categoryNum: metaData.category_num,
            proposalName: proposalName,
            category: category,
            postID: postID
        })
    }
    componentWillReceiveProps(nextProps) {
        const {proposalName, category, postID, page} = this.props.params
        const nextProposalName = nextProps.params.proposalName
        const nextCategory = nextProps.params.category
        const nextPage = nextProps.params.page

        if (nextProps.gitbook.length) {
            const {topic_list} = nextProps.talk || {}
            const {proposal_cht, category_cht} = categoryData[proposalName][category]
            const icon = category.replace(/\d+$/, '') + '.png'
            const base = `/${proposalName}/`

            this.props.setNavList([
                { path: base, label: proposal_cht, type: 'title' },
            ].concat(nextProps.gitbook.map(({title, children}, pageID)=>
                (pageID === 0) ? [{
                    icon,
                    path: `${base}${category}/`,
                    label: category_cht,
                    type: 'sub',
                }] : [{
                    path: `${base}${category}/${pageID}/`,
                    label: title.replace(/<[^>]*>/g, '').replace(/&#x([a-fA-F0-9]+);/g, (match, hex) => { return String.fromCharCode(Number.parseInt(hex, 16)); }),
                    type: 'section',
                }].concat( (topic_list && topic_list.topics) ? children.map((c)=>{
                    const label = c.title.replace(/<[^>]*>/g, '').replace(/<[^>]*>/g, '').replace(/&#x([a-fA-F0-9]+);/g, (match, hex) => { return String.fromCharCode(Number.parseInt(hex, 16)); });
                    const {id} = topic_list.topics.filter(
                        ({fancy_title})=>fancy_title === label
                    )[0] || {}
                    let path = `${base}${category}/${pageID}/`
                    if (id) { path += (id + '/') }
                    return { path, label, type: 'sub' } }) : []
                )
            ).reduce((a, b)=>a.concat(b))))

        }

        if (!proposalName || !nextProposalName) { return }
        if ((proposalName === nextProposalName) &&
            (category === nextCategory) &&
            (page === nextPage) ){ return }

        //When changing page, always hide the discussion
        this.setState({
            showDiscussion: false
        })

        const metaData = categoryData[nextProposalName][nextCategory]
        this.props.setQueryParams({
            gitbookURL: metaData.gitbook_url,
            categoryNum: metaData.category_num,
            proposalName: nextProposalName,
            category: nextCategory,
            postID: postID
        })
    }
    _removeLexicon (text) {
        const hint = /<span\ class=\"hint\"\ data-hint=\"(?:.+\n?)+\">(.+)<\/span>/
        return text.replace(hint, (matched, raw) => raw)
    }
    render() {

        const {gitbookURL, categoryNum, gitbook, talk, posts, onChange} = this.props
        const {proposalName, category, postID} = this.props.params
        const page = Number(this.props.params.page) || 0

        if(!gitbook || !gitbook[page]) { return <div/> }

        // Get post_count, topic_id from talk.vtaiwan json
        const titleToPostCount = {}

        if(talk && talk.topics){
            talk.topics.map((value)=>{
                titleToPostCount[value.fancy_title] = {
                    id: value.id,
                    post_count: value.posts_count
                }
            })
        }
        // Add post_count, topic_id data to gitbook data
        const {title, content, children} = gitbook[page]
        if (children){ children.forEach((item)=>{

            item.title = this._removeLexicon(item.title)
            if (titleToPostCount[item.title]){
                item.post_count = titleToPostCount[item.title].post_count || 0<commit_msg>* Fix category showing<commit_after>"use strict"
import React from 'react'
import Transmit from 'react-transmit'
import request from '../utils/request'
import './Category.css'
import './lexicon.css'
import {Link} from 'react-router'
import classNames from 'classnames'
import categoryData from './data/Category'
import Posts from '../Posts/Posts.jsx'

class Issue extends React.Component {
    render() {
        var {title, content, post_count, clickHandler} = this.props

        return (
            <div className="issue_item"
                 onClick={clickHandler}>
                <div className="issue_item_title">
                    <span className="prompt"></span>
                    <span className="q_text" dangerouslySetInnerHTML={{ __html: title }} />
                    <span className="Issue-titleIcon"><i className="fa fa-comments-o"></i></span>
                    <span className="issue_item_discuss_count">{post_count || ''}</span>
                </div>
                <div className="q_text" dangerouslySetInnerHTML={{ __html: content }} />
            </div>
        )
    }
}

class Category extends React.Component {
    static contextTypes = { router: React.PropTypes.func }

    constructor (props) { super(props)
        this.state = {
            showDiscussion: !!this.props.params.postID,
            showFullDiscussion: false,
            posts: [] // 主題底下的 post id，用來做隱藏 link 
        }
    }

    _toggleShowDiscussion (postID, event){
        window.scrollTo(500, 0)

        if(postID) {
            this.props.setQueryParams({
                postID: postID
            })
        }

        this.setState({
            showDiscussion: !this.state.showDiscussion,
            showFullDiscussion: false
        })

        var {proposalName, category, page} = this.props.params
        var currentState = history.state

        if(!this.state.showDiscussion){
            var newURL = `/${proposalName}/${category}/${page}/${postID}/`
            history.pushState(currentState, document.title, newURL)
            
        }else{
            var newURL = `/${proposalName}/${category}/${page}/`
            history.pushState(currentState, document.title, newURL)
        }

    }

    _toggleShowDiscussionWeb (postID, event){
        window.scrollTo(500, 0)

        var discussion = document.getElementsByClassName("Category-discussion")
        discussion.scrollTop = 0

        if(postID){
            this.props.setQueryParams({
                postID: postID
            })
        }

        var {proposalName, category, page} = this.props.params
        var currentState = history.state

        if(!this.state.showDiscussion){
            this.setState({
                showDiscussion: true
            })

            var newURL = `/${proposalName}/${category}/${page}/${postID}/`
            history.pushState(currentState, document.title, newURL)
            
        }else{
            var newURL = `/${proposalName}/${category}/${page}/`
            istory.pushState(currentState, document.title, newURL)
        }

        this.setState({
            showFullDiscussion: false
        })
    }

    _hideDiscussion(){
        this.setState({
            showDiscussion: false
        })
        var {proposalName, category, page} = this.props.params
        var currentState = history.state
        var newURL = `/${proposalName}/${category}/${page}/`
        history.pushState(currentState, document.title, newURL)
    }

    _toggleShowFullDiscussion(){
        this.setState({
            showFullDiscussion: !this.state.showFullDiscussion
        })
    }

    componentWillMount() {
        const {proposalName, category, postID} = this.props.params
        const metaData = categoryData[proposalName][category]
        //console.log(metaData.discussions_id)
        this.setState({
            posts: metaData.discussions_id
        })

        this.props.setQueryParams({
            gitbookURL: metaData.gitbook_url,
            categoryNum: metaData.category_num,
            proposalName: proposalName,
            category: category,
            postID: postID
        })
    }
    componentWillReceiveProps(nextProps) {
        const {proposalName, category, postID, page} = this.props.params
        const nextProposalName = nextProps.params.proposalName
        const nextCategory = nextProps.params.category
        const nextPage = nextProps.params.page

        if (nextProps.gitbook.length) {
            const {topic_list} = nextProps.talk || {}
            const {proposal_cht, category_cht} = categoryData[proposalName][category]
            const icon = category.replace(/\d+$/, '') + '.png'
            const base = `/${proposalName}/`

            this.props.setNavList([
                { path: base, label: proposal_cht, type: 'title' },
            ].concat(nextProps.gitbook.map(({title, children}, pageID)=>
                (pageID === 0) ? [{
                    icon,
                    path: `${base}${category}/`,
                    label: category_cht,
                    type: 'sub',
                }] : [{
                    path: `${base}${category}/${pageID}/`,
                    label: title.replace(/<[^>]*>/g, '').replace(/&#x([a-fA-F0-9]+);/g, (match, hex) => { return String.fromCharCode(Number.parseInt(hex, 16)); }),
                    type: 'section',
                }].concat( (topic_list && topic_list.topics) ? children.map((c)=>{
                    const label = c.title.replace(/<[^>]*>/g, '').replace(/<[^>]*>/g, '').replace(/&#x([a-fA-F0-9]+);/g, (match, hex) => { return String.fromCharCode(Number.parseInt(hex, 16)); });
                    const {id} = topic_list.topics.filter(
                        ({fancy_title})=>fancy_title === label
                    )[0] || {}
                    let path = `${base}${category}/${pageID}/`
                    if (id) { path += (id + '/') }
                    return { path, label, type: 'sub' } }) : []
                )
            ).reduce((a, b)=>a.concat(b))))

        }

        if (!proposalName || !nextProposalName) { return }
        if ((proposalName === nextProposalName) &&
            (category === nextCategory) &&
            (page === nextPage) ){ return }

        //When changing page, always hide the discussion
        this.setState({
            showDiscussion: false
        })

        const metaData = categoryData[nextProposalName][nextCategory]
        this.props.setQueryParams({
            gitbookURL: metaData.gitbook_url,
            categoryNum: metaData.category_num,
            proposalName: nextProposalName,
            category: nextCategory,
            postID: postID
        })
    }
    _removeLexicon (text) {
        const hint = /<span\ class=\"hint\"\ data-hint=\"(?:.+\n?)+\">(.+)<\/span>/
        return text.replace(hint, (matched, raw) => raw).replace(/<[^>]*>/g, '').replace(/&#x([a-fA-F0-9]+);/g, (match, hex) => { return String.fromCharCode(Number.parseInt(hex, 16)); });
    }
    render() {

        const {gitbookURL, categoryNum, gitbook, talk, posts, onChange} = this.props
        const {proposalName, category, postID} = this.props.params
        const page = Number(this.props.params.page) || 0

        if(!gitbook || !gitbook[page]) { return <div/> }

        // Get post_count, topic_id from talk.vtaiwan json
        const titleToPostCount = {}

        if(talk && talk.topics){
            talk.topics.map((value)=>{
                titleToPostCount[value.fancy_title] = {
                    id: value.id,
                    post_count: value.posts_count
                }
            })
        }
        // Add post_count, topic_id data to gitbook data
        const {title, content, children} = gitbook[page]
        if (children){ children.forEach((item)=>{

            item.title = this._removeLexicon(item.title)
            if (titleToPostCount[item.title]){
                item.post_count = titleToPostCount[item.title].post_count || 0
{'fraction_non_alphanumeric': 0.13676563487049906, 'fraction_numerical': 0.0029058749210360076, 'mean_word_length': 1.8994505494505494, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 70, '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': '1520634', 'n_tokens_mistral': 4532, 'n_tokens_neox': 4351, 'n_words': 852}