import React from 'react' ; import MessageDetail from './MessageDetail' ; import FullscreenMedia from './FullscreenMedia' ; import EnterAutocryptSetupMessage from './EnterAutocryptSetupMessage' ; import About from './About' ; import Settings from './Settings' ; import ForwardMessage from './ForwardMessage' ; import EncryptionInfo from './EncryptionInfo' ; import CreateChat from './CreateChat' ; import ViewGroup from './ViewGroup' ; import ViewProfile from './ViewProfile' ; import ImportQrCode from './ImportQrCode' ; import AlertDialog from './AlertDialog' ; import ConfirmationDialog from './ConfirmationDialog' ; import UnblockContacts from './UnblockContacts' ; import MuteChat from './MuteChat' ; import QrCode from './QrCode' ; import DisappearingMessages from './DisappearingMessages' ; import ChatAuditLogDialog from './ChatAuditLogDialog' ; import { getLogger } from '../../../shared/logger' ; export const allDialogs = { FullscreenMedia , MessageDetail , EnterAutocryptSetupMessage , About , Settings , ForwardMessage , EncryptionInfo , CreateChat , ViewGroup , ViewProfile , ImportQrCode , ConfirmationDialog , AlertDialog , UnblockContacts , MuteChat , DisappearingMessages , ChatAuditLogDialog , QrCode , } ; export type DialogId = keyof typeof allDialogs | string ; const log = getLogger ( 'renderer/dialogs/DialogController' ) ; export type DialogAdditionProps = { [ key ] : any ; } ; export type Dialog = { id : number ; additionalProps : DialogAdditionProps ; fnc : any ; } ; export type DialogProps = { isOpen : boolean ; onClose : ( ) => void ; userFeedback : todo ; key : string ; openDialog : todo ; closeDialog : todo ; [ key ] : any ; } ; let dialogCounter = 1 ; export type OpenDialogFunctionType = typeof DialogController . prototype . openDialog ; export type CloseDialogFunctionType = typeof DialogController . prototype . closeDialog ; export default class DialogController extends React . Component < any , { dialogs : { [ key ] : Dialog ; } ; } > { constructor ( props ) { super ( props ) ; this . state = { dialogs : { } } ; this . openDialog = this . openDialog . bind ( this ) ; this . closeDialog = this . closeDialog . bind ( this ) ; } openDialog < T extends { [ key ] : any ; } > ( fnc , additionalProps ? ) { if ( typeof fnc === 'string' ) { fnc = allDialogs [ fnc ] ; if ( ! fnc ) throw new Error ( `Dialog with name ${ name } does not exist` ) ; } log . debug ( 'openDialog: ' , typeof fnc === 'string' ? fnc : 'Anonymous' , additionalProps ) ; let id = dialogCounter ++ ; if ( id >= Number . MAX_SAFE_INTEGER - 1 ) { id = dialogCounter = 0 ; } log . debug ( `Add dialog with id: ${ id }` ) ; this . setState ( state => { return { dialogs : { ... state . dialogs , [ id ] : { id , fnc , additionalProps : additionalProps || { } , } , } , } ; } ) ; return id ; } closeDialog ( id ) { this . setState ( ( prevState ) => { const { [ id ] : closedDialog , ... dialogs } = prevState . dialogs ; log . debug ( `Close dialog with id: ${ id }` ) ; return { ... prevState , dialogs } ; } ) ; } render ( ) { const { userFeedback } = this . props ; const { dialogs } = this . state ; return ( < div > { Object . keys ( dialogs ) . map ( ( id ) => { log . debug ( `Rendering dialog with id ${ id }` ) ; const dialog = dialogs [ id ] ; const props = { isOpen : true , onClose : ( ) => this . closeDialog ( id ) , userFeedback , key : id , openDialog : this . openDialog . bind ( this ) , closeDialog : this . closeDialog . bind ( this ) , ... dialog . additionalProps , } ; return ( < div key = { 'dialog-' + id } > < dialog . fnc { ... props } / > < /div>); } ) } < /div>); } }