implement sepearte replay/heatmap enabled routes and collection
This commit is contained in:
1 parent
4b40be6b5a
commit
5bd92ce950
39 files changed
+586
-295
No files matched your search
@@ -1,14 +1,7 @@
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json } from '@/lib/response';
|
||||
import { getRecorderConfig } from '@/lib/recorder';
|
||||
import { getWebsite } from '@/queries/prisma';
|
||||
|
||||
interface ReplayConfig {
|
||||
sampleRate?: number;
|
||||
maskLevel?: string;
|
||||
maxDuration?: number;
|
||||
blockSelector?: string;
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
@@ -26,16 +19,19 @@ export async function GET(
|
||||
'Cache-Control': 'public, max-age=60, stale-while-revalidate=300',
|
||||
};
|
||||
|
||||
if (!website || !website.replayEnabled) {
|
||||
if (!website || !website.recorderEnabled) {
|
||||
return Response.json({ enabled: false }, { headers });
|
||||
}
|
||||
|
||||
const config = (website.replayConfig as ReplayConfig) || {};
|
||||
const config = getRecorderConfig(website.replayConfig);
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
enabled: true,
|
||||
replayEnabled: config.replayEnabled === true,
|
||||
heatmapEnabled: config.heatmapEnabled === true,
|
||||
sampleRate: config.sampleRate ?? 0.15,
|
||||
heatmapSampleRate: config.heatmapSampleRate ?? 0.15,
|
||||
maskLevel: config.maskLevel ?? 'moderate',
|
||||
maxDuration: config.maxDuration ?? 300000,
|
||||
blockSelector: config.blockSelector ?? '',
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { z } from 'zod';
|
||||
import type { Prisma } from '@/generated/prisma/client';
|
||||
import { ENTITY_TYPE } from '@/lib/constants';
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { getRecorderConfig, getRecorderEnabled } from '@/lib/recorder';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, json, ok, serverError, unauthorized } from '@/lib/response';
|
||||
import { canDeleteWebsite, canUpdateWebsite, canViewWebsite } from '@/permissions';
|
||||
@@ -42,10 +44,12 @@ export async function POST(
|
||||
name: z.string().optional(),
|
||||
domain: z.string().optional(),
|
||||
shareId: z.string().max(50).nullable().optional(),
|
||||
replayEnabled: z.boolean().optional(),
|
||||
replayConfig: z
|
||||
.object({
|
||||
replayEnabled: z.boolean().optional(),
|
||||
heatmapEnabled: z.boolean().optional(),
|
||||
sampleRate: z.number().min(0).max(1).optional(),
|
||||
heatmapSampleRate: z.number().min(0).max(1).optional(),
|
||||
maskLevel: z.enum(['strict', 'moderate']).optional(),
|
||||
maxDuration: z.number().int().positive().optional(),
|
||||
blockSelector: z.string().optional(),
|
||||
@@ -61,18 +65,35 @@ export async function POST(
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { name, domain, shareId, replayEnabled, replayConfig } = body;
|
||||
const { name, domain, shareId, replayConfig } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
try {
|
||||
const currentWebsite = await getWebsite(websiteId);
|
||||
|
||||
if (!currentWebsite) {
|
||||
return badRequest({ message: 'Website not found.' });
|
||||
}
|
||||
|
||||
const nextReplayConfig = getRecorderConfig(
|
||||
replayConfig === null
|
||||
? {}
|
||||
: {
|
||||
...getRecorderConfig(currentWebsite.replayConfig),
|
||||
...(replayConfig ?? {}),
|
||||
},
|
||||
);
|
||||
|
||||
const website = await updateWebsite(websiteId, {
|
||||
name,
|
||||
domain,
|
||||
...(replayEnabled !== undefined && { replayEnabled }),
|
||||
...(replayConfig !== undefined && { replayConfig }),
|
||||
...(replayConfig !== undefined && {
|
||||
replayConfig: nextReplayConfig as Prisma.InputJsonObject,
|
||||
recorderEnabled: getRecorderEnabled(nextReplayConfig),
|
||||
}),
|
||||
});
|
||||
|
||||
if (shareId === null) {
|
||||
|
||||
Reference in new issue
Block a user