// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import {Certificate} from 'electron/renderer'; import React, {Fragment} from 'react'; import {Modal, Button, Table, Row, Col} from 'react-bootstrap'; import {FormattedMessage} from 'react-intl'; import {CertificateModalData} from 'types/certificate'; import {ModalMessage} from 'types/modals'; import {MODAL_INFO} from 'common/communication'; import IntlProvider from 'renderer/intl_provider'; import ShowCertificateModal from '../../components/showCertificateModal'; type Props = { onSelect: (cert: Certificate) => void; onCancel?: () => void; getCertInfo: () => void; } type State = { selectedIndex?: number; showCertificate?: Certificate; url?: string; list?: Certificate[]; } export default class SelectCertificateModal extends React.PureComponent { constructor(props: Props) { super(props); this.state = {}; } componentDidMount() { window.addEventListener('message', this.handleCertInfoMessage); this.props.getCertInfo(); } componentWillUnmount() { window.removeEventListener('message', this.handleCertInfoMessage); } handleCertInfoMessage = (event: {data: ModalMessage}) => { switch (event.data.type) { case MODAL_INFO: { const {url, list} = event.data.data; this.setState({url, list}); break; } default: break; } } selectfn = (index: number) => { return (() => { this.setState({selectedIndex: index}); }); }; renderCert = (cert: Certificate, index: number) => { const issuer = (cert.issuerName || (cert.issuer && cert.issuer.commonName) || ''); const subject = (cert.subjectName || (cert.subject && cert.subject.commonName) || ''); const serial = cert.serialNumber || ''; return ( {subject} {issuer} {serial} ); }; renderCerts = (certificateList: Certificate[]) => { if (certificateList) { const certs = certificateList.map(this.renderCert); return ( {certs} ); } return ( ); } getSelectedCert = () => { if (this.state.list && this.state.selectedIndex !== undefined) { return this.state.list[this.state.selectedIndex]; } return undefined; }; handleOk = () => { const cert = this.getSelectedCert(); if (cert) { this.props.onSelect(cert); } } handleCertificateInfo = () => { const certificate = this.getSelectedCert(); this.setState({showCertificate: certificate}); } certificateInfoClose = () => { this.setState({showCertificate: undefined}); } render() { if (this.state.showCertificate) { return ( ); } return ( {}} >

{this.renderCerts(this.state.list!)}
); } }