-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dashboard improvement with animated screenshots
- Loading branch information
1 parent
c916541
commit 41f252b
Showing
9 changed files
with
766 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
openadapt/app/dashboard/components/FramePlayer/FramePlayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"use client" | ||
|
||
import { Recording } from '@/types/recording'; | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import { Stage, Layer, Image as KonvaImage, Group, Rect } from "react-konva"; | ||
import Konva from "konva"; | ||
|
||
interface FramePlayerProps { | ||
recording: Recording; | ||
frameRate: number; | ||
} | ||
|
||
export const FramePlayer: React.FC<FramePlayerProps> = ({ recording, frameRate }) => { | ||
const [currentScreenshotIndex, setCurrentScreenshotIndex] = useState(0); | ||
const [currentImage, setCurrentImage] = useState<HTMLImageElement | null>(null); | ||
const [isHovering, setIsHovering] = useState(false); | ||
const imageRef = useRef<Konva.Image>(null); | ||
const intervalRef = useRef<NodeJS.Timeout | null>(null); | ||
|
||
const STAGE_WIDTH = 300; | ||
const STAGE_HEIGHT = 200; | ||
const CORNER_RADIUS = 8; | ||
|
||
// Load initial image | ||
useEffect(() => { | ||
if (!recording?.screenshots?.length) return; | ||
|
||
const img = new window.Image(); | ||
img.src = recording.screenshots[0]; | ||
img.onload = () => { | ||
setCurrentImage(img); | ||
}; | ||
}, [recording]); | ||
|
||
useEffect(() => { | ||
if (!recording?.screenshots?.length ) { | ||
if (isHovering && currentScreenshotIndex !== 0) { | ||
setCurrentScreenshotIndex(0); | ||
} | ||
return; | ||
} | ||
|
||
intervalRef.current = setInterval(() => { | ||
setCurrentScreenshotIndex((prevIndex) => { | ||
return (prevIndex + 1) % recording.screenshots.length; | ||
}); | ||
}, 1000 / frameRate); | ||
|
||
return () => { | ||
if (intervalRef.current) { | ||
clearInterval(intervalRef.current); | ||
} | ||
}; | ||
}, [recording, frameRate, isHovering]); | ||
|
||
// Handle image loading when screenshot index changes | ||
useEffect(() => { | ||
if (!recording?.screenshots?.length) return; | ||
|
||
const img = new window.Image(); | ||
img.src = recording.screenshots[currentScreenshotIndex]; | ||
img.onload = () => { | ||
setCurrentImage(img); | ||
}; | ||
}, [currentScreenshotIndex, recording]); | ||
|
||
if (!recording?.screenshots?.length) { | ||
return ( | ||
<div className="flex items-center justify-center w-full h-[150px] bg-gray-100 rounded-lg"> | ||
<span className="text-sm text-gray-500">No screenshots</span> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div | ||
className="relative cursor-pointer" | ||
onMouseEnter={() => setIsHovering(true)} | ||
onMouseLeave={() => { | ||
setIsHovering(false); | ||
setCurrentScreenshotIndex(0); | ||
}} | ||
> | ||
<Stage width={STAGE_WIDTH} height={STAGE_HEIGHT}> | ||
<Layer> | ||
<Group | ||
clipFunc={(ctx) => { | ||
ctx.beginPath(); | ||
ctx.moveTo(CORNER_RADIUS, 0); | ||
ctx.lineTo(STAGE_WIDTH - CORNER_RADIUS, 0); | ||
ctx.quadraticCurveTo(STAGE_WIDTH, 0, STAGE_WIDTH, CORNER_RADIUS); | ||
ctx.lineTo(STAGE_WIDTH, STAGE_HEIGHT - CORNER_RADIUS); | ||
ctx.quadraticCurveTo(STAGE_WIDTH, STAGE_HEIGHT, STAGE_WIDTH - CORNER_RADIUS, STAGE_HEIGHT); | ||
ctx.lineTo(CORNER_RADIUS, STAGE_HEIGHT); | ||
ctx.quadraticCurveTo(0, STAGE_HEIGHT, 0, STAGE_HEIGHT - CORNER_RADIUS); | ||
ctx.lineTo(0, CORNER_RADIUS); | ||
ctx.quadraticCurveTo(0, 0, CORNER_RADIUS, 0); | ||
ctx.closePath(); | ||
}} | ||
> | ||
{currentImage && ( | ||
<KonvaImage | ||
ref={imageRef} | ||
image={currentImage} | ||
width={STAGE_WIDTH} | ||
height={STAGE_HEIGHT} | ||
listening={false} | ||
/> | ||
)} | ||
</Group> | ||
</Layer> | ||
</Stage> | ||
{!isHovering && ( | ||
<div className="absolute bottom-1 left-1 right-1 flex justify-between text-xs text-white bg-black/50 px-2 py-0.5 rounded w-fit"> | ||
<span>Frame {currentScreenshotIndex + 1}/{recording.screenshots.length}</span> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { FramePlayer } from './FramePlayer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
/** @type {import('next').NextConfig} */ | ||
// get values from environment variables | ||
const { DASHBOARD_SERVER_PORT } = process.env | ||
// Get values from environment variables | ||
const { DASHBOARD_SERVER_PORT } = process.env; | ||
|
||
const nextConfig = { | ||
rewrites: async () => { | ||
return [ | ||
{ | ||
source: '/', | ||
destination: `/recordings`, | ||
}, | ||
{ | ||
source: '/api/:path*', | ||
destination: `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/api/:path*` | ||
}, | ||
{ | ||
source: '/docs', | ||
destination: | ||
process.env.NODE_ENV === 'development' | ||
? `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/docs` | ||
: '/api/docs', | ||
}, | ||
{ | ||
source: '/openapi.json', | ||
destination: | ||
process.env.NODE_ENV === 'development' | ||
? `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/openapi.json` | ||
: '/api/openapi.json', | ||
}, | ||
] | ||
}, | ||
output: 'export', | ||
reactStrictMode: false, | ||
} | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/', | ||
destination: `/recordings`, | ||
}, | ||
{ | ||
source: '/api/:path*', | ||
destination: `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/api/:path*`, | ||
}, | ||
{ | ||
source: '/docs', | ||
destination: | ||
process.env.NODE_ENV === 'development' | ||
? `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/docs` | ||
: '/api/docs', | ||
}, | ||
{ | ||
source: '/openapi.json', | ||
destination: | ||
process.env.NODE_ENV === 'development' | ||
? `http://127.0.0.1:${DASHBOARD_SERVER_PORT}/openapi.json` | ||
: '/api/openapi.json', | ||
}, | ||
]; | ||
}, | ||
|
||
module.exports = nextConfig | ||
webpack: (config, { isServer }) => { | ||
if (!isServer) { | ||
// Prevent `canvas` from being bundled on the client side | ||
config.resolve.alias['canvas'] = false; | ||
} else { | ||
// Exclude canvas from the server bundle, treating it as an external dependency for Node | ||
config.externals.push({ canvas: 'commonjs canvas' }); | ||
} | ||
return config; | ||
}, | ||
|
||
output: 'export', | ||
reactStrictMode: false, | ||
}; | ||
|
||
module.exports = nextConfig; |
Oops, something went wrong.