@@ -16,9 +16,13 @@ import {
1616 type State ,
1717} from '@/config/graphql/analaytics-queries' ;
1818import { features } from '@/config/site' ;
19+ import {
20+ hasSubDistrictSupport ,
21+ isModuleReportDownloadable ,
22+ isModuleViewEnabled ,
23+ } from '@/lib/analytics/module-config' ;
1924import { getLatestDate } from '@/lib/analytics/utils' ;
20- import { routes } from '@/lib/routes' ;
21- import { hasSubDistrictSupport } from '@/lib/state-map-config' ;
25+ import { ANALYTICS_VIEWS , routes } from '@/lib/routes' ;
2226import { type JsonScalar } from '@/lib/types' ;
2327import { cn , downloadStateReport } from '@/lib/utils' ;
2428import Icons from '@/components/icons' ;
@@ -74,6 +78,10 @@ export function AnalyticsMobileLayout({
7478 currentSelectedState ?. slug ,
7579 analyticsModule
7680 ) ;
81+ const downloadReportEnabled = isModuleReportDownloadable (
82+ currentSelectedState . slug ,
83+ analyticsModule
84+ ) ;
7785 const copyURL = useCopyURL ( ) ;
7886 //Remove default page scroll to make only the content scrollable
7987 useLockBody ( ) ;
@@ -88,41 +96,45 @@ export function AnalyticsMobileLayout({
8896 parseAsString . withDefault ( timePeriod )
8997 ) ;
9098
99+ const viewButtonConfig = {
100+ map : { icon : Icons . IconMap , title : t ( 'views.map' ) } ,
101+ chart : { icon : Icons . IconChartBar , title : t ( 'views.chart' ) } ,
102+ table : { icon : Icons . IconTableAlias , title : t ( 'views.table' ) } ,
103+ } as const ;
104+
91105 const buttons = [
92- {
93- icon : Icons . IconMap ,
94- title : t ( 'views.map' ) ,
95- value : 'map' ,
96- disabled : false ,
97- } ,
98- ...( features . chart
99- ? [
100- {
101- icon : Icons . IconChartBar ,
102- title : t ( 'views.chart' ) ,
103- value : 'chart' ,
104- disabled : false ,
105- } ,
106- ]
107- : [ ] ) ,
108- {
109- icon : Icons . IconTableAlias ,
110- title : t ( 'views.table' ) ,
111- value : 'table' ,
112- disabled : false ,
113- } ,
106+ ...ANALYTICS_VIEWS . filter ( ( tabValue ) =>
107+ isModuleViewEnabled ( currentSelectedState . slug , analyticsModule , tabValue )
108+ ) . map ( ( tabValue ) => ( {
109+ ...viewButtonConfig [ tabValue ] ,
110+ value : tabValue ,
111+ } ) ) ,
114112 {
115113 icon : Icons . IconDots ,
116114 title : t ( 'views.more' ) ,
117115 value : 'more' ,
118- disabled : false ,
119116 } ,
120117 ] ;
121118
122119 const [ view , setView ] = useQueryState (
123120 'view' ,
124121 parseAsString . withDefault ( 'map' )
125122 ) ;
123+
124+ React . useEffect ( ( ) => {
125+ if ( ! view || view === 'map' || view === 'more' ) return ;
126+ if (
127+ isModuleViewEnabled (
128+ currentSelectedState . slug ,
129+ analyticsModule ,
130+ view as 'chart' | 'table'
131+ )
132+ ) {
133+ return ;
134+ }
135+ setView ( 'map' , { shallow : true } ) ;
136+ } , [ view , currentSelectedState . slug , analyticsModule , setView ] ) ;
137+
126138 const searchParams = useSearchParams ( ) ;
127139 const isMapView = ! view || view === 'map' ;
128140 const districtCode = searchParams . get ( 'district-code' ) ;
@@ -244,6 +256,15 @@ export function AnalyticsMobileLayout({
244256 ) ;
245257
246258 case 'chart' :
259+ if (
260+ ! isModuleViewEnabled (
261+ currentSelectedState . slug ,
262+ analyticsModule ,
263+ 'chart'
264+ )
265+ ) {
266+ return null ;
267+ }
247268 return (
248269 < div className = "pt-[84px]" >
249270 < ChartView
@@ -353,28 +374,21 @@ export function AnalyticsMobileLayout({
353374 trigger = {
354375 < Button
355376 size = "slim"
356- className = { cn (
357- 'basis-1/3 border-t-1 py-4' ,
358- button . disabled && 'cursor-not-allowed opacity-50'
359- ) }
377+ className = "basis-1/3 border-t-1 py-4"
360378 kind = "tertiary"
361- disabled = { button . disabled }
362379 >
363380 < div className = "flex flex-col items-center justify-center gap-1 bg-baseIndigoSolid1 " >
364381 < Icon
365382 source = { button . icon }
366383 size = { 24 }
367384 stroke = { activeButton === button . value ? 3 : 2 }
368- className = { button . disabled ? 'opacity-50' : '' }
369385 />
370386 < Text
371387 variant = "headingMd"
372388 fontWeight = {
373389 activeButton === button . value ? 'bold' : 'medium'
374390 }
375- className = {
376- button . disabled ? 'opacity-50' : 'text-textSubdued'
377- }
391+ className = "text-textSubdued"
378392 >
379393 { button . title }
380394 </ Text >
@@ -393,6 +407,7 @@ export function AnalyticsMobileLayout({
393407 {
394408 content : t ( 'actions.download.label' ) ,
395409 icon : Icons . download ,
410+ disabled : ! downloadReportEnabled ,
396411 onAction : ( ) => {
397412 const confirmation = window . confirm (
398413 t ( 'actions.download.confirm' , {
@@ -422,32 +437,23 @@ export function AnalyticsMobileLayout({
422437 < Button
423438 key = { index }
424439 size = "slim"
425- className = { cn (
426- 'basis-1/3 border-t-1 py-4' ,
427- button . disabled && 'cursor-not-allowed opacity-50'
428- ) }
440+ className = "basis-1/3 border-t-1 py-4"
429441 kind = "tertiary"
430442 onClick = { ( ) => {
431- if ( ! button . disabled ) {
432- setActiveButton ( button . value ) ;
433- setView ( button . value , { shallow : false } ) ;
434- }
443+ setActiveButton ( button . value ) ;
444+ setView ( button . value , { shallow : false } ) ;
435445 } }
436- disabled = { button . disabled }
437446 >
438447 < div className = "flex flex-col items-center justify-center gap-1 bg-baseIndigoSolid1 " >
439448 < Icon
440449 source = { button . icon }
441450 size = { 24 }
442451 stroke = { activeButton === button . value ? 3 : 2 }
443- className = { button . disabled ? 'opacity-50' : '' }
444452 />
445453 < Text
446454 variant = "headingMd"
447455 fontWeight = { activeButton === button . value ? 'bold' : 'medium' }
448- className = {
449- button . disabled ? 'opacity-50' : 'text-textSubdued'
450- }
456+ className = "text-textSubdued"
451457 >
452458 { button . title }
453459 </ Text >
0 commit comments