Normalize replay events for rrweb-player playback
This commit is contained in:
1 parent
a5f78e3e3d
commit
95ece5ba56
7 files changed
+278
-56
No files matched your search
@@ -7,5 +7,5 @@ export default async function ({
|
||||
}) {
|
||||
const { websiteId, sessionId } = await params;
|
||||
|
||||
return <ReplayModal websiteId={websiteId} sessionId={sessionId} />;
|
||||
return <ReplayModal websiteId={websiteId} replayId={sessionId} />;
|
||||
}
|
||||
@@ -2,21 +2,32 @@
|
||||
import { Column, Dialog, Modal, type ModalProps } from '@umami/react-zen';
|
||||
import { ReplayPlayback } from '@/app/(main)/websites/[websiteId]/replays/[replayId]/ReplayPlayback';
|
||||
import { useNavigation } from '@/components/hooks';
|
||||
import { buildPath } from '@/lib/url';
|
||||
|
||||
export interface ReplayModalProps extends ModalProps {
|
||||
websiteId: string;
|
||||
replayId?: string;
|
||||
}
|
||||
|
||||
export function ReplayModal({ websiteId, ...props }: ReplayModalProps) {
|
||||
export function ReplayModal({ websiteId, replayId, ...props }: ReplayModalProps) {
|
||||
const {
|
||||
router,
|
||||
query: { replay },
|
||||
searchParams,
|
||||
updateParams,
|
||||
} = useNavigation();
|
||||
const activeReplayId = replayId || replay;
|
||||
|
||||
const handleOpenChange = (isOpen: boolean) => {
|
||||
if (!isOpen) {
|
||||
router.push(updateParams({ replay: undefined }));
|
||||
if (replayId) {
|
||||
const query = Object.fromEntries(searchParams.entries());
|
||||
delete query.replay;
|
||||
|
||||
router.push(buildPath(`/websites/${websiteId}/replays`, query));
|
||||
} else {
|
||||
router.push(updateParams({ replay: undefined }));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,7 +35,7 @@ export function ReplayModal({ websiteId, ...props }: ReplayModalProps) {
|
||||
<Modal
|
||||
placement="bottom"
|
||||
offset="80px"
|
||||
isOpen={!!replay}
|
||||
isOpen={!!activeReplayId}
|
||||
onOpenChange={handleOpenChange}
|
||||
isDismissable
|
||||
{...props}
|
||||
@@ -33,7 +44,9 @@ export function ReplayModal({ websiteId, ...props }: ReplayModalProps) {
|
||||
<Dialog variant="sheet">
|
||||
{({ close }) => (
|
||||
<Column padding="6">
|
||||
<ReplayPlayback websiteId={websiteId} replayId={replay} onClose={close} />
|
||||
{activeReplayId && (
|
||||
<ReplayPlayback websiteId={websiteId} replayId={activeReplayId} onClose={close} />
|
||||
)}
|
||||
</Column>
|
||||
)}
|
||||
</Dialog>
|
||||
|
||||
@@ -1,71 +1,124 @@
|
||||
'use client';
|
||||
import { Column } from '@umami/react-zen';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Empty } from '@/components/common/Empty';
|
||||
import { useMobile } from '@/components/hooks';
|
||||
import { hasReplayFullSnapshot } from '@/lib/replay';
|
||||
import { getReplayPlayerEvents, getReplayViewport } from '@/lib/replay';
|
||||
import 'rrweb-player/dist/style.css';
|
||||
|
||||
const DEFAULT_REPLAY_ASPECT_RATIO = 9 / 16;
|
||||
|
||||
export function ReplayPlayer({ events }: { events: any[] }) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const playerRootRef = useRef<HTMLDivElement>(null);
|
||||
const playerRef = useRef<any>(null);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [playerError, setPlayerError] = useState(false);
|
||||
const { isMobile, isPhone } = useMobile();
|
||||
const canReplay = hasReplayFullSnapshot(events);
|
||||
const showUnavailable = !events?.length || !canReplay;
|
||||
const replayEvents = useMemo(() => getReplayPlayerEvents(events), [events]);
|
||||
const replayViewport = useMemo(() => getReplayViewport(replayEvents), [replayEvents]);
|
||||
const canReplay = replayEvents.length >= 2;
|
||||
const showUnavailable = !events?.length || !canReplay || playerError;
|
||||
|
||||
const playerWidth = isPhone ? 360 : isMobile ? 640 : 1024;
|
||||
const playerHeight = isPhone ? 202 : isMobile ? 360 : 576;
|
||||
const replayAspectRatio = replayViewport
|
||||
? replayViewport.height / replayViewport.width
|
||||
: DEFAULT_REPLAY_ASPECT_RATIO;
|
||||
const playerHeight = Math.round(
|
||||
playerWidth * Math.min(Math.max(replayAspectRatio, DEFAULT_REPLAY_ASPECT_RATIO), 1.5),
|
||||
);
|
||||
const playerOuterHeight = playerHeight + 80;
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef.current || !events?.length || !canReplay) return;
|
||||
let cancelled = false;
|
||||
const playerRoot = playerRootRef.current;
|
||||
|
||||
import('rrweb-player').then(mod => {
|
||||
const RRWebPlayer = mod.default;
|
||||
setPlayerError(false);
|
||||
|
||||
if (containerRef.current) {
|
||||
containerRef.current.innerHTML = '';
|
||||
}
|
||||
if (playerRef.current) {
|
||||
playerRef.current.$destroy?.();
|
||||
playerRef.current = null;
|
||||
}
|
||||
|
||||
playerRef.current = new RRWebPlayer({
|
||||
target: containerRef.current,
|
||||
props: {
|
||||
events: events,
|
||||
width: playerWidth,
|
||||
height: playerHeight,
|
||||
autoPlay: false,
|
||||
showController: true,
|
||||
speedOption: [1, 2, 4, 8],
|
||||
useVirtualDom: false,
|
||||
showWarning: false,
|
||||
},
|
||||
playerRoot?.replaceChildren();
|
||||
|
||||
if (!playerRoot || !canReplay) return;
|
||||
|
||||
import('rrweb-player')
|
||||
.then(mod => {
|
||||
if (cancelled) return;
|
||||
|
||||
const RRWebPlayer = mod.default;
|
||||
|
||||
playerRoot.replaceChildren();
|
||||
|
||||
try {
|
||||
const player = new RRWebPlayer({
|
||||
target: playerRoot,
|
||||
props: {
|
||||
events: replayEvents,
|
||||
width: playerWidth,
|
||||
height: playerHeight,
|
||||
autoPlay: false,
|
||||
showController: true,
|
||||
speedOption: [1, 2, 4, 8],
|
||||
useVirtualDom: false,
|
||||
showWarning: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (cancelled) {
|
||||
(player as any).$destroy?.();
|
||||
return;
|
||||
}
|
||||
|
||||
playerRef.current = player;
|
||||
} catch {
|
||||
playerRoot.replaceChildren();
|
||||
|
||||
if (!cancelled) {
|
||||
setPlayerError(true);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) {
|
||||
setPlayerError(true);
|
||||
}
|
||||
});
|
||||
|
||||
setLoaded(true);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
|
||||
if (playerRef.current) {
|
||||
playerRef.current.$destroy?.();
|
||||
playerRef.current = null;
|
||||
}
|
||||
|
||||
playerRoot.replaceChildren();
|
||||
};
|
||||
}, [canReplay, events, playerWidth, playerHeight]);
|
||||
}, [canReplay, replayEvents, playerWidth, playerHeight]);
|
||||
|
||||
return (
|
||||
<Column alignItems="center">
|
||||
<Column alignItems="center" width="100%">
|
||||
<div
|
||||
ref={containerRef}
|
||||
style={{
|
||||
width: playerWidth,
|
||||
minHeight: loaded && canReplay ? undefined : playerHeight,
|
||||
maxWidth: '100%',
|
||||
overflow: 'hidden',
|
||||
width: '100%',
|
||||
maxWidth: playerWidth,
|
||||
height: showUnavailable ? playerHeight : playerOuterHeight,
|
||||
overflowX: showUnavailable ? 'hidden' : 'auto',
|
||||
overflowY: 'hidden',
|
||||
borderRadius: '8px',
|
||||
border: '1px solid var(--base300)',
|
||||
background: 'var(--base75)',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={playerRootRef}
|
||||
style={{
|
||||
display: showUnavailable ? 'none' : 'block',
|
||||
width: playerWidth,
|
||||
height: playerOuterHeight,
|
||||
}}
|
||||
/>
|
||||
{showUnavailable && <Empty message="Replay unavailable." />}
|
||||
</div>
|
||||
</Column>
|
||||
|
||||
Reference in new issue
Block a user