11'use client' ;
22
33import { useMemo , useState } from 'react' ;
4+ import { toast } from 'sonner' ;
45import { MetricsChart } from './metrics-chart' ;
56import { EditAnnotationModal } from './edit-annotation-modal' ;
67import { trpc } from '@/lib/trpc' ;
7- import { toast } from 'sonner' ;
88import { usePersistentState } from '@/hooks/use-persistent-state' ;
9- import type { Annotation , ChartContext , CreateAnnotationData , AnnotationFormData } from '@/types/annotations' ;
109import { ANNOTATION_STORAGE_KEYS } from '@/lib/annotation-constants' ;
10+ import type { Annotation , ChartContext , CreateAnnotationData , AnnotationFormData } from '@/types/annotations' ;
1111
1212interface MetricsChartWithAnnotationsProps {
1313 websiteId : string ;
@@ -40,21 +40,20 @@ export function MetricsChartWithAnnotations({
4040 onRangeSelect,
4141 dateRange,
4242} : MetricsChartWithAnnotationsProps ) {
43- const createAnnotation = trpc . annotations . create . useMutation ( ) ;
44- const updateAnnotation = trpc . annotations . update . useMutation ( ) ;
45- const deleteAnnotation = trpc . annotations . delete . useMutation ( ) ;
46-
4743 const [ editingAnnotation , setEditingAnnotation ] = useState < Annotation | null > ( null ) ;
4844 const [ isEditing , setIsEditing ] = useState ( false ) ;
4945 const [ showAnnotations , setShowAnnotations ] = usePersistentState (
50- ANNOTATION_STORAGE_KEYS . visibility ( websiteId ) ,
46+ ANNOTATION_STORAGE_KEYS . visibility ( websiteId ) ,
5147 true
5248 ) ;
53-
54- // Build chart context for fetching annotations
49+
50+ const createAnnotation = trpc . annotations . create . useMutation ( ) ;
51+ const updateAnnotation = trpc . annotations . update . useMutation ( ) ;
52+ const deleteAnnotation = trpc . annotations . delete . useMutation ( ) ;
53+
5554 const chartContext = useMemo ( ( ) : ChartContext | null => {
56- if ( ! dateRange || ! data || data . length === 0 ) return null ;
57-
55+ if ( ! dateRange || ! data ? .length ) return null ;
56+
5857 return {
5958 dateRange : {
6059 start_date : dateRange . startDate . toISOString ( ) ,
@@ -65,8 +64,7 @@ export function MetricsChartWithAnnotations({
6564 } ;
6665 } , [ dateRange , data ] ) ;
6766
68- // Fetch annotations for this chart
69- const { data : annotations , refetch : refetchAnnotations } = trpc . annotations . list . useQuery (
67+ const { data : allAnnotations , refetch : refetchAnnotations } = trpc . annotations . list . useQuery (
7068 {
7169 websiteId,
7270 chartType : 'metrics' as const ,
@@ -77,6 +75,23 @@ export function MetricsChartWithAnnotations({
7775 }
7876 ) ;
7977
78+ const annotations = useMemo ( ( ) => {
79+ if ( ! allAnnotations || ! dateRange ) return [ ] ;
80+
81+ const { startDate, endDate } = dateRange ;
82+
83+ return allAnnotations . filter ( ( annotation ) => {
84+ const annotationStart = new Date ( annotation . xValue ) ;
85+ const annotationEnd = annotation . xEndValue ? new Date ( annotation . xEndValue ) : annotationStart ;
86+
87+ return (
88+ ( annotationStart >= startDate && annotationStart <= endDate ) ||
89+ ( annotationEnd >= startDate && annotationEnd <= endDate ) ||
90+ ( annotationStart <= startDate && annotationEnd >= endDate )
91+ ) ;
92+ } ) ;
93+ } , [ allAnnotations , dateRange ] ) ;
94+
8095 const handleCreateAnnotation = async ( annotation : {
8196 annotationType : 'range' ;
8297 xValue : string ;
@@ -86,13 +101,8 @@ export function MetricsChartWithAnnotations({
86101 color : string ;
87102 isPublic : boolean ;
88103 } ) => {
89- if ( ! websiteId ) {
90- toast . error ( 'Website ID is required' ) ;
91- return ;
92- }
93-
94- if ( ! chartContext ) {
95- toast . error ( 'Chart context is required' ) ;
104+ if ( ! websiteId || ! chartContext ) {
105+ toast . error ( 'Missing required data for annotation creation' ) ;
96106 return ;
97107 }
98108
@@ -119,9 +129,7 @@ export function MetricsChartWithAnnotations({
119129 } ,
120130 error : ( err ) => {
121131 console . error ( 'Failed to create annotation:' , err ) ;
122- const errorMessage = err ?. message || 'Failed to create annotation' ;
123- toast . error ( `Failed to create annotation: ${ errorMessage } ` ) ;
124- return errorMessage ;
132+ return err ?. message || 'Failed to create annotation' ;
125133 } ,
126134 } ) ;
127135
@@ -193,6 +201,7 @@ export function MetricsChartWithAnnotations({
193201 onDeleteAnnotation = { handleDeleteAnnotation }
194202 showAnnotations = { showAnnotations }
195203 onToggleAnnotations = { setShowAnnotations }
204+ websiteId = { websiteId }
196205 />
197206
198207 < EditAnnotationModal
0 commit comments