From 4c057a30f77ae316552f17878692f91f92f92e50 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 20 Jan 2023 20:12:22 -0500 Subject: [PATCH] fix(striker-ui): distinct server preview loading and power off --- striker-ui/components/Display/Preview.tsx | 105 ++++++++++++---------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/striker-ui/components/Display/Preview.tsx b/striker-ui/components/Display/Preview.tsx index 20c32053..486706b9 100644 --- a/striker-ui/components/Display/Preview.tsx +++ b/striker-ui/components/Display/Preview.tsx @@ -1,19 +1,21 @@ -import { FC, ReactNode, useEffect, useState } from 'react'; +import { + DesktopWindows as DesktopWindowsIcon, + PowerOffOutlined as PowerOffOutlinedIcon, +} from '@mui/icons-material'; import { Box, IconButton as MUIIconButton, IconButtonProps as MUIIconButtonProps, } from '@mui/material'; -import { - DesktopWindows as DesktopWindowsIcon, - PowerOffOutlined as PowerOffOutlinedIcon, -} from '@mui/icons-material'; +import { FC, ReactNode, useEffect, useMemo, useState } from 'react'; import API_BASE_URL from '../../lib/consts/API_BASE_URL'; import { BORDER_RADIUS, GREY } from '../../lib/consts/DEFAULT_THEME'; +import FlexBox from '../FlexBox'; import IconButton, { IconButtonProps } from '../IconButton'; import { InnerPanel, InnerPanelHeader, Panel, PanelHeader } from '../Panels'; +import Spinner from '../Spinner'; import { BodyText, HeaderText } from '../Text'; type PreviewOptionalProps = { @@ -85,9 +87,35 @@ const Preview: FC = ({ serverUUID, onClickConnectButton: connectButtonClickHandle = previewClickHandler, }) => { + const [isPreviewLoading, setIsPreviewLoading] = useState(true); const [isPreviewStale, setIsPreviewStale] = useState(false); const [preview, setPreview] = useState(''); + const previewButtonContent = useMemo( + () => + preview ? ( + + ) : ( + + ), + [isPreviewStale, isUseInnerPanel, preview], + ); + useEffect(() => { if (isFetchPreview) { (async () => { @@ -107,11 +135,14 @@ const Preview: FC = ({ setIsPreviewStale(false); } catch { setIsPreviewStale(true); + } finally { + setIsPreviewLoading(false); } })(); } else if (externalPreview) { setPreview(externalPreview); setIsPreviewStale(isExternalPreviewStale); + setIsPreviewLoading(false); } }, [externalPreview, isExternalPreviewStale, isFetchPreview, serverUUID]); @@ -120,57 +151,33 @@ const Preview: FC = ({ {headerEndAdornment} - :not(:last-child)': { - marginRight: '1em', - }, - }} - > + :first-child': { flexGrow: 1 } }}> + {/* Box wrapper below is required to keep external preview size sane. */} - - {preview ? ( - - ) : ( - - )} - + {isPreviewLoading ? ( + + ) : ( + + {previewButtonContent} + + )} {isShowControls && ( - + - + )} - + ); };