import { useModal } from 'Components/Modal'; import React, { useState, useEffect } from 'react'; import { connect } from 'react-redux'; import { toast } from 'react-toastify'; import { Icon, Loader } from 'UI'; import styles from './sharePopup.module.css'; import IntegrateSlackButton from '../IntegrateSlackButton/IntegrateSlackButton'; import SessionCopyLink from './SessionCopyLink'; import Select from 'Shared/Select'; import { fetchList as fetchSlack, sendSlackMsg } from 'Duck/integrations/slack'; import { fetchList as fetchTeams, sendMsTeamsMsg } from 'Duck/integrations/teams'; import { Button, Segmented } from 'antd'; import { PlayerContext } from 'App/components/Session/playerContext'; import { observer } from 'mobx-react-lite'; interface Msg { integrationId: string; entity: 'sessions'; entityId: string; data: { comment: string }; } const SharePopup = ({ trigger, showCopyLink = false, }: { trigger: React.ReactNode; showCopyLink?: boolean; }) => { const { store } = React.useContext(PlayerContext); const { showModal, hideModal } = useModal(); const openModal = () => { showModal( , { right: true, width: 300 } ); }; return (
{trigger}
); }; interface Props { sessionId: string; channels: { webhookId: string; name: string }[]; slackLoaded: boolean; msTeamsChannels: { webhookId: string; name: string }[]; msTeamsLoaded: boolean; tenantId: string; fetchSlack: () => void; fetchTeams: () => void; sendSlackMsg: (msg: Msg) => any; sendMsTeamsMsg: (msg: Msg) => any; showCopyLink?: boolean; hideModal: () => void; time: number; } function ShareModalComp({ sessionId, sendSlackMsg, sendMsTeamsMsg, showCopyLink, channels, slackLoaded, msTeamsChannels, msTeamsLoaded, fetchSlack, fetchTeams, hideModal, time, }: Props) { const [shareTo, setShareTo] = useState('slack'); const [comment, setComment] = useState(''); // @ts-ignore const [channelId, setChannelId] = useState(channels.getIn([0, 'webhookId'])); // @ts-ignore const [teamsChannel, setTeamsChannel] = useState(msTeamsChannels.getIn([0, 'webhookId'])); const [loadingSlack, setLoadingSlack] = useState(false); const [loadingTeams, setLoadingTeams] = useState(false); const isLoading = loadingSlack || loadingTeams; useEffect(() => { // @ts-ignore if (channels.size === 0 && !slackLoaded) { fetchSlack(); } // @ts-ignore if (msTeamsChannels.size === 0 && !msTeamsLoaded) { fetchTeams(); } }, [channels, slackLoaded, msTeamsChannels, msTeamsLoaded, fetchSlack, fetchTeams]); const editMessage = (e: React.ChangeEvent) => setComment(e.target.value); const shareToSlack = () => { setLoadingSlack(true); sendSlackMsg({ integrationId: channelId, entity: 'sessions', entityId: sessionId, data: { comment }, }).then(() => handleSuccess('Slack')); }; const shareToMSTeams = () => { setLoadingTeams(true); sendMsTeamsMsg({ integrationId: teamsChannel, entity: 'sessions', entityId: sessionId, data: { comment }, }).then(() => handleSuccess('MS Teams')); }; const handleSuccess = (endpoint: string) => { if (endpoint === 'Slack') { setLoadingSlack(false); } else { setLoadingTeams(false); } // @ts-ignore toast.success(`Sent to ${endpoint}.`); }; const changeSlackChannel = (value: any) => setChannelId(value.value); const changeTeamsChannel = (value: any) => setTeamsChannel(value.value); const slackOptions = channels .map(({ webhookId, name }) => ({ value: webhookId, label: name, })) // @ts-ignore .toJS(); const msTeamsOptions = msTeamsChannels .map(({ webhookId, name }) => ({ value: webhookId, label: name, })) // @ts-ignore .toJS(); const sendMsg = () => { if (shareTo === 'slack') { shareToSlack(); } else { shareToMSTeams(); } hideModal(); } const hasBoth = slackOptions.length > 0 && msTeamsOptions.length > 0; const hasNothing = slackOptions.length === 0 && msTeamsOptions.length === 0; return (
{isLoading ? ( ) : ( <>
Share Session
{!hasNothing ? (
Share via
{hasBoth ? (
Slack
, value: 'slack', }, { label:
MS Teams
, value: 'teams', }, ]} onChange={(value) => setShareTo(value as 'slack' | 'teams')} /> ) : (
0 ? 'integrations/slack-bw' : 'integrations/teams-white' } size={16} />
{slackOptions.length > 0 ? 'Slack' : 'MS Teams'}
)}
Select a channel or individual
{shareTo === 'slack' ? ( )}
Message