- wrap root layout with QueryProvider for React Query - replace market UnderConstruction placeholder with MarketClient and real data fetching - implement search, pagination, and sort via search params using getActiveListings - add necessary client components and queries - update dependencies (e.g., prisma, pkg-types)
101 lines
2.5 KiB
TypeScript
101 lines
2.5 KiB
TypeScript
/**
|
|
* Hooks para tracking de telemetria
|
|
*/
|
|
|
|
import { useEffect, useRef, useCallback } from "react";
|
|
import { getTelemetry } from "../telemetry/MarketTelemetry";
|
|
import { useSession } from "./use-session";
|
|
|
|
/**
|
|
* Hook para tracking automático de visualização de anúncio
|
|
*/
|
|
export function useAdTracking(adId: number | null) {
|
|
const startTimeRef = useRef<number | null>(null);
|
|
const { user } = useSession();
|
|
|
|
useEffect(() => {
|
|
if (!adId) return;
|
|
|
|
const telemetry = getTelemetry(user?.userId);
|
|
startTimeRef.current = Date.now();
|
|
telemetry.trackAdView(adId);
|
|
|
|
return () => {
|
|
if (startTimeRef.current && adId) {
|
|
telemetry.trackAdExit(adId);
|
|
}
|
|
};
|
|
}, [adId, user?.userId]);
|
|
}
|
|
|
|
/**
|
|
* Hook para tracking global do marketplace
|
|
*/
|
|
export function useMarketTelemetry() {
|
|
const { user } = useSession();
|
|
const telemetryRef = useRef(getTelemetry(user?.userId));
|
|
|
|
// Atualizar user_id quando mudar
|
|
useEffect(() => {
|
|
if (user?.userId) {
|
|
telemetryRef.current.setUserId(user.userId);
|
|
}
|
|
}, [user?.userId]);
|
|
|
|
// Cleanup ao desmontar
|
|
useEffect(() => {
|
|
return () => {
|
|
telemetryRef.current.stop();
|
|
};
|
|
}, []);
|
|
|
|
const trackSearch = useCallback((query: string, filters?: Record<string, any>) => {
|
|
telemetryRef.current.trackSearch(query, filters);
|
|
}, []);
|
|
|
|
const trackFilter = useCallback((filters: Record<string, any>) => {
|
|
telemetryRef.current.trackFilterApplied(filters);
|
|
}, []);
|
|
|
|
const trackFavorite = useCallback((adId: number, action: "favorite" | "unfavorite") => {
|
|
telemetryRef.current.trackFavorite(adId, action);
|
|
}, []);
|
|
|
|
const trackPurchase = useCallback((adId: number, price: number) => {
|
|
telemetryRef.current.trackPurchase(adId, price);
|
|
}, []);
|
|
|
|
const trackInquiry = useCallback((adId: number) => {
|
|
telemetryRef.current.trackInquiry(adId);
|
|
}, []);
|
|
|
|
const trackPageView = useCallback((page: string) => {
|
|
telemetryRef.current.trackPageView(page);
|
|
}, []);
|
|
|
|
const trackAdClick = useCallback((adId: number, position?: number) => {
|
|
telemetryRef.current.trackAdClick(adId, position);
|
|
}, []);
|
|
|
|
const trackCustomEvent = useCallback((
|
|
eventType: string,
|
|
adId?: number,
|
|
metadata?: Record<string, any>
|
|
) => {
|
|
telemetryRef.current.trackCustomEvent(eventType, adId, metadata);
|
|
}, []);
|
|
|
|
return {
|
|
trackSearch,
|
|
trackFilter,
|
|
trackFavorite,
|
|
trackPurchase,
|
|
trackInquiry,
|
|
trackPageView,
|
|
trackAdClick,
|
|
trackCustomEvent,
|
|
telemetry: telemetryRef.current,
|
|
};
|
|
}
|
|
|