saved session replays updates and polish

This commit is contained in:
Francis Cao committed 2026-03-09 15:00:36 -07:00
1 parent 1bffb3cbf3
commit a28ea79228
64 files changed
+170 -111

No files matched your search

@@ -69,7 +69,7 @@ export function ReplaysTable({ ...props }: DataTableProps) {
</TypeIcon>
)}
</DataColumn>
<DataColumn id="createdAt" label={t(labels.recordedAt)} width="160px">
<DataColumn id="createdAt" label={t(labels.recorded)} width="160px">
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
</DataColumn>
</DataTable>
@@ -1,5 +1,6 @@
import { Button, DataColumn, DataTable, type DataTableProps, Icon } from '@umami/react-zen';
import { Play } from 'lucide-react';
import { DateDistance } from '@/components/common/DateDistance';
import { useMessages, useNavigation } from '@/components/hooks';
export function SavedReplaysTable({ ...props }: DataTableProps) {
@@ -23,7 +24,7 @@ export function SavedReplaysTable({ ...props }: DataTableProps) {
<DataColumn id="name" label={t(labels.name)} />
<DataColumn id="visitId" label={t(labels.replayId)} />
<DataColumn id="createdAt" label={t(labels.created)} width="160px">
{(row: any) => new Date(row.createdAt).toLocaleString()}
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
</DataColumn>
</DataTable>
);
@@ -8,6 +8,7 @@ import { LoadingPanel } from '@/components/common/LoadingPanel';
import {
useMessages,
useReplayQuery,
useReplaySavedQuery,
useUpdateQuery,
useWebsiteSessionQuery,
} from '@/components/hooks';
@@ -18,19 +19,22 @@ import { ReplaySaveForm } from './ReplaySaveForm';
export function ReplayPlayback({
websiteId,
replayId,
showSessionInfo = true,
onClose,
}: {
websiteId: string;
replayId: string;
showSessionInfo?: boolean;
onClose?: () => void;
}) {
const { data: replay, isLoading, error } = useReplayQuery(websiteId, replayId);
const { data: replaySaved } = useReplaySavedQuery(websiteId, replayId);
const { data: session } = useWebsiteSessionQuery(websiteId, replay?.sessionId);
const { t, labels } = useMessages();
const [isSaved, setIsSaved] = useState<boolean | null>(null);
const { mutate } = useUpdateQuery(`/websites/${websiteId}/replays/${replayId}`);
const { mutate } = useUpdateQuery(`/websites/${websiteId}/replays/saved/${replayId}`);
const saved = isSaved ?? replay?.isSaved ?? false;
const saved = isSaved ?? replaySaved?.isSaved ?? false;
const handleUnsave = () => {
setIsSaved(false);
@@ -100,7 +104,7 @@ export function ReplayPlayback({
</Row>
)}
<ReplayPlayer events={replay.events} />
{session && <SessionInfo data={session} />}
{showSessionInfo && session && <SessionInfo data={session} />}
</Column>
)}
</LoadingPanel>
@@ -8,6 +8,7 @@ import {
TextField,
} from '@umami/react-zen';
import { useMessages, useUpdateQuery } from '@/components/hooks';
import { touch } from '@/components/hooks/useModified';
export function ReplaySaveForm({
websiteId,
@@ -22,7 +23,7 @@ export function ReplaySaveForm({
}) {
const { t, labels, getErrorMessage } = useMessages();
const { mutateAsync, error, isPending } = useUpdateQuery(
`/websites/${websiteId}/replays/${replayId}`,
`/websites/${websiteId}/replays/saved/${replayId}`,
);
const handleSubmit = async (formData: { name: string }) => {
@@ -30,6 +31,7 @@ export function ReplaySaveForm({
{ isSaved: true, name: formData.name },
{
onSuccess: () => {
touch('replays');
onSave?.();
onClose?.();
},
@@ -2,18 +2,27 @@
import { Column } from '@umami/react-zen';
import { useState } from 'react';
import { DataGrid } from '@/components/common/DataGrid';
import { useReplayQuery, useSessionReplaysQuery } from '@/components/hooks';
import { ReplayPlayer } from '../replays/[replayId]/ReplayPlayer';
import { useSessionReplaysQuery } from '@/components/hooks';
import { ReplayPlayback } from '../replays/[replayId]/ReplayPlayback';
import { SessionReplaysTable } from './SessionReplaysTable';
function InlinePlayer({ websiteId, replayId }: { websiteId: string; replayId: string }) {
const { data: replay } = useReplayQuery(websiteId, replayId);
if (!replay?.events?.length) return null;
function InlinePlayer({
websiteId,
replayId,
onClose,
}: {
websiteId: string;
replayId: string;
onClose: () => void;
}) {
return (
<div style={{ padding: '1.5rem 0' }}>
<ReplayPlayer events={replay.events} />
<ReplayPlayback
websiteId={websiteId}
replayId={replayId}
showSessionInfo={false}
onClose={onClose}
/>
</div>
);
}
@@ -34,7 +43,13 @@ export function SessionReplaysDataTable({
return (
<Column>
{selectedId && <InlinePlayer websiteId={websiteId} replayId={selectedId} />}
{selectedId && (
<InlinePlayer
websiteId={websiteId}
replayId={selectedId}
onClose={() => setSelectedId(undefined)}
/>
)}
<DataGrid query={queryResult} allowPaging>
{({ data }) => (
<SessionReplaysTable data={data} onPlay={handlePlay} selectedId={selectedId} />
@@ -36,7 +36,7 @@ export function SessionReplaysTable({
{(row: any) => formatDuration(row.duration || 0)}
</DataColumn>
<DataColumn id="eventCount" label={t(labels.actions)} width="80px" />
<DataColumn id="createdAt" label={t(labels.recordedAt)} width="160px">
<DataColumn id="createdAt" label={t(labels.recorded)} width="160px">
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
</DataColumn>
</DataTable>
@@ -1,12 +1,6 @@
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canUpdateWebsite, canViewWebsite } from '@/permissions';
import {
createReplaySaved,
deleteReplaySaved,
getReplaySaved,
} from '@/queries/prisma/sessionReplay';
import { canViewWebsite } from '@/permissions';
import { getReplayChunks } from '@/queries/sql';
export async function GET(
@@ -25,10 +19,7 @@ export async function GET(
return unauthorized();
}
const [chunks, isSaved] = await Promise.all([
getReplayChunks(websiteId, replayId),
getReplaySaved(websiteId, replayId),
]);
const chunks = await getReplayChunks(websiteId, replayId);
const allEvents = chunks.flatMap(chunk => chunk.events);
const sessionId = chunks.length > 0 ? chunks[0].sessionId : null;
@@ -42,35 +33,5 @@ export async function GET(
endedAt,
eventCount: allEvents.length,
chunkCount: chunks.length,
isSaved,
});
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ websiteId: string; replayId: string }> },
) {
const schema = z.object({
isSaved: z.boolean(),
name: z.string().max(100).optional(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { websiteId, replayId } = await params;
if (!(await canUpdateWebsite(auth, websiteId))) {
return unauthorized();
}
if (body.isSaved) {
await createReplaySaved(websiteId, replayId, body.name ?? '');
} else {
await deleteReplaySaved(websiteId, replayId);
}
return json({ ok: true });
}
@@ -0,0 +1,60 @@
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canUpdateWebsite, canViewWebsite } from '@/permissions';
import {
createReplaySaved,
deleteReplaySaved,
getReplaySaved,
} from '@/queries/prisma/sessionReplay';
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string; replayId: string }> },
) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { websiteId, replayId } = await params;
if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}
const isSaved = await getReplaySaved(websiteId, replayId);
return json({ isSaved });
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ websiteId: string; replayId: string }> },
) {
const schema = z.object({
isSaved: z.boolean(),
name: z.string().max(100).optional(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { websiteId, replayId } = await params;
if (!(await canUpdateWebsite(auth, websiteId))) {
return unauthorized();
}
if (body.isSaved) {
await createReplaySaved(websiteId, replayId, body.name ?? '');
} else {
await deleteReplaySaved(websiteId, replayId);
}
return json({ ok: true });
}