Last active
October 3, 2018 16:14
-
-
Save vladanyes/d5313f93cb22aeb8622c3224d4024d29 to your computer and use it in GitHub Desktop.
Bit of content script file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from "lodash"; | |
import React from "react"; | |
import { connect } from "react-redux"; | |
import Header from "./Header.jsx"; | |
import NavigationBar from "./NavigationBar.jsx"; | |
import TabContainer from "./TabContainer.jsx"; | |
import { getFavIcon, getDomain } from "../../tools/utils.js"; | |
import { INJECT_SLIDER_SELECTORS } from '../../tools/const.js'; | |
import Settings from './Routes/Settings.jsx'; | |
import UpdatePopUp from './Routes/UpdatePopUp.jsx'; | |
import SearchEngine from './Routes/SearchEngine.jsx'; | |
class Slider extends React.Component { | |
constructor(props) { | |
super(props); | |
this.packProps = props.props; | |
this.state = { activeTab: 'loader', | |
prevTab: 'overview', | |
scrollTo: false }; | |
} | |
checkMessage = (msg, sender, sendResponse) => { | |
if (msg.action === 'show_updatePopUp') { | |
this.setState({ | |
activeTab: 'updatePopUp' | |
}); | |
chrome.extension.onMessage.removeListener(this.checkMessage); | |
} | |
} | |
async componentDidMount() { | |
document.addEventListener('mousedown', this.handleClickOutside); | |
chrome.runtime.sendMessage({ | |
action: "eventSlider", | |
message: this.props.store.getState().storedTabs[getDomain(window.location.href)] | |
.IsSmall? "SmallSite" : "BigSite", | |
domain: this.props.store.getState().lastAction.domain | |
}); | |
await chrome.extension.onMessage.addListener(this.checkMessage); | |
} | |
componentWillUnmount() { | |
document.removeEventListener('mousedown', this.handleClickOutside); | |
} | |
handleClickOutside = (event) => { | |
if (this.wrapperRef && !this.wrapperRef.contains(event.target)) { | |
this.props.hide(INJECT_SLIDER_SELECTORS); | |
} | |
} | |
setWrapperRef = (node) => { | |
this.wrapperRef = node; | |
} | |
componentWillMount() { | |
if (!_.isEmpty(this.props.store.getState().storedTabs[getDomain(window.location.href)])) { | |
if (this.props.store.getState().storedTabs[getDomain(window.location.href)].IsSmall) { | |
this.setState({ | |
activeTab: 'small' | |
}) | |
this.setState(state => ({bool: !state.bool})) | |
} else { | |
this.setState({ | |
activeTab: 'overview' | |
}) | |
} | |
} else { | |
this.setState({ | |
activeTab: 'loader' | |
}); | |
} | |
} | |
componentWillReceiveProps(nextProps) { | |
if (!_.isEmpty(nextProps.storedTabs[getDomain(window.location.href)])) { | |
if (nextProps.storedTabs[getDomain(window.location.href)].IsSmall) { | |
this.setState({ | |
activeTab: 'small' | |
}) | |
} | |
} else { | |
this.setState({ | |
activeTab: 'loader' | |
}) | |
} | |
} | |
changeTab = (tabName, scrollTo = false) => { | |
if (scrollTo) { | |
this.setState({ | |
activeTab: tabName, | |
prevTab: tabName, | |
scrollTo: true | |
}) | |
} else { | |
this.setState({ | |
activeTab: tabName, | |
prevTab: tabName, | |
scrollTo: false | |
}) | |
} | |
} | |
render () { | |
const data = _.find(this.props.store.getState().storedTabs, (item) => { | |
if (item.SiteName === getDomain(window.location.href)) { | |
return item | |
} | |
}); | |
let popUp; | |
switch(this.state.activeTab) { | |
case 'settings': | |
popUp = <Settings changeTab={this.changeTab.bind(this)}/>; | |
break; | |
case 'updatePopUp': | |
popUp = <UpdatePopUp changeTab={this.changeTab.bind(this)}/>; | |
break; | |
} | |
return ( | |
<div id="similar-web-notification" ref={this.setWrapperRef}> | |
{popUp} | |
<Header | |
favIcon = { _.isEmpty(data) ? '' : getFavIcon(data.SiteName)} | |
url = { _.isEmpty(data) ? 'Data loading...' : data.SiteName} | |
changeTab={this.changeTab} | |
activeTab={this.state.activeTab} | |
/> | |
<div id="body-wrapper"> | |
<NavigationBar | |
changeTab={this.changeTab} | |
activeTab={this.state.activeTab} | |
/> | |
<TabContainer | |
changeTab={this.changeTab.bind(this)} | |
tabData={data} | |
activeTab={this.state.activeTab} | |
scrollTo={this.state.scrollTo} | |
/> | |
</div> | |
</div> | |
); | |
} | |
} | |
function mapStateToProps(state, props) { | |
return { | |
storedTabs: state.storedTabs, | |
} | |
} | |
export default Slider = connect(mapStateToProps)(Slider); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment