From eb7b2481f631cec5953265eef4ebc3f2fa7e496a Mon Sep 17 00:00:00 2001 From: muhamedsalih-tw <104364298+muhamedsalih-tw@users.noreply.github.com> Date: Wed, 16 Nov 2022 23:30:39 +0530 Subject: Transform JSX components to TSX (#755) * color picker types * Import * SetupAssistant * Services & appear * ServiceWebView * SettingsLayout * ImportantScreen * WorkspaceDrawer * SetupAssistant * chore: update vscode settings * chore: removed stale Import screen component & its tree --- src/components/services/content/ServiceWebview.jsx | 132 ----------------- src/components/services/content/ServiceWebview.tsx | 140 +++++++++++++++++ src/components/services/content/Services.jsx | 165 --------------------- src/components/services/content/Services.tsx | 162 ++++++++++++++++++++ 4 files changed, 302 insertions(+), 297 deletions(-) delete mode 100644 src/components/services/content/ServiceWebview.jsx create mode 100644 src/components/services/content/ServiceWebview.tsx delete mode 100644 src/components/services/content/Services.jsx create mode 100644 src/components/services/content/Services.tsx (limited to 'src/components/services') diff --git a/src/components/services/content/ServiceWebview.jsx b/src/components/services/content/ServiceWebview.jsx deleted file mode 100644 index 835c5125e..000000000 --- a/src/components/services/content/ServiceWebview.jsx +++ /dev/null @@ -1,132 +0,0 @@ -import { Component } from 'react'; -import PropTypes from 'prop-types'; -import { observer } from 'mobx-react'; -import { action, makeObservable, observable, reaction } from 'mobx'; -import ElectronWebView from 'react-electron-web-view'; -import { join } from 'path'; - -import ServiceModel from '../../../models/Service'; - -const debug = require('../../../preload-safe-debug')('Ferdium:Services'); - -class ServiceWebview extends Component { - static propTypes = { - service: PropTypes.instanceOf(ServiceModel).isRequired, - setWebviewReference: PropTypes.func.isRequired, - detachService: PropTypes.func.isRequired, - isSpellcheckerEnabled: PropTypes.bool.isRequired, - }; - - @observable webview = null; - - constructor(props) { - super(props); - - makeObservable(this); - - reaction( - () => this.webview, - () => { - if (this.webview && this.webview.view) { - this.webview.view.addEventListener('console-message', e => { - debug('Service logged a message:', e.message); - }); - this.webview.view.addEventListener('did-navigate', () => { - if (this.props.service._webview) { - document.title = `Ferdium - ${this.props.service.name} ${ - this.props.service.dialogTitle - ? ` - ${this.props.service.dialogTitle}` - : '' - } ${`- ${this.props.service._webview.getTitle()}`}`; - } - }); - } - }, - ); - } - - componentWillUnmount() { - const { service, detachService } = this.props; - detachService({ service }); - } - - refocusWebview = () => { - const { webview } = this; - debug('Refocus Webview is called', this.props.service); - if (!webview) return; - if (this.props.service.isActive) { - webview.view.blur(); - webview.view.focus(); - window.setTimeout(() => { - document.title = `Ferdium - ${this.props.service.name} ${ - this.props.service.dialogTitle - ? ` - ${this.props.service.dialogTitle}` - : '' - } ${`- ${this.props.service._webview.getTitle()}`}`; - }, 100); - } else { - debug('Refocus not required - Not active service'); - } - }; - - @action _setWebview(webview) { - this.webview = webview; - } - - render() { - const { service, setWebviewReference, isSpellcheckerEnabled } = this.props; - - const preloadScript = join( - __dirname, - '..', - '..', - '..', - 'webview', - 'recipe.js', - ); - - return ( - { - this._setWebview(webview); - if (webview && webview.view) { - webview.view.addEventListener( - 'did-stop-loading', - this.refocusWebview, - ); - } - }} - autosize - src={service.url} - preload={preloadScript} - partition={service.partition} - onDidAttach={() => { - // Force the event handler to run in a new task. - // This resolves a race condition when the `did-attach` is called, - // but the webview is not attached to the DOM yet: - // https://github.com/electron/electron/issues/31918 - // This prevents us from immediately attaching listeners such as `did-stop-load`: - // https://github.com/ferdium/ferdium-app/issues/157 - setTimeout(() => { - setWebviewReference({ - serviceId: service.id, - webview: this.webview.view, - }); - }, 0); - }} - onUpdateTargetUrl={this.updateTargetUrl} - useragent={service.userAgent} - disablewebsecurity={ - service.recipe.disablewebsecurity ? true : undefined - } - allowpopups - nodeintegration - webpreferences={`spellcheck=${ - isSpellcheckerEnabled ? 1 : 0 - }, contextIsolation=1`} - /> - ); - } -} - -export default observer(ServiceWebview); diff --git a/src/components/services/content/ServiceWebview.tsx b/src/components/services/content/ServiceWebview.tsx new file mode 100644 index 000000000..ac8d1ea66 --- /dev/null +++ b/src/components/services/content/ServiceWebview.tsx @@ -0,0 +1,140 @@ +import { Component, ReactElement } from 'react'; +import { observer } from 'mobx-react'; +import { action, makeObservable, observable, reaction } from 'mobx'; +import ElectronWebView from 'react-electron-web-view'; +import { join } from 'path'; +import ServiceModel from '../../../models/Service'; + +const debug = require('../../../preload-safe-debug')('Ferdium:Services'); + +interface IProps { + service: ServiceModel; + setWebviewReference: (options: { + serviceId: string; + webview: ElectronWebView | null; + }) => void; + detachService: (options: { service: ServiceModel }) => void; + isSpellcheckerEnabled: boolean; +} + +@observer +class ServiceWebview extends Component { + @observable webview: ElectronWebView | null = null; + + constructor(props: IProps) { + super(props); + + this.refocusWebview = this.refocusWebview.bind(this); + this._setWebview = this._setWebview.bind(this); + + makeObservable(this); + + reaction( + () => this.webview, + () => { + if (this.webview && this.webview.view) { + this.webview.view.addEventListener('console-message', e => { + debug('Service logged a message:', e.message); + }); + this.webview.view.addEventListener('did-navigate', () => { + if (this.props.service._webview) { + document.title = `Ferdium - ${this.props.service.name} ${ + this.props.service.dialogTitle + ? ` - ${this.props.service.dialogTitle}` + : '' + } ${`- ${this.props.service._webview.getTitle()}`}`; + } + }); + } + }, + ); + } + + componentWillUnmount(): void { + const { service, detachService } = this.props; + detachService({ service }); + } + + refocusWebview(): void { + const { webview } = this; + debug('Refocus Webview is called', this.props.service); + if (!webview) { + return; + } + + if (this.props.service.isActive) { + webview.view.blur(); + webview.view.focus(); + window.setTimeout(() => { + document.title = `Ferdium - ${this.props.service.name} ${ + this.props.service.dialogTitle + ? ` - ${this.props.service.dialogTitle}` + : '' + } ${`- ${this.props.service._webview.getTitle()}`}`; + }, 100); + } else { + debug('Refocus not required - Not active service'); + } + } + + @action _setWebview(webview): void { + this.webview = webview; + } + + render(): ReactElement { + const { service, setWebviewReference, isSpellcheckerEnabled } = this.props; + + const preloadScript = join( + __dirname, + '..', + '..', + '..', + 'webview', + 'recipe.js', + ); + + return ( + { + this._setWebview(webview); + if (webview && webview.view) { + webview.view.addEventListener( + 'did-stop-loading', + this.refocusWebview, + ); + } + }} + autosize + src={service.url} + preload={preloadScript} + partition={service.partition} + onDidAttach={() => { + // Force the event handler to run in a new task. + // This resolves a race condition when the `did-attach` is called, + // but the webview is not attached to the DOM yet: + // https://github.com/electron/electron/issues/31918 + // This prevents us from immediately attaching listeners such as `did-stop-load`: + // https://github.com/ferdium/ferdium-app/issues/157 + setTimeout(() => { + setWebviewReference({ + serviceId: service.id, + webview: this.webview.view, + }); + }, 0); + }} + // onUpdateTargetUrl={this.updateTargetUrl} // TODO - [TS DEBT] need to check where its from + useragent={service.userAgent} + disablewebsecurity={ + service.recipe.disablewebsecurity ? true : undefined + } + allowpopups + nodeintegration + webpreferences={`spellcheck=${ + isSpellcheckerEnabled ? 1 : 0 + }, contextIsolation=1`} + /> + ); + } +} + +export default ServiceWebview; diff --git a/src/components/services/content/Services.jsx b/src/components/services/content/Services.jsx deleted file mode 100644 index da700b5b1..000000000 --- a/src/components/services/content/Services.jsx +++ /dev/null @@ -1,165 +0,0 @@ -import { Component } from 'react'; -import PropTypes from 'prop-types'; -import { observer, PropTypes as MobxPropTypes, inject } from 'mobx-react'; -import { Link } from 'react-router-dom'; -import { defineMessages, injectIntl } from 'react-intl'; -import Confetti from 'react-confetti'; -import ms from 'ms'; -import injectSheet from 'react-jss'; - -import ServiceView from './ServiceView'; -import Appear from '../../ui/effects/Appear'; - -const messages = defineMessages({ - getStarted: { - id: 'services.getStarted', - defaultMessage: 'Get started', - }, - login: { - id: 'services.login', - defaultMessage: 'Please login to use Ferdium.', - }, - serverless: { - id: 'services.serverless', - defaultMessage: 'Use Ferdium without an Account', - }, - serverInfo: { - id: 'services.serverInfo', - defaultMessage: - 'Optionally, you can change your Ferdium server by clicking the cog in the bottom left corner. If you are switching over (from one of the hosted servers) to using Ferdium without an account, please be informed that you can export your data from that server and subsequently import it using the Help menu to resurrect all your workspaces and configured services!', - }, -}); - -const styles = { - confettiContainer: { - position: 'absolute', - width: '100%', - zIndex: 9999, - pointerEvents: 'none', - }, -}; - -class Services extends Component { - static propTypes = { - services: MobxPropTypes.arrayOrObservableArray, - setWebviewReference: PropTypes.func.isRequired, - detachService: PropTypes.func.isRequired, - handleIPCMessage: PropTypes.func.isRequired, - openWindow: PropTypes.func.isRequired, - reload: PropTypes.func.isRequired, - openSettings: PropTypes.func.isRequired, - update: PropTypes.func.isRequired, - userHasCompletedSignup: PropTypes.bool.isRequired, - // eslint-disable-next-line react/forbid-prop-types - classes: PropTypes.object.isRequired, - isSpellcheckerEnabled: PropTypes.bool.isRequired, - }; - - static defaultProps = { - services: [], - }; - - _confettiTimeout = null; - - constructor() { - super(); - - this.state = { - showConfetti: true, - }; - } - - componentDidMount() { - this._confettiTimeout = window.setTimeout(() => { - this.setState({ - showConfetti: false, - }); - }, ms('8s')); - } - - componentWillUnmount() { - if (this._confettiTimeout) { - clearTimeout(this._confettiTimeout); - } - } - - render() { - const { - services, - handleIPCMessage, - setWebviewReference, - detachService, - openWindow, - reload, - openSettings, - update, - userHasCompletedSignup, - classes, - isSpellcheckerEnabled, - } = this.props; - - const { showConfetti } = this.state; - - const { intl } = this.props; - - return ( -
- {userHasCompletedSignup && ( -
- -
- )} - {services.length === 0 && ( - -
- Logo - - - {intl.formatMessage(messages.getStarted)} - - -
-
- )} - {services - .filter(service => !service.isTodosService) - .map(service => ( - reload({ serviceId: service.id })} - edit={() => openSettings({ path: `services/edit/${service.id}` })} - enable={() => - update({ - serviceId: service.id, - serviceData: { - isEnabled: true, - }, - redirect: false, - }) - } - isSpellcheckerEnabled={isSpellcheckerEnabled} - /> - ))} -
- ); - } -} - -export default injectIntl( - injectSheet(styles, { injectTheme: true })( - inject('actions')(observer(Services)), - ), -); diff --git a/src/components/services/content/Services.tsx b/src/components/services/content/Services.tsx new file mode 100644 index 000000000..53cddd907 --- /dev/null +++ b/src/components/services/content/Services.tsx @@ -0,0 +1,162 @@ +import { Component, ReactElement } from 'react'; +import { observer } from 'mobx-react'; +import { Link } from 'react-router-dom'; +import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl'; +import Confetti from 'react-confetti'; +import ms from 'ms'; +import withStyles, { WithStylesProps } from 'react-jss'; +import ServiceView from './ServiceView'; +import Appear from '../../ui/effects/Appear'; +import Service from '../../../models/Service'; + +const messages = defineMessages({ + getStarted: { + id: 'services.getStarted', + defaultMessage: 'Get started', + }, + login: { + id: 'services.login', + defaultMessage: 'Please login to use Ferdium.', + }, + serverless: { + id: 'services.serverless', + defaultMessage: 'Use Ferdium without an Account', + }, + serverInfo: { + id: 'services.serverInfo', + defaultMessage: + 'Optionally, you can change your Ferdium server by clicking the cog in the bottom left corner. If you are switching over (from one of the hosted servers) to using Ferdium without an account, please be informed that you can export your data from that server and subsequently import it using the Help menu to resurrect all your workspaces and configured services!', + }, +}); + +const styles = { + confettiContainer: { + position: 'absolute', + width: '100%', + zIndex: 9999, + pointerEvents: 'none', + }, +}; + +interface IProps extends WrappedComponentProps, WithStylesProps { + services?: Service[]; + setWebviewReference: () => void; + detachService: () => void; + handleIPCMessage: () => void; + openWindow: () => void; + reload: (options: { serviceId: string }) => void; + openSettings: (options: { path: string }) => void; + update: (options: { + serviceId: string; + serviceData: { isEnabled: boolean }; + redirect: boolean; + }) => void; + userHasCompletedSignup: boolean; + isSpellcheckerEnabled: boolean; +} + +interface IState { + showConfetti: boolean; +} + +@observer +class Services extends Component { + _confettiTimeout: number | null = null; + + constructor(props: IProps) { + super(props); + + this.state = { + showConfetti: true, + }; + } + + componentDidMount(): void { + this._confettiTimeout = window.setTimeout(() => { + this.setState({ + showConfetti: false, + }); + }, ms('8s')); + } + + componentWillUnmount(): void { + if (this._confettiTimeout) { + clearTimeout(this._confettiTimeout); + } + } + + render(): ReactElement { + const { + services = [], + handleIPCMessage, + setWebviewReference, + detachService, + openWindow, + reload, + openSettings, + update, + userHasCompletedSignup, + classes, + isSpellcheckerEnabled, + intl, + } = this.props; + + const { showConfetti } = this.state; + + return ( +
+ {userHasCompletedSignup && ( +
+ +
+ )} + {services.length === 0 && ( + +
+ Logo + + + {intl.formatMessage(messages.getStarted)} + + +
+
+ )} + {services + .filter(service => !service.isTodosService) + .map(service => ( + reload({ serviceId: service.id })} + edit={() => openSettings({ path: `services/edit/${service.id}` })} + enable={() => + update({ + serviceId: service.id, + serviceData: { + isEnabled: true, + }, + redirect: false, + }) + } + isSpellcheckerEnabled={isSpellcheckerEnabled} + /> + ))} +
+ ); + } +} + +export default injectIntl(withStyles(styles, { injectTheme: true })(Services)); -- cgit v1.2.3-54-g00ecf