@@ -17,7 +17,117 @@ import {
1717 TableRow ,
1818} from '@/components/ui/table' ;
1919import { cn } from '@/lib/utils' ;
20- import { useTableRowPercentage } from '../hooks/use-table-row-percentage' ;
20+
21+ // Row percentage thresholds for gradient colors
22+ const PERCENTAGE_THRESHOLDS = {
23+ HIGH : 50 ,
24+ MEDIUM : 25 ,
25+ LOW : 10 ,
26+ } as const ;
27+
28+ interface PercentageRow {
29+ percentage ?: string | number ;
30+ }
31+
32+ function getRowPercentage ( row : PercentageRow ) : number {
33+ const value = row . percentage ;
34+ return value !== undefined ? Number . parseFloat ( String ( value ) ) || 0 : 0 ;
35+ }
36+
37+ // Color schemes for different percentage ranges
38+ const GRADIENT_COLORS = {
39+ high : {
40+ rgb : '34, 197, 94' ,
41+ opacity : {
42+ background : 0.08 ,
43+ hover : 0.12 ,
44+ border : 0.3 ,
45+ accent : 0.8 ,
46+ glow : 0.2 ,
47+ } ,
48+ } ,
49+ medium : {
50+ rgb : '59, 130, 246' ,
51+ opacity : {
52+ background : 0.08 ,
53+ hover : 0.12 ,
54+ border : 0.3 ,
55+ accent : 0.8 ,
56+ glow : 0.2 ,
57+ } ,
58+ } ,
59+ low : {
60+ rgb : '245, 158, 11' ,
61+ opacity : {
62+ background : 0.08 ,
63+ hover : 0.12 ,
64+ border : 0.3 ,
65+ accent : 0.8 ,
66+ glow : 0.2 ,
67+ } ,
68+ } ,
69+ default : {
70+ rgb : '107, 114, 128' ,
71+ opacity : {
72+ background : 0.06 ,
73+ hover : 0.1 ,
74+ border : 0.2 ,
75+ accent : 0.7 ,
76+ glow : 0.15 ,
77+ } ,
78+ } ,
79+ } as const ;
80+
81+ function createGradient (
82+ rgb : string ,
83+ opacity : typeof GRADIENT_COLORS . high . opacity ,
84+ percentage : number
85+ ) {
86+ const {
87+ background : bgOpacity ,
88+ hover : hoverOpacity ,
89+ border : borderOpacity ,
90+ accent : accentOpacity ,
91+ glow : glowOpacity ,
92+ } = opacity ;
93+
94+ return {
95+ background : `linear-gradient(90deg, rgba(${ rgb } , ${ bgOpacity } ) 0%, rgba(${ rgb } , ${ bgOpacity + 0.07 } ) ${ percentage * 0.8 } %, rgba(${ rgb } , ${ bgOpacity + 0.04 } ) ${ percentage } %, rgba(${ rgb } , ${ bgOpacity - 0.06 } ) ${ percentage + 5 } %, transparent 100%)` ,
96+ hoverBackground : `linear-gradient(90deg, rgba(${ rgb } , ${ hoverOpacity } ) 0%, rgba(${ rgb } , ${ hoverOpacity + 0.1 } ) ${ percentage * 0.8 } %, rgba(${ rgb } , ${ hoverOpacity + 0.06 } ) ${ percentage } %, rgba(${ rgb } , ${ hoverOpacity - 0.08 } ) ${ percentage + 5 } %, transparent 100%)` ,
97+ borderColor : `rgba(${ rgb } , ${ borderOpacity } )` ,
98+ accentColor : `rgba(${ rgb } , ${ accentOpacity } )` ,
99+ glowColor : `rgba(${ rgb } , ${ glowOpacity } )` ,
100+ } ;
101+ }
102+
103+ function getPercentageGradient ( percentage : number ) {
104+ if ( percentage >= PERCENTAGE_THRESHOLDS . HIGH ) {
105+ return createGradient (
106+ GRADIENT_COLORS . high . rgb ,
107+ GRADIENT_COLORS . high . opacity ,
108+ percentage
109+ ) ;
110+ }
111+ if ( percentage >= PERCENTAGE_THRESHOLDS . MEDIUM ) {
112+ return createGradient (
113+ GRADIENT_COLORS . medium . rgb ,
114+ GRADIENT_COLORS . medium . opacity ,
115+ percentage
116+ ) ;
117+ }
118+ if ( percentage >= PERCENTAGE_THRESHOLDS . LOW ) {
119+ return createGradient (
120+ GRADIENT_COLORS . low . rgb ,
121+ GRADIENT_COLORS . low . opacity ,
122+ percentage
123+ ) ;
124+ }
125+ return createGradient (
126+ GRADIENT_COLORS . default . rgb ,
127+ GRADIENT_COLORS . default . opacity ,
128+ percentage
129+ ) ;
130+ }
21131
22132interface TableContentProps < TData extends { name : string | number } > {
23133 table : Table < TData > ;
@@ -59,16 +169,19 @@ export function TableContent<TData extends { name: string | number }>({
59169 className,
60170} : TableContentProps < TData > ) {
61171 const [ expandedRow , setExpandedRow ] = useState < string | null > ( null ) ;
62- const { getRowPercentage, getRowGradient, hasPercentageColumn } =
63- useTableRowPercentage ( ) ;
64172
65173 const toggleRowExpansion = useCallback ( ( rowId : string ) => {
66174 setExpandedRow ( ( prev ) => ( prev === rowId ? null : rowId ) ) ;
67175 } , [ ] ) ;
68176
69177 const displayData = table . getRowModel ( ) . rows ;
70178 const tableData = displayData . map ( ( row ) => row . original ) ;
71- const hasPercentageData = hasPercentageColumn ( tableData ) ;
179+
180+ // Check if any row has percentage data
181+ const hasPercentageData = tableData . some ( ( row ) => {
182+ const percentage = getRowPercentage ( row as PercentageRow ) ;
183+ return percentage > 0 ;
184+ } ) ;
72185
73186 if ( ! displayData . length ) {
74187 return (
@@ -200,11 +313,12 @@ export function TableContent<TData extends { name: string | number }>({
200313 const hasSubRows = subRows && subRows . length > 0 ;
201314 const isExpanded = expandedRow === row . id ;
202315 const percentage = hasPercentageData
203- ? getRowPercentage ( row . original )
316+ ? getRowPercentage ( row . original as PercentageRow )
204317 : 0 ;
205- const gradient = hasPercentageData
206- ? getRowGradient ( row . original )
207- : null ;
318+ const gradient =
319+ hasPercentageData && percentage > 0
320+ ? getPercentageGradient ( percentage )
321+ : null ;
208322
209323 return (
210324 < Fragment key = { row . id } >
0 commit comments