import * as React from 'react'; import { useLazyQuery, useMutation } from '@apollo/client'; import Card from '@mui/material/Card'; import Box from '@mui/material/Box'; import Stack from '@mui/material/Stack'; import CircularProgress from '@mui/material/CircularProgress'; import CardActionArea from '@mui/material/CardActionArea'; import MoreHorizIcon from '@mui/icons-material/MoreHoriz'; import CheckCircleIcon from '@mui/icons-material/CheckCircle'; import ErrorIcon from '@mui/icons-material/Error'; import { useSnackbar } from 'notistack'; import { DateTime } from 'luxon'; import type { IConnection } from '@automatisch/types'; import { DELETE_CONNECTION } from 'graphql/mutations/delete-connection'; import { TEST_CONNECTION } from 'graphql/queries/test-connection'; import ConnectionContextMenu from 'components/AppConnectionContextMenu'; import useFormatMessage from 'hooks/useFormatMessage'; import { CardContent, Typography } from './style'; type AppConnectionRowProps = { connection: IConnection; } const countTranslation = (value: React.ReactNode) => ( <> {value}
); function AppConnectionRow(props: AppConnectionRowProps): React.ReactElement { const { enqueueSnackbar } = useSnackbar(); const [verificationVisible, setVerificationVisible] = React.useState(false); const [testConnection, { called: testCalled, loading: testLoading }] = useLazyQuery(TEST_CONNECTION, { fetchPolicy: 'network-only', onCompleted: () => { setTimeout(() => setVerificationVisible(false), 3000); }, onError: () => { setTimeout(() => setVerificationVisible(false), 3000); }, }); const [deleteConnection] = useMutation(DELETE_CONNECTION); const formatMessage = useFormatMessage(); const { id, key, formattedData, verified, createdAt, flowCount } = props.connection; const contextButtonRef = React.useRef(null); const [anchorEl, setAnchorEl] = React.useState(null); const handleClose = () => { setAnchorEl(null); }; const onContextMenuClick = () => setAnchorEl(contextButtonRef.current); const onContextMenuAction = React.useCallback(async (event, action: { [key: string]: string }) => { if (action.type === 'delete') { await deleteConnection({ variables: { input: { id } }, update: (cache) => { const connectionCacheId = cache.identify({ __typename: 'Connection', id, }); cache.evict({ id: connectionCacheId, }); } }); enqueueSnackbar(formatMessage('connection.deletedMessage'), { variant: 'success' }); } else if (action.type === 'test') { setVerificationVisible(true); testConnection({ variables: { id } }); } }, [deleteConnection, id, testConnection, formatMessage, enqueueSnackbar]); const relativeCreatedAt = DateTime.fromMillis(parseInt(createdAt, 10)).toRelative(); return ( <> {formattedData?.screenName} {formatMessage('connection.addedAt', { datetime: relativeCreatedAt })} {verificationVisible && testCalled && testLoading && ( <> {formatMessage('connection.testing')} )} {verificationVisible && testCalled && !testLoading && verified && ( <> {formatMessage('connection.testSuccessful')} )} {verificationVisible && testCalled && !testLoading && !verified && ( <> {formatMessage('connection.testFailed')} )} {formatMessage('connection.flowCount', { count: countTranslation(flowCount) })} {anchorEl && } ); } export default AppConnectionRow;