/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {FunctionComponent} from 'preact';
import {useEffect, useState} from 'preact/hooks';
import {NavigationIcon, SnapshotIcon, TimespanIcon} from './icons';
import {getFilmstripFrames, getScreenDimensions} from './util';
import {Util} from '../../shared/util.js';
const ANIMATION_FRAME_DURATION_MS = 500;
const Separator: FunctionComponent = () => {
return
;
};
const FlowStepIcon: FunctionComponent<{mode: LH.Result.GatherMode}> = ({mode}) => {
return <>
{
mode === 'navigation' &&
}
{
mode === 'timespan' &&
}
{
mode === 'snapshot' &&
}
>;
};
const FlowSegment: FunctionComponent<{mode?: LH.Result.GatherMode}> = ({mode}) => {
return (
);
};
const FlowStepAnimatedThumbnail: FunctionComponent<{
frames: Array<{data: string}>,
width: number,
height: number,
}> = ({frames, width, height}) => {
const [frameIndex, setFrameIndex] = useState(0);
// Handle a frame array of a different length being set.
const effectiveFrameIndex = frameIndex % frames.length;
useEffect(() => {
const interval = setInterval(
() => setFrameIndex(i => (i + 1) % frames.length),
ANIMATION_FRAME_DURATION_MS
);
return () => clearInterval(interval);
}, [frames.length]);
return (
);
};
const FlowStepThumbnail: FunctionComponent<{
lhr: LH.Result,
width?: number,
height?: number,
}> = ({lhr, width, height}) => {
const frames = getFilmstripFrames(lhr);
// Resize the image to fit the viewport aspect ratio.
const dimensions = getScreenDimensions(lhr);
if (width && height === undefined) {
height = dimensions.height * width / dimensions.width;
} else if (height && width === undefined) {
width = dimensions.width * height / dimensions.height;
}
if (!width || !height) {
// eslint-disable-next-line no-console
console.warn(new Error('FlowStepThumbnail requested without any dimensions').stack);
return <>>;
}
let thumbnail;
if (frames?.length) {
thumbnail = frames[frames.length - 1].data;
if (lhr.gatherMode === 'timespan') {
return ;
}
} else {
thumbnail = Util.getFullPageScreenshot(lhr)?.screenshot.data;
}
return <>
{
thumbnail &&
}
>;
};
export {
Separator,
FlowStepIcon,
FlowSegment,
FlowStepThumbnail,
};