1+ "use client" ;
2+
13import React , { useEffect , useState } from "react" ;
2- import { Box } from "@mui/material" ;
4+ import Box from "@mui/material/Box " ;
35import DetailsDrawer from "@/app/dashboard/components/performance/DetailsDrawer" ;
46import LineChart from "@/app/dashboard/components/performance/LineChart" ;
57import ContentsTable from "@/app/dashboard/components/performance/ContentsTable" ;
@@ -9,15 +11,17 @@ import {
911 getPerformanceDrawerAISummary ,
1012} from "@/app/dashboard/api" ;
1113import {
12- ApexData ,
14+ ContentLineChartTSData ,
1315 Period ,
1416 RowDataType ,
15- DrawerData ,
1617 CustomDateParams ,
18+ DrawerData ,
19+ ApexData ,
1720} from "@/app/dashboard/types" ;
1821import { useAuth } from "@/utils/auth" ;
1922
20- const N_TOP_CONTENT = 10 ;
23+ const CHART_COLORS = [ "#003049" , "#d62828" , "#f77f00" , "#fcbf49" , "#eae2b7" ] ;
24+ const N_TOP_CONTENT = 5 ;
2125
2226interface PerformanceProps {
2327 timePeriod : Period ;
@@ -29,78 +33,63 @@ const ContentPerformance: React.FC<PerformanceProps> = ({
2933 customDateParams,
3034} ) => {
3135 const { token } = useAuth ( ) ;
32- const [ drawerOpen , setDrawerOpen ] = useState ( false ) ;
33- const [ lineChartData , setLineChartData ] = useState < ApexData [ ] > ( [ ] ) ;
3436 const [ contentTableData , setContentTableData ] = useState < RowDataType [ ] > ( [ ] ) ;
37+ const [ itemsToDisplay , setItemsToDisplay ] = useState < RowDataType [ ] > ( [ ] ) ;
38+ const [ lineChartData , setLineChartData ] = useState < ContentLineChartTSData [ ] > ( [ ] ) ;
39+ const [ drawerOpen , setDrawerOpen ] = useState ( false ) ;
3540 const [ drawerData , setDrawerData ] = useState < DrawerData | null > ( null ) ;
3641 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 ) ;
3748
3849 useEffect ( ( ) => {
3950 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 ) ;
8973 } ) ;
90- setContentTableData ( rows ) ;
91- } ;
74+ } , [ timePeriod , token , customDateParams ] ) ;
9275
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 ] ) ;
9684
85+ // When a table row is clicked, fetch and parse drawer data and AI summary.
9786 const tableRowClickHandler = ( contentId : number ) => {
98- setDrawerAISummary ( null ) ;
9987 if ( ! token ) return ;
88+ setDrawerAISummary ( null ) ;
10089 if (
10190 timePeriod === "custom" &&
10291 customDateParams ?. startDate &&
103- customDateParams . endDate
92+ customDateParams ? .endDate
10493 ) {
10594 getPerformanceDrawerData (
10695 "custom" ,
@@ -183,32 +172,42 @@ const ContentPerformance: React.FC<PerformanceProps> = ({
183172 } ) ;
184173 } ;
185174
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+
186184 return (
187185 < >
188186 < DetailsDrawer
189187 open = { drawerOpen }
190- onClose = { toggleDrawer }
188+ onClose = { ( ) => ( _event ) => setDrawerOpen ( false ) }
191189 data = { drawerData }
192190 aiSummary = { drawerAISummary }
193191 />
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 } } >
202193 < LineChart
203194 data = { lineChartData }
204195 nTopContent = { N_TOP_CONTENT }
205196 timePeriod = { timePeriod }
197+ chartColors = { CHART_COLORS }
198+ orderBy = { selectedOrderColumn }
199+ orderDirection = { selectedOrderDirection }
200+ pageNumber = { page }
206201 />
207202 </ Box >
208203 < ContentsTable
209204 rows = { contentTableData }
210205 onClick = { tableRowClickHandler }
211206 rowsPerPage = { N_TOP_CONTENT }
207+ chartColors = { CHART_COLORS }
208+ onItemsToDisplayChange = { ( items ) => setItemsToDisplay ( items ) }
209+ onSortChange = { handleSortChange }
210+ onPageChange = { handlePageChange }
212211 />
213212 </ >
214213 ) ;
0 commit comments