2021-02-17 17:51:57 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './snapshotTab.css';
|
2021-04-19 19:50:11 -07:00
|
|
|
import './tabbedPane.css';
|
2021-02-17 17:51:57 -08:00
|
|
|
import * as React from 'react';
|
2021-02-17 22:10:13 -08:00
|
|
|
import { useMeasure } from './helpers';
|
2022-03-25 13:12:00 -08:00
|
|
|
import type { ActionTraceEvent } from '@playwright-core/server/trace/common/traceEvents';
|
2022-02-07 17:05:42 -08:00
|
|
|
import { context } from './modelUtil';
|
2021-02-17 17:51:57 -08:00
|
|
|
|
|
|
|
export const SnapshotTab: React.FunctionComponent<{
|
2021-05-13 20:41:32 -07:00
|
|
|
action: ActionTraceEvent | undefined,
|
2021-11-23 11:36:18 -08:00
|
|
|
}> = ({ action }) => {
|
2021-02-17 17:51:57 -08:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
2021-07-28 18:21:55 +02:00
|
|
|
const [snapshotIndex, setSnapshotIndex] = React.useState(0);
|
2021-02-17 17:51:57 -08:00
|
|
|
|
2021-04-19 19:50:11 -07:00
|
|
|
const snapshotMap = new Map<string, { title: string, snapshotName: string }>();
|
2021-05-13 20:41:32 -07:00
|
|
|
for (const snapshot of action?.metadata.snapshots || [])
|
2021-04-19 19:50:11 -07:00
|
|
|
snapshotMap.set(snapshot.title, snapshot);
|
2021-04-20 00:19:17 -07:00
|
|
|
const actionSnapshot = snapshotMap.get('action') || snapshotMap.get('after');
|
2021-04-19 19:50:11 -07:00
|
|
|
const snapshots = [actionSnapshot ? { ...actionSnapshot, title: 'action' } : undefined, snapshotMap.get('before'), snapshotMap.get('after')].filter(Boolean) as { title: string, snapshotName: string }[];
|
|
|
|
|
2021-08-10 17:06:14 -07:00
|
|
|
let snapshotUrl = 'data:text/html,<body style="background: #ddd"></body>';
|
2021-11-02 16:35:23 -08:00
|
|
|
let snapshotInfoUrl: string | undefined;
|
2021-08-10 17:06:14 -07:00
|
|
|
let pointX: number | undefined;
|
|
|
|
let pointY: number | undefined;
|
|
|
|
if (action) {
|
|
|
|
const snapshot = snapshots[snapshotIndex];
|
|
|
|
if (snapshot && snapshot.snapshotName) {
|
2022-02-07 17:05:42 -08:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.set('trace', context(action).traceUrl);
|
|
|
|
params.set('name', snapshot.snapshotName);
|
|
|
|
snapshotUrl = new URL(`snapshot/${action.metadata.pageId}?${params.toString()}`, window.location.href).toString();
|
|
|
|
snapshotInfoUrl = new URL(`snapshotInfo/${action.metadata.pageId}?${params.toString()}`, window.location.href).toString();
|
2021-08-10 17:06:14 -07:00
|
|
|
if (snapshot.snapshotName.includes('action')) {
|
|
|
|
pointX = action.metadata.point?.x;
|
|
|
|
pointY = action.metadata.point?.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:21:55 +02:00
|
|
|
React.useEffect(() => {
|
2021-08-06 16:13:05 +02:00
|
|
|
if (snapshots.length >= 1 && snapshotIndex >= snapshots.length)
|
2021-07-28 18:21:55 +02:00
|
|
|
setSnapshotIndex(snapshots.length - 1);
|
|
|
|
}, [snapshotIndex, snapshots]);
|
2021-02-17 17:51:57 -08:00
|
|
|
|
2021-08-10 17:06:14 -07:00
|
|
|
const iframeRef = React.useRef<HTMLIFrameElement>(null);
|
2021-11-23 11:36:18 -08:00
|
|
|
const [snapshotInfo, setSnapshotInfo] = React.useState({ viewport: kDefaultViewport, url: '' });
|
2021-02-17 17:51:57 -08:00
|
|
|
React.useEffect(() => {
|
2021-08-10 17:06:14 -07:00
|
|
|
(async () => {
|
2021-11-02 16:35:23 -08:00
|
|
|
if (snapshotInfoUrl) {
|
|
|
|
const response = await fetch(snapshotInfoUrl);
|
|
|
|
const info = await response.json();
|
2021-11-08 18:03:10 -08:00
|
|
|
if (!info.error)
|
|
|
|
setSnapshotInfo(info);
|
2021-11-11 21:31:19 +01:00
|
|
|
} else {
|
2021-11-23 11:36:18 -08:00
|
|
|
setSnapshotInfo({ viewport: kDefaultViewport, url: '' });
|
2021-03-10 11:43:26 -08:00
|
|
|
}
|
2021-08-10 17:06:14 -07:00
|
|
|
if (!iframeRef.current)
|
|
|
|
return;
|
|
|
|
try {
|
2021-10-12 20:21:06 -08:00
|
|
|
iframeRef.current.src = snapshotUrl + (pointX === undefined ? '' : `&pointX=${pointX}&pointY=${pointY}`);
|
2021-08-10 17:06:14 -07:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
})();
|
2021-11-23 11:36:18 -08:00
|
|
|
}, [iframeRef, snapshotUrl, snapshotInfoUrl, pointX, pointY]);
|
2021-08-05 16:04:09 -07:00
|
|
|
|
2021-11-02 16:35:23 -08:00
|
|
|
const snapshotSize = snapshotInfo.viewport;
|
2021-10-12 13:42:50 -08:00
|
|
|
const scale = Math.min(measure.width / snapshotSize.width, measure.height / snapshotSize.height, 1);
|
2021-03-11 11:22:59 -08:00
|
|
|
const scaledSize = {
|
|
|
|
width: snapshotSize.width * scale,
|
|
|
|
height: snapshotSize.height * scale,
|
|
|
|
};
|
2021-04-19 11:09:50 -10:00
|
|
|
return <div
|
|
|
|
className='snapshot-tab'
|
|
|
|
tabIndex={0}
|
|
|
|
onKeyDown={event => {
|
2021-04-19 19:50:11 -07:00
|
|
|
if (event.key === 'ArrowRight')
|
2021-04-19 11:09:50 -10:00
|
|
|
setSnapshotIndex(Math.min(snapshotIndex + 1, snapshots.length - 1));
|
2021-04-19 19:50:11 -07:00
|
|
|
if (event.key === 'ArrowLeft')
|
2021-04-19 11:09:50 -10:00
|
|
|
setSnapshotIndex(Math.max(snapshotIndex - 1, 0));
|
|
|
|
}}
|
2021-04-19 19:50:11 -07:00
|
|
|
><div className='tab-strip'>
|
2021-04-08 05:32:12 +08:00
|
|
|
{snapshots.map((snapshot, index) => {
|
2021-04-19 19:50:11 -07:00
|
|
|
return <div className={'tab-element ' + (snapshotIndex === index ? ' selected' : '')}
|
|
|
|
onClick={() => setSnapshotIndex(index)}
|
|
|
|
key={snapshot.title}>
|
|
|
|
<div className='tab-label'>{renderTitle(snapshot.title)}</div>
|
2021-07-28 18:21:55 +02:00
|
|
|
</div>;
|
2021-04-19 19:50:11 -07:00
|
|
|
})}
|
|
|
|
</div>
|
2021-11-02 16:35:23 -08:00
|
|
|
<div className='snapshot-url' title={snapshotInfo.url}>{snapshotInfo.url}</div>
|
2021-02-17 17:51:57 -08:00
|
|
|
<div ref={ref} className='snapshot-wrapper'>
|
2022-02-01 09:26:38 -08:00
|
|
|
{ snapshots.length ? <div className='snapshot-container' style={{
|
2021-02-17 17:51:57 -08:00
|
|
|
width: snapshotSize.width + 'px',
|
|
|
|
height: snapshotSize.height + 'px',
|
2021-03-11 11:22:59 -08:00
|
|
|
transform: `translate(${-snapshotSize.width * (1 - scale) / 2 + (measure.width - scaledSize.width) / 2}px, ${-snapshotSize.height * (1 - scale) / 2 + (measure.height - scaledSize.height) / 2}px) scale(${scale})`,
|
2021-02-17 17:51:57 -08:00
|
|
|
}}>
|
2021-10-12 20:21:06 -08:00
|
|
|
<iframe ref={iframeRef} id='snapshot' name='snapshot'></iframe>
|
2022-02-01 09:26:38 -08:00
|
|
|
</div> : <div className='no-snapshot'>Action does not have snapshots</div>
|
|
|
|
}
|
2021-02-17 17:51:57 -08:00
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
};
|
2021-04-19 19:50:11 -07:00
|
|
|
|
|
|
|
function renderTitle(snapshotTitle: string): string {
|
|
|
|
if (snapshotTitle === 'before')
|
|
|
|
return 'Before';
|
|
|
|
if (snapshotTitle === 'after')
|
|
|
|
return 'After';
|
|
|
|
if (snapshotTitle === 'action')
|
|
|
|
return 'Action';
|
|
|
|
return snapshotTitle;
|
|
|
|
}
|
2021-11-23 11:36:18 -08:00
|
|
|
|
|
|
|
const kDefaultViewport = { width: 1280, height: 720 };
|