import { Dispatch, FC, ReactNode, SetStateAction, useEffect, useState, } from 'react'; import { Box, IconButton as MUIIconButton } from '@mui/material'; import { DesktopWindows as DesktopWindowsIcon, PowerOffOutlined as PowerOffOutlinedIcon, } from '@mui/icons-material'; import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import { BORDER_RADIUS, GREY } from '../../lib/consts/DEFAULT_THEME'; import IconButton from '../IconButton'; import { InnerPanel, InnerPanelHeader, Panel, PanelHeader } from '../Panels'; import { BodyText, HeaderText } from '../Text'; type PreviewOptionalProps = { externalPreview?: string; headerEndAdornment?: ReactNode; isExternalPreviewStale?: boolean; isFetchPreview?: boolean; isShowControls?: boolean; isUseInnerPanel?: boolean; setMode?: Dispatch> | null; }; type PreviewProps = PreviewOptionalProps & { serverName: string | string[] | undefined; serverUUID: string; }; const PREVIEW_DEFAULT_PROPS: Required = { externalPreview: '', headerEndAdornment: null, isExternalPreviewStale: false, isFetchPreview: true, isShowControls: true, isUseInnerPanel: false, setMode: null, }; const PreviewPanel: FC<{ isUseInnerPanel: boolean }> = ({ children, isUseInnerPanel, }) => isUseInnerPanel ? ( {children} ) : ( {children} ); const PreviewPanelHeader: FC<{ isUseInnerPanel: boolean; text: string }> = ({ children, isUseInnerPanel, text, }) => isUseInnerPanel ? ( {children} ) : ( {children} ); const Preview: FC = ({ externalPreview = PREVIEW_DEFAULT_PROPS.externalPreview, headerEndAdornment, isExternalPreviewStale = PREVIEW_DEFAULT_PROPS.isExternalPreviewStale, isFetchPreview = PREVIEW_DEFAULT_PROPS.isFetchPreview, isShowControls = PREVIEW_DEFAULT_PROPS.isShowControls, isUseInnerPanel = PREVIEW_DEFAULT_PROPS.isUseInnerPanel, serverName, serverUUID, setMode, }) => { const [isPreviewStale, setIsPreviewStale] = useState(false); const [preview, setPreview] = useState(''); useEffect(() => { if (isFetchPreview) { (async () => { try { const response = await fetch( `${API_BASE_URL}/server/${serverUUID}?ss`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }, ); const { screenshot: fetchedScreenshot } = await response.json(); setPreview(fetchedScreenshot); setIsPreviewStale(false); } catch { setIsPreviewStale(true); } })(); } else if (externalPreview) { setPreview(externalPreview); setIsPreviewStale(isExternalPreviewStale); } }, [externalPreview, isExternalPreviewStale, isFetchPreview, serverUUID]); return ( {headerEndAdornment} :not(:last-child)': { marginRight: '1em', }, }} > setMode?.call(null, false)} sx={{ borderRadius: BORDER_RADIUS, color: GREY, padding: 0, }} > {preview ? ( ) : ( )} {isShowControls && ( setMode?.call(null, false)}> )} ); }; Preview.defaultProps = PREVIEW_DEFAULT_PROPS; export default Preview;