1
+ "use client" ;
2
+
1
3
import React , { useEffect , useState } from "react" ;
2
- import { Box } from "@mui/material" ;
4
+ import Box from "@mui/material/Box " ;
3
5
import DetailsDrawer from "@/app/dashboard/components/performance/DetailsDrawer" ;
4
6
import LineChart from "@/app/dashboard/components/performance/LineChart" ;
5
7
import ContentsTable from "@/app/dashboard/components/performance/ContentsTable" ;
@@ -9,15 +11,17 @@ import {
9
11
getPerformanceDrawerAISummary ,
10
12
} from "@/app/dashboard/api" ;
11
13
import {
12
- ApexData ,
14
+ ContentLineChartTSData ,
13
15
Period ,
14
16
RowDataType ,
15
- DrawerData ,
16
17
CustomDateParams ,
18
+ DrawerData ,
19
+ ApexData ,
17
20
} from "@/app/dashboard/types" ;
18
21
import { useAuth } from "@/utils/auth" ;
19
22
20
- const N_TOP_CONTENT = 10 ;
23
+ const CHART_COLORS = [ "#003049" , "#d62828" , "#f77f00" , "#fcbf49" , "#eae2b7" ] ;
24
+ const N_TOP_CONTENT = 5 ;
21
25
22
26
interface PerformanceProps {
23
27
timePeriod : Period ;
@@ -29,78 +33,63 @@ const ContentPerformance: React.FC<PerformanceProps> = ({
29
33
customDateParams,
30
34
} ) => {
31
35
const { token } = useAuth ( ) ;
32
- const [ drawerOpen , setDrawerOpen ] = useState ( false ) ;
33
- const [ lineChartData , setLineChartData ] = useState < ApexData [ ] > ( [ ] ) ;
34
36
const [ contentTableData , setContentTableData ] = useState < RowDataType [ ] > ( [ ] ) ;
37
+ const [ itemsToDisplay , setItemsToDisplay ] = useState < RowDataType [ ] > ( [ ] ) ;
38
+ const [ lineChartData , setLineChartData ] = useState < ContentLineChartTSData [ ] > ( [ ] ) ;
39
+ const [ drawerOpen , setDrawerOpen ] = useState ( false ) ;
35
40
const [ drawerData , setDrawerData ] = useState < DrawerData | null > ( null ) ;
36
41
const [ drawerAISummary , setDrawerAISummary ] = useState < string | null > ( null ) ;
42
+ const [ selectedOrderColumn , setSelectedOrderColumn ] =
43
+ useState < string > ( "Daily Average Sent" ) ;
44
+ const [ selectedOrderDirection , setSelectedOrderDirection ] = useState <
45
+ "ascending" | "descending"
46
+ > ( "descending" ) ;
47
+ const [ page , setPage ] = useState < number > ( 1 ) ;
37
48
38
49
useEffect ( ( ) => {
39
50
if ( ! token ) return ;
40
- if (
41
- timePeriod === "custom" &&
42
- customDateParams ?. startDate &&
43
- customDateParams . endDate
44
- ) {
45
- getPerformancePageData (
46
- "custom" ,
47
- token ,
48
- customDateParams . startDate ,
49
- customDateParams . endDate ,
50
- ) . then ( ( response ) => {
51
- parseLineChartData ( response . content_time_series . slice ( 0 , N_TOP_CONTENT ) ) ;
52
- parseContentTableData ( response . content_time_series ) ;
53
- } ) ;
54
- } else {
55
- getPerformancePageData ( timePeriod , token ) . then ( ( response ) => {
56
- parseLineChartData ( response . content_time_series . slice ( 0 , N_TOP_CONTENT ) ) ;
57
- parseContentTableData ( response . content_time_series ) ;
58
- } ) ;
59
- }
60
- } , [ timePeriod , token , customDateParams ] ) ;
61
-
62
- const parseLineChartData = ( timeseriesData : Record < string , any > [ ] ) => {
63
- const apexTimeSeriesData : ApexData [ ] = timeseriesData . map ( ( series , idx ) => {
64
- const zIndex = idx === 0 ? 3 : 2 ;
65
- return {
66
- name : series . title ,
67
- zIndex,
68
- data : Object . entries ( series . query_count_time_series ) . map (
69
- ( [ period , queryCount ] ) => {
70
- const date = new Date ( period ) ;
71
- return { x : String ( date ) , y : queryCount as number } ;
72
- } ,
73
- ) ,
74
- } ;
75
- } ) ;
76
- setLineChartData ( apexTimeSeriesData ) ;
77
- } ;
78
-
79
- const parseContentTableData = ( timeseriesData : Record < string , any > [ ] ) => {
80
- const rows : RowDataType [ ] = timeseriesData . map ( ( series ) => {
81
- return {
82
- id : series . id ,
83
- title : series . title ,
84
- query_count : series . total_query_count ,
85
- positive_votes : series . positive_votes ,
86
- negative_votes : series . negative_votes ,
87
- query_count_timeseries : Object . values ( series . query_count_time_series ) ,
88
- } ;
51
+ getPerformancePageData (
52
+ timePeriod ,
53
+ token ,
54
+ customDateParams ?. startDate ?? undefined ,
55
+ customDateParams ?. endDate ?? undefined ,
56
+ ) . then ( ( response ) => {
57
+ const tableData : RowDataType [ ] = response . content_time_series . map (
58
+ ( series : any ) => ( {
59
+ id : series . id ,
60
+ title : series . title ,
61
+ query_count : series . total_query_count ,
62
+ positive_votes : series . positive_votes ,
63
+ negative_votes : series . negative_votes ,
64
+ query_count_timeseries : Object . entries ( series . query_count_time_series ) . map (
65
+ ( [ timestamp , value ] ) => ( {
66
+ x : new Date ( timestamp ) . getTime ( ) ,
67
+ y : value as number ,
68
+ } ) ,
69
+ ) ,
70
+ } ) ,
71
+ ) ;
72
+ setContentTableData ( tableData ) ;
89
73
} ) ;
90
- setContentTableData ( rows ) ;
91
- } ;
74
+ } , [ timePeriod , token , customDateParams ] ) ;
92
75
93
- const toggleDrawer = ( newOpen : boolean ) => ( ) => {
94
- setDrawerOpen ( newOpen ) ;
95
- } ;
76
+ // Build line chart data based on the items currently displayed
77
+ useEffect ( ( ) => {
78
+ const chartData : ContentLineChartTSData [ ] = itemsToDisplay . map ( ( row ) => ( {
79
+ name : row . title ,
80
+ data : row . query_count_timeseries ,
81
+ } ) ) ;
82
+ setLineChartData ( chartData ) ;
83
+ } , [ itemsToDisplay ] ) ;
96
84
85
+ // When a table row is clicked, fetch and parse drawer data and AI summary.
97
86
const tableRowClickHandler = ( contentId : number ) => {
98
- setDrawerAISummary ( null ) ;
99
87
if ( ! token ) return ;
88
+ setDrawerAISummary ( null ) ;
100
89
if (
101
90
timePeriod === "custom" &&
102
91
customDateParams ?. startDate &&
103
- customDateParams . endDate
92
+ customDateParams ? .endDate
104
93
) {
105
94
getPerformanceDrawerData (
106
95
"custom" ,
@@ -183,32 +172,42 @@ const ContentPerformance: React.FC<PerformanceProps> = ({
183
172
} ) ;
184
173
} ;
185
174
175
+ const handleSortChange = ( column : string , direction : "ascending" | "descending" ) => {
176
+ setSelectedOrderColumn ( column ) ;
177
+ setSelectedOrderDirection ( direction ) ;
178
+ } ;
179
+
180
+ const handlePageChange = ( newPage : number ) => {
181
+ setPage ( newPage ) ;
182
+ } ;
183
+
186
184
return (
187
185
< >
188
186
< DetailsDrawer
189
187
open = { drawerOpen }
190
- onClose = { toggleDrawer }
188
+ onClose = { ( ) => ( _event ) => setDrawerOpen ( false ) }
191
189
data = { drawerData }
192
190
aiSummary = { drawerAISummary }
193
191
/>
194
- < Box
195
- bgcolor = "white"
196
- sx = { {
197
- height : 300 ,
198
- border : 0 ,
199
- padding : 2 ,
200
- } }
201
- >
192
+ < Box bgcolor = "white" sx = { { height : 430 , border : 0 , padding : 2 } } >
202
193
< LineChart
203
194
data = { lineChartData }
204
195
nTopContent = { N_TOP_CONTENT }
205
196
timePeriod = { timePeriod }
197
+ chartColors = { CHART_COLORS }
198
+ orderBy = { selectedOrderColumn }
199
+ orderDirection = { selectedOrderDirection }
200
+ pageNumber = { page }
206
201
/>
207
202
</ Box >
208
203
< ContentsTable
209
204
rows = { contentTableData }
210
205
onClick = { tableRowClickHandler }
211
206
rowsPerPage = { N_TOP_CONTENT }
207
+ chartColors = { CHART_COLORS }
208
+ onItemsToDisplayChange = { ( items ) => setItemsToDisplay ( items ) }
209
+ onSortChange = { handleSortChange }
210
+ onPageChange = { handlePageChange }
212
211
/>
213
212
</ >
214
213
) ;
0 commit comments