fix(striker-ui): visually differentiate between fresh and stale server screenshots

main
Tsu-ba-me 3 years ago
parent 1d39e4d918
commit 89549cd317
  1. 36
      striker-ui/components/Display/Preview.tsx
  2. 26
      striker-ui/pages/index.tsx
  3. 2
      striker-ui/pages/server/index.tsx

@ -14,20 +14,22 @@ import { BodyText, HeaderText } from '../Text';
type PreviewOptionalProps = { type PreviewOptionalProps = {
externalPreview?: string; externalPreview?: string;
isFetchScreenshot?: boolean; isExternalPreviewStale?: boolean;
isFetchPreview?: boolean;
isShowControls?: boolean; isShowControls?: boolean;
isUseInnerPanel?: boolean; isUseInnerPanel?: boolean;
setMode?: Dispatch<SetStateAction<boolean>> | null; setMode?: Dispatch<SetStateAction<boolean>> | null;
}; };
type PreviewProps = PreviewOptionalProps & { type PreviewProps = PreviewOptionalProps & {
uuid: string;
serverName: string | string[] | undefined; serverName: string | string[] | undefined;
serverUUID: string;
}; };
const PREVIEW_DEFAULT_PROPS: Required<PreviewOptionalProps> = { const PREVIEW_DEFAULT_PROPS: Required<PreviewOptionalProps> = {
externalPreview: '', externalPreview: '',
isFetchScreenshot: true, isExternalPreviewStale: false,
isFetchPreview: true,
isShowControls: true, isShowControls: true,
isUseInnerPanel: false, isUseInnerPanel: false,
setMode: null, setMode: null,
@ -62,36 +64,43 @@ const PreviewPanelHeader: FC<{ isUseInnerPanel: boolean; text: string }> = ({
const Preview: FC<PreviewProps> = ({ const Preview: FC<PreviewProps> = ({
externalPreview = PREVIEW_DEFAULT_PROPS.externalPreview, externalPreview = PREVIEW_DEFAULT_PROPS.externalPreview,
isFetchScreenshot = PREVIEW_DEFAULT_PROPS.isFetchScreenshot, isExternalPreviewStale = PREVIEW_DEFAULT_PROPS.isExternalPreviewStale,
isFetchPreview = PREVIEW_DEFAULT_PROPS.isFetchPreview,
isShowControls = PREVIEW_DEFAULT_PROPS.isShowControls, isShowControls = PREVIEW_DEFAULT_PROPS.isShowControls,
isUseInnerPanel = PREVIEW_DEFAULT_PROPS.isUseInnerPanel, isUseInnerPanel = PREVIEW_DEFAULT_PROPS.isUseInnerPanel,
serverName, serverName,
serverUUID,
setMode, setMode,
uuid,
}) => { }) => {
const [isPreviewStale, setIsPreviewStale] = useState<boolean>(false);
const [preview, setPreview] = useState<string>(''); const [preview, setPreview] = useState<string>('');
useEffect(() => { useEffect(() => {
if (isFetchScreenshot) { if (isFetchPreview) {
(async () => { (async () => {
try { try {
const response = await fetch(`${API_BASE_URL}/server/${uuid}?ss`, { const response = await fetch(
method: 'GET', `${API_BASE_URL}/server/${serverUUID}?ss`,
headers: { {
'Content-Type': 'application/json', method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}, },
}); );
const { screenshot: fetchedScreenshot } = await response.json(); const { screenshot: fetchedScreenshot } = await response.json();
setPreview(fetchedScreenshot); setPreview(fetchedScreenshot);
setIsPreviewStale(false);
} catch { } catch {
setPreview(''); setIsPreviewStale(true);
} }
})(); })();
} else if (externalPreview) { } else if (externalPreview) {
setPreview(externalPreview); setPreview(externalPreview);
setIsPreviewStale(isExternalPreviewStale);
} }
}, [externalPreview, isFetchScreenshot, uuid]); }, [externalPreview, isExternalPreviewStale, isFetchPreview, serverUUID]);
return ( return (
<PreviewPanel isUseInnerPanel={isUseInnerPanel}> <PreviewPanel isUseInnerPanel={isUseInnerPanel}>
@ -126,6 +135,7 @@ const Preview: FC<PreviewProps> = ({
src={`data:image/png;base64,${preview}`} src={`data:image/png;base64,${preview}`}
sx={{ sx={{
height: '100%', height: '100%',
opacity: isPreviewStale ? '0.4' : '1',
width: '100%', width: '100%',
}} }}
/> />

@ -34,17 +34,20 @@ const createServerPreviewContainer = (servers: ServerListItem[]) => (
}, },
}} }}
> >
{servers.map(({ screenshot, serverName, serverUUID }) => ( {servers.map(
<Preview ({ isScreenshotStale, screenshot, serverName, serverUUID }) => (
key={`server-preview-${serverUUID}`} <Preview
isFetchScreenshot={false} key={`server-preview-${serverUUID}`}
isShowControls={false} isExternalPreviewStale={isScreenshotStale}
isUseInnerPanel isFetchPreview={false}
externalPreview={screenshot} isShowControls={false}
serverName={serverName} isUseInnerPanel
uuid={serverUUID} externalPreview={screenshot}
/> serverName={serverName}
))} serverUUID={serverUUID}
/>
),
)}
</Box> </Box>
); );
@ -109,6 +112,7 @@ const Dashboard: FC = () => {
) )
.then(({ screenshot }) => { .then(({ screenshot }) => {
item.screenshot = screenshot; item.screenshot = screenshot;
item.isScreenshotStale = false;
const allServersWithScreenshots = [...serverListItems]; const allServersWithScreenshots = [...serverListItems];

@ -48,8 +48,8 @@ const Server = (): JSX.Element => {
<Box className={classes.preview}> <Box className={classes.preview}>
<Preview <Preview
setMode={setPreviewMode} setMode={setPreviewMode}
uuid={uuid}
serverName={server_name} serverName={server_name}
serverUUID={uuid}
/> />
</Box> </Box>
) : ( ) : (

Loading…
Cancel
Save