/******************************************************************************** * Copyright (c) 2020 TypeFox and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ import { FunctionComponent, useContext, useState, useEffect, useRef, ReactNode } from 'react'; import { Box, Typography, Paper, Button, Dialog, DialogContent, DialogContentText, Link } from '@mui/material'; import { UserData, isError, ReportedError } from '../../extension-registry-types'; import { SanitizedMarkdown } from '../../components/sanitized-markdown'; import { Timestamp } from '../../components/timestamp'; import { ButtonWithProgress } from '../../components/button-with-progress'; import { createAbsoluteURL } from '../../utils'; import { MainContext } from '../../context'; import CircularProgress from '@mui/material/CircularProgress'; export const UserPublisherAgreement: FunctionComponent = props => { const { service, pageSettings, updateUser, handleError } = useContext(MainContext); const [dialogOpen, setDialogOpen] = useState(false); const [working, setWorking] = useState(false); const [agreementText, setAgreementText] = useState(''); const abortController = useRef(new AbortController()); useEffect(() => { return () => { abortController.current.abort(); }; }, []); useEffect(() => { if (dialogOpen) { onDialogOpened(); } }, [dialogOpen]); const closePublisherAgreement = () => { setDialogOpen(false); }; const signPublisherAgreement = async (): Promise => { try { setWorking(true); const result = await service.signPublisherAgreement(abortController.current); if (isError(result)) { throw result; } updateUser(); setDialogOpen(false); } catch (err) { if (!(err as ReportedError).code) { Object.assign(err, { code: 'publisher-agreement-problem' }); } handleError(err); } finally { setWorking(false); } }; const openPublisherAgreement = () => { if (!pageSettings.urls.publisherAgreement) { handleError({ error: 'Publisher agreement text is not available.' }); } else { setDialogOpen(true); } }; const onDialogOpened = async () => { const agreementURL = pageSettings.urls.publisherAgreement; if (agreementURL) { try { const agreementMd = await service.getStaticContent(abortController.current, agreementURL); setAgreementText(agreementMd); } catch (err) { handleError(err); } } else { setAgreementText('Publisher agreement text is not available.'); } }; const onClose = () => { setDialogOpen(false); }; const user = props.user; if (!user.publisherAgreement) { return null; } const publisherAgreementName = pageSettings?.publisherAgreement?.name ?? ''; const publisherAgreementSigned = user.publisherAgreement.status == 'signed'; let content: ReactNode; if (publisherAgreementSigned) { content = { user.publisherAgreement.timestamp ? <>You signed the {publisherAgreementName} Publisher Agreement . : <>You signed the {publisherAgreementName} Publisher Agreement. } ; } else if (user.additionalLogins?.find(login => login.provider === 'eclipse')) { content = <> You need to sign the {publisherAgreementName} Publisher Agreement before you can publish any extension to this registry. ; } else { content = <> You need to sign the {publisherAgreementName} Publisher Agreement before you can publish any extension to this registry. To start the signing process, please log in with an Eclipse Foundation account. ; } return <> {content} { agreementText ? { !publisherAgreementSigned && Agree } : } ; }; export interface UserPublisherAgreementProps { user: UserData; }