/** * We want to avoid importing anything from material-ui in this file so that we can lazy load it * in the popup */ import React, { Suspense, useRef } from 'react' import { MouseEvent, ReactNode, useEffect, useState } from 'react' import { IntrospectionData } from './types' import logo from './logo.png' const Popup = React.lazy(() => import('./Popup').then(({ Popup }) => ({ default: Popup }))) export enum PromotedIntrospectionTrigger { ContextMenu = 1, Overlay = 2, OverlayOnHover = 3, } export interface IntrospectionItem { logUserId: string contentId: string } interface ByLogUserIdResult { insertion_data: { [contentId: string]: IntrospectionData } request_id: string } export interface PromotedIntrospectionArgs { endpoint: string apiKey?: string children: ReactNode item: IntrospectionItem isOpen?: boolean renderTrigger?: (onTrigger: (e: Event) => any) => ReactNode disableDefaultTrigger?: boolean onClose?: () => any triggerType?: PromotedIntrospectionTrigger direction?: 'left' | 'right' } export enum REQUEST_ERRORS { DATA_NOT_FOUND = 'DATA_NOT_FOUND', INVALID_RESPONSE = 'INVALID_RESPONSE', FETCH_FAILED = 'FETCH_FAILED', } // TODO: investigate why navigator is undefined let isMobile = false if (typeof navigator !== 'undefined') { isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) } export const PromotedIntrospection = ({ item, endpoint, apiKey, children, renderTrigger, disableDefaultTrigger, isOpen, onClose, triggerType = PromotedIntrospectionTrigger.ContextMenu, direction = 'left', }: PromotedIntrospectionArgs) => { const [contextMenuOpen, setContextMenuOpen] = useState(false) const [error, setError] = useState() const [introspectionPayload, setIntrospectionPayload] = useState() const triggerContainerRef = useRef(null) const getIntrospectionPayload = async () => { let result try { const headers = { 'Content-Type': 'application/json', } if (apiKey) { headers['x-api-key'] = apiKey } result = await fetch( `${endpoint[endpoint.length - 1] === '/' ? endpoint.slice(0, -1) : endpoint}/v1/introspectiondata/byloguserid/${ item.logUserId }`, { headers, } ) } catch (e) { console.error(e) throw REQUEST_ERRORS.FETCH_FAILED } let data: ByLogUserIdResult[] try { data = await result.json() } catch (e) { console.error(e) throw REQUEST_ERRORS.INVALID_RESPONSE } const matchingRequest = data?.find((r) => r.insertion_data[item.contentId]) if (!matchingRequest?.insertion_data?.[item.contentId]) throw REQUEST_ERRORS.DATA_NOT_FOUND return matchingRequest } useEffect(() => { setContextMenuOpen(Boolean(isOpen)) if (isOpen) { onTrigger() } }, [isOpen]) const onTrigger = async (e?: Event | MouseEvent) => { setError() if (!contextMenuOpen && endpoint && item.logUserId && item.contentId) { e?.preventDefault() setContextMenuOpen(true) try { const payload = await getIntrospectionPayload() setIntrospectionPayload(payload) } catch (e) { setError(e) } } } const handleClose = () => { setContextMenuOpen(false) onClose?.() } const onClickBackground = (e: MouseEvent) => { e.preventDefault() handleClose() } const [showTriggerOverlay, setShowTriggerOverlay] = useState( () => triggerType === PromotedIntrospectionTrigger.Overlay || (triggerType === PromotedIntrospectionTrigger.OverlayOnHover && isMobile) ) // This could be done with a :hover pseudoselector, but we're restricted to inline CSS unless we import a // CSS-in-JS library (which we want to avoid in this file), or use a separate CSS file that the client would have to import. const onMouseEnter = () => { if (triggerType === PromotedIntrospectionTrigger.OverlayOnHover && !isMobile) { setShowTriggerOverlay(true) } } const onMouseLeave = () => { if (triggerType === PromotedIntrospectionTrigger.OverlayOnHover && !isMobile) { setShowTriggerOverlay(false) } } return ( <>
{(triggerType === PromotedIntrospectionTrigger.Overlay || triggerType === PromotedIntrospectionTrigger.OverlayOnHover) && ( )} {typeof renderTrigger === 'function' ? renderTrigger(onTrigger) : null}
{children}
{contextMenuOpen && ( }> )}
{contextMenuOpen && (
)} ) }