saved session replay implementation checkpoint
This commit is contained in:
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);
|
||||
}
|
||||
@@ -35,6 +35,7 @@ export * from './queries/useReportQuery';
|
||||
export * from './queries/useReportsQuery';
|
||||
export * from './queries/useResultQuery';
|
||||
export * from './queries/useRevenueSessionsQuery';
|
||||
export * from './queries/useSavedReplaysQuery';
|
||||
export * from './queries/useSessionActivityQuery';
|
||||
export * from './queries/useSessionDataPropertiesQuery';
|
||||
export * from './queries/useSessionDataQuery';
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useApi } from '../useApi';
|
||||
import { useModified } from '../useModified';
|
||||
import { usePagedQuery } from '../usePagedQuery';
|
||||
|
||||
export function useSavedReplaysQuery(websiteId: string) {
|
||||
const { get } = useApi();
|
||||
const { modified } = useModified('replays');
|
||||
|
||||
return usePagedQuery({
|
||||
queryKey: ['replays:saved', { websiteId, modified }],
|
||||
queryFn: pageParams => {
|
||||
return get(`/websites/${websiteId}/replays/saved`, {
|
||||
...pageParams,
|
||||
pageSize: 20,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -343,6 +343,7 @@ export const labels: Record<string, string> = {
|
||||
version: 'label.version',
|
||||
saveSegment: 'label.save-segment',
|
||||
saveCohort: 'label.save-cohort',
|
||||
saveReplay: 'label.save-replay',
|
||||
analysis: 'label.analysis',
|
||||
destinationUrl: 'label.destination-url',
|
||||
slug: 'label.slug',
|
||||
@@ -366,6 +367,7 @@ export const labels: Record<string, string> = {
|
||||
sampleSize: 'label.sample-size',
|
||||
play: 'label.play',
|
||||
replays: 'label.replays',
|
||||
saved: 'label.saved',
|
||||
replay: 'label.replay',
|
||||
replayId: 'label.replay-id',
|
||||
replayEnabled: 'label.replay-enabled',
|
||||
|
||||
@@ -72,7 +72,7 @@ export function ListTable({
|
||||
alignItems="center"
|
||||
justifyContent="space-between"
|
||||
paddingLeft="2"
|
||||
columns={`1fr ${showPercentage ? '100px' : '150px'}`}
|
||||
columns={'1fr 100px'}
|
||||
>
|
||||
<Text weight="bold">{title}</Text>
|
||||
<Text weight="bold" align="center">
|
||||
@@ -117,7 +117,7 @@ const AnimatedRow = ({
|
||||
|
||||
return (
|
||||
<Grid
|
||||
columns="1fr 50px 50px"
|
||||
columns={showPercentage ? '1fr 50px 50px' : '1fr 100px'}
|
||||
paddingLeft="2"
|
||||
alignItems="center"
|
||||
borderRadius
|
||||
@@ -129,7 +129,11 @@ const AnimatedRow = ({
|
||||
{label}
|
||||
</Text>
|
||||
</Row>
|
||||
<Row alignItems="center" height="30px" justifyContent="flex-end">
|
||||
<Row
|
||||
alignItems="center"
|
||||
height="30px"
|
||||
justifyContent={showPercentage ? 'flex-end' : 'center'}
|
||||
>
|
||||
{change}
|
||||
<Text weight="bold">
|
||||
<AnimatedDiv title={props?.y as any}>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import prisma from '@/lib/prisma';
|
||||
import type { QueryFilters } from '@/lib/types';
|
||||
|
||||
export interface CreateReplayChunkArgs {
|
||||
websiteId: string;
|
||||
@@ -62,3 +63,49 @@ export async function deleteReplaysByWebsite(websiteId: string) {
|
||||
where: { websiteId },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getReplaySaved(websiteId: string, visitId: string): Promise<boolean> {
|
||||
const record = await prisma.client.sessionReplaySaved.findUnique({
|
||||
where: { websiteId_visitId: { websiteId, visitId } },
|
||||
select: { id: true },
|
||||
});
|
||||
return record !== null;
|
||||
}
|
||||
|
||||
export async function createReplaySaved(websiteId: string, visitId: string, name: string) {
|
||||
return prisma.client.sessionReplaySaved.create({
|
||||
data: { id: uuid(), websiteId, visitId, name },
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateReplaySaved(websiteId: string, visitId: string, name: string) {
|
||||
return prisma.client.sessionReplaySaved.updateMany({
|
||||
where: { websiteId, visitId },
|
||||
data: { name },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteReplaySaved(websiteId: string, visitId: string) {
|
||||
return prisma.client.sessionReplaySaved.deleteMany({
|
||||
where: { websiteId, visitId },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getSavedReplays(websiteId: string, filters: QueryFilters) {
|
||||
const { search } = filters;
|
||||
const { getSearchParameters, pagedQuery } = prisma;
|
||||
|
||||
const where = {
|
||||
websiteId,
|
||||
...getSearchParameters(search, [{ name: 'contains' }, { visitId: 'contains' }]),
|
||||
};
|
||||
|
||||
return pagedQuery(
|
||||
'sessionReplaySaved',
|
||||
{
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
},
|
||||
filters,
|
||||
);
|
||||
}
|
||||
@@ -136,6 +136,10 @@ export async function resetWebsite(websiteId: string) {
|
||||
|
||||
return transaction(
|
||||
async tx => {
|
||||
await tx.sessionReplaySaved.deleteMany({
|
||||
where: { websiteId },
|
||||
});
|
||||
|
||||
await tx.sessionReplay.deleteMany({
|
||||
where: { websiteId },
|
||||
});
|
||||
@@ -187,6 +191,10 @@ export async function deleteWebsite(websiteId: string) {
|
||||
|
||||
return transaction(
|
||||
async tx => {
|
||||
await tx.sessionReplaySaved.deleteMany({
|
||||
where: { websiteId },
|
||||
});
|
||||
|
||||
await tx.sessionReplay.deleteMany({
|
||||
where: { websiteId },
|
||||
});
|
||||
|
||||
@@ -132,7 +132,7 @@ async function clickhouseQuery(
|
||||
),
|
||||
user_activities AS (
|
||||
select distinct
|
||||
website_event.session_id,
|
||||
website_event.session_id as session_id,
|
||||
toInt32((${getDateSQL('created_at', unit, timezone)} - cohort_items.cohort_date) / 86400) as day_number
|
||||
from website_event
|
||||
join cohort_items
|
||||
|
||||
Reference in new issue
Block a user