import React, { SyntheticEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { CircularProgress, Box, Tab, Tabs, ThemeProvider } from '@material-ui/core' import { createTheme } from '@material-ui/core' import { ModerationPanel } from './ModerationPanel' import { ModerationLogPanel } from './ModerationLogPanel' import { PropertiesPanel } from './PropertiesPanel' import { StatsPanel } from './StatsPanel' import { IntrospectionData } from './types' import { createGenerateClassName, makeStyles, StylesProvider } from '@material-ui/core/styles' import { blue } from '@material-ui/core/colors' import { REQUEST_ERRORS } from './PromotedIntrospection' import { IntrospectionItem } from './PromotedIntrospection' import logo from './logo.png' export interface IntrospectionIds { label: string value?: string } export interface PopupArgs { direction?: 'left' | 'right' triggerContainerRef: React.RefObject introspectionData?: IntrospectionData introspectionIds: IntrospectionIds[] item: IntrospectionItem error?: string | void handleClose: () => any } const useStyles = makeStyles((theme) => ({ loading: { display: 'flex', justifyContent: 'center', alignItems: 'center', height: '300px', }, error: { padding: theme.spacing(2), paddingBottom: theme.spacing(3), color: theme.palette.error.main, height: '300px', }, popupContainer: { visibility: 'hidden', position: 'absolute', left: '-425px', }, ['popupContainer--right']: { right: '-425px', left: 'auto', }, outerContainer: { color: 'black', maxWidth: '100vw', width: '400px', zIndex: 1001, maxHeight: '100vh', }, innerContainer: { background: '#eee', paddingTop: theme.spacing(1), position: 'relative', }, callout: { display: 'none', [theme.breakpoints.up('sm')]: { display: 'block', }, backgroundColor: '#eee', transform: 'rotate(45deg) translateX(-50%)', height: '30px', left: 'calc(100% - 4px)', position: 'absolute', top: '49%', width: '30px', }, ['callout--right']: { right: 'calc(100% - 25px)', left: 'auto', }, promotedLogo: { bottom: '26px', height: '20px', left: '20px', position: 'absolute', width: '20px', }, })) const generateClassName = createGenerateClassName({ productionPrefix: 'promoted-introspection', }) export const Popup = ({ error, triggerContainerRef, introspectionData, introspectionIds, item, handleClose, direction = 'left', }: PopupArgs) => { const theme = createTheme({ typography: { body1: { fontSize: 12, }, }, palette: { primary: blue, }, }) const popupContainerRef = useRef(null) const classes = useStyles(theme) const [tabIndex, setTabIndex] = useState(0) const handleTabChange = (_: SyntheticEvent, newTabIndex: number) => { setTabIndex(newTabIndex) } const [promotedLogoVisible, setPromotedLogoVisible] = useState(true) const handleCopyButtonVisibilityChange = (visible: boolean) => { setPromotedLogoVisible(visible) } const repositionPopup = useCallback(() => { if (!triggerContainerRef.current || !popupContainerRef.current) return const triggerRect = triggerContainerRef.current.getBoundingClientRect() const popupRect = popupContainerRef.current.getBoundingClientRect() popupContainerRef.current.style.visibility = 'visible' popupContainerRef.current.style.position = 'absolute' popupContainerRef.current.style[direction] = `${-Math.min(popupRect.width + 25, triggerRect.x)}px` popupContainerRef.current.style.top = `${-(popupRect.height - triggerRect.height) / 2}px` }, []) useEffect(() => { repositionPopup() }, [repositionPopup]) const resizeObserver = useMemo(() => { return new ResizeObserver(() => { repositionPopup() }) }, []) useEffect(() => { if (!popupContainerRef.current) return resizeObserver.observe(popupContainerRef.current) window.addEventListener('resize', repositionPopup) return () => { if (popupContainerRef.current) { resizeObserver.unobserve(popupContainerRef.current) } window.removeEventListener('resize', repositionPopup) } }, []) const errorMap = { [REQUEST_ERRORS.DATA_NOT_FOUND]: `Could not find introspection data for Log User ID ${item.logUserId} and Content ID ${item.contentId}. This is likely because the user is not flagged as an internal user.`, [REQUEST_ERRORS.INVALID_RESPONSE]: `Response from server invalid for Log User ID ${item.logUserId}`, [REQUEST_ERRORS.FETCH_FAILED]: `Fetch failed for Log User ID ${item.logUserId}`, } return (
{!introspectionData && !error && (
)} {error &&
{errorMap[error]}
} {!error && introspectionData && ( <> {/* */} {promotedLogoVisible && } {tabIndex == 0 && ( )} {tabIndex == 1 && } {tabIndex == 2 && } {tabIndex == 3 && } )}
) }