Skip to content

Commit

Permalink
reafactor(report): Optimize the style of the playground (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw authored Oct 22, 2024
1 parent ab815fe commit 388f22e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26,788 deletions.
4 changes: 2 additions & 2 deletions packages/visualizer/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
platform: 'browser',
outDir: 'dist/report',
minify: {
compress: true,
compress: !!process.env.CI,
},
define: {
__VERSION__: version,
Expand All @@ -36,7 +36,7 @@ export default defineConfig({
platform: 'browser',
outDir: 'dist/playground',
minify: {
compress: true,
compress: !!process.env.CI,
},
define: {
__VERSION__: JSON.stringify(version),
Expand Down
26,750 changes: 1 addition & 26,749 deletions packages/visualizer/scripts/fixture/demo-mobile-dump.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/visualizer/src/component/blackboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const Blackboard = (props: {
const canvasEl = domRef.current;
domRef.current.appendChild(app.canvas); // Ensure app.view is appended
const { clientWidth } = domRef.current.parentElement!;
const targetHeight = window.innerHeight * 0.7;
const targetHeight = window.innerHeight * 0.5;
const viewportRatio = clientWidth / targetHeight;
if (screenWidth / screenHeight <= viewportRatio) {
const ratio = targetHeight / screenHeight;
Expand Down
5 changes: 2 additions & 3 deletions packages/visualizer/src/component/detail-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
import { ConfigProvider, Segmented } from 'antd';
import { useEffect, useState } from 'react';
import Blackboard from './blackboard';
import OpenPlayground from './open-playground';
import Player from './player';
import SendToPlayground from './send-to-playground';

const ScreenshotItem = (props: { time: string; img: string }) => {
return (
Expand Down Expand Up @@ -165,7 +165,6 @@ const DetailPanel = (): JSX.Element => {
value: type,
};
});

return (
<div className="detail-panel">
<div className="view-switcher">
Expand All @@ -187,7 +186,7 @@ const DetailPanel = (): JSX.Element => {
}}
/>

<SendToPlayground context={insightDump?.context} />
<OpenPlayground context={insightDump?.context} />
</ConfigProvider>
</div>
<div className="detail-content">{content}</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SendOutlined } from '@ant-design/icons';
import { PlayCircleOutlined, SendOutlined } from '@ant-design/icons';
import type { UIContext } from '@midscene/core/.';
import { Button, Tooltip } from 'antd';
import { Button, Drawer, Tooltip } from 'antd';
import { useEffect, useState } from 'react';
import { Playground } from '../playground';

declare const __VERSION__: string;

Expand All @@ -10,8 +11,8 @@ export const serverBase = 'http://localhost:5800';
const errorMessageServerNotReady = `To debug the prompt together with this UI context, please follow the steps below:
1. Start the local playground server (Select one of the commands):
a. npx --yes @midscene/web
b. npx midscene-playground (Under the midscene project directory)
a. npx midscene-playground (Under the midscene project directory)
b. npx --yes @midscene/web
2. Click this button again.
`;

Expand Down Expand Up @@ -57,8 +58,10 @@ export const useServerValid = () => {
return serverValid;
};

export default function SendToPlayground(props?: { context?: UIContext }) {
export default function OpenPlayground(props?: { context?: UIContext }) {
const serverValid = useServerValid();
const [context, setContext] = useState<UIContext | undefined>();
const [isDrawerVisible, setIsDrawerVisible] = useState(false);

let ifPlaygroundValid = true;
let invalidReason: React.ReactNode = '';
Expand All @@ -70,22 +73,13 @@ export default function SendToPlayground(props?: { context?: UIContext }) {
invalidReason = errorMessageNoContext;
}

const launchPlayground = async () => {
// post a form to server, use a new window to open the playground
const showPlayground = () => {
setContext(props?.context || undefined);
setIsDrawerVisible(true);
};

const res = await fetch(`${serverBase}/playground-with-context`, {
method: 'POST',
body: JSON.stringify({
context: JSON.stringify(props?.context),
}),
headers: {
'Content-Type': 'application/json',
},
credentials: 'omit',
});
const data = await res.json();
const location = data.location;
window.open(`${serverBase}${location}`, '_blank');
const handleClose = () => {
setIsDrawerVisible(false);
};

if (!ifPlaygroundValid) {
Expand All @@ -105,15 +99,33 @@ export default function SendToPlayground(props?: { context?: UIContext }) {
}
overlayInnerStyle={{ width: '380px' }}
>
<Button disabled icon={<SendOutlined />}>
Send to Playground
<Button disabled icon={<PlayCircleOutlined />}>
Open in Playground
</Button>
</Tooltip>
);
}
return (
<Button onClick={launchPlayground} icon={<SendOutlined />}>
Send to Playground
</Button>
<>
<Button onClick={showPlayground} icon={<PlayCircleOutlined />}>
Open in Playground
</Button>
<Drawer
title={null}
placement="right"
onClose={handleClose}
open={isDrawerVisible}
width="90%"
styles={{
header: { display: 'none' },
}}
>
<Playground
propsContext={context}
hideLogo={true}
key={Math.random().toString(36).substring(7)}
/>
</Drawer>
</>
);
}
1 change: 1 addition & 0 deletions packages/visualizer/src/playground.less
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ body {
display: flex;
flex-direction: column;
justify-content: space-between;
overflow-y: auto !important;

.ant-form {
flex-grow: 1;
Expand Down
23 changes: 15 additions & 8 deletions packages/visualizer/src/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { allScriptsFromDump } from './component/replay-scripts';

import './playground.less';
import Logo from './component/logo';
import { serverBase, useServerValid } from './component/send-to-playground';
import { serverBase, useServerValid } from './component/open-playground';

const requestPlaygroundServer = async (
context: UIContext,
Expand Down Expand Up @@ -53,11 +53,16 @@ const useContextId = () => {
const match = path.match(/^\/playground\/([a-zA-Z0-9-]+)$/);
return match ? match[1] : null;
};

const { TextArea } = Input;
function Playground() {

export function Playground({
propsContext,
hideLogo,
}: { propsContext?: UIContext; hideLogo?: boolean }) {
const contextId = useContextId();
const [uiContext, setUiContext] = useState<UIContext | null>(null);
const [uiContext, setUiContext] = useState<UIContext | null>(
propsContext || null,
);
const [loading, setLoading] = useState(false);
const [result, setResult] = useState<{
result: any;
Expand All @@ -73,7 +78,7 @@ function Playground() {
const serverValid = useServerValid();

useEffect(() => {
if (contextId) {
if (!uiContext && contextId) {
fetch(`${serverBase}/context/${contextId}`)
.then((res) => res.json())
.then((data) => {
Expand Down Expand Up @@ -200,9 +205,11 @@ function Playground() {
minSize={20}
className="playground-left-panel"
>
<div className="playground-header">
<Logo />
</div>
{!hideLogo && (
<div className="playground-header">
<Logo />
</div>
)}

<Form
form={form}
Expand Down

0 comments on commit 388f22e

Please sign in to comment.