heatmap migration/code clean-up

This commit is contained in:
Francis Cao committed 2026-06-23 15:01:00 -07:00
1 parent 3888ec6f4f
commit 8a0bdc2727
13 files changed
+49 -1326

No files matched your search

@@ -14,7 +14,6 @@ import { Laptop, Monitor, Smartphone, Tablet } from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { LoadingPanel } from '@/components/common/LoadingPanel';
import { useResultQuery } from '@/components/hooks';
import { getClientAuthToken } from '@/lib/client';
import { formatLongNumber } from '@/lib/format';
import type { HeatmapMode, HeatmapPoint, HeatmapResult, HeatmapSnapshot } from '@/queries/sql';
import styles from './Heatmap.module.css';
@@ -882,90 +881,14 @@ function SnapshotPreview({
snapshot: HeatmapSnapshot;
onReady: () => void;
}) {
if (snapshot.kind === 'iframe') {
return <IframeSnapshot snapshot={snapshot} onReady={onReady} />;
}
return <SnapshotImage snapshot={snapshot} onReady={onReady} />;
}
function SnapshotImage({
snapshot,
onReady,
}: {
snapshot: Extract<HeatmapSnapshot, { kind: 'image' }>;
onReady: () => void;
}) {
const [src, setSrc] = useState<string | null>(null);
const imageUrl = snapshot.imageUrl;
useEffect(() => {
if (!imageUrl) {
setSrc(null);
onReady();
return;
}
const controller = new AbortController();
const token = getClientAuthToken();
let objectUrl: string | null = null;
setSrc(null);
fetch(imageUrl, {
signal: controller.signal,
headers: {
...(token ? { authorization: `Bearer ${token}` } : {}),
},
})
.then(async response => {
if (!response.ok) {
throw new Error(`Snapshot image request failed: ${response.status}`);
}
const blob = await response.blob();
objectUrl = URL.createObjectURL(blob);
setSrc(objectUrl);
})
.catch(() => {
setSrc(null);
onReady();
});
return () => {
controller.abort();
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
};
}, [imageUrl, onReady, snapshot.id]);
const handleLoad = useCallback(() => onReady(), [onReady]);
const imageWidth = Math.max(snapshot.pageW, snapshot.viewportW);
return (
<div className={styles.snapshot}>
<img
className={styles.snapshotImage}
src={src || undefined}
alt=""
draggable={false}
onLoad={handleLoad}
style={{
width: imageWidth,
height: snapshot.pageH,
}}
/>
</div>
);
return <IframeSnapshot snapshot={snapshot} onReady={onReady} />;
}
function IframeSnapshot({
snapshot,
onReady,
}: {
snapshot: Extract<HeatmapSnapshot, { kind: 'iframe' }>;
snapshot: HeatmapSnapshot;
onReady: () => void;
}) {
const [available, setAvailable] = useState(true);
-4
View File
@@ -217,7 +217,6 @@ export async function POST(request: Request) {
sessionId,
visitId,
eventType: event.type === 'click' ? HEATMAP_EVENT_TYPE.click : HEATMAP_EVENT_TYPE.scroll,
nodeId: null,
x: event.type === 'click' ? (event.x ?? null) : null,
y: event.type === 'click' ? (event.y ?? null) : null,
pageX: event.type === 'click' ? (event.pageX ?? null) : null,
@@ -229,9 +228,6 @@ export async function POST(request: Request) {
scrollPct: event.type === 'scroll' ? (event.scrollPct ?? null) : null,
urlPath: getUrlPath(event.url),
createdAt: new Date(event.timestamp ?? fallbackMs),
replayChunkIndex: null,
replayEventIndex: null,
replayTimeMs: null,
}));
if (heatmapRows.length) {
@@ -1,34 +0,0 @@
import { parseRequest } from '@/lib/request';
import { notFound, unauthorized } from '@/lib/response';
import { canViewAuthenticatedWebsite } from '@/permissions';
import { getHeatmapSnapshotImage } from '@/queries/sql/heatmap/ensureHeatmapSnapshot';
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string; snapshotId: string }> },
) {
const { auth, error } = await parseRequest(request);
const { websiteId, snapshotId } = await params;
if (error) {
return error();
}
if (!(await canViewAuthenticatedWebsite(auth, websiteId))) {
return unauthorized();
}
const snapshot = await getHeatmapSnapshotImage(websiteId, snapshotId);
if (!snapshot) {
return notFound({ message: 'Snapshot not found.' });
}
return new Response(new Uint8Array(snapshot.imageData), {
status: 200,
headers: {
'Content-Type': snapshot.mimeType,
'Cache-Control': 'private, max-age=300',
},
});
}