saved session replay implementation checkpoint

This commit is contained in:
Francis Cao committed 2026-03-09 13:51:08 -07:00
1 parent 81fd8e63a1
commit 1bffb3cbf3
21 files changed
+392 -78

No files matched your search

@@ -1,17 +1,42 @@
'use client';
import { Column } from '@umami/react-zen';
import { Column, Tab, TabList, TabPanel, Tabs } from '@umami/react-zen';
import { type Key, useState } from 'react';
import { SessionModal } from '@/app/(main)/websites/[websiteId]/sessions/SessionModal';
import { WebsiteControls } from '@/app/(main)/websites/[websiteId]/WebsiteControls';
import { Panel } from '@/components/common/Panel';
import { useMessages } from '@/components/hooks';
import { getItem, setItem } from '@/lib/storage';
import { ReplayModal } from './ReplayModal';
import { ReplaysDataTable } from './ReplaysDataTable';
import { SavedReplaysDataTable } from './SavedReplaysDataTable';
const KEY_NAME = 'umami.replays.tab';
export function ReplaysPage({ websiteId }: { websiteId: string }) {
const [tab, setTab] = useState(getItem(KEY_NAME) || 'replays');
const { t, labels } = useMessages();
const handleSelect = (value: Key) => {
setItem(KEY_NAME, value);
setTab(value);
};
return (
<Column gap="3">
<WebsiteControls websiteId={websiteId} />
<Panel>
<ReplaysDataTable websiteId={websiteId} />
<Tabs selectedKey={tab} onSelectionChange={handleSelect}>
<TabList>
<Tab id="replays">{t(labels.replays)}</Tab>
<Tab id="saved">{t(labels.saved)}</Tab>
</TabList>
<TabPanel id="replays">
<ReplaysDataTable websiteId={websiteId} />
</TabPanel>
<TabPanel id="saved">
<SavedReplaysDataTable websiteId={websiteId} />
</TabPanel>
</Tabs>
</Panel>
<SessionModal websiteId={websiteId} />
<ReplayModal websiteId={websiteId} />
@@ -69,7 +69,7 @@ export function ReplaysTable({ ...props }: DataTableProps) {
</TypeIcon>
)}
</DataColumn>
<DataColumn id="createdAt" label={t(labels.recordedAt)} width="140px">
<DataColumn id="createdAt" label={t(labels.recordedAt)} width="160px">
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
</DataColumn>
</DataTable>
@@ -0,0 +1,15 @@
import { DataGrid } from '@/components/common/DataGrid';
import { useSavedReplaysQuery } from '@/components/hooks';
import { SavedReplaysTable } from './SavedReplaysTable';
export function SavedReplaysDataTable({ websiteId }: { websiteId: string }) {
const queryResult = useSavedReplaysQuery(websiteId);
return (
<DataGrid query={queryResult} allowPaging allowSearch>
{({ data }) => {
return <SavedReplaysTable data={data} />;
}}
</DataGrid>
);
}
@@ -0,0 +1,30 @@
import { Button, DataColumn, DataTable, type DataTableProps, Icon } from '@umami/react-zen';
import { Play } from 'lucide-react';
import { useMessages, useNavigation } from '@/components/hooks';
export function SavedReplaysTable({ ...props }: DataTableProps) {
const { t, labels } = useMessages();
const { router, updateParams } = useNavigation();
return (
<DataTable {...props}>
<DataColumn id="play" label="" width="80px">
{(row: any) => (
<Button
variant="quiet"
onClick={() => router.push(updateParams({ replay: row.visitId }))}
>
<Icon>
<Play />
</Icon>
</Button>
)}
</DataColumn>
<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()}
</DataColumn>
</DataTable>
);
}
@@ -1,11 +1,19 @@
'use client';
import { Button, Column, Icon, Row, Text } from '@umami/react-zen';
import { X } from 'lucide-react';
import { Button, Column, Dialog, DialogTrigger, Icon, Popover, Row, Text } from '@umami/react-zen';
import { Bookmark, X } from 'lucide-react';
import { useState } from 'react';
import { SessionInfo } from '@/app/(main)/websites/[websiteId]/sessions/SessionInfo';
import { Avatar } from '@/components/common/Avatar';
import { LoadingPanel } from '@/components/common/LoadingPanel';
import { useMessages, useReplayQuery, useWebsiteSessionQuery } from '@/components/hooks';
import {
useMessages,
useReplayQuery,
useUpdateQuery,
useWebsiteSessionQuery,
} from '@/components/hooks';
import { touch } from '@/components/hooks/useModified';
import { ReplayPlayer } from './ReplayPlayer';
import { ReplaySaveForm } from './ReplaySaveForm';
export function ReplayPlayback({
websiteId,
@@ -19,6 +27,15 @@ export function ReplayPlayback({
const { data: replay, isLoading, error } = useReplayQuery(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 saved = isSaved ?? replay?.isSaved ?? false;
const handleUnsave = () => {
setIsSaved(false);
mutate({ isSaved: false }, { onSuccess: () => touch('replays') });
};
return (
<LoadingPanel
@@ -41,13 +58,45 @@ export function ReplayPlayback({
</Text>
</Column>
</Row>
{onClose && (
<Button onPress={onClose} variant="quiet">
<Icon>
<X />
</Icon>
</Button>
)}
<Row gap="2">
{saved ? (
<Button onPress={handleUnsave} variant="quiet">
<Icon>
<Bookmark fill="currentColor" />
</Icon>
</Button>
) : (
<DialogTrigger>
<Button variant="quiet">
<Icon>
<Bookmark fill="none" />
</Icon>
</Button>
<Popover placement="bottom end">
<Dialog title={t(labels.saveReplay)} style={{ width: '300px' }}>
{({ close }) => (
<ReplaySaveForm
websiteId={websiteId}
replayId={replayId}
onSave={() => {
setIsSaved(true);
touch('replays');
}}
onClose={close}
/>
)}
</Dialog>
</Popover>
</DialogTrigger>
)}
{onClose && (
<Button onPress={onClose} variant="quiet">
<Icon>
<X />
</Icon>
</Button>
)}
</Row>
</Row>
)}
<ReplayPlayer events={replay.events} />
@@ -0,0 +1,55 @@
'use client';
import {
Button,
Form,
FormButtons,
FormField,
FormSubmitButton,
TextField,
} from '@umami/react-zen';
import { useMessages, useUpdateQuery } from '@/components/hooks';
export function ReplaySaveForm({
websiteId,
replayId,
onSave,
onClose,
}: {
websiteId: string;
replayId: string;
onSave?: () => void;
onClose?: () => void;
}) {
const { t, labels, getErrorMessage } = useMessages();
const { mutateAsync, error, isPending } = useUpdateQuery(
`/websites/${websiteId}/replays/${replayId}`,
);
const handleSubmit = async (formData: { name: string }) => {
await mutateAsync(
{ isSaved: true, name: formData.name },
{
onSuccess: () => {
onSave?.();
onClose?.();
},
},
);
};
return (
<Form onSubmit={handleSubmit} error={getErrorMessage(error)}>
<FormField name="name" label={t(labels.name)} rules={{ required: t(labels.required) }}>
<TextField autoFocus />
</FormField>
<FormButtons>
<Button isDisabled={isPending} onPress={onClose}>
{t(labels.cancel)}
</Button>
<FormSubmitButton variant="primary" isDisabled={isPending}>
{t(labels.save)}
</FormSubmitButton>
</FormButtons>
</Form>
);
}
@@ -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="140px">
<DataColumn id="createdAt" label={t(labels.recordedAt)} width="160px">
{(row: any) => <DateDistance date={new Date(row.createdAt)} />}
</DataColumn>
</DataTable>
@@ -1,6 +1,12 @@
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { canViewWebsite } from '@/permissions';
import { canUpdateWebsite, canViewWebsite } from '@/permissions';
import {
createReplaySaved,
deleteReplaySaved,
getReplaySaved,
} from '@/queries/prisma/sessionReplay';
import { getReplayChunks } from '@/queries/sql';
export async function GET(
@@ -19,7 +25,10 @@ export async function GET(
return unauthorized();
}
const chunks = await getReplayChunks(websiteId, replayId);
const [chunks, isSaved] = await Promise.all([
getReplayChunks(websiteId, replayId),
getReplaySaved(websiteId, replayId),
]);
const allEvents = chunks.flatMap(chunk => chunk.events);
const sessionId = chunks.length > 0 ? chunks[0].sessionId : null;
@@ -33,5 +42,35 @@ 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,32 @@
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { pagingParams, searchParams } from '@/lib/schema';
import { canViewWebsite } from '@/permissions';
import { getSavedReplays } from '@/queries/prisma/sessionReplay';
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
) {
const schema = z.object({
...pagingParams,
...searchParams,
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { websiteId } = await params;
if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}
const data = await getSavedReplays(websiteId, query);
return json(data);
}