// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import classNames from 'classnames/bind'; import { desktopCapturer, DesktopCapturerSource } from 'electron'; import React, { useEffect, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import LoadingSpinner from './LoadingSpinner'; import styles from './ScreenPicker.css'; const cx = classNames.bind(styles); enum ShareType { Screen, Window } type Props = { onClickShareButton: (selectedSourceId: string) => void; onClickCancelButton: () => void; }; export default function ScreenPicker(props: Props) { const { onClickCancelButton, onClickShareButton } = props; const [sources, setSources] = useState(null); const [shareType, setShareType] = useState(ShareType.Window); const [selectedSourceId, setSelectedSourceId] = useState(null); useEffect(() => { desktopCapturer .getSources({ types: ['screen', 'window'], thumbnailSize: { width: 600, height: 600 } }) .then(async (desktopCapturerSources: DesktopCapturerSource[]) => { setSources(desktopCapturerSources); return null; }) .catch(error => { // eslint-disable-next-line console.error(error); }); }, []); const { screenSources, windowSources } = ( sources || ([] as DesktopCapturerSource[]) ).reduce( ( result: { screenSources: DesktopCapturerSource[]; windowSources: DesktopCapturerSource[]; }, source: DesktopCapturerSource ) => { if (source.name === document.title) { return result; } if (source.id.startsWith('screen')) { result.screenSources.push(source); } else { result.windowSources.push(source); } return result; }, { screenSources: [] as DesktopCapturerSource[], windowSources: [] as DesktopCapturerSource[] } ); const selectedSources = shareType === ShareType.Screen ? screenSources : windowSources; return (

{!sources && } {sources && selectedSources && !selectedSources.length && (
)} {sources && selectedSources && selectedSources.map(source => (
{ setSelectedSourceId(source.id); }} onKeyPress={() => {}} role="button" tabIndex={0} >
{source.name}
{source.name}
))}
); }