diff --git a/src/components/SettingsBar.js b/src/components/SettingsBar.js index 6494257..ceb29d1 100644 --- a/src/components/SettingsBar.js +++ b/src/components/SettingsBar.js @@ -3,70 +3,126 @@ import preact from 'preact' import Icon from './Icon' import storageHelper from '../utils/storageHelper' +import keyHelper from '../utils/keyHelper' const ACCESS_TOKEN_REGEXP = /^[0-9a-f]{40}$/ +const OperatingSystems = { + Windows: 'Windows', + macOS: 'Macintosh', + others: 'unknown', +} + +function detectOS() { + const { + navigator: { userAgent }, + } = window + if (userAgent.indexOf(OperatingSystems.Windows) !== -1) return OperatingSystems.Windows + else if (userAgent.indexOf(OperatingSystems.macOS) !== -1) return OperatingSystems.macOS + return OperatingSystems.others +} + +function friendlyFormatShortcut(shortcut) { + const OS = detectOS() + if (OS === OperatingSystems.Windows) { + return shortcut.replace(/meta/, 'win') + } else if (OS === OperatingSystems.macOS) { + return shortcut + .replace(/meta/, '⌘') + .replace(/ctrl/, '⌃') + .replace(/shift/, '⇧') + .replace(/alt/, '⌥') + } else { + return shortcut + } +} + export default class SettingsBar extends preact.Component { state = { - hint: null, - hasAccessToken: false, + accessTokenHint: null, accessToken: '', + shortcutHint: null, + toggleShowSideBarShortcut: '', } componentWillMount() { - const { hasAccessToken } = this.props - this.setState({ hasAccessToken }) + const { toggleShowSideBarShortcut } = this.props + this.setState({ toggleShowSideBarShortcut }) } - - componentWillReceiveProps({ hasAccessToken }) { - this.setState({ hasAccessToken }) + + componentWillReceiveProps({ toggleShowSideBarShortcut }) { + this.setState({ toggleShowSideBarShortcut }) } onInputAccessToken = event => { const value = event.target.value this.setState({ accessToken: value }) this.setState({ - hint: ACCESS_TOKEN_REGEXP.test(value) - ? '' - : 'This token is in unknown format.' + accessTokenHint: ACCESS_TOKEN_REGEXP.test(value) ? '' : 'This token is in unknown format.', }) } - saveToken = () => { + saveToken = async () => { + const { onHasAccessTokenChange } = this.props const { accessToken } = this.state if (accessToken) { - storageHelper.setAccessToken(accessToken) + await storageHelper.setAccessToken(accessToken) + onHasAccessTokenChange(true) this.setState({ - hasAccessToken: true, - hint: 'Your token is saved, will work after reloading the page!', + accessTokenHint: 'Your token is saved, will work after reloading the page!', }) } } - clearToken = () => { - storageHelper.setAccessToken('') - this.setState({ accessToken: '', hasAccessToken: false }) + clearToken = async () => { + const { onHasAccessTokenChange } = this.props + await storageHelper.setAccessToken('') + onHasAccessTokenChange(false) + this.setState({ accessToken: '' }) + } + + saveShortcut = async () => { + const { onShortcutChange } = this.props + const { toggleShowSideBarShortcut } = this.state + await storageHelper.setShortcut(toggleShowSideBarShortcut) + onShortcutChange(toggleShowSideBarShortcut) + this.setState({ + shortcutHint: 'Shortcut is saved!', + }) + } + + /** + * @param {KeyboardEvent} e + */ + onShortCutInputKeyDown = e => { + e.preventDefault() + const shortcut = keyHelper.parseEvent(e) + this.setState({ toggleShowSideBarShortcut: shortcut }) } render() { - const { hint, accessToken, hasAccessToken } = this.state - const { toggleShowSettings, activated} = this.props + const { accessTokenHint, accessToken, toggleShowSideBarShortcut, shortcutHint } = this.state + const { hasAccessToken } = this.props + const { toggleShowSettings, activated } = this.props return (