fix(striker-ui): replace stale ss label with time-ago

main
Tsu-ba-me 1 year ago
parent eb523c739d
commit 673898260e
  1. 24
      striker-ui/components/Display/Preview.tsx
  2. 5
      striker-ui/pages/index.tsx

@ -21,10 +21,11 @@ import IconButton, { IconButtonProps } from '../IconButton';
import { InnerPanel, InnerPanelHeader, Panel, PanelHeader } from '../Panels'; import { InnerPanel, InnerPanelHeader, Panel, PanelHeader } from '../Panels';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { BodyText, HeaderText } from '../Text'; import { BodyText, HeaderText } from '../Text';
import { last } from '../../lib/time'; import { elapsed, last, now } from '../../lib/time';
type PreviewOptionalProps = { type PreviewOptionalProps = {
externalPreview?: string; externalPreview?: string;
externalTimestamp?: number;
headerEndAdornment?: ReactNode; headerEndAdornment?: ReactNode;
isExternalLoading?: boolean; isExternalLoading?: boolean;
isExternalPreviewStale?: boolean; isExternalPreviewStale?: boolean;
@ -46,6 +47,7 @@ const PREVIEW_DEFAULT_PROPS: Required<
> & > &
Pick<PreviewOptionalProps, 'onClickConnectButton' | 'onClickPreview'> = { Pick<PreviewOptionalProps, 'onClickConnectButton' | 'onClickPreview'> = {
externalPreview: '', externalPreview: '',
externalTimestamp: 0,
headerEndAdornment: null, headerEndAdornment: null,
isExternalLoading: false, isExternalLoading: false,
isExternalPreviewStale: false, isExternalPreviewStale: false,
@ -86,6 +88,7 @@ const PreviewPanelHeader: FC<{
const Preview: FC<PreviewProps> = ({ const Preview: FC<PreviewProps> = ({
externalPreview = PREVIEW_DEFAULT_PROPS.externalPreview, externalPreview = PREVIEW_DEFAULT_PROPS.externalPreview,
externalTimestamp = PREVIEW_DEFAULT_PROPS.externalTimestamp,
headerEndAdornment, headerEndAdornment,
isExternalLoading = PREVIEW_DEFAULT_PROPS.isExternalLoading, isExternalLoading = PREVIEW_DEFAULT_PROPS.isExternalLoading,
isExternalPreviewStale = PREVIEW_DEFAULT_PROPS.isExternalPreviewStale, isExternalPreviewStale = PREVIEW_DEFAULT_PROPS.isExternalPreviewStale,
@ -101,6 +104,7 @@ const Preview: FC<PreviewProps> = ({
const [isPreviewLoading, setIsPreviewLoading] = useState<boolean>(true); const [isPreviewLoading, setIsPreviewLoading] = useState<boolean>(true);
const [isPreviewStale, setIsPreviewStale] = useState<boolean>(false); const [isPreviewStale, setIsPreviewStale] = useState<boolean>(false);
const [preview, setPreview] = useState<string>(''); const [preview, setPreview] = useState<string>('');
const [previewTimstamp, setPreviewTimestamp] = useState<number>(0);
const previewButtonContent = useMemo( const previewButtonContent = useMemo(
() => () =>
@ -117,9 +121,16 @@ const Preview: FC<PreviewProps> = ({
width: '100%', width: '100%',
}} }}
/> />
{isPreviewStale && ( {isPreviewStale &&
<BodyText sx={{ position: 'absolute' }}>Outdated</BodyText> ((sst: number) => {
)} const { unit, value } = elapsed(now() - sst);
return (
<BodyText position="absolute">
Lost ~{value} {unit} ago
</BodyText>
);
})(previewTimstamp)}
</> </>
) : ( ) : (
<PowerSettingsNewOutlinedIcon <PowerSettingsNewOutlinedIcon
@ -130,7 +141,7 @@ const Preview: FC<PreviewProps> = ({
}} }}
/> />
), ),
[isPreviewStale, isUseInnerPanel, preview, serverState], [isPreviewStale, isUseInnerPanel, preview, previewTimstamp, serverState],
); );
useEffect(() => { useEffect(() => {
@ -145,6 +156,7 @@ const Preview: FC<PreviewProps> = ({
const { screenshot, timestamp } = data; const { screenshot, timestamp } = data;
setPreview(screenshot); setPreview(screenshot);
setPreviewTimestamp(timestamp);
setIsPreviewStale(!last(timestamp, 300)); setIsPreviewStale(!last(timestamp, 300));
} catch { } catch {
setIsPreviewStale(true); setIsPreviewStale(true);
@ -154,11 +166,13 @@ const Preview: FC<PreviewProps> = ({
})(); })();
} else if (!isExternalLoading) { } else if (!isExternalLoading) {
setPreview(externalPreview); setPreview(externalPreview);
setPreviewTimestamp(externalTimestamp);
setIsPreviewStale(isExternalPreviewStale); setIsPreviewStale(isExternalPreviewStale);
setIsPreviewLoading(false); setIsPreviewLoading(false);
} }
}, [ }, [
externalPreview, externalPreview,
externalTimestamp,
isExternalLoading, isExternalLoading,
isExternalPreviewStale, isExternalPreviewStale,
isFetchPreview, isFetchPreview,

@ -23,6 +23,7 @@ type ServerListItem = ServerOverviewMetadata & {
isScreenshotStale?: boolean; isScreenshotStale?: boolean;
loading?: boolean; loading?: boolean;
screenshot: string; screenshot: string;
timestamp: number;
}; };
const createServerPreviewContainer = ( const createServerPreviewContainer = (
@ -54,9 +55,11 @@ const createServerPreviewContainer = (
serverName, serverName,
serverState, serverState,
serverUUID, serverUUID,
timestamp,
}) => ( }) => (
<Preview <Preview
externalPreview={screenshot} externalPreview={screenshot}
externalTimestamp={timestamp}
headerEndAdornment={[ headerEndAdornment={[
<Link <Link
href={`/server?uuid=${serverUUID}&server_name=${serverName}&server_state=${serverState}`} href={`/server?uuid=${serverUUID}&server_name=${serverName}&server_state=${serverState}`}
@ -158,6 +161,7 @@ const Dashboard: FC = () => {
...serverOverview, ...serverOverview,
loading: true, loading: true,
screenshot: previousScreenshot, screenshot: previousScreenshot,
timestamp: 0,
}; };
fetchJSON<{ screenshot: string; timestamp: number }>( fetchJSON<{ screenshot: string; timestamp: number }>(
@ -169,6 +173,7 @@ const Dashboard: FC = () => {
item.isScreenshotStale = !last(timestamp, 300); item.isScreenshotStale = !last(timestamp, 300);
item.loading = false; item.loading = false;
item.screenshot = screenshot; item.screenshot = screenshot;
item.timestamp = timestamp;
const allServersWithScreenshots = [...serverListItems]; const allServersWithScreenshots = [...serverListItems];

Loading…
Cancel
Save