From 438cab51e655ce51dd9d983c78cdeb0078679344 Mon Sep 17 00:00:00 2001 From: David Ly Date: Tue, 25 Jun 2024 08:17:15 +0200 Subject: [PATCH] Fixed compliances posthog events resend after page refresh --- .../src/PhotoCapture/PhotoCapture.tsx | 41 ++++++++++--------- .../hooks/useComplianceAnalytics.ts | 14 ++++--- .../hooks/usePhotoCaptureSightState.ts | 3 ++ .../test/PhotoCapture/PhotoCapture.test.tsx | 4 +- .../hooks/usePhotoCaptureSightState.test.ts | 1 + 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/packages/inspection-capture-web/src/PhotoCapture/PhotoCapture.tsx b/packages/inspection-capture-web/src/PhotoCapture/PhotoCapture.tsx index 415b0dfd5..0bdb0c91f 100644 --- a/packages/inspection-capture-web/src/PhotoCapture/PhotoCapture.tsx +++ b/packages/inspection-capture-web/src/PhotoCapture/PhotoCapture.tsx @@ -1,22 +1,11 @@ -import { useState } from 'react'; +import { useAnalytics } from '@monkvision/analytics'; import { Camera, CameraHUDProps, CameraProps } from '@monkvision/camera-web'; -import { - CaptureAppConfig, - ComplianceOptions, - DeviceOrientation, - Sight, - CompressionOptions, - CameraConfig, -} from '@monkvision/types'; import { useI18nSync, useLoadingState, - useWindowDimensions, useObjectMemo, + useWindowDimensions, } from '@monkvision/common'; -import { MonkApiConfig } from '@monkvision/network'; -import { useAnalytics } from '@monkvision/analytics'; -import { useMonitoring } from '@monkvision/monitoring'; import { BackdropDialog, Icon, @@ -24,21 +13,32 @@ import { NavigateToCaptureOptions, NavigateToCaptureReason, } from '@monkvision/common-ui-web'; +import { useMonitoring } from '@monkvision/monitoring'; +import { MonkApiConfig } from '@monkvision/network'; +import { + CameraConfig, + CaptureAppConfig, + ComplianceOptions, + CompressionOptions, + DeviceOrientation, + Sight, +} from '@monkvision/types'; +import { useState } from 'react'; import { useTranslation } from 'react-i18next'; +import { styles } from './PhotoCapture.styles'; +import { PhotoCaptureHUD, PhotoCaptureHUDProps } from './PhotoCaptureHUD'; import { + useAdaptiveCameraConfig, useAddDamageMode, - usePhotoCaptureImages, + useBadConnectionWarning, useComplianceAnalytics, + usePhotoCaptureImages, usePhotoCaptureSightState, usePictureTaken, useStartTasksOnComplete, - useUploadQueue, - useBadConnectionWarning, - useAdaptiveCameraConfig, useTracking, + useUploadQueue, } from './hooks'; -import { PhotoCaptureHUD, PhotoCaptureHUDProps } from './PhotoCaptureHUD'; -import { styles } from './PhotoCapture.styles'; /** * Props of the PhotoCapture component. @@ -146,7 +146,7 @@ export function PhotoCapture({ const loading = useLoadingState(); const addDamageHandle = useAddDamageMode(); useTracking({ inspectionId, authToken: apiConfig.authToken }); - useComplianceAnalytics({ inspectionId, sights }); + const { setIsInitialInspectionFetched } = useComplianceAnalytics({ inspectionId, sights }); const { adaptiveCameraConfig, uploadEventHandlers: adaptiveUploadEventHandlers } = useAdaptiveCameraConfig({ initialCameraConfig, @@ -172,6 +172,7 @@ export function PhotoCapture({ onLastSightTaken, tasksBySight, complianceOptions, + setIsInitialInspectionFetched, }); const { isBadConnectionWarningDialogDisplayed, diff --git a/packages/inspection-capture-web/src/PhotoCapture/hooks/useComplianceAnalytics.ts b/packages/inspection-capture-web/src/PhotoCapture/hooks/useComplianceAnalytics.ts index 4faa82120..dc1ceff9d 100644 --- a/packages/inspection-capture-web/src/PhotoCapture/hooks/useComplianceAnalytics.ts +++ b/packages/inspection-capture-web/src/PhotoCapture/hooks/useComplianceAnalytics.ts @@ -26,6 +26,7 @@ export interface ComplianceAnalyticsParams { */ export function useComplianceAnalytics({ inspectionId, sights }: ComplianceAnalyticsParams) { const [imagesEventTracking, setImagesEventTracking] = useState([]); + const [isInitialInspectionFetched, setIsInitialInspectionFetched] = useState(false); const { state } = useMonkState(); const { trackEvent } = useAnalytics(); @@ -38,11 +39,13 @@ export function useComplianceAnalytics({ inspectionId, sights }: ComplianceAnaly return imageEventTracking; } if (image.status === ImageStatus.NOT_COMPLIANT && image.complianceIssues) { - trackEvent('Compliance Issue', { - complianceIssue: image.complianceIssues.at(0), - sightId: image.sightId, - sightLabel: sights.find((sight) => sight.id === image.sightId)?.label, - }); + if (isInitialInspectionFetched) { + trackEvent('Compliance Issue', { + complianceIssue: image.complianceIssues.at(0), + sightId: image.sightId, + sightLabel: sights.find((sight) => sight.id === image.sightId)?.label, + }); + } return { ...image, isAlreadySent: true }; } return { ...image, isAlreadySent: false }; @@ -50,4 +53,5 @@ export function useComplianceAnalytics({ inspectionId, sights }: ComplianceAnaly setImagesEventTracking(newImagesEventTracking); }, [state]); + return { setIsInitialInspectionFetched }; } diff --git a/packages/inspection-capture-web/src/PhotoCapture/hooks/usePhotoCaptureSightState.ts b/packages/inspection-capture-web/src/PhotoCapture/hooks/usePhotoCaptureSightState.ts index b426f2b17..865d7a455 100644 --- a/packages/inspection-capture-web/src/PhotoCapture/hooks/usePhotoCaptureSightState.ts +++ b/packages/inspection-capture-web/src/PhotoCapture/hooks/usePhotoCaptureSightState.ts @@ -85,6 +85,7 @@ export interface PhotoCaptureSightsParams { * The options for the compliance conf */ complianceOptions: ComplianceOptions; + setIsInitialInspectionFetched: (state: boolean) => void; /** * Record associating each sight with a list of tasks to execute for it. If not provided, the default tasks of the * sight will be used. @@ -166,6 +167,7 @@ export function usePhotoCaptureSightState({ loading, onLastSightTaken, tasksBySight, + setIsInitialInspectionFetched, complianceOptions, }: PhotoCaptureSightsParams): PhotoCaptureSightState { if (captureSights.length === 0) { @@ -182,6 +184,7 @@ export function usePhotoCaptureSightState({ const onFetchInspection = (response: MonkApiResponse) => { try { + setIsInitialInspectionFetched(true); assertInspectionIsValid(inspectionId, response, captureSights, tasksBySight); const alreadyTakenSights = getSightsTaken(inspectionId, response); setSightsTaken(alreadyTakenSights); diff --git a/packages/inspection-capture-web/test/PhotoCapture/PhotoCapture.test.tsx b/packages/inspection-capture-web/test/PhotoCapture/PhotoCapture.test.tsx index 300e40581..0c9e9a0d0 100644 --- a/packages/inspection-capture-web/test/PhotoCapture/PhotoCapture.test.tsx +++ b/packages/inspection-capture-web/test/PhotoCapture/PhotoCapture.test.tsx @@ -25,7 +25,9 @@ jest.mock('../../src/PhotoCapture/hooks', () => ({ })), useStartTasksOnComplete: jest.fn(() => jest.fn()), usePhotoCaptureImages: jest.fn(() => [{ id: 'test' }]), - useComplianceAnalytics: jest.fn(), + useComplianceAnalytics: jest.fn(() => ({ + isInitialInspectionFetched: jest.fn(), + })), useBadConnectionWarning: jest.fn(() => ({ isBadConnectionWarningDialogDisplayed: true, closeBadConnectionWarningDialog: jest.fn(), diff --git a/packages/inspection-capture-web/test/PhotoCapture/hooks/usePhotoCaptureSightState.test.ts b/packages/inspection-capture-web/test/PhotoCapture/hooks/usePhotoCaptureSightState.test.ts index fdbf9b422..4acb37fa9 100644 --- a/packages/inspection-capture-web/test/PhotoCapture/hooks/usePhotoCaptureSightState.test.ts +++ b/packages/inspection-capture-web/test/PhotoCapture/hooks/usePhotoCaptureSightState.test.ts @@ -35,6 +35,7 @@ function createParams(): PhotoCaptureSightsParams { complianceIssuesPerSight: { test: [ComplianceIssue.OVEREXPOSURE] }, useLiveCompliance: true, }, + setIsInitialInspectionFetched: jest.fn(), }; }