"Fossies" - the Fresh Open Source Software Archive

Member "deltachat-desktop-1.3.4/src/renderer/ScreenController.tsx" (18 May 2020, 4205 Bytes) of package /linux/misc/deltachat-desktop-1.3.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TSX (TypeScript with React) source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "ScreenController.tsx": 1.3.3_vs_1.3.4.

    1 import { Login, AppState } from '../shared/shared-types'
    2 
    3 import React from 'react'
    4 import { Component, createRef } from 'react'
    5 const { ipcRenderer } = window.electron_functions
    6 
    7 import { ScreenContext } from './contexts'
    8 import LoginScreen from './components/LoginScreen'
    9 import MainScreen from './components/MainScreen'
   10 import {
   11   Controller as DialogController,
   12   DialogId,
   13 } from './components/dialogs/index'
   14 import { processOPENPGP4FPRUrl } from './components/dialogs/ImportQrCode'
   15 
   16 import * as logger from '../shared/logger'
   17 
   18 const log = logger.getLogger('renderer/ScreenController')
   19 
   20 export interface userFeedback {
   21   type: 'error' | 'success'
   22   text: string
   23 }
   24 
   25 export default class ScreenController extends Component {
   26   dialogs: React.RefObject<DialogController>
   27   state: { message: userFeedback | false }
   28   changeScreen: any
   29   onShowAbout: any
   30 
   31   constructor(
   32     public props: { logins: Login[]; deltachat: AppState['deltachat'] }
   33   ) {
   34     super(props)
   35     this.state = {
   36       message: false,
   37     }
   38 
   39     this.onError = this.onError.bind(this)
   40     this.onSuccess = this.onSuccess.bind(this)
   41     this.userFeedback = this.userFeedback.bind(this)
   42     this.userFeedbackClick = this.userFeedbackClick.bind(this)
   43     this.openDialog = this.openDialog.bind(this)
   44     window.__openDialog = this.openDialog.bind(this)
   45     window.__userFeedback = this.userFeedback.bind(this)
   46     this.closeDialog = this.closeDialog.bind(this)
   47     this.onShowAbout = this.showAbout.bind(this, true)
   48     this.dialogs = createRef()
   49   }
   50 
   51   userFeedback(message: userFeedback | false) {
   52     if (message !== false && this.state.message === message) return // one at a time, cowgirl
   53     this.setState({ message })
   54   }
   55 
   56   userFeedbackClick() {
   57     this.userFeedback(false)
   58   }
   59 
   60   componentDidMount() {
   61     ipcRenderer.on('error', this.onError)
   62     ipcRenderer.on('DC_EVENT_ERROR', this.onError)
   63     ipcRenderer.on('DC_EVENT_LOGIN_FAILED', this.onError)
   64     ipcRenderer.on('DC_EVENT_ERROR_NETWORK', this.onError)
   65     ipcRenderer.on('success', this.onSuccess)
   66     ipcRenderer.on('showAboutDialog', this.onShowAbout)
   67     ipcRenderer.on('open-url', this.onOpenUrl)
   68 
   69     ipcRenderer.send('frontendReady')
   70   }
   71 
   72   componentWillUnmount() {
   73     ipcRenderer.removeListener('showAboutDialog', this.onShowAbout)
   74     ipcRenderer.removeListener('error', this.onError)
   75     ipcRenderer.removeListener('DC_EVENT_ERROR', this.onError)
   76     ipcRenderer.removeListener('DC_EVENT_LOGIN_FAILED', this.onError)
   77     ipcRenderer.removeListener('DC_EVENT_ERROR_NETWORK', this.onError)
   78     ipcRenderer.removeListener('success', this.onSuccess)
   79     ipcRenderer.removeListener('open-url', this.onOpenUrl)
   80   }
   81 
   82   onError(_event: any, data1: string, data2: string) {
   83     if (!data2) data2 = ''
   84     const text = data1 + ' ' + data2
   85     this.userFeedback({ type: 'error', text })
   86   }
   87 
   88   onSuccess(_event: any, text: string) {
   89     this.userFeedback({ type: 'success', text })
   90   }
   91 
   92   showAbout() {
   93     this.openDialog('About')
   94   }
   95 
   96   async onOpenUrl(_event: Event, url: string) {
   97     processOPENPGP4FPRUrl(url)
   98   }
   99 
  100   openDialog(name: DialogId, props?: any) {
  101     this.dialogs.current.open(name, props)
  102   }
  103 
  104   closeDialog(name: DialogId) {
  105     this.dialogs.current.close(name)
  106   }
  107 
  108   render() {
  109     const { logins, deltachat } = this.props
  110 
  111     window.__isReady = deltachat.ready
  112     return (
  113       <div>
  114         {this.state.message && (
  115           <div
  116             onClick={this.userFeedbackClick}
  117             className={`user-feedback ${this.state.message.type}`}
  118           >
  119             <p>{this.state.message.text}</p>
  120           </div>
  121         )}
  122         <ScreenContext.Provider
  123           value={{
  124             openDialog: this.openDialog,
  125             closeDialog: this.closeDialog,
  126             userFeedback: this.userFeedback,
  127             changeScreen: this.changeScreen,
  128           }}
  129         >
  130           {!deltachat.ready ? (
  131             <LoginScreen logins={logins} deltachat={deltachat} />
  132           ) : (
  133             <MainScreen />
  134           )}
  135           <DialogController
  136             ref={this.dialogs}
  137             deltachat={deltachat}
  138             userFeedback={this.userFeedback}
  139           />
  140         </ScreenContext.Provider>
  141       </div>
  142     )
  143   }
  144 }